19declare(strict_types=1);
39 if ($this->query_utils ===
null) {
48 return $this->db_instance;
54 public function listTables(?
string $database =
null): array
56 $str =
'SHOW TABLES ' . ($database ?
' IN ' . $database :
'');
57 $r = $this->pdo->query($str);
60 $sequence_identifier =
"_seq";
61 while (
$data = $r->fetchColumn()) {
62 if (!preg_match(
"/$sequence_identifier$/um", (
string)
$data)) {
73 $seq_name = preg_replace($seq_pattern,
'\\1', $sqn);
74 if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
86 $query =
"SHOW TABLES LIKE '%_seq'";
87 if (!is_null($database)) {
88 $query .=
" FROM $database";
91 $res = $this->db_instance->query($query);
94 while ($table_name = $this->db_instance->fetchAssoc(
$res)) {
96 if ($sqn !==
'' && $sqn !==
'0') {
101 if ($this->db_instance->options[
'portability'] ??
null) {
103 ($this->db_instance->options[
'field_case'] === CASE_LOWER ?
'strtolower' :
'strtoupper'),
116 $db = $this->db_instance;
118 $table = $db->quoteIdentifier($table,
true);
119 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
120 $query =
"ALTER TABLE $table ADD CONSTRAINT $name";
121 if (!empty($definition[
'primary'])) {
122 $query .=
' PRIMARY KEY';
123 } elseif (!empty($definition[
'unique'])) {
127 foreach (array_keys($definition[
'fields']) as $field) {
128 $fields[] = $db->quoteIdentifier($field,
true);
130 $query .=
' (' . implode(
', ', $fields) .
')';
132 return (
bool) $this->pdo->exec($query);
137 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
140 $options_strings = [];
143 $options_strings[
'comment'] =
'COMMENT = ' . $this->db_instance->quote(
$options[
'comment'],
'text');
147 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET ' .
$options[
'charset'];
149 $options_strings[
'charset'] .=
' COLLATE ' .
$options[
'collate'];
158 $options_strings[] =
"ENGINE = $type";
161 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
163 if (!empty($options_strings)) {
164 $query .=
' ' . implode(
' ', $options_strings);
166 $this->pdo->exec($query);
172 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (" . ($start - 1) .
')';
173 $this->pdo->exec($query);
183 $db = $this->db_instance;
185 foreach (array_keys($changes) as $change_name) {
186 switch ($change_name) {
203 if (!empty($changes[
'name'])) {
204 $change_name = $db->quoteIdentifier($changes[
'name']);
205 $query .=
'RENAME TO ' . $change_name;
208 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
209 foreach ($changes[
'add'] as $field_name => $field) {
213 $fd = $db->getFieldDefinition();
215 $query .=
'ADD ' . $fd->getDeclaration($field[
'type'], $field_name, $field);
220 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
221 foreach (array_keys($changes[
'remove']) as $field_name) {
225 $field_name = $db->quoteIdentifier($field_name);
226 $query .=
'DROP ' . $field_name;
231 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
232 foreach ($changes[
'rename'] as $field_name => $field) {
233 $rename[$field[
'name']] = $field_name;
237 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
238 foreach ($changes[
'change'] as $field_name => $field) {
242 if (isset($rename[$field_name])) {
243 $old_field_name = $rename[$field_name];
244 unset($rename[$field_name]);
246 $old_field_name = $field_name;
248 $old_field_name = $db->quoteIdentifier($old_field_name);
249 $fd = $this->db_instance->getFieldDefinition();
251 $query .=
"CHANGE $old_field_name " . $fd
253 $field[
'definition'][
'type'],
261 if (!empty($rename) && is_array($rename)) {
262 foreach ($rename as $renamed_field) {
266 $field = $changes[
'rename'][$renamed_field];
267 $renamed_field = $db->quoteIdentifier($renamed_field);
268 $fd = $this->db_instance->getFieldDefinition();
270 $query .=
'CHANGE ' . $renamed_field .
' ' . $fd
272 $field[
'definition'][
'type'],
284 $name = $db->quoteIdentifier($name,
true);
286 $statement =
"ALTER TABLE $name $query";
288 return (
bool) $this->pdo->exec($statement);
293 $options[
'type'] = $this->db_instance->getStorageEngine();
300 return $this->db_instance->getIndexName($idx);
305 return $this->db_instance->getSequenceName($sqn);
310 $table = $this->db_instance->quoteIdentifier($table);
311 $query =
"SHOW COLUMNS FROM $table";
312 $result = $this->db_instance->query($query);
314 while (
$data = $this->db_instance->fetchObject($result)) {
315 $return[] =
$data->Field;
326 $key_name =
'Key_name';
327 $non_unique =
'Non_unique';
331 if ($db->options[
'portability'] ??
null) {
332 if ($db->options[
'field_case'] == CASE_LOWER) {
333 $key_name = strtolower($key_name);
334 $non_unique = strtolower($non_unique);
336 $key_name = strtoupper($key_name);
337 $non_unique = strtoupper($non_unique);
341 $table = $this->db_instance->quoteIdentifier($table);
342 $query =
"SHOW INDEX FROM $table";
343 $result_set = $this->db_instance->query($query);
346 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
347 if (!$index_data[$non_unique]) {
348 $index = $index_data[$key_name] !==
'PRIMARY' ? $this->
fixIndexName($index_data[$key_name]) :
'PRIMARY';
349 if (!empty($index)) {
350 $index = strtolower($index);
351 $result[$index] =
true;
356 if ($this->db_instance->options[
'portability'] ??
null) {
358 $result = array_change_key_case($result, $this->db_instance->options[
'field_case']);
361 return array_keys($result);
369 $key_name =
'Key_name';
370 $non_unique =
'Non_unique';
372 if ($this->db_instance->options[
'portability'] ??
null) {
373 if ($this->db_instance->options[
'field_case'] == CASE_LOWER) {
374 $key_name = strtolower($key_name);
375 $non_unique = strtolower($non_unique);
377 $key_name = strtoupper($key_name);
378 $non_unique = strtoupper($non_unique);
382 $table = $this->db_instance->quoteIdentifier($table);
383 $query =
"SHOW INDEX FROM $table";
384 $result_set = $this->db_instance->query($query);
386 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
387 $indexes[] = $index_data;
390 foreach ($indexes as $index_data) {
391 if ($index_data[$non_unique] && ($index = $this->
fixIndexName($index_data[$key_name]))) {
392 $result[$index] =
true;
397 if ($this->db_instance->options[
'portability'] ??
null) {
398 $result = array_change_key_case($result, $this->db_instance->options[
'field_case']);
401 return array_keys($result);
406 $idx_pattern =
'/^' . preg_replace(
'/%s/',
'([a-z0-9_]+)', FieldDefinition::INDEX_FORMAT) .
'$/i';
407 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
408 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
415 public function createIndex(
string $table,
string $name, array $definition): bool
417 $table = $this->db_instance->quoteIdentifier($table,
true);
418 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name),
true);
419 $query =
"CREATE INDEX $name ON $table";
421 foreach ($definition[
'fields'] as $field => $fieldinfo) {
422 if (!empty($fieldinfo[
'length'])) {
423 $fields[] = $this->db_instance->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
425 $fields[] = $this->db_instance->quoteIdentifier($field,
true);
428 $query .=
' (' . implode(
', ', $fields) .
')';
430 return (
bool) $this->pdo->exec($query);
433 public function dropIndex(
string $table,
string $name): bool
435 $table = $this->db_instance->quoteIdentifier($table,
true);
436 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name),
true);
438 return (
bool) $this->pdo->exec(
"DROP INDEX $name ON $table");
443 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
445 return (
bool) $this->pdo->exec(
"DROP TABLE $sequence_name");
456 public function dropConstraint(
string $table,
string $name,
bool $primary =
false): bool
459 $table = $db->quoteIdentifier($table,
true);
460 if ($primary || strtolower($name) ===
'primary') {
461 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
463 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
464 $query =
"ALTER TABLE $table DROP INDEX $name";
467 return (
bool) $this->pdo->exec($query);
473 $name = $db->quoteIdentifier($name,
true);
475 return (
bool) $this->pdo->exec(
"DROP TABLE $name");
483 string $foreign_key_name,
486 array $reference_field_names,
487 string $reference_table,
491 $table = $this->db_instance->quoteIdentifier($table_name,
true);
492 $reference_table = $this->db_instance->quoteIdentifier($reference_table,
true);
493 $field_names = implode(
",", $field_names);
494 $field_names = $this->db_instance->quoteIdentifier($field_names,
true);
495 $reference_field_names = implode(
",", $reference_field_names);
496 $reference_field_names = $this->db_instance->quoteIdentifier($reference_field_names,
true);
497 $foreign_key_name = $this->db_instance->quoteIdentifier($foreign_key_name,
true);
499 if ($on_update !==
null) {
500 $on_update = $on_update->value;
501 $update =
"ON UPDATE $on_update";
504 if ($on_delete !==
null) {
505 $on_delete = $on_delete->value;
506 $delete =
"ON DELETE $on_delete";
508 $query =
"ALTER TABLE
509 $table ADD CONSTRAINT
510 $foreign_key_name FOREIGN KEY ($field_names)
511 REFERENCES $reference_table ($reference_field_names)
516 return (
bool) $this->pdo->exec($query);
519 public function dropForeignKey(
string $foreign_key_name,
string $table_name): bool
521 $table = $this->db_instance->quoteIdentifier($table_name,
true);
522 $name = $this->db_instance->quoteIdentifier($foreign_key_name,
true);
523 $query =
"ALTER TABLE $table DROP FOREIGN KEY $name;";
525 return (
bool) $this->pdo->exec($query);
530 $query =
"SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='FOREIGN KEY';";
531 $result_set = $this->db_instance->query($query);
532 while ($foreign_data = $this->db_instance->fetchAssoc($result_set)) {
533 if (array_key_exists(
536 ) && $foreign_data[
'CONSTRAINT_NAME'] === $foreign_key_name) {
const SEQUENCE_COLUMNS_NAME
ilQueryUtilsInterface $query_utils
alterTable(string $name, array $changes, bool $check)
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)
dropSequence(string $seq_name)
__construct(protected \PDO $pdo, protected Internal $db_instance)
dropConstraint(string $table, string $name, bool $primary=false)
listTableConstraints(string $table)
dropForeignKey(string $foreign_key_name, string $table_name)
getTableCreationQuery(string $name, array $fields, array $options=[])
fixSequenceName(string $sqn, bool $check=false)
createConstraint(string $table, string $name, array $definition)
createSequence(string $seq_name, int $start=1, array $options=[])
fixIndexName(string $idx)
listTables(?string $database=null)
createIndex(string $table, string $name, array $definition)
foreignKeyExists(string $foreign_key_name, string $table_name)
getIndexName(string $idx)
getSequenceName(string $sqn)
createTable(string $name, array $fields, array $options=[])
listTableIndexes(string $table)
dropIndex(string $table, string $name)
listSequences(?string $database=null)
listTableFields(string $table)
Class ilDatabaseException.
Interface ilDBPdoManagerInterface All these methods are not in MDB 2 will be moved to a seperate inte...
Interface ilQueryUtilsInterface.