ILIAS  release_7 Revision v7.30-3-g800a261c036
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{
14
18 protected function returnDB()
19 {
20 global $DIC;
21 $ilDB = $DIC['ilDB'];
22
23 return $ilDB;
24 }
25
26
32 public function checkConnection(ActiveRecord $ar)
33 {
34 return is_object($this->returnDB());
35 }
36
37
43 public function nextID(ActiveRecord $ar)
44 {
45 return $this->returnDB()->nextId($ar->getConnectorContainerName());
46 }
47
48
55 public function installDatabase(ActiveRecord $ar, $fields)
56 {
57 $ilDB = $this->returnDB();
58 $ilDB->createTable($ar->getConnectorContainerName(), $fields);
59 $arFieldList = $ar->getArFieldList();
60 if ($arFieldList->getPrimaryField()->getName()) {
61 $ilDB->addPrimaryKey($ar->getConnectorContainerName(), array( $arFieldList->getPrimaryField()->getName() ));
62 }
63 if ($ar->getArFieldList()->getPrimaryField()->getSequence() and !$ilDB->sequenceExists($ar->getConnectorContainerName())) {
64 $ilDB->createSequence($ar->getConnectorContainerName());
65 }
66 $this->updateIndices($ar);
67
68 return true;
69 }
70
71
75 public function updateIndices(ActiveRecord $ar)
76 {
77 $ilDB = $this->returnDB();
78 $arFieldList = $ar->getArFieldList();
79 $existing_indices = $ilDB->loadModule('Manager')->listTableIndexes($ar->getConnectorContainerName());
80
81 foreach ($arFieldList->getFields() as $i => $arField) {
82 if ($arField->getIndex() === true) {
83 if (!in_array($arField->getName(), $existing_indices)) {
84 if (!$ilDB->indexExistsByFields($ar->getConnectorContainerName(), array( $arField->getName() ))) {
85 $ilDB->addIndex($ar->getConnectorContainerName(), array( $arField->getName() ), 'i' . $i);
86 }
87 }
88 }
89 }
90 }
91
92
98 public function updateDatabase(ActiveRecord $ar)
99 {
100 $ilDB = $this->returnDB();
101 foreach ($ar->getArFieldList()->getFields() as $field) {
102 if (!$ilDB->tableColumnExists($ar->getConnectorContainerName(), $field->getName())) {
103 $ilDB->addTableColumn($ar->getConnectorContainerName(), $field->getName(), $field->getAttributesForConnector());
104 }
105 }
106 $this->updateIndices($ar);
107
108 return true;
109 }
110
111
117 public function resetDatabase(ActiveRecord $ar)
118 {
119 $ilDB = $this->returnDB();
120 if ($ilDB->tableExists($ar->getConnectorContainerName())) {
121 $ilDB->dropTable($ar->getConnectorContainerName());
122 }
123 $ar->installDB();
124
125 return true;
126 }
127
128
132 public function truncateDatabase(ActiveRecord $ar)
133 {
134 $ilDB = $this->returnDB();
135 $query = 'TRUNCATE TABLE ' . $ar->getConnectorContainerName();
136 $ilDB->query($query);
137 if ($ilDB->tableExists($ar->getConnectorContainerName() . '_seq')) {
138 $ilDB->dropSequence($ar->getConnectorContainerName());
139 $ilDB->createSequence($ar->getConnectorContainerName());
140 }
141 }
142
143
149 public function checkTableExists(ActiveRecord $ar)
150 {
151 $ilDB = $this->returnDB();
152
157 return $ilDB->tableExists($ar->getConnectorContainerName());
158 }
159
160
167 public function checkFieldExists(ActiveRecord $ar, $field_name)
168 {
169 $ilDB = $this->returnDB();
170
171 return $ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name);
172 }
173
174
182 public function removeField(ActiveRecord $ar, $field_name)
183 {
184 $ilDB = $this->returnDB();
185 if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name)) {
186 //throw new arException($field_name, arException::COLUMN_DOES_NOT_EXIST);
187 }
188 if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $field_name)) {
189 $ilDB->dropTableColumn($ar->getConnectorContainerName(), $field_name);
190
191 return true;
192 }
193 }
194
195
204 public function renameField(ActiveRecord $ar, $old_name, $new_name)
205 {
206 $ilDB = $this->returnDB();
207 if ($ilDB->tableColumnExists($ar->getConnectorContainerName(), $old_name)) {
208 //throw new arException($old_name, arException::COLUMN_DOES_NOT_EXIST);
209
210 if (!$ilDB->tableColumnExists($ar->getConnectorContainerName(), $new_name)) {
211 //throw new arException($new_name, arException::COLUMN_DOES_ALREADY_EXIST);
212 $ilDB->renameTableColumn($ar->getConnectorContainerName(), $old_name, $new_name);
213 }
214 }
215
216 return true;
217 }
218
219
223 public function create(ActiveRecord $ar)
224 {
225 $ilDB = $this->returnDB();
227 }
228
229
235 public function read(ActiveRecord $ar)
236 {
237 $ilDB = $this->returnDB();
238
239 $query = 'SELECT * FROM ' . $ar->getConnectorContainerName() . ' ' . ' WHERE ' . arFieldCache::getPrimaryFieldName($ar) . ' = '
241
242 $set = $ilDB->query($query);
243 $records = array();
244 while ($rec = $ilDB->fetchObject($set)) {
245 $records[] = $rec;
246 }
247
248 return $records;
249 }
250
251
255 public function update(ActiveRecord $ar)
256 {
257 $ilDB = $this->returnDB();
258
259 $ilDB->update($ar->getConnectorContainerName(), $ar->getArrayForConnector(), array(
263 ),
264 ));
265 }
266
267
271 public function delete(ActiveRecord $ar)
272 {
273 $ilDB = $this->returnDB();
274
275 $ilDB->manipulate('DELETE FROM ' . $ar->getConnectorContainerName() . ' WHERE ' . arFieldCache::getPrimaryFieldName($ar) . ' = '
276 . $ilDB->quote($ar->getPrimaryFieldValue(), arFieldCache::getPrimaryFieldType($ar)));
277 }
278
279
287 public function readSet(ActiveRecordList $arl)
288 {
289 $ilDB = $this->returnDB();
290 $set = $ilDB->query(self::buildQuery($arl));
291 $records = array();
292 while ($rec = $ilDB->fetchAssoc($set)) {
293 $records[] = $rec;
294 }
295
296 return $records;
297 }
298
299
305 public function affectedRows(ActiveRecordList $arl)
306 {
307 $ilDB = $this->returnDB();
308 $q = self::buildQuery($arl);
309
310 $set = $ilDB->query($q);
311
312 return $ilDB->numRows($set);
313 }
314
315
321 protected function buildQuery(ActiveRecordList $arl)
322 {
323 $method = 'asSQLStatement';
324
325 // SELECTS
326 $q = $arl->getArSelectCollection()->{$method}();
327 // Concats
328 $q .= $arl->getArConcatCollection()->{$method}();
329 $q .= ' FROM ' . $arl->getAR()->getConnectorContainerName();
330 // JOINS
331 $q .= $arl->getArJoinCollection()->{$method}();
332 // WHERE
333 $q .= $arl->getArWhereCollection()->{$method}();
334 // HAVING
335 $q .= $arl->getArHavingCollection()->{$method}();
336 // ORDER
337 $q .= $arl->getArOrderCollection()->{$method}();
338 // LIMIT
339 $q .= $arl->getArLimitCollection()->{$method}();
340
341 //TODO: using template in the model.
342 if ($arl->getDebug()) {
343 global $DIC;
344 $tpl = $DIC['tpl'];
345 if ($tpl instanceof ilTemplate) {
347 } else {
348 var_dump($q); // FSX
349 }
350 }
351 $arl->setLastQuery($q);
352
353 return $q;
354 }
355
356
363 public function quote($value, $type)
364 {
365 $ilDB = $this->returnDB();
366
367 return $ilDB->quote($value, $type);
368 }
369
370
375 public function fixDate($value)
376 {
377 return parent::fixDate($value);
378 }
379}
Class ActiveRecordList.
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)
buildQuery(ActiveRecordList $arl)
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.
global $DIC
Definition: goto.php:24
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$i
Definition: metadata.php:24
$query
$type
global $ilDB