19 require_once(BASE.
"wcmf/lib/util/class.Message.php");
20 require_once(BASE.
"wcmf/lib/util/class.StringUtil.php");
21 require_once(BASE.
"wcmf/lib/model/class.Node.php");
22 require_once(BASE.
"wcmf/lib/model/mapper/class.NodeRDBMapper.php");
60 if ($object != null) {
61 $object->addChangeListener($this);
70 $oid = $object->getOID();
72 $pkValues = $this->getPKNames();
74 for($i=0; $i<
sizeof($pkColumns); $i++)
77 $pkColumn = $pkColumns[$i];
80 $pkValue = $oidParts[
'id'][$i];
85 $object->setValue($pkColumn, $nextId, $pkValues[pkColumn]);
95 static $result = null;
101 $result[
'_properties'] = $nodeDef[
'_properties'];
102 $result[
'_datadef'] = $nodeDef[
'_datadef'];
105 foreach ($nodeDef[
'_ref'] as $reference)
107 $reference[
'is_editable'] =
false;
109 array_push($result[
'_datadef'], $reference);
113 $result[
'_parents'] = array();
114 for ($i=0; $i<
sizeof($nodeDef[
'_parents']); $i++)
116 if (!isset($nodeDef[
'_parents'][$i][
'is_navigable']) || $nodeDef[
'_parents'][$i][
'is_navigable']) {
117 array_push($result[
'_parents'], $nodeDef[
'_parents'][$i]);
120 $result[
'_children'] = array();
121 for ($i=0; $i<
sizeof($nodeDef[
'_children']); $i++)
123 if (!isset($nodeDef[
'_children'][$i][
'is_navigable']) || $nodeDef[
'_children'][$i][
'is_navigable']) {
124 array_push($result[
'_children'], $nodeDef[
'_children'][$i]);
133 function getSelectSQL($condStr, $orderStr=null, $attribs=null, $asArray=
false)
144 foreach($nodeDef[
'_parents'] as $curParent)
146 if (!isset($curParent[
'is_navigable']) || $curParent[
'is_navigable'])
148 $parentStr .= $this->
quote($curParent[
'type']).
" AS ptype$i, ".$this->
getTableName().
".".$curParent[
'fk_columns'].
" AS pid$i, ";
152 if (strlen($parentStr) > 0) {
156 $parentStr =
"'' AS ptype0, null AS pid0";
162 $tableStr = $tableName;
165 if ($attribs !== null) {
168 foreach($nodeDef[
'_datadef'] as $curDef) {
169 if ($attribs === null || in_array($curDef[
'name'], $attribs)) {
170 $attribStr .= $tableName.
".".$curDef[
'column_name'].
" AS ".$this->
quote($curDef[
'name']).
", ";
175 $refStrings = $this->
getSQLForRefs($nodeDef, $attribStr, $tableStr, $condStr, $orderStr, $attribs);
176 $attribStr = $refStrings[
'attribStr'];
177 $tableStr = $refStrings[
'tableStr'];
178 $condStr = $refStrings[
'condStr'];
179 $orderStr = $refStrings[
'orderStr'];
181 if (strlen($attribStr) > 0) {
184 if (strlen($condStr) == 0) {
187 $completeOrderStr = $orderStr;
188 if (strlen($orderStr) > 0)
189 $completeOrderStr =
" ORDER BY ".$orderStr;
194 if (is_array($orderByCols))
196 $completeOrderStr =
'';
197 foreach($orderByCols as $orderByCol) {
198 if (strlen(trim($orderByCol)) > 0) {
202 if (strlen($completeOrderStr) > 0) {
203 $completeOrderStr =
' ORDER BY '.StringUtil::removeTrailingComma($completeOrderStr);
210 'attributeStr' => $attribStr.
" ".$parentStr,
211 'tableStr' => $this->_dbPrefix.$tableStr,
212 'conditionStr' => $condStr,
213 'orderStr' => $orderStr
217 return "SELECT ".$attribStr.
" ".$parentStr.
" FROM ".$this->_dbPrefix.$tableStr.
" WHERE ".$condStr.$completeOrderStr.
";";
228 foreach($nodeDef[
'_children'] as $childDef)
230 if (!isset($childDef[
'is_navigable']) || $childDef[
'is_navigable'])
232 if (!$compositionOnly || ($compositionOnly && $childDef[
'composition'] ==
true))
234 $sqlStr = $childDef[
'table_name'].
".".$childDef[
'fk_columns'].
"=".$this->
quote($dbid[0]);
235 $sqlArray[$childDef[
'type']] = $sqlStr;
249 foreach($nodeDef[
'_children'] as $childDef) {
250 if (!$sharedOnly || ($sharedOnly && $childDef[
'composition'] ==
false)) {
251 array_push($sqlArray,
"UPDATE ".$this->_dbPrefix.$childDef[
'table_name'].
" SET ".$childDef[
'fk_columns'].
"=NULL WHERE ".
252 $childDef[
'fk_columns'].
"=".$this->quote($dbid[0]).
";");
264 $insertedAttributes = array();
266 $attribValueStr =
'';
270 $oid = $object->getOID();
273 for($i=0; $i<
sizeof($pkColumns); $i++)
276 $pkColumn = $pkColumns[$i];
279 $pkValue = $oidParts[
'id'][$i];
280 $attribNameStr .= $pkColumn.
", ";
281 $attribValueStr .= $this->
quote($pkValue).
", ";
282 array_push($insertedAttributes, $pkColumn);
287 $parents = &$object->getParents();
288 for ($i=0; $i<
sizeof($parents); $i++)
290 $parent = &$parents[$i];
296 $attribNameStr .= $attribName.
", ";
297 $attribValueStr .= $this->
quote($parentDef[
'id'][0]).
", ";
298 array_push($insertedAttributes, $attribName);
304 foreach($nodeDef[
'_datadef'] as $curDef)
307 if (in_array($curDef[
'name'], $object->getValueNames($curDef[
'app_data_type'])))
309 $attribName = $curDef[
'name'];
311 if (!in_array($attribName, $insertedAttributes))
313 $attribNameStr .= $tableName.
".".$curDef[
'column_name'].
", ";
314 $attribValueStr .= $this->
quote($object->getValue($attribName, $curDef[
'app_data_type'])).
", ";
315 array_push($insertedAttributes, $attribName);
325 "INSERT INTO ".$this->_dbPrefix.$tableName.
" (".$attribNameStr.
") VALUES (".$attribValueStr.
");"
334 $updatedAttributes = array();
342 $parents = &$object->getParents();
343 for ($i=0; $i<
sizeof($parents); $i++)
345 $parent = &$parents[$i];
350 $attribStr = $tableName.
".".$attribName.
"=".$this->
quote($parentDef[
'id'][0]).
", ";
351 array_push($updatedAttributes, $attribName);
357 foreach($nodeDef[
'_datadef'] as $curDef)
360 if (in_array($curDef[
'name'], $object->getValueNames($curDef[
'app_data_type'])))
362 $attribName = $curDef[
'name'];
364 if (!in_array($attribName, $updatedAttributes))
366 $attribStr .= $tableName.
".".$curDef[
'column_name'].
"=".$this->
quote($object->getValue($attribName, $curDef[
'app_data_type'])).
", ";
367 array_push($updatedAttributes, $attribName);
374 if (strlen($attribStr) > 0) {
377 "UPDATE ".$this->_dbPrefix.$tableName.
" SET ".$attribStr.
" WHERE ".$pkStr.
";"
395 "DELETE FROM ".$this->_dbPrefix.$this->getTableName().
" WHERE ".$pkStr.
";"
408 for ($i=0; $i<
sizeof($pkColumns); $i++)
411 $str .= $tableName.
".".$pkColumns[$i].
"=".$this->
quote($pkValue).
' AND ';
413 return substr($str, 0, -5);
423 foreach($nodeDef[
'_datadef'] as $curDef) {
424 if ($curDef[
'name'] == $name) {
448 foreach($childDef as $curChild) {
449 if ($curChild[
'type'] == $type) {
464 foreach($nodeDef[
'_parents'] as $parent)
466 if ($parent[
'fk_columns'] == $column) {
482 function getSQLForRefs($nodeDef, $attribStr, $tableStr, $condStr, $orderStr, $attribs=null)
488 $referencedTables = array();
489 foreach($nodeDef[
'_ref'] as $curDef)
491 if ($attribs == null || in_array($curDef[
'name'], $attribs))
493 $referencedType = $curDef[
'ref_type'];
494 $referencedTable = $curDef[
'ref_table'];
495 $referencedValue = $curDef[
'ref_value'];
496 $referencedIdColumn = $curDef[
'id_column'];
497 $referencedFkColumn = $curDef[
'fk_columns'];
498 $referencedColumn = $curDef[
'ref_column'];
503 $attribStr .= $referencedTable.
".".$referencedColumn.
" AS ".$this->
quote($curDef[
'name']).
", ";
504 if (!in_array($referencedTable, $referencedTables))
506 $joinStr .=
" LEFT JOIN ".$this->_dbPrefix.$referencedTable.
" ON ".
507 $referencedTable.
".".$referencedIdColumn.
"=".$this->_dbPrefix.$tableName.
".".$this->
getMyFKColumnName($referencedType);
508 array_push($referencedTables, $referencedTable);
510 $condStr = str_replace($curDef[
'ref_type'].
".".$curDef[
'name'], $referencedTable.
".".$referencedColumn, $condStr);
514 $childDef = $this->
getChildDef($referencedType, $nodeDef[
'_children']);
515 if ($childDef != null)
518 $attribStr .= $referencedTable.
".".$referencedColumn.
" AS ".$this->
quote($curDef[
'name']).
", ";
519 if (!in_array($referencedTable, $referencedTables))
521 $joinStr .=
" LEFT JOIN ".$this->_dbPrefix.$referencedTable.
" ON ";
523 $joinStr .= $referencedTable.
".".$referencedFkColumn.
"=".$this->_dbPrefix.$tableName.
".".$pkColumns[0];
524 $joinStr .=
" AND ".$referencedTable.
".".$referencedIdColumn.
" = (SELECT MIN(".$referencedTable.
".".$referencedIdColumn.
") FROM ".$referencedTable.
" WHERE ".$referencedTable.
".".$referencedFkColumn.
"=".$this->_dbPrefix.$tableName.
".".$pkColumns[0].
")";
525 array_push($referencedTables, $referencedTable);
527 $condStr = str_replace($curDef[
'ref_type'].
".".$curDef[
'name'], $referencedTable.
".".$referencedColumn, $condStr);
530 if (
sizeof($childDef[
'order_by']) > 0)
533 foreach($childDef[
'order_by'] as $orderBy) {
534 if (strlen($orderBy)) {
535 $tmpOrderStr .= $referencedTable.
".".$orderBy.
", ";
538 $orderStr .= $tmpOrderStr;
550 $tableStr .= $joinStr;
552 return array(
'attribStr' => $attribStr,
'tableStr' => $tableStr,
'condStr' => $condStr,
'orderStr' => $orderStr);
563 foreach($nodeDef[
'_datadef'] as $curDef) {
564 $str = preg_replace(
'/\b'.$curDef[
'name'].
'\b/', $curDef[
'column_name'], $str);
579 foreach($nodeDef[
'_datadef'] as $dataItem)
581 if ($dataItem[
'name'] == $attributeName && ($dataType == null || ($dataType != null && $dataType == $dataItem[
'app_data_type']))) {
582 return $dataItem[
'column_name'];
586 foreach($nodeDef[
'_ref'] as $dataItem)
588 if ($dataItem[
'name'] == $attributeName) {
604 foreach($nodeDef[
'_children'] as $childDef)
606 if ($childDef[
'type'] == $childType) {
607 return $childDef[
'fk_columns'];
611 WCMFException::throwEx(
"No foreign key name found for '".$childType.
"' in '".$this->getType().
"'", __FILE__, __LINE__);
625 if (strlen($fkName) > 0) {
629 $nextParentType = get_parent_class($parentType);
630 while ($nextParentType !==
false && $nextParentType !=
'Node' && $nextParentType !=
'node')
633 if (strlen($fkName) > 0) {
636 $nextParentType = get_parent_class($nextParentType);
639 WCMFException::throwEx(
"No foreign key name found for '".$parentType.
"' in '".$this->getType().
"'", __FILE__, __LINE__);
649 if ($value == null) {
654 return $conn->quote($value);
673 if ($oidParts[
'type'] != $this->
getType()) {
677 foreach ($oidParts[
'id'] as $id)
679 if (!is_numeric($id)) {
706 if ($name ==
'parentoids')
711 foreach($newValue as $newOID)
714 foreach($nodeDef[
'_parents'] as $curParent)
716 if ($curParent[
'type'] == $oidParts[
'type']) {
717 $object->setValue($curParent[
'fk_columns'], $oidParts[
'id'][0]);
749 WCMFException::throwEx(
"getObjectDefinitionImpl() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
757 WCMFException::throwEx(
"getTableName() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
767 WCMFException::throwEx(
"getMyFKColumnNameImpl() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
removeTrailingComma($string)
getSelectSQL($condStr, $orderStr=null, $attribs=null, $asArray=false)
getMyFKColumnName($parentType, $isRequired=true)
getColumnName($attributeName, $dataType=null)
valueChanged(&$object, $name, $type, $oldValue, $newValue)
getChildFKColumnName($childType, $isRequired=true)
getChildrenSelectSQL($oid, $compositionOnly=false)
throwEx($message, $file='', $line='')
getMyFKColumnNameImpl($parentType)
getSQLForRefs($nodeDef, $attribStr, $tableStr, $condStr, $orderStr, $attribs=null)
getObjectDefinitionImpl()
decomposeOID($oid, $validate=true)
translateAppToDatabase($str)
getChildrenDisassociateSQL($oid, $sharedOnly=false)
NodeRDBMapper maps Node objects to a relational database schema where each Node type has its own tabl...
propertyChanged(&$object, $name, $oldValue, $newValue)
getOIDParameter($oid, $param, $validate=true)
isChild($type, $childDef)
getChildDef($type, $childDef)
stateChanged(&$object, $oldValue, $newValue)
NodeUnifiedRDBMapper maps Node objects to a relational database schema where each Node type has its o...