wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.TableUnifiedRDBMapper.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.TableUnifiedRDBMapper.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
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.Table.php");
22 require_once(BASE."wcmf/lib/model/mapper/class.TableRDBMapper.php");
23 
24 /**
25  * @class TableUnifiedRDBMapper
26  * @ingroup Mapper
27  * @brief TableRDBMapper maps Table objects to a relational database schema where one
28  * object contains several table rows.
29  * In comparison to TableRDBMapper it implements the template methods in a way that
30  * handles unified table definitions (e.g. tables are named as the type, primary keys are
31  * named 'id' and attribute names correspond to table columns).
32  * Subclasses simply need to define $_type as the mapper type and implement the
33  * getObjectDefinition() method.
34  *
35  * @author ingo herwig <ingo@wemove.com>
36  */
38 {
39  /**
40  * @see TableRDBMapper::getSelectSQL()
41  */
42  function getSelectSQL($condStr, $orderStr=null, $attribs=null, $asArray=false)
43  {
44  // construct query parts
45  $type = $this->getType();
46  $attribStr = '';
47  $tableStr = $type;
48 
49  // attributes
50  $tableDef = $this->getObjectDefinition();
51  foreach($tableDef['_datadef'] as $curDef)
52  if ($attribs == null || in_array($curDef['name'], $attribs))
53  $attribStr .= $type.".".$curDef['name'].", ";
54  $attribStr = StringUtil::removeTrailingComma($attribStr);
55 
56  if (strlen($condStr) == 0)
57  $condStr = "1";
58 
59  $completeOrderStr = $orderStr;
60  if (strlen($orderStr) > 0)
61  $completeOrderStr = " ORDER BY ".$orderStr;
62 
63  if ($asArray)
64  return array(
65  'attributeStr' => $attribStr,
66  'tableStr' => $this->_dbPrefix.$tableStr,
67  'conditionStr' => $condStr,
68  'orderStr' => $orderStr
69  );
70  else
71  return "SELECT ".$attribStr." FROM ".$this->_dbPrefix.$tableStr." WHERE ".$condStr.$completeOrderStr.";";
72  }
73  /**
74  * @see TableRDBMapper::getInsertSQL()
75  * @note This mapper empties the corresponding database table first and fills in the values afterwards.
76  */
77  function getInsertSQL(&$object)
78  {
79  return $this->getUpdateSQL($object);
80  }
81  /**
82  * @see TableRDBMapper::getUpdateSQL()
83  * @note This mapper empties the corresponding database table first and fills in the values afterwards.
84  */
85  function getUpdateSQL(&$object)
86  {
87  $type = $this->getType();
88 
89  $sqlArray = $this->getDeleteSQL(0);
90  $rows = $object->getRows();
91  for($i=0; $i<sizeof($rows); $i++)
92  {
93  $valueStr = '';
94  foreach($object->getRow($rows[$i]) as $value)
95  $valueStr .= $this->_conn->qstr($value).", ";
96 
97  array_push($sqlArray, "INSERT INTO ".$this->_dbPrefix.$type." VALUES(".$i.", ".StringUtil::removeTrailingComma($valueStr).");");
98  }
99  return $sqlArray;
100  }
101  /**
102  * @see TableRDBMapper::getDeleteSQL()
103  * @note This mapper empties the corresponding database table.
104  */
105  function getDeleteSQL($oid)
106  {
107  return array("DELETE FROM ".$this->_dbPrefix.$this->getType());
108  }
109  /**
110  * @see PersistenceMapper::isValidOID()
111  */
112  function isValidOID($oid)
113  {
114  $oidParts = PersistenceFacade::decomposeOID($oid);
115 
116  // check the type parameter
117  if ($oidParts['type'] != $this->getType())
118  return false;
119 
120  return true;
121  }
122  /**
123  * @see NodeRDBMapper::createPKCondition()
124  */
125  function createPKCondition($oid)
126  {
127  return "1";
128  }
129 }
130 ?>
removeTrailingComma($string)
TableRDBMapper maps Table objects to a relational database schema where one object contains several t...
getSelectSQL($condStr, $orderStr=null, $attribs=null, $asArray=false)
TableRDBMapper maps Table objects to a relational database schema where one object contains several t...
decomposeOID($oid, $validate=true)