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