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/persistence/class.PersistenceMapper.php");
22 require_once(BASE.
"wcmf/lib/persistence/class.PersistenceFacade.php");
23 require_once(BASE.
"wcmf/lib/persistence/converter/class.DataConverter.php");
24 require_once(BASE.
"wcmf/lib/persistence/pdo/class.PDOConnection.php");
25 require_once(BASE.
"wcmf/lib/util/class.InifileParser.php");
57 $this->_connParams = &$params;
66 unset($this->_connParams[
'dbConnection']);
67 return array(
'_connParams',
'_dbPrefix');
74 if (isset($this->_connParams[
'dbType']) && isset($this->_connParams[
'dbHostName']) &&
75 isset($this->_connParams[
'dbUserName']) && isset($this->_connParams[
'dbPassword']) &&
76 isset($this->_connParams[
'dbName']))
78 $dns = $this->_connParams[
'dbType'].
':host='.$this->_connParams[
'dbHostName'].
79 ((!empty($this->_connParams[
'dbPort'])) ? (
';port='.$this->_connParams[
'dbPort']) :
'').
80 ';dbname='.$this->_connParams[
'dbName'];
81 $charSet = isset($this->_connParams[
'dbCharSet']) ? $this->_connParams[
'dbCharSet'] :
'utf8';
84 $driverOptions = array();
85 if (strtolower($this->_connParams[
'dbType']) ==
'mysql') {
86 $driverOptions[PDO::MYSQL_ATTR_INIT_COMMAND] =
"SET NAMES ".$charSet;
90 $this->_conn =
new PDOConnection($dns, $this->_connParams[
'dbUserName'], $this->_connParams[
'dbPassword'], $driverOptions);
91 $this->_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
93 catch(PDOException $ex) {
94 WCMFException::throwEx(
"Connection to ".$this->_connParams[
'dbHostName'].
".".$this->_connParams[
'dbName'].
" failed: ".
95 $ex->getMessage(), __FILE__, __LINE__);
99 $this->_dbPrefix = $this->_connParams[
'dbPrefix'];
103 $persistenceFacade->storeConnection($this->_connParams, $this->_conn);
105 elseif (isset($this->_connParams[
'dbConnection']))
108 $this->_conn = &$this->_connParams[
'dbConnection'];
121 if ($this->_idSelectStmt == null || $this->_idInsertStmt == null || $this->_idUpdateStmt == null) {
124 $mapper = $persistenceFacade->getMapper(
"Adodbseq");
126 $connection = $mapper->getConnection();
127 $this->_idSelectStmt = $connection->prepare(
"SELECT id FROM adodbseq");
128 $this->_idInsertStmt = $connection->prepare(
"INSERT INTO adodbseq (id) VALUES (0)");
129 $this->_idUpdateStmt = $connection->prepare(
"UPDATE adodbseq SET id=LAST_INSERT_ID(id+1);");
132 $this->_idSelectStmt->execute();
133 $rows = $this->_idSelectStmt->fetchAll(PDO::FETCH_ASSOC);
134 if (
sizeof($rows) == 0) {
135 $this->_idInsertStmt->execute();
136 $this->_idInsertStmt->closeCursor();
137 $row = array(array(
'id' => 0));
139 $id = $rows[0][
'id'];
140 $this->_idUpdateStmt->execute();
141 $this->_idUpdateStmt->closeCursor();
142 $this->_idSelectStmt->closeCursor();
145 catch (Exception $ex) {
146 Log::error(
"The query: ".$sql.
"\ncaused the following exception:\n".$ex->getMessage(), __CLASS__);
157 $mapper = &$persistenceFacade->getMapper(
'Adodbseq');
159 return $mapper->getTableName();
170 if ($this->_conn == null)
178 $stmt = $this->_conn->prepare($sql);
180 $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
181 $stmt->closeCursor();
185 return $this->_conn->exec($sql);
188 catch (Exception $ex) {
189 Log::error(
"The query: ".$sql.
"\ncaused the following exception:\n".$ex->getMessage(), __CLASS__);
201 if ($this->_conn == null)
205 if ($pagingInfo != null && $pagingInfo->getPageSize() > 0) {
207 $countSql = preg_replace(
'/^\s*SELECT\s+(DISTINCT\s+|)([^,]+)\s.+\sFROM\s/Uis',
'SELECT COUNT($1 $2) AS nRows FROM ', $sql);
208 $countSql = preg_replace(
'/\s*ORDER BY.*$/Uis',
'', $countSql);
210 $nRows = $result[0][
'nRows'];
212 $pagingInfo->setTotalCount($nRows);
214 $limit = $pagingInfo->getPageSize();
215 $offset = $pagingInfo->getIndex();
216 $sql = preg_replace(
'/;$/',
'', $sql);
217 $sql .=
' LIMIT '.$limit;
219 $sql .=
' OFFSET '.$offset;
225 catch (Exception $ex) {
226 Log::error(
"The query: ".$sql.
"\ncaused the following exception:\n".$ex->getMessage(), __CLASS__);
238 $pkNames = $this->getPKNames();
239 return (in_array($name, array_keys($pkNames)) && $pkNames[$name] == $dataType);
250 $params = array(
'type' => $type,
'id' => array());
251 foreach ($pkNames as $pkName)
252 array_push($params[
'id'], $data[$pkName]);
264 $mapper = &$persistenceFacade->getMapper($type);
265 return array_keys($mapper->getPkNames());
271 function &
loadImpl($oid, $buildDepth, $buildAttribs=null, $buildTypes=null)
279 $pagingInfo, $buildAttribs, $buildTypes);
280 if (
sizeof($objects) > 0)
298 if ($buildAttribs != null && isset($buildAttribs[$this->
getType()]))
299 $attribs = $buildAttribs[$this->
getType()];
306 $newBuildDepth = $buildDepth-1;
308 $newBuildDepth = $buildDepth;
317 foreach ($objectData[
'_children'] as $childData)
320 if (!isset($childData[
'minOccurs']))
321 $childData[
'minOccurs'] = 0;
322 if (!isset($childData[
'maxOccurs']))
323 $childData[
'maxOccurs'] = 1;
326 (($buildDepth ==
BUILDDEPTH_REQUIRED) && $childData[
'minOccurs'] > 0 && $childData[
'aggregation'] ==
true)) )
328 $childObject = &$persistenceFacade->create($childData[
'type'], $newBuildDepth, $buildAttribs);
329 $childObject->setProperty(
'minOccurs', $childData[
'minOccurs']);
330 $childObject->setProperty(
'maxOccurs', $childData[
'maxOccurs']);
331 $childObject->setProperty(
'aggregation', $childData[
'aggregation']);
332 $childObject->setProperty(
'composition', $childData[
'composition']);
345 if ($this->_conn == null)
350 $appValues = array();
351 foreach ($object->getDataTypes() as $type)
353 foreach ($object->getValueNames($type) as $valueName) {
354 if (!$this->
isPkValue($valueName, $type))
356 if (!isset($appValues[$type])) {
357 $appValues[$type] = array();
359 $properties = $object->getValueProperties($valueName, $type);
360 $value = $object->getValue($valueName, $type);
361 $appValues[$type][$valueName] = $value;
362 $convertedValue = $this->_dataConverter->convertApplicationToStorage($value, $properties[
'db_data_type'], $valueName);
363 $object->setValue($valueName, $convertedValue, $type,
true);
374 foreach($sqlArray as $sqlStr)
390 foreach($sqlArray as $sqlStr)
395 foreach ($object->getDataTypes() as $type)
397 foreach ($object->getValueNames($type) as $valueName) {
398 if (!$this->
isPkValue($valueName, $type)) {
399 $object->setValue($valueName, $appValues[$type][$valueName], $type);
415 if ($this->_conn == null)
430 foreach($sqlArray as $sqlStr)
438 foreach($sqlArray as $childType => $criteria)
440 $attribs = $this->getPkNamesForType($childType);
441 $childoids = $persistenceFacade->getOIDs($childType, $criteria);
442 foreach($childoids as $childoid)
443 $persistenceFacade->delete($childoid, $recursive);
447 foreach($sqlArray as $sqlStr)
459 if ($this->_conn == null)
468 function getOIDs($type, $criteria=null, $orderby=null, &$pagingInfo)
474 $data = $this->
loadObjects($type,
BUILDDEPTH_SINGLE, $criteria, $orderby, $pagingInfo, array($type => array()), array($type),
false,
true);
477 for ($i=0, $count=
sizeof($data); $i<$count; $i++) {
478 $objectData[
'_data'] = $data[$i];
480 array_push($oids, $oid);
490 function loadObjects($type, $buildDepth, $criteria=null, $orderby=null, &$pagingInfo, $buildAttribs=null, $buildTypes=null, $selectChildOIDs=
true, $omitObjects=
false)
495 if ($this->_conn == null)
501 if (is_array($buildTypes) && !in_array($type, $buildTypes))
506 if ($buildAttribs != null && isset($buildAttribs[$this->
getType()]))
507 $attribs = $buildAttribs[$this->
getType()];
511 if ($criteria != null)
514 if (is_array($criteria))
516 foreach($criteria as $name => $value)
517 $attribCondStr .= $name.
"=".$this->_conn->quote($value).
" AND ";
518 $attribCondStr = substr($attribCondStr, 0, strlen($attribCondStr)-strlen(
" AND "));
521 $attribCondStr = $criteria;
526 if ($orderby != null)
527 $orderbyStr = join(
', ', $orderby);
531 $sqlStr = $this->
getSelectSQL($attribCondStr, $orderbyStr, $attribs);
532 $data = $this->
select($sqlStr, $pagingInfo);
533 if (
sizeof($data) == 0)
540 $numObjects =
sizeof($data);
541 for ($i=0; $i<$numObjects; $i++)
547 if ($selectChildOIDs)
548 $this->
appendChildData($object, $buildDepth, $buildAttribs, $buildTypes);
551 $objects[] = &$object;
564 $createFromLoadedData =
false;
566 $createFromLoadedData =
true;
572 if ($createFromLoadedData)
575 $objectData[
'_data'] = $data;
585 foreach($objectData[
'_properties'] as $property)
586 $object->setProperty($property[
'name'], $property[
'value']);
589 if ($createFromLoadedData)
610 $newBuildDepth = $buildDepth-1;
612 $newBuildDepth = $buildDepth;
615 $childoids = array();
617 foreach($sqlArray as $childType => $childCriteria)
619 $childoids = array_merge($childoids, $persistenceFacade->getOIDs($childType, $childCriteria));
624 $childPagingInfo = null;
625 $children = $persistenceFacade->loadObjects($childType, $newBuildDepth, $childCriteria, null, $childPagingInfo, $buildAttribs, $buildTypes);
626 for ($j=0; $j<
sizeof($children); $j++)
629 $object->setProperty(
'childoids', $childoids);
637 if ($this->_conn == null)
640 $this->_conn->beginTransaction();
647 if ($this->_conn == null)
650 $this->_conn->commit();
658 if ($this->_conn == null)
661 $this->_conn->rollBack();
677 WCMFException::throwEx(
"createObject() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
687 WCMFException::throwEx(
"appendObject() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
700 WCMFException::throwEx(
"applyDataOnLoad() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
711 WCMFException::throwEx(
"applyDataOnCreate() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
722 WCMFException::throwEx(
"prepareInsert() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
731 WCMFException::throwEx(
"getType() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
752 WCMFException::throwEx(
"getObjectDefinition() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
767 function getSelectSQL($condStr, $orderStr=null, $attribs=null, $asArray=
false)
769 WCMFException::throwEx(
"getSelectSQL() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
782 WCMFException::throwEx(
"getChildrenSelectSQL() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
793 WCMFException::throwEx(
"getChildrenDisassociateSQL() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
803 WCMFException::throwEx(
"getInsertSQL() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
813 WCMFException::throwEx(
"getUpdateSQL() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
823 WCMFException::throwEx(
"getDeleteSQL() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
833 WCMFException::throwEx(
"createPKCondition() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
RDBMapper maps objects of one type to a relational database schema. It defines a persistence mechanis...
error($message, $category)
constructOID($type, $data)
debug($message, $category)
appendObject(&$object, &$dependendObject)
executeSql($sql, $isSelect=false)
deleteImpl($oid, $recursive=true)
throwEx($message, $file='', $line='')
appendChildData(&$object, $buildDepth, $buildAttribs=null, $buildTypes=null)
applyDataOnCreate(&$object, $objectData, $attribs)
getChildrenSelectSQL($oid, $compositionOnly=false)
const BUILDDEPTH_INFINITE
& createObject($oid=null)
& createObjectFromData($attribs, $data=null)
applyDataOnLoad(&$object, $objectData, $attribs)
isPkValue($name, $dataType)
& createImpl($type, $buildDepth, $buildAttribs=null)
select($sql, &$pagingInfo)
isDebugEnabled($category)
PDOConnection extends PDO.
const BUILDDEPTH_REQUIRED
DataConverter is the base class for all converter classes. It defines the interface for converting da...
loadObjects($type, $buildDepth, $criteria=null, $orderby=null, &$pagingInfo, $buildAttribs=null, $buildTypes=null, $selectChildOIDs=true, $omitObjects=false)
getOIDParameter($oid, $param, $validate=true)
& loadImpl($oid, $buildDepth, $buildAttribs=null, $buildTypes=null)
PersistenceMapper is the base class for all mapper classes.
getOIDs($type, $criteria=null, $orderby=null, &$pagingInfo)
getChildrenDisassociateSQL($oid, $sharedOnly=false)
getSelectSQL($condStr, $orderStr=null, $attribs=null, $asArray=false)