ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DatabaseQuerier.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
31
33{
34 use TableNamesHandler;
35
37 protected \ilDBInterface $db;
38 protected \ilLogger $logger;
39
40 public function __construct(
44 ) {
45 $this->data_row_factory = $data_row_factory;
46 $this->db = $db;
47 $this->logger = $logger;
48 }
49
50 public function manipulate(
51 RessourceIDInterface $ressource_id,
53 ): void {
54 $create_assignments = [];
55 $update_assignments = [];
56 $delete_assignments = [];
57 $delete_full_row = false;
58 $create_new_row = false;
59
60 foreach ($row->actions() as $action) {
61 switch ($action->action()) {
62 case Action::CREATE:
63 $create_assignments[] = $action;
64 if ($action->tag()->hasRowInTable()) {
65 $create_new_row = true;
66 }
67 break;
68
69 case Action::UPDATE:
70 $update_assignments[] = $action;
71 break;
72
73 case Action::DELETE:
74 $delete_assignments[] = $action;
75 if ($action->tag()->hasRowInTable()) {
76 $delete_full_row = true;
77 }
78 }
79 }
80
81 if ($delete_full_row) {
82 $this->delete($row->table(), $row->id());
83 return;
84 }
85 if ($create_new_row) {
86 $this->create(
87 $row->table(),
88 $row->id(),
89 $ressource_id,
90 $row->idFromParentTable(),
91 ...$create_assignments
92 );
93 return;
94 }
95 $this->update(
96 $row->table(),
97 $row->id(),
98 ...$create_assignments,
99 ...$update_assignments,
100 ...$delete_assignments
101 );
102 }
103
104 protected function create(
105 string $table,
106 int $id,
107 RessourceIDInterface $ressource_id,
108 int $id_from_parent_table,
109 ActionAssignmentInterface ...$assignments
110 ): void {
111 $this->checkTable($table);
112 $table_name = $this->table($table);
113 $has_parent = $assignments[0]->tag()->hasParent();
114 $parent = $assignments[0]->tag()->parent();
115 $id_field = $this->IDName($table);
116
117 $fields = [
118 $this->db->quoteIdentifier($id_field),
119 'rbac_id',
120 'obj_id',
121 'obj_type',
122 ];
123 $values = [
124 $this->db->quote($id, \ilDBConstants::T_INTEGER),
125 $this->db->quote($ressource_id->objID(), \ilDBConstants::T_INTEGER),
126 $this->db->quote($ressource_id->subID(), \ilDBConstants::T_INTEGER),
127 $this->db->quote($ressource_id->type(), \ilDBConstants::T_TEXT)
128 ];
129 if ($has_parent) {
130 $fields[] = 'parent_type';
131 $values[] = $this->db->quote($parent, \ilDBConstants::T_TEXT);
132 $fields[] = 'parent_id';
133 $values[] = $this->db->quote($id_from_parent_table, \ilDBConstants::T_INTEGER);
134 }
135 foreach ($assignments as $assignment) {
136 $tag = $assignment->tag();
137 if ($tag->hasData() && ($data = $assignment->value()) !== '') {
138 $fields[] = $this->db->quoteIdentifier($tag->dataField());
139 $values[] = $this->db->quote($data, \ilDBConstants::T_TEXT);
140 }
141 }
142
143 $this->db->manipulate(
144 'INSERT INTO ' . $this->db->quoteIdentifier($table_name) . ' (' .
145 implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')'
146 );
147 }
148
152 public function read(
153 RessourceIDInterface $ressource_id,
154 int $id_from_parent_table,
155 TagInterface ...$tags
156 ): \Generator {
157 $table = $tags[0]->table();
158 $this->checkTable($table);
159 $has_parent = $tags[0]->hasParent();
160 $parent = $tags[0]->parent();
161 $id_field = $this->IDName($table);
162
163 $selected_fields[] = $this->db->quoteIdentifier($id_field);
164 foreach ($tags as $tag) {
165 if ($tag->hasData()) {
166 $selected_fields[] = $this->db->quoteIdentifier($tag->dataField());
167 }
168 }
169
170 $where[] = 'rbac_id = ' . $this->db->quote($ressource_id->objID(), \ilDBConstants::T_INTEGER);
171 $where[] = 'obj_id = ' . $this->db->quote($ressource_id->subID(), \ilDBConstants::T_INTEGER);
172 $where[] = 'obj_type = ' . $this->db->quote($ressource_id->type(), \ilDBConstants::T_TEXT);
173 if ($has_parent) {
174 $where[] = 'parent_type = ' . $this->db->quote($parent, \ilDBConstants::T_TEXT);
175 $where[] = 'parent_id = ' . $this->db->quote($id_from_parent_table, \ilDBConstants::T_INTEGER);
176 }
177
178 $order = 'ORDER BY ' . $this->db->quoteIdentifier($id_field);
179
180 $result = $this->db->query(
181 'SELECT ' . implode(', ', $selected_fields) . ' FROM ' .
182 $this->db->quoteIdentifier($this->table($table)) . ' WHERE ' .
183 implode(' AND ', $where) . ' ' . $order
184 );
185
186 while ($row = $this->db->fetchAssoc($result)) {
187 $data = [];
188 $id = 0;
189 foreach ($row as $field => $value) {
190 if ($field === $id_field) {
191 $id = $value;
192 continue;
193 }
194 $data[] = $this->data_row_factory->field($field, $value ?? '');
195 }
196 yield $this->data_row_factory->row($id, $table, ...$data);
197 }
198 }
199
200 protected function update(
201 string $table,
202 int $id,
203 ActionAssignmentInterface ...$assignments
204 ): void {
205 $this->checkTable($table);
206 $id_field = $this->IDName($table);
207
208 $updated_fields = [];
209 foreach ($assignments as $assignment) {
210 $tag = $assignment->tag();
211 if (!$tag->hasData()) {
212 continue;
213 }
214 if ($assignment->action() === Action::DELETE) {
215 $updated_fields[] = $this->db->quoteIdentifier($tag->dataField()) . " = ''";
216 continue;
217 }
218 if (($data = $assignment->value()) !== '') {
219 $updated_fields[] = $this->db->quoteIdentifier($tag->dataField()) . ' = ' .
220 $this->db->quote($data, \ilDBConstants::T_TEXT);
221 }
222 }
223
224 if (empty($updated_fields)) {
225 return;
226 }
227
228 $this->db->manipulate(
229 'UPDATE ' . $this->db->quoteIdentifier($this->table($table)) . ' SET ' .
230 implode(', ', $updated_fields) . ' WHERE ' . $this->db->quoteIdentifier($id_field) .
231 ' = ' . $this->db->quote($id, \ilDBConstants::T_INTEGER)
232 );
233 }
234
235 protected function delete(
236 string $table,
237 int $id
238 ): void {
239 $this->checkTable($table);
240 $table_name = $this->table($table);
241 $id_field = $this->IDName($table);
242
243 $this->db->manipulate(
244 'DELETE FROM ' . $this->db->quoteIdentifier($table_name) . ' WHERE ' .
245 $this->db->quoteIdentifier($id_field) . ' = ' .
246 $this->db->quote($id, \ilDBConstants::T_INTEGER)
247 );
248 }
249
250 public function deleteAll(RessourceIDInterface $ressource_id): void
251 {
252 $rbac_id = $ressource_id->objID();
253 $obj_id = $ressource_id->subID();
254 $obj_type = $ressource_id->type();
255 foreach (LOMDictionaryInitiator::TABLES as $table) {
256 $query = "DELETE FROM " . $this->db->quoteIdentifier($table) . " " .
257 "WHERE rbac_id = " . $this->db->quote($rbac_id, \ilDBConstants::T_INTEGER) . " " .
258 "AND obj_id = " . $this->db->quote($obj_id, \ilDBConstants::T_INTEGER) . " " .
259 "AND obj_type = " . $this->db->quote($obj_type, \ilDBConstants::T_TEXT);
260
261 $this->db->manipulate($query);
262 }
263 }
264
265 public function nextID(string $table): int
266 {
267 $this->checkTable($table);
268 return $this->db->nextId($this->table($table));
269 }
270}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
manipulate(RessourceIDInterface $ressource_id, AssignmentRowInterface $row)
read(RessourceIDInterface $ressource_id, int $id_from_parent_table, TagInterface ... $tags)
create(string $table, int $id, RessourceIDInterface $ressource_id, int $id_from_parent_table, ActionAssignmentInterface ... $assignments)
update(string $table, int $id, ActionAssignmentInterface ... $assignments)
__construct(ResultFactoryInterface $data_row_factory, \ilDBInterface $db, \ilLogger $logger)
Component logger with individual log levels by component id.
objID()
Object ID (NOT ref_id!) of the parent repository object (e.g for page objects the obj_id of the conte...
type()
(Sub-)Type of the object (e.g st,pg,crs ...) NOTE: In the metadata tables, this corresponds to the fi...
subID()
ID of the object carrying the metadata, which might be a subobject of an enclosing repository object ...
Interface ilDBInterface.