ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
class.arConnectorDB.php
Go to the documentation of this file.
1 <?php
2 require_once('class.arConnector.php');
3 require_once(dirname(__FILE__) . '/../Exception/class.arException.php');
4 
12 class arConnectorDB extends arConnector {
13 
17  protected function returnDB() {
18  global $ilDB;
19 
20  return $ilDB;
21  }
22 
23 
29  public function checkConnection(ActiveRecord $ar) {
30  return is_object($this->returnDB());
31  }
32 
33 
39  public function nextID(ActiveRecord $ar) {
40  return $this->returnDB()->nextId($ar->getConnectorContainerName());
41  }
42 
43 
50  public function installDatabase(ActiveRecord $ar, $fields) {
51  $ilDB = $this->returnDB();
52  $ilDB->createTable($ar->getConnectorContainerName(), $fields);
53  $arFieldList = $ar->getArFieldList();
54  if ($arFieldList->getPrimaryField()->getName()) {
55  $ilDB->addPrimaryKey($ar->getConnectorContainerName(), array( $arFieldList->getPrimaryField()->getName() ));
56  }
57  if ($ar->getArFieldList()->getPrimaryField()->getSequence() AND ! $ilDB->sequenceExists($ar->getConnectorContainerName())) {
58  $ilDB->createSequence($ar->getConnectorContainerName());
59  }
60  $this->updateIndices($ar);
61 
62  return true;
63  }
64 
65 
69  public function updateIndices(ActiveRecord $ar) {
70  $ilDB = $this->returnDB();
71  $arFieldList = $ar->getArFieldList();
72  $res = $ilDB->query('SHOW INDEX FROM ' . $ar->getConnectorContainerName());
73  $existing_indices = array();
74  while ($rec = $ilDB->fetchObject($res)) {
75  $existing_indices[] = $rec->column_name;
76  }
77  foreach ($arFieldList->getFields() as $i => $arField) {
78  if ($arField->getIndex() === true) {
79  if (!in_array($arField->getName(), $existing_indices)) {
80  $ilDB->addIndex($ar->getConnectorContainerName(), array( $arField->getName() ), 'i' . $i);
81  }
82  }
83  }
84  }
85 
86 
92  public function updateDatabase(ActiveRecord $ar) {
93  $ilDB = $this->returnDB();
94  foreach ($ar->getArFieldList()->getFields() as $field) {
95  if (!$ilDB->tableColumnExists($ar->getConnectorContainerName(), $field->getName())) {
96  $ilDB->addTableColumn($ar->getConnectorContainerName(), $field->getName(), $field->getAttributesForConnector());
97  }
98  }
99  $this->updateIndices($ar);
100 
101  return true;
102  }
103 
104 
110  public function resetDatabase(ActiveRecord $ar) {
111  $ilDB = $this->returnDB();
112  if ($ilDB->tableExists($ar->getConnectorContainerName())) {
113  $ilDB->dropTable($ar->getConnectorContainerName());
114  }
115  $ar->installDB();
116 
117  return true;
118  }
119 
120 
124  public function truncateDatabase(ActiveRecord $ar) {
125  $ilDB = $this->returnDB();
126  $query = 'TRUNCATE TABLE ' . $ar->getConnectorContainerName();
127  $ilDB->query($query);
128  if ($ilDB->tableExists($ar->getConnectorContainerName() . '_seq')) {
129  $ilDB->dropSequence($ar->getConnectorContainerName());
130  $ilDB->createSequence($ar->getConnectorContainerName());
131  }
132  }
133 
134 
140  public function checkTableExists(ActiveRecord $ar) {
141  $ilDB = $this->returnDB();
142 
147  return $ilDB->tableExists($ar->getConnectorContainerName());
148  }
149 
150 
157  public function checkFieldExists(ActiveRecord $ar, $field_name) {
158  $ilDB = $this->returnDB();
159 
160  return $ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name);
161  }
162 
163 
171  public function removeField(ActiveRecord $ar, $field_name) {
172  $ilDB = $this->returnDB();
173  if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name)) {
174  //throw new arException($field_name, arException::COLUMN_DOES_NOT_EXIST);
175  }
176  if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name)) {
177  $ilDB->dropTableColumn($ar->getConnectorContainerName(), $field_name);
178 
179  return true;
180  }
181  }
182 
183 
192  public function renameField(ActiveRecord $ar, $old_name, $new_name) {
193  $ilDB = $this->returnDB();
194  if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $old_name)) {
195  //throw new arException($old_name, arException::COLUMN_DOES_NOT_EXIST);
196 
197  if (!$ilDB->tableColumnExists($ar->getConnectorContainerName(), $new_name)) {
198  //throw new arException($new_name, arException::COLUMN_DOES_ALREADY_EXIST);
199  $ilDB->renameTableColumn($ar->getConnectorContainerName(), $old_name, $new_name);
200  }
201  }
202 
203  return true;
204  }
205 
206 
210  public function create(ActiveRecord $ar) {
211  $ilDB = $this->returnDB();
212  $ilDB->insert($ar->getConnectorContainerName(), $ar->getArrayForConnector());
213  }
214 
215 
221  public function read(ActiveRecord $ar) {
222  $ilDB = $this->returnDB();
223 
224  $query = 'SELECT * FROM ' . $ar->getConnectorContainerName() . ' ' . ' WHERE ' . arFieldCache::getPrimaryFieldName($ar) . ' = '
226 
227  $set = $ilDB->query($query);
228  $records = array();
229  while ($rec = $ilDB->fetchObject($set)) {
230  $records[] = $rec;
231  }
232 
233  return $records;
234  }
235 
236 
240  public function update(ActiveRecord $ar) {
241  $ilDB = $this->returnDB();
242 
243  $ilDB->update($ar->getConnectorContainerName(), $ar->getArrayForConnector(), array(
244  arFieldCache::getPrimaryFieldName($ar) => array(
246  $ar->getPrimaryFieldValue()
247  ),
248  ));
249  }
250 
251 
255  public function delete(ActiveRecord $ar) {
256  $ilDB = $this->returnDB();
257 
258  $ilDB->manipulate('DELETE FROM ' . $ar->getConnectorContainerName() . ' WHERE ' . arFieldCache::getPrimaryFieldName($ar) . ' = '
259  . $ilDB->quote($ar->getPrimaryFieldValue(), arFieldCache::getPrimaryFieldType($ar)));
260  }
261 
262 
270  public function readSet(ActiveRecordList $arl) {
271  $ilDB = $this->returnDB();
272  $set = $ilDB->query(self::buildQuery($arl));
273  $records = array();
274  while ($rec = $ilDB->fetchAssoc($set)) {
275  $records[] = $rec;
276  }
277 
278  return $records;
279  }
280 
281 
287  public function affectedRows(ActiveRecordList $arl) {
288  $ilDB = $this->returnDB();
289  $q = self::buildQuery($arl);
290 
291  $set = $ilDB->query($q);
292 
293  return $ilDB->numRows($set);
294  }
295 
296 
302  protected function buildQuery(ActiveRecordList $arl) {
303  // SELECTS
304  $q = $arl->getArSelectCollection()->asSQLStatement();
305  // Concats
306  $q .= $arl->getArConcatCollection()->asSQLStatement();
307  $q .= ' FROM '.$arl->getAR()->getConnectorContainerName();
308  // JOINS
309  $q .= $arl->getArJoinCollection()->asSQLStatement();
310  // WHERE
311  $q .= $arl->getArWhereCollection()->asSQLStatement();
312  // ORDER
313  $q .= $arl->getArOrderCollection()->asSQLStatement();
314  // LIMIT
315  $q .= $arl->getArLimitCollection()->asSQLStatement();
316 
317  //TODO: using template in the model.
318  if ($arl->getDebug()) {
319  global $tpl;
320  if ($tpl instanceof ilTemplate) {
321  ilUtil::sendInfo($q);
322  } else {
323  var_dump($q); // FSX
324  }
325  }
326  $arl->setLastQuery($q);
327 
328  return $q;
329  }
330 
331 
338  public function quote($value, $type) {
339  $ilDB = $this->returnDB();
340 
341  return $ilDB->quote($value, $type);
342  }
343 }
344 
345 ?>
read(ActiveRecord $ar)
static getPrimaryFieldName(ActiveRecord $ar)
static getPrimaryFieldType(ActiveRecord $ar)
Class arConnectorDB.
buildQuery(ActiveRecordList $arl)
readSet(ActiveRecordList $arl)
Class ActiveRecord.
Class ActiveRecordList.
static setLastQuery($last_query)
updateIndices(ActiveRecord $ar)
updateDatabase(ActiveRecord $ar)
Class arConnector.
resetDatabase(ActiveRecord $ar)
nextID(ActiveRecord $ar)
checkConnection(ActiveRecord $ar)
removeField(ActiveRecord $ar, $field_name)
quote($value, $type)
$records
Definition: simple_test.php:17
truncateDatabase(ActiveRecord $ar)
global $tpl
Definition: ilias.php:8
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
checkFieldExists(ActiveRecord $ar, $field_name)
special template class to simplify handling of ITX/PEAR
renameField(ActiveRecord $ar, $old_name, $new_name)
affectedRows(ActiveRecordList $arl)
checkTableExists(ActiveRecord $ar)
global $ilDB
update(ActiveRecord $ar)
create(ActiveRecord $ar)
installDatabase(ActiveRecord $ar, $fields)