ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.arConnectorDB.php
Go to the documentation of this file.
1<?php
2require_once('class.arConnector.php');
3require_once(dirname(__FILE__) . '/../Exception/class.arException.php');
4
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 $existing_indices = $ilDB->loadModule('Manager')->listTableIndexes($ar->getConnectorContainerName());
73
74 foreach ($arFieldList->getFields() as $i => $arField) {
75 if ($arField->getIndex() === true) {
76 if (!in_array($arField->getName(), $existing_indices)) {
77 if (!$ilDB->indexExistsByFields($ar->getConnectorContainerName(), array( $arField->getName() ))) {
78 $ilDB->addIndex($ar->getConnectorContainerName(), array( $arField->getName() ), 'i' . $i);
79 }
80 }
81 }
82 }
83 }
84
85
91 public function updateDatabase(ActiveRecord $ar) {
92 $ilDB = $this->returnDB();
93 foreach ($ar->getArFieldList()->getFields() as $field) {
94 if (!$ilDB->tableColumnExists($ar->getConnectorContainerName(), $field->getName())) {
95 $ilDB->addTableColumn($ar->getConnectorContainerName(), $field->getName(), $field->getAttributesForConnector());
96 }
97 }
98 $this->updateIndices($ar);
99
100 return true;
101 }
102
103
109 public function resetDatabase(ActiveRecord $ar) {
110 $ilDB = $this->returnDB();
111 if ($ilDB->tableExists($ar->getConnectorContainerName())) {
112 $ilDB->dropTable($ar->getConnectorContainerName());
113 }
114 $ar->installDB();
115
116 return true;
117 }
118
119
123 public function truncateDatabase(ActiveRecord $ar) {
124 $ilDB = $this->returnDB();
125 $query = 'TRUNCATE TABLE ' . $ar->getConnectorContainerName();
126 $ilDB->query($query);
127 if ($ilDB->tableExists($ar->getConnectorContainerName() . '_seq')) {
128 $ilDB->dropSequence($ar->getConnectorContainerName());
129 $ilDB->createSequence($ar->getConnectorContainerName());
130 }
131 }
132
133
139 public function checkTableExists(ActiveRecord $ar) {
140 $ilDB = $this->returnDB();
141
146 return $ilDB->tableExists($ar->getConnectorContainerName());
147 }
148
149
156 public function checkFieldExists(ActiveRecord $ar, $field_name) {
157 $ilDB = $this->returnDB();
158
159 return $ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name);
160 }
161
162
170 public function removeField(ActiveRecord $ar, $field_name) {
171 $ilDB = $this->returnDB();
172 if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name)) {
173 //throw new arException($field_name, arException::COLUMN_DOES_NOT_EXIST);
174 }
175 if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name)) {
176 $ilDB->dropTableColumn($ar->getConnectorContainerName(), $field_name);
177
178 return true;
179 }
180 }
181
182
191 public function renameField(ActiveRecord $ar, $old_name, $new_name) {
192 $ilDB = $this->returnDB();
193 if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $old_name)) {
194 //throw new arException($old_name, arException::COLUMN_DOES_NOT_EXIST);
195
196 if (!$ilDB->tableColumnExists($ar->getConnectorContainerName(), $new_name)) {
197 //throw new arException($new_name, arException::COLUMN_DOES_ALREADY_EXIST);
198 $ilDB->renameTableColumn($ar->getConnectorContainerName(), $old_name, $new_name);
199 }
200 }
201
202 return true;
203 }
204
205
209 public function create(ActiveRecord $ar) {
210 $ilDB = $this->returnDB();
212 }
213
214
220 public function read(ActiveRecord $ar) {
221 $ilDB = $this->returnDB();
222
223 $query = 'SELECT * FROM ' . $ar->getConnectorContainerName() . ' ' . ' WHERE ' . arFieldCache::getPrimaryFieldName($ar) . ' = '
225
226 $set = $ilDB->query($query);
227 $records = array();
228 while ($rec = $ilDB->fetchObject($set)) {
229 $records[] = $rec;
230 }
231
232 return $records;
233 }
234
235
239 public function update(ActiveRecord $ar) {
240 $ilDB = $this->returnDB();
241
242 $ilDB->update($ar->getConnectorContainerName(), $ar->getArrayForConnector(), array(
246 ),
247 ));
248 }
249
250
254 public function delete(ActiveRecord $ar) {
255 $ilDB = $this->returnDB();
256
257 $ilDB->manipulate('DELETE FROM ' . $ar->getConnectorContainerName() . ' WHERE ' . arFieldCache::getPrimaryFieldName($ar) . ' = '
258 . $ilDB->quote($ar->getPrimaryFieldValue(), arFieldCache::getPrimaryFieldType($ar)));
259 }
260
261
269 public function readSet(ActiveRecordList $arl) {
270 $ilDB = $this->returnDB();
271 $set = $ilDB->query(self::buildQuery($arl));
272 $records = array();
273 while ($rec = $ilDB->fetchAssoc($set)) {
274 $records[] = $rec;
275 }
276
277 return $records;
278 }
279
280
286 public function affectedRows(ActiveRecordList $arl) {
287 $ilDB = $this->returnDB();
288 $q = self::buildQuery($arl);
289
290 $set = $ilDB->query($q);
291
292 return $ilDB->numRows($set);
293 }
294
295
301 protected function buildQuery(ActiveRecordList $arl) {
302 $ilDB = $this->returnDB();
306 if ($ilDB->getDBType() == ilDBConstants::TYPE_ORACLE) {
307 $method = 'asORACLEStatement';
308 } else {
309 $method = 'asSQLStatement';
310 }
311 // SELECTS
312 $q = $arl->getArSelectCollection()->{$method}();
313 // Concats
314 $q .= $arl->getArConcatCollection()->{$method}();
315 $q .= ' FROM ' . $arl->getAR()->getConnectorContainerName();
316 // JOINS
317 $q .= $arl->getArJoinCollection()->{$method}();
318 // WHERE
319 $q .= $arl->getArWhereCollection()->{$method}();
320 // HAVING
321 $q .= $arl->getArHavingCollection()->{$method}();
322 // ORDER
323 $q .= $arl->getArOrderCollection()->{$method}();
324 // LIMIT
325 $q .= $arl->getArLimitCollection()->{$method}();
326
327 //TODO: using template in the model.
328 if ($arl->getDebug()) {
329 global $tpl;
330 if ($tpl instanceof ilTemplate) {
332 } else {
333 var_dump($q); // FSX
334 }
335 }
336 $arl->setLastQuery($q);
337
338 return $q;
339 }
340
341
348 public function quote($value, $type) {
349 $ilDB = $this->returnDB();
350
351 return $ilDB->quote($value, $type);
352 }
353
354
359 public function fixDate($value) {
360 $ilDB = $this->returnDB();
364 if ($ilDB->getDBType() != ilDBConstants::TYPE_ORACLE) {
365 return parent::fixDate($value);
366 }
367
368 return parent::fixDate($value);
369 // return "TO_DATE('{$value}','YYYY-MM-DD HH24:MI:SS')";
370 }
371}
global $tpl
Definition: ilias.php:8
Class ActiveRecordList.
static setLastQuery($last_query)
Class ActiveRecord.
An exception for terminatinating execution or to throw for unit testing.
Class arConnectorDB.
updateIndices(ActiveRecord $ar)
renameField(ActiveRecord $ar, $old_name, $new_name)
installDatabase(ActiveRecord $ar, $fields)
nextID(ActiveRecord $ar)
read(ActiveRecord $ar)
checkTableExists(ActiveRecord $ar)
checkConnection(ActiveRecord $ar)
update(ActiveRecord $ar)
resetDatabase(ActiveRecord $ar)
affectedRows(ActiveRecordList $arl)
removeField(ActiveRecord $ar, $field_name)
updateDatabase(ActiveRecord $ar)
quote($value, $type)
checkFieldExists(ActiveRecord $ar, $field_name)
readSet(ActiveRecordList $arl)
create(ActiveRecord $ar)
truncateDatabase(ActiveRecord $ar)
Class arConnector.
static getPrimaryFieldName(ActiveRecord $ar)
static getPrimaryFieldType(ActiveRecord $ar)
special template class to simplify handling of ITX/PEAR
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
$records
Definition: simple_test.php:22
global $ilDB