19 require_once(BASE.
"wcmf/lib/core/class.WCMFException.php");
20 require_once(BASE.
"wcmf/lib/util/class.Log.php");
21 require_once(BASE.
"wcmf/lib/util/class.Message.php");
22 require_once(BASE.
"wcmf/lib/model/class.NodeIterator.php");
23 require_once(BASE.
"wcmf/lib/persistence/class.PersistenceFacade.php");
24 require_once(BASE.
"wcmf/lib/persistence/class.ChangeListener.php");
25 require_once(BASE.
"wcmf/lib/model/class.NodeProcessor.php");
30 define(
"QUERYOP_AND",
'AND');
31 define(
"QUERYOP_OR",
'OR');
135 $this->_root =
new Node(
'ROOT');
136 $this->_id = $this->_root->getOID();
151 $template->setValue(
"inter_operator", $interOperator,
DATATYPE_IGNORE);
152 $template->addChangeListener($this);
153 $this->_root->addChild($template);
164 if ($template != null)
170 $template->addChangeListener($this);
177 foreach ($mapper->getPkNames() as $pkName => $pkValueType)
178 $template->setValue($pkName, $oidParts[
'id'][$i++], $pkValueType,
true);
184 $template->setValue(
"inter_operator", $interOperator,
DATATYPE_IGNORE);
185 $this->_root->addChild($template);
197 $this->groups[
sizeof($this->groups)] = array(
'tpls' => $templates,
'pre_operator' => $preOperator,
'inter_operator' => $interOperator);
199 for ($i=0; $i<
sizeof($templates); $i++)
201 if ($templates[$i] != null)
214 $value = str_replace(
"'",
"\'", $value);
226 function execute($buildDepth, $orderby=null, &$pagingInfo, $attribs=null)
229 $this->_query = $this->
buildQuery($buildDepth, $attribs);
244 function executeString($type, $query, $buildDepth, $orderby=null, &$pagingInfo)
248 if (strlen($query) == 0)
261 $rows = $mapper->select($query, $pagingInfo);
262 foreach ($rows as $row)
265 if ($buildDepth ===
false)
267 $oid = $mapper->constructOID($type, $row);
272 $obj = &$mapper->createObjectFromData($this->_typeNode->getValueNames(), $row);
273 $mapper->appendChildData($obj, $buildDepth);
274 $result[
sizeof($result)] = &$obj;
287 return $this->
buildQuery($buildDepth, $attribs);
303 $tableArray = array();
304 $relationArray = array();
308 if ($this->_root->getNumChildren() == 0)
309 $this->_root->addChild($this->_typeNode);
313 if ($buildDepth ===
false)
315 $select = $mapper->getSelectSQL(
'', null, $attribs,
true);
316 $attributeStr = $select[
'attributeStr'];
320 while(!($iterator->isEnd()))
322 $currentObject = &$iterator->getCurrentObject();
323 if ($currentObject->getOID() != $this->_root->getOID() && !in_array($currentObject->getOID(),
$this->_groupedOIDs))
326 $iterator->proceed();
330 for ($i=0; $i<
sizeof($this->groups); $i++)
332 $group = $this->groups[$i];
333 $groupConditionStr =
'';
334 for ($j=0; $j<
sizeof($group[
'tpls']); $j++)
336 $tpl = &$group[
'tpls'][$j];
339 $tpl->removeChangeListener($this);
340 $tpl->setValue(
"pre_operator", $group[
'inter_operator'],
DATATYPE_IGNORE);
341 $tpl->addChangeListener($this);
345 if (strlen($conditionStr) > 0)
346 $conditionStr .=
' '.$group[
'pre_operator'].
' ';
347 $conditionStr .=
'('.$groupConditionStr.
')';
351 $tableStr = $select[
'tableStr'];
352 foreach ($tableArray as $table)
354 if (preg_match(
'/\b'.$table.
'\b/', $tableStr) == 0)
355 $tableStr = $table.
", ".$tableStr;
359 $query =
'SELECT DISTINCT '.$attributeStr.
' FROM '.$tableStr;
360 if (strlen($conditionStr) > 0)
361 $query .=
' WHERE '.$conditionStr;
363 $query .=
' WHERE 1';
364 if (
sizeof($relationArray) > 0)
365 $query .=
' AND '.join(
' AND ', array_unique($relationArray));
384 $processor->run($tpl,
false);
386 if (strlen($curConditionStr) > 0)
388 if (strlen($conditionStr) > 0)
389 $conditionStr .=
' '.$tpl->getValue(
"pre_operator",
DATATYPE_IGNORE).
' ';
390 $conditionStr .=
'('.$curConditionStr.
')';
394 $children = &$tpl->getChildren();
395 for($i=0; $i<
sizeof($children); $i++)
398 array_push($relationArray, $relationStr);
414 $mapperTablename = $mapper->getTableName();;
422 $tablename = $mapperTablename;
426 if (
sizeof($tpl->getParentsEx(null, $tpl->getType(), null, null)) > 0)
427 $tablename .= time();
433 if ($asAliasString && $tablename != $mapperTablename)
434 return $mapperTablename.
' as '.$tablename;
448 if ($parentMapper != null && $childMapper != null)
451 $pkColumns = $parentMapper->getPKColumnNames();
452 $fkColumn = $childMapper->getMyFKColumnName($parentTpl->getType(),
false);
468 $mapper = &$persistenceFacade->getMapper($type);
472 if ($orderby == null)
473 $orderby = $mapper->getOrderBy();
476 preg_match(
'/^SELECT DISTINCT ([^\.]+?)\./', $query, $matches);
477 $tablename = $matches[1];
479 if ($orderby != null && is_array($orderby))
483 for($i=0; $i<
sizeof($orderby); $i++)
485 if (strpos($orderby[$i],
'.') ===
false && $mapper->isAttribute($orderby[$i]))
486 $orderby[$i] = $tablename.
'.'.$orderby[$i];
488 return " ORDER BY ".$mapper->translateAppToDatabase(join(
', ', $orderby));
505 if (isset($this->_conditions[$node->getOID()][$dataType][$valueName]))
509 if ($mapper && $mapper->isForeignKey($valueName))
512 $currentCondition = $node->getValue(
"query_condition",
DATATYPE_IGNORE);
513 if (strlen($currentCondition))
514 $currentCondition .=
' '.$operator.
' ';
516 $value = $node->getValue($valueName, $dataType);
519 $parts = split(
' ', $value);
520 if (
sizeof($parts) == 1)
522 if (!in_array($valueName, array_keys($mapper->getPkNames()))) {
523 $value =
"LIKE '%".$this->escapeValue($value).
"%'";
527 $value =
"= '".$this->escapeValue($value).
"'";
531 $colName = $mapper->getColumnName($valueName, $dataType);
532 if ($colName !== null)
540 $currentCondition .=
"TRUE";
542 $currentCondition .=
"FALSE";
545 $node->removeChangeListener($this);
546 $node->setValue(
"query_condition", $currentCondition,
DATATYPE_IGNORE);
547 $node->addChangeListener($this);
558 $mapper = &$persistenceFacade->getMapper($type);
559 $conn = &$mapper->getConnection();
571 $mapper = &$node->getMapper();
584 if (!is_a($mapper,
'nodeunifiedrdbmapper') && !is_a($mapper,
'NodeUnifiedRDBMapper'))
610 $oid = $object->getOID();
612 if (!isset($this->_conditions[$oid]))
613 $this->_conditions[] = array();
614 if (!isset($this->_conditions[$oid][$type]))
615 $this->_conditions[$oid][$type] = array();
617 $this->_conditions[$oid][$type][$name] = $newValue;
executeString($type, $query, $buildDepth, $orderby=null, &$pagingInfo)
ObjectQuery is the base class for object queries. This class provides the user with object templates ...
& getObjectTemplate($type, $preOperator=QUERYOP_AND, $interOperator=QUERYOP_AND)
buildQuery($buildDepth, $attribs=null)
ChangeListener defines an interface for classes that want to be notified when a value of an persisten...
getOrderby($type, $query, $orderby)
get($message, $parameters=null, $domain='', $lang='')
Node is the basic component for building trees (although a Node can have one than more parents)...
makeGroup($templates, $preOperator=QUERYOP_AND, $interOperator=QUERYOP_OR)
NodeIterator is used to iterate over a tree/list build of objects using a Depth-First-Algorithm. Classes used with the NodeIterator must implement the getChildren() and getOID() methods. NodeIterator implements the 'Iterator Pattern'. The base class NodeIterator defines the interface for all specialized Iterator classes.
NodeProcessor is used to iterate over all values of a Node and apply a given callback function...
getRelationCondition(&$parentTpl, &$childTpl)
throwEx($message, $file='', $line='')
stateChanged(&$object, $oldValue, $newValue)
makeConditionStr(&$node, $valueName, $dataType, $operator)
propertyChanged(&$object, $name, $oldValue, $newValue)
valueChanged(&$object, $name, $type, $oldValue, $newValue)
decomposeOID($oid, $validate=true)
toString($buildDepth=BUILDDEPTH_SINGLE, $attribs=null)
getTableName(&$tpl, $asAliasString=false)
processObjectTemplate(&$tpl, &$tableArray, &$conditionStr, &$relationArray)
registerObjectTemplate(&$template, $preOperator=QUERYOP_AND, $interOperator=QUERYOP_AND)
execute($buildDepth, $orderby=null, &$pagingInfo, $attribs=null)