ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.arConnectorDB.php
Go to the documentation of this file.
1<?php
2
26{
27 private ?ilDBInterface $db = null;
28
29 public function __construct(?ilDBInterface $ilDB = null)
30 {
31 if (is_null($ilDB)) {
32 global $DIC;
33
34 $this->db = $GLOBALS['ilDB'] ?? $DIC['ilDB'] ?? null;
35 } else {
36 $this->db = $ilDB;
37 }
38 }
39
40 protected function returnDB(): ilDBInterface
41 {
42 if (is_null($this->db)) {
43 throw new arException("No DB-Connection available");
44 }
45 return $this->db;
46 }
47
48 public function checkConnection(ActiveRecord $activeRecord): bool
49 {
50 return is_object($this->returnDB());
51 }
52
56 public function nextID(ActiveRecord $activeRecord): int
57 {
58 return $this->returnDB()->nextId($activeRecord->getConnectorContainerName());
59 }
60
61 public function installDatabase(ActiveRecord $activeRecord, array $fields): bool
62 {
63 $ilDB = $this->returnDB();
64 $ilDB->createTable($activeRecord->getConnectorContainerName(), $fields);
65 $arFieldList = $activeRecord->getArFieldList();
66 if ($arFieldList->getPrimaryField()->getName() !== '' && $arFieldList->getPrimaryField()->getName() !== '0') {
67 $ilDB->addPrimaryKey(
68 $activeRecord->getConnectorContainerName(),
69 [$arFieldList->getPrimaryField()->getName()]
70 );
71 }
72 if (!$ilDB->sequenceExists($activeRecord->getConnectorContainerName()) && $activeRecord->getArFieldList(
73 )->getPrimaryField()->getSequence()) {
74 $ilDB->createSequence($activeRecord->getConnectorContainerName());
75 }
76 $this->updateIndices($activeRecord);
77
78 return true;
79 }
80
81 public function updateIndices(ActiveRecord $activeRecord): void
82 {
83 $ilDB = $this->returnDB();
84 $arFieldList = $activeRecord->getArFieldList();
85 $existing_indices = $ilDB->loadModule('Manager')->listTableIndexes($activeRecord->getConnectorContainerName());
86
87 foreach ($arFieldList->getFields() as $i => $arField) {
88 if (!$arField->getIndex()) {
89 continue;
90 }
91 if (in_array($arField->getName(), $existing_indices)) {
92 continue;
93 }
94 if ($ilDB->indexExistsByFields($activeRecord->getConnectorContainerName(), [$arField->getName()])) {
95 continue;
96 }
97 $ilDB->addIndex($activeRecord->getConnectorContainerName(), [$arField->getName()], 'i' . $i);
98 }
99 }
100
101 public function updateDatabase(ActiveRecord $activeRecord): bool
102 {
103 $ilDB = $this->returnDB();
104 foreach ($activeRecord->getArFieldList()->getFields() as $arField) {
105 if (!$ilDB->tableColumnExists($activeRecord->getConnectorContainerName(), $arField->getName())) {
106 $ilDB->addTableColumn(
107 $activeRecord->getConnectorContainerName(),
108 $arField->getName(),
109 $arField->getAttributesForConnector()
110 );
111 }
112 }
113 $this->updateIndices($activeRecord);
114
115 return true;
116 }
117
118 public function resetDatabase(ActiveRecord $activeRecord): bool
119 {
120 $ilDB = $this->returnDB();
121 if ($ilDB->tableExists($activeRecord->getConnectorContainerName())) {
122 $ilDB->dropTable($activeRecord->getConnectorContainerName());
123 }
124 $activeRecord->installDB();
125
126 return true;
127 }
128
129 public function truncateDatabase(ActiveRecord $activeRecord): bool
130 {
131 $ilDB = $this->returnDB();
132 $query = 'TRUNCATE TABLE ' . $activeRecord->getConnectorContainerName();
133 $ilDB->query($query);
134 if ($ilDB->tableExists($activeRecord->getConnectorContainerName() . '_seq')) {
135 $ilDB->dropSequence($activeRecord->getConnectorContainerName());
136 $ilDB->createSequence($activeRecord->getConnectorContainerName());
137 }
138
139 return true;
140 }
141
142 public function checkTableExists(ActiveRecord $activeRecord): bool
143 {
144 $ilDB = $this->returnDB();
145
150 return $ilDB->tableExists($activeRecord->getConnectorContainerName());
151 }
152
153 public function checkFieldExists(ActiveRecord $activeRecord, string $field_name): bool
154 {
155 $ilDB = $this->returnDB();
156
157 return $ilDB->tableColumnExists($activeRecord->getConnectorContainerName(), $field_name);
158 }
159
160 public function removeField(ActiveRecord $activeRecord, string $field_name): bool
161 {
162 $ilDB = $this->returnDB();
163 if (!$ilDB->tableColumnExists($activeRecord->getConnectorContainerName(), $field_name)) {
164 throw new arException($field_name, arException::COLUMN_DOES_NOT_EXIST);
165 }
166 $ilDB->dropTableColumn($activeRecord->getConnectorContainerName(), $field_name);
167 return true;
168 }
169
170 public function renameField(ActiveRecord $activeRecord, string $old_name, string $new_name): bool
171 {
172 $ilDB = $this->returnDB();
173 //throw new arException($old_name, arException::COLUMN_DOES_NOT_EXIST);
174 if (!$ilDB->tableColumnExists($activeRecord->getConnectorContainerName(), $old_name)) {
175 return true;
176 }
177 if ($ilDB->tableColumnExists($activeRecord->getConnectorContainerName(), $new_name)) {
178 return true;
179 }
180 //throw new arException($new_name, arException::COLUMN_DOES_ALREADY_EXIST);
181 $ilDB->renameTableColumn($activeRecord->getConnectorContainerName(), $old_name, $new_name);
182 return true;
183 }
184
185 public function create(ActiveRecord $activeRecord): void
186 {
187 $ilDB = $this->returnDB();
188 $ilDB->insert($activeRecord->getConnectorContainerName(), $activeRecord->getArrayForConnector());
189 }
190
194 public function read(ActiveRecord $activeRecord): array
195 {
196 $ilDB = $this->returnDB();
197
198 $query = 'SELECT * FROM ' . $activeRecord->getConnectorContainerName(
199 ) . ' ' . ' WHERE ' . arFieldCache::getPrimaryFieldName($activeRecord) . ' = '
200 . $ilDB->quote($activeRecord->getPrimaryFieldValue(), arFieldCache::getPrimaryFieldType($activeRecord));
201
202 $set = $ilDB->query($query);
203 $records = [];
204 while ($rec = $ilDB->fetchObject($set)) {
205 $records[] = $rec;
206 }
207
208 return $records;
209 }
210
211 public function update(ActiveRecord $activeRecord): void
212 {
213 $ilDB = $this->returnDB();
214
215 $ilDB->update(
216 $activeRecord->getConnectorContainerName(),
217 $activeRecord->getArrayForConnector(),
218 [
219 arFieldCache::getPrimaryFieldName($activeRecord) => [
221 $activeRecord->getPrimaryFieldValue()
222 ]
223 ]
224 );
225 }
226
227 public function delete(ActiveRecord $activeRecord): void
228 {
229 $ilDB = $this->returnDB();
230
231 $ilDB->manipulate(
232 'DELETE FROM ' . $activeRecord->getConnectorContainerName() . ' WHERE ' . arFieldCache::getPrimaryFieldName(
233 $activeRecord
234 ) . ' = '
235 . $ilDB->quote($activeRecord->getPrimaryFieldValue(), arFieldCache::getPrimaryFieldType($activeRecord))
236 );
237 }
238
243 public function readSet(ActiveRecordList $activeRecordList): array
244 {
245 $ilDB = $this->returnDB();
246 $set = $ilDB->query($this->buildQuery($activeRecordList));
247 $records = [];
248 while ($rec = $ilDB->fetchAssoc($set)) {
249 $records[] = $rec;
250 }
251
252 return $records;
253 }
254
255 public function affectedRows(ActiveRecordList $activeRecordList): int
256 {
257 $ilDB = $this->returnDB();
258 $q = $this->buildQuery($activeRecordList);
259
260 $set = $ilDB->query($q);
261
263 return $ilDB->numRows($set);
264 }
265
269 protected function buildQuery(ActiveRecordList $activeRecordList): string
270 {
271 // SELECTS
272 $q = $activeRecordList->getArSelectCollection()->asSQLStatement($this->db);
273 // Concats
274 $q .= $activeRecordList->getArConcatCollection()->asSQLStatement($this->db);
275 $q .= ' FROM ' . $activeRecordList->getAR()->getConnectorContainerName();
276 // JOINS
277 $q .= $activeRecordList->getArJoinCollection()->asSQLStatement($this->db);
278 // WHERE
279 $q .= $activeRecordList->getArWhereCollection()->asSQLStatement($this->db);
280 // HAVING
281 $q .= $activeRecordList->getArHavingCollection()->asSQLStatement($this->db);
282 // ORDER
283 $q .= $activeRecordList->getArOrderCollection()->asSQLStatement($this->db);
284 // LIMIT
285 $q .= $activeRecordList->getArLimitCollection()->asSQLStatement($this->db);
286
287 $activeRecordList->setLastQuery($q);
288
289 return $q;
290 }
291
295 public function quote($value, string $type): string
296 {
297 $ilDB = $this->returnDB();
298
299 return $ilDB->quote($value, $type);
300 }
301}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static setLastQuery(string $last_query)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getConnectorContainerName()
@description Return the Name of your Connector Table
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
nextID(ActiveRecord $activeRecord)
installDatabase(ActiveRecord $activeRecord, array $fields)
renameField(ActiveRecord $activeRecord, string $old_name, string $new_name)
ilDBInterface $db
removeField(ActiveRecord $activeRecord, string $field_name)
quote($value, string $type)
update(ActiveRecord $activeRecord)
affectedRows(ActiveRecordList $activeRecordList)
checkTableExists(ActiveRecord $activeRecord)
truncateDatabase(ActiveRecord $activeRecord)
checkConnection(ActiveRecord $activeRecord)
create(ActiveRecord $activeRecord)
checkFieldExists(ActiveRecord $activeRecord, string $field_name)
resetDatabase(ActiveRecord $activeRecord)
__construct(?ilDBInterface $ilDB=null)
updateIndices(ActiveRecord $activeRecord)
updateDatabase(ActiveRecord $activeRecord)
buildQuery(ActiveRecordList $activeRecordList)
read(ActiveRecord $activeRecord)
readSet(ActiveRecordList $activeRecordList)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const COLUMN_DOES_NOT_EXIST
static getPrimaryFieldType(ActiveRecord $activeRecord)
static getPrimaryFieldName(ActiveRecord $activeRecord)
Interface ilDBInterface.
global $DIC
Definition: shib_login.php:26
$q
Definition: shib_logout.php:23
$GLOBALS["DIC"]
Definition: wac.php:54