ILIAS  trunk Revision v11.0_alpha-1761-g6dbbfa7b760
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilDBPdoManager Class Reference

Class ilDBPdoManager. More...

+ Inheritance diagram for ilDBPdoManager:
+ Collaboration diagram for ilDBPdoManager:

Public Member Functions

 __construct (protected \PDO $pdo, protected \ilDBPdo $db_instance)
 ilDBPdoManager constructor. More...
 
 getQueryUtils ()
 
 getDBInstance ()
 
 listTables (?string $database=null)
 
 listSequences (?string $database=null)
 
 createConstraint (string $table, string $name, array $definition)
 
 createSequence (string $seq_name, int $start=1, array $options=[])
 
 alterTable (string $name, array $changes, bool $check)
 
 createTable (string $name, array $fields, array $options=[])
 
 getIndexName (string $idx)
 
 getSequenceName (string $sqn)
 
 listTableFields (string $table)
 
 listTableConstraints (string $table)
 
 listTableIndexes (string $table)
 
 createIndex (string $table, string $name, array $definition)
 
 dropIndex (string $table, string $name)
 
 dropSequence (string $seq_name)
 
 getTableCreationQuery (string $name, array $fields, array $options=[])
 
 dropConstraint (string $table, string $name, bool $primary=false)
 
 dropTable (string $name)
 
 addForeignKey (string $foreign_key_name, array $field_names, string $table_name, array $reference_field_names, string $reference_table, ?ForeignKeyConstraints $on_update=null, ?ForeignKeyConstraints $on_delete=null)
 
 dropForeignKey (string $foreign_key_name, string $table_name)
 
 foreignKeyExists (string $foreign_key_name, string $table_name)
 

Protected Member Functions

 fixSequenceName (string $sqn, bool $check=false)
 
 fixIndexName (string $idx)
 

Protected Attributes

ilQueryUtils $query_utils = null
 

Detailed Description

Constructor & Destructor Documentation

◆ __construct()

ilDBPdoManager::__construct ( protected \PDO  $pdo,
protected \ilDBPdo  $db_instance 
)

ilDBPdoManager constructor.

Definition at line 32 of file class.ilDBPdoManager.php.

33  {
34  }

Member Function Documentation

◆ addForeignKey()

ilDBPdoManager::addForeignKey ( string  $foreign_key_name,
array  $field_names,
string  $table_name,
array  $reference_field_names,
string  $reference_table,
?ForeignKeyConstraints  $on_update = null,
?ForeignKeyConstraints  $on_delete = null 
)
Parameters
string[]$field_names
string[]$reference_field_names

Definition at line 476 of file class.ilDBPdoManager.php.

References null.

484  : bool {
485  $table = $this->db_instance->quoteIdentifier($table_name, true);
486  $reference_table = $this->db_instance->quoteIdentifier($reference_table, true);
487  $field_names = implode(",", $field_names);
488  $field_names = $this->db_instance->quoteIdentifier($field_names, true);
489  $reference_field_names = implode(",", $reference_field_names);
490  $reference_field_names = $this->db_instance->quoteIdentifier($reference_field_names, true);
491  $foreign_key_name = $this->db_instance->quoteIdentifier($foreign_key_name, true);
492  $update = '';
493  if ($on_update !== null) {
494  $on_update = $on_update->value;
495  $update = "ON UPDATE $on_update";
496  }
497  $delete = '';
498  if ($on_delete !== null) {
499  $on_delete = $on_delete->value;
500  $delete = "ON DELETE $on_delete";
501  }
502  $query = "ALTER TABLE
503  $table ADD CONSTRAINT
504  $foreign_key_name FOREIGN KEY ($field_names)
505  REFERENCES $reference_table ($reference_field_names)
506  $update
507  $delete
508  ";
509 
510  return (bool) $this->pdo->exec($query);
511  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null

◆ alterTable()

ilDBPdoManager::alterTable ( string  $name,
array  $changes,
bool  $check 
)
Exceptions

Implements ilDBManager.

Definition at line 179 of file class.ilDBPdoManager.php.

References null.

179  : bool
180  {
181  $db = $this->db_instance;
182 
183  foreach (array_keys($changes) as $change_name) {
184  switch ($change_name) {
185  case 'add':
186  case 'remove':
187  case 'change':
188  case 'rename':
189  case 'name':
190  break;
191  default:
192  throw new ilDatabaseException('change type "' . $change_name . '" not yet supported');
193  }
194  }
195 
196  if ($check) {
197  return true;
198  }
199 
200  $query = '';
201  if (!empty($changes['name'])) {
202  $change_name = $db->quoteIdentifier($changes['name']);
203  $query .= 'RENAME TO ' . $change_name;
204  }
205 
206  if (!empty($changes['add']) && is_array($changes['add'])) {
207  foreach ($changes['add'] as $field_name => $field) {
208  if ($query !== '') {
209  $query .= ', ';
210  }
211  $fd = $db->getFieldDefinition();
212  if ($fd !== null) {
213  $query .= 'ADD ' . $fd->getDeclaration($field['type'], $field_name, $field);
214  }
215  }
216  }
217 
218  if (!empty($changes['remove']) && is_array($changes['remove'])) {
219  foreach (array_keys($changes['remove']) as $field_name) {
220  if ($query !== '') {
221  $query .= ', ';
222  }
223  $field_name = $db->quoteIdentifier($field_name);
224  $query .= 'DROP ' . $field_name;
225  }
226  }
227 
228  $rename = [];
229  if (!empty($changes['rename']) && is_array($changes['rename'])) {
230  foreach ($changes['rename'] as $field_name => $field) {
231  $rename[$field['name']] = $field_name;
232  }
233  }
234 
235  if (!empty($changes['change']) && is_array($changes['change'])) {
236  foreach ($changes['change'] as $field_name => $field) {
237  if ($query !== '') {
238  $query .= ', ';
239  }
240  if (isset($rename[$field_name])) {
241  $old_field_name = $rename[$field_name];
242  unset($rename[$field_name]);
243  } else {
244  $old_field_name = $field_name;
245  }
246  $old_field_name = $db->quoteIdentifier($old_field_name);
247  $fd = $this->db_instance->getFieldDefinition();
248  if ($fd !== null) {
249  $query .= "CHANGE $old_field_name " . $fd
250  ->getDeclaration(
251  $field['definition']['type'],
252  $field_name,
253  $field['definition']
254  );
255  }
256  }
257  }
258 
259  if (!empty($rename) && is_array($rename)) {
260  foreach ($rename as $renamed_field) {
261  if ($query !== '') {
262  $query .= ', ';
263  }
264  $field = $changes['rename'][$renamed_field];
265  $renamed_field = $db->quoteIdentifier($renamed_field);
266  $fd = $this->db_instance->getFieldDefinition();
267  if ($fd !== null) {
268  $query .= 'CHANGE ' . $renamed_field . ' ' . $fd
269  ->getDeclaration(
270  $field['definition']['type'],
271  $field['name'],
272  $field['definition']
273  );
274  }
275  }
276  }
277 
278  if ($query === '') {
279  return true;
280  }
281 
282  $name = $db->quoteIdentifier($name, true);
283 
284  $statement = "ALTER TABLE $name $query";
285 
286  return (bool) $this->pdo->exec($statement);
287  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$check
Definition: buildRTE.php:81

◆ createConstraint()

ilDBPdoManager::createConstraint ( string  $table,
string  $name,
array  $definition 
)
Exceptions

Implements ilDBManager.

Definition at line 112 of file class.ilDBPdoManager.php.

Referenced by ilDBPdo\addUniqueConstraint().

112  : bool
113  {
114  $db = $this->db_instance;
115 
116  $table = $db->quoteIdentifier($table, true);
117  $name = $db->quoteIdentifier($db->getIndexName($name), true);
118  $query = "ALTER TABLE $table ADD CONSTRAINT $name";
119  if (!empty($definition['primary'])) {
120  $query .= ' PRIMARY KEY';
121  } elseif (!empty($definition['unique'])) {
122  $query .= ' UNIQUE';
123  }
124  $fields = [];
125  foreach (array_keys($definition['fields']) as $field) {
126  $fields[] = $db->quoteIdentifier($field, true);
127  }
128  $query .= ' (' . implode(', ', $fields) . ')';
129 
130  return (bool) $this->pdo->exec($query);
131  }
+ Here is the caller graph for this function:

◆ createIndex()

ilDBPdoManager::createIndex ( string  $table,
string  $name,
array  $definition 
)

Implements ilDBManager.

Definition at line 409 of file class.ilDBPdoManager.php.

409  : bool
410  {
411  $table = $this->db_instance->quoteIdentifier($table, true);
412  $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name), true);
413  $query = "CREATE INDEX $name ON $table";
414  $fields = [];
415  foreach ($definition['fields'] as $field => $fieldinfo) {
416  if (!empty($fieldinfo['length'])) {
417  $fields[] = $this->db_instance->quoteIdentifier($field, true) . '(' . $fieldinfo['length'] . ')';
418  } else {
419  $fields[] = $this->db_instance->quoteIdentifier($field, true);
420  }
421  }
422  $query .= ' (' . implode(', ', $fields) . ')';
423 
424  return (bool) $this->pdo->exec($query);
425  }

◆ createSequence()

ilDBPdoManager::createSequence ( string  $seq_name,
int  $start = 1,
array  $options = [] 
)

Implements ilDBManager.

Definition at line 133 of file class.ilDBPdoManager.php.

References ilDBConstants\SEQUENCE_COLUMNS_NAME.

133  : bool
134  {
135  $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
136  $seqcol_name = $this->db_instance->quoteIdentifier(ilDBConstants::SEQUENCE_COLUMNS_NAME);
137 
138  $options_strings = [];
139 
140  if (!empty($options['comment'])) {
141  $options_strings['comment'] = 'COMMENT = ' . $this->db_instance->quote($options['comment'], 'text');
142  }
143 
144  if (!empty($options['charset'])) {
145  $options_strings['charset'] = 'DEFAULT CHARACTER SET ' . $options['charset'];
146  if (!empty($options['collate'])) {
147  $options_strings['charset'] .= ' COLLATE ' . $options['collate'];
148  }
149  }
150 
151  $type = false;
152  if (!empty($options['type'])) {
153  $type = $options['type'];
154  }
155  if ($type) {
156  $options_strings[] = "ENGINE = $type";
157  }
158 
159  $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
160 
161  if (!empty($options_strings)) {
162  $query .= ' ' . implode(' ', $options_strings);
163  }
164  $this->pdo->exec($query);
165 
166  if ($start == 1) {
167  return true;
168  }
169 
170  $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (" . ($start - 1) . ')';
171  $this->pdo->exec($query);
172 
173  return true;
174  }

◆ createTable()

ilDBPdoManager::createTable ( string  $name,
array  $fields,
array  $options = [] 
)

Definition at line 289 of file class.ilDBPdoManager.php.

References getQueryUtils().

289  : bool
290  {
291  $options['type'] = $this->db_instance->getStorageEngine();
292 
293  return (bool) $this->pdo->exec($this->getQueryUtils()->createTable($name, $fields, $options));
294  }
createTable(string $name, array $fields, array $options=[])
+ Here is the call graph for this function:

◆ dropConstraint()

ilDBPdoManager::dropConstraint ( string  $table,
string  $name,
bool  $primary = false 
)

Implements ilDBManager.

Definition at line 450 of file class.ilDBPdoManager.php.

References getDBInstance().

450  : bool
451  {
452  $db = $this->getDBInstance();
453  $table = $db->quoteIdentifier($table, true);
454  if ($primary || strtolower($name) === 'primary') {
455  $query = "ALTER TABLE $table DROP PRIMARY KEY";
456  } else {
457  $name = $db->quoteIdentifier($db->getIndexName($name), true);
458  $query = "ALTER TABLE $table DROP INDEX $name";
459  }
460 
461  return (bool) $this->pdo->exec($query);
462  }
+ Here is the call graph for this function:

◆ dropForeignKey()

ilDBPdoManager::dropForeignKey ( string  $foreign_key_name,
string  $table_name 
)

Definition at line 513 of file class.ilDBPdoManager.php.

513  : bool
514  {
515  $table = $this->db_instance->quoteIdentifier($table_name, true);
516  $name = $this->db_instance->quoteIdentifier($foreign_key_name, true);
517  $query = "ALTER TABLE $table DROP FOREIGN KEY $name;";
518 
519  return (bool) $this->pdo->exec($query);
520  }

◆ dropIndex()

ilDBPdoManager::dropIndex ( string  $table,
string  $name 
)

Implements ilDBManager.

Definition at line 427 of file class.ilDBPdoManager.php.

427  : bool
428  {
429  $table = $this->db_instance->quoteIdentifier($table, true);
430  $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name), true);
431 
432  return (bool) $this->pdo->exec("DROP INDEX $name ON $table");
433  }

◆ dropSequence()

ilDBPdoManager::dropSequence ( string  $seq_name)

Implements ilDBManager.

Definition at line 435 of file class.ilDBPdoManager.php.

435  : bool
436  {
437  $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
438 
439  return (bool) $this->pdo->exec("DROP TABLE $sequence_name");
440  }

◆ dropTable()

ilDBPdoManager::dropTable ( string  $name)
Parameters
$namestring

Implements ilDBManager.

Definition at line 464 of file class.ilDBPdoManager.php.

References getDBInstance().

464  : bool
465  {
466  $db = $this->getDBInstance();
467  $name = $db->quoteIdentifier($name, true);
468 
469  return (bool) $this->pdo->exec("DROP TABLE $name");
470  }
+ Here is the call graph for this function:

◆ fixIndexName()

ilDBPdoManager::fixIndexName ( string  $idx)
protected

Definition at line 398 of file class.ilDBPdoManager.php.

References ilDBPdoFieldDefinition\INDEX_FORMAT.

Referenced by listTableConstraints(), and listTableIndexes().

398  : string
399  {
400  $idx_pattern = '/^' . preg_replace('/%s/', '([a-z0-9_]+)', ilDBPdoFieldDefinition::INDEX_FORMAT) . '$/i';
401  $idx_name = preg_replace($idx_pattern, '\\1', $idx);
402  if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
403  return $idx_name;
404  }
405 
406  return $idx;
407  }
+ Here is the caller graph for this function:

◆ fixSequenceName()

ilDBPdoManager::fixSequenceName ( string  $sqn,
bool  $check = false 
)
protected

Definition at line 69 of file class.ilDBPdoManager.php.

References ilDBConstants\SEQUENCE_FORMAT.

Referenced by listSequences().

69  : string
70  {
71  $seq_pattern = '/^' . preg_replace('/%s/', '([a-z0-9_]+)', ilDBConstants::SEQUENCE_FORMAT) . '$/i';
72  $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
73  if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
74  return $seq_name;
75  }
76 
77  return $sqn;
78  }
+ Here is the caller graph for this function:

◆ foreignKeyExists()

ilDBPdoManager::foreignKeyExists ( string  $foreign_key_name,
string  $table_name 
)

Definition at line 522 of file class.ilDBPdoManager.php.

522  : bool
523  {
524  $query = "SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='FOREIGN KEY';";
525  $result_set = $this->db_instance->query($query);
526  while ($foreign_data = $this->db_instance->fetchAssoc($result_set)) {
527  if (array_key_exists(
528  'CONSTRAINT_NAME',
529  $foreign_data
530  ) && $foreign_data['CONSTRAINT_NAME'] === $foreign_key_name) {
531  return true;
532  }
533  }
534  return false;
535  }

◆ getDBInstance()

ilDBPdoManager::getDBInstance ( )

Definition at line 45 of file class.ilDBPdoManager.php.

Referenced by dropConstraint(), dropTable(), and listTableConstraints().

45  : \ilDBPdo
46  {
47  return $this->db_instance;
48  }
Class pdoDB.
+ Here is the caller graph for this function:

◆ getIndexName()

ilDBPdoManager::getIndexName ( string  $idx)

Implements ilDBPdoManagerInterface.

Definition at line 296 of file class.ilDBPdoManager.php.

296  : string
297  {
298  return $this->db_instance->getIndexName($idx);
299  }

◆ getQueryUtils()

ilDBPdoManager::getQueryUtils ( )

Definition at line 36 of file class.ilDBPdoManager.php.

References $query_utils, and null.

Referenced by createTable(), and getTableCreationQuery().

37  {
38  if ($this->query_utils === null) {
39  $this->query_utils = new ilMySQLQueryUtils($this->db_instance);
40  }
41 
42  return $this->query_utils;
43  }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Class ilMySQLQueryUtils.
ilQueryUtils $query_utils
Class ilQueryUtils.
+ Here is the caller graph for this function:

◆ getSequenceName()

ilDBPdoManager::getSequenceName ( string  $sqn)

Implements ilDBPdoManagerInterface.

Definition at line 301 of file class.ilDBPdoManager.php.

301  : string
302  {
303  return $this->db_instance->getSequenceName($sqn);
304  }

◆ getTableCreationQuery()

ilDBPdoManager::getTableCreationQuery ( string  $name,
array  $fields,
array  $options = [] 
)
Exceptions

Definition at line 445 of file class.ilDBPdoManager.php.

References getQueryUtils().

445  : string
446  {
447  return $this->getQueryUtils()->createTable($name, $fields, $options);
448  }
+ Here is the call graph for this function:

◆ listSequences()

ilDBPdoManager::listSequences ( ?string  $database = null)
Returns
string[]

Implements ilDBManager.

Definition at line 83 of file class.ilDBPdoManager.php.

References $res, fixSequenceName(), and null.

83  : array
84  {
85  $query = "SHOW TABLES LIKE '%_seq'";
86  if (!is_null($database)) {
87  $query .= " FROM $database";
88  }
89 
90  $res = $this->db_instance->query($query);
91 
92  $result = [];
93  while ($table_name = $this->db_instance->fetchAssoc($res)) {
94  $sqn = $this->fixSequenceName(reset($table_name), true);
95  if ($sqn !== '' && $sqn !== '0') {
96  $result[] = $sqn;
97  }
98  }
99  if ($this->db_instance->options['portability'] ?? null) {
100  return array_map(
101  ($this->db_instance->options['field_case'] === CASE_LOWER ? 'strtolower' : 'strtoupper'),
102  $result
103  );
104  }
105 
106  return $result;
107  }
$res
Definition: ltiservices.php:66
fixSequenceName(string $sqn, bool $check=false)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:

◆ listTableConstraints()

ilDBPdoManager::listTableConstraints ( string  $table)
Returns
string[]

Implements ilDBManager.

Definition at line 322 of file class.ilDBPdoManager.php.

References fixIndexName(), getDBInstance(), and null.

322  : array
323  {
324  $key_name = 'Key_name';
325  $non_unique = 'Non_unique';
326 
327  $db = $this->getDBInstance();
328  if ($db->options['portability'] ?? null) {
329  if ($db->options['field_case'] == CASE_LOWER) {
330  $key_name = strtolower($key_name);
331  $non_unique = strtolower($non_unique);
332  } else {
333  $key_name = strtoupper($key_name);
334  $non_unique = strtoupper($non_unique);
335  }
336  }
337 
338  $table = $this->db_instance->quoteIdentifier($table);
339  $query = "SHOW INDEX FROM $table";
340  $result_set = $this->db_instance->query($query);
341 
342  $result = [];
343  while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
344  if (!$index_data[$non_unique]) {
345  $index = $index_data[$key_name] !== 'PRIMARY' ? $this->fixIndexName($index_data[$key_name]) : 'PRIMARY';
346  if (!empty($index)) {
347  $index = strtolower($index);
348  $result[$index] = true;
349  }
350  }
351  }
352 
353  if ($this->db_instance->options['portability'] ?? null) {
354  $result = array_change_key_case($result, $this->db_instance->options['field_case']);
355  }
356 
357  return array_keys($result);
358  }
fixIndexName(string $idx)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:

◆ listTableFields()

ilDBPdoManager::listTableFields ( string  $table)
Returns
string[]

Implements ilDBManager.

Definition at line 306 of file class.ilDBPdoManager.php.

References $data.

306  : array
307  {
308  $table = $this->db_instance->quoteIdentifier($table);
309  $query = "SHOW COLUMNS FROM $table";
310  $result = $this->db_instance->query($query);
311  $return = [];
312  while ($data = $this->db_instance->fetchObject($result)) {
313  $return[] = $data->Field;
314  }
315 
316  return $return;
317  }

◆ listTableIndexes()

ilDBPdoManager::listTableIndexes ( string  $table)
Returns
string[]

Implements ilDBManager.

Definition at line 363 of file class.ilDBPdoManager.php.

References fixIndexName(), and null.

363  : array
364  {
365  $key_name = 'Key_name';
366  $non_unique = 'Non_unique';
367  if ($this->db_instance->options['portability'] ?? null) {
368  if ($this->db_instance->options['field_case'] == CASE_LOWER) {
369  $key_name = strtolower($key_name);
370  $non_unique = strtolower($non_unique);
371  } else {
372  $key_name = strtoupper($key_name);
373  $non_unique = strtoupper($non_unique);
374  }
375  }
376 
377  $table = $this->db_instance->quoteIdentifier($table);
378  $query = "SHOW INDEX FROM $table";
379  $result_set = $this->db_instance->query($query);
380  $indexes = [];
381  while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
382  $indexes[] = $index_data;
383  }
384  $result = [];
385  foreach ($indexes as $index_data) {
386  if ($index_data[$non_unique] && ($index = $this->fixIndexName($index_data[$key_name]))) {
387  $result[$index] = true;
388  }
389  }
390 
391  if ($this->db_instance->options['portability'] ?? null) {
392  $result = array_change_key_case($result, $this->db_instance->options['field_case']);
393  }
394 
395  return array_keys($result);
396  }
fixIndexName(string $idx)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
+ Here is the call graph for this function:

◆ listTables()

ilDBPdoManager::listTables ( ?string  $database = null)
Returns
int[]|string[]

Implements ilDBManager.

Definition at line 53 of file class.ilDBPdoManager.php.

References $data, and $r.

Referenced by ilDBPdoMySQL\migrateAllTablesToCollation().

53  : array
54  {
55  $str = 'SHOW TABLES ' . ($database ? ' IN ' . $database : '');
56  $r = $this->pdo->query($str);
57  $tables = [];
58 
59  $sequence_identifier = "_seq";
60  while ($data = $r->fetchColumn()) {
61  if (!preg_match("/$sequence_identifier$/um", $data)) {
62  $tables[] = $data;
63  }
64  }
65 
66  return $tables;
67  }
$r
+ Here is the caller graph for this function:

Field Documentation

◆ $query_utils

ilQueryUtils ilDBPdoManager::$query_utils = null
protected

Definition at line 27 of file class.ilDBPdoManager.php.

Referenced by getQueryUtils().


The documentation for this class was generated from the following file: