19 declare(strict_types=1);
    44         if ($this->query_utils === null) {
    59     public function listTables(?
string $database = null): array
    61         $str = 
'SHOW TABLES ' . ($database ? 
' IN ' . $database : 
'');
    62         $r = $this->pdo->query($str);
    65         $sequence_identifier = 
"_seq";
    66         while (
$data = 
$r->fetchColumn()) {
    67             if (!preg_match(
"/$sequence_identifier$/um", 
$data)) {
    78         $seq_name = preg_replace($seq_pattern, 
'\\1', $sqn);
    79         if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
    91         $query = 
"SHOW TABLES LIKE '%_seq'";
    92         if (!is_null($database)) {
    93             $query .= 
" FROM $database";
    96         $res = $this->db_instance->query($query);
    99         while ($table_name = $this->db_instance->fetchAssoc(
$res)) {
   104         if ($this->db_instance->options[
'portability'] ?? null) {
   106                 ($this->db_instance->options[
'field_case'] === CASE_LOWER ? 
'strtolower' : 
'strtoupper'),
   122         $name = $db->quoteIdentifier($db->getIndexName($name), 
true);
   123         $query = 
"ALTER TABLE $table ADD CONSTRAINT $name";
   124         if (!empty($definition[
'primary'])) {
   125             $query .= 
' PRIMARY KEY';
   126         } elseif (!empty($definition[
'unique'])) {
   130         foreach (array_keys($definition[
'fields']) as $field) {
   131             $fields[] = $db->quoteIdentifier($field, 
true);
   133         $query .= 
' (' . implode(
', ', $fields) . 
')';
   135         return (
bool) $this->pdo->exec($query);
   138     public function createSequence(
string $seq_name, 
int $start = 1, array $options = []): bool
   140         $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
   143         $options_strings = array();
   145         if (!empty($options[
'comment'])) {
   146             $options_strings[
'comment'] = 
'COMMENT = ' . $this->db_instance->quote($options[
'comment'], 
'text');
   149         if (!empty($options[
'charset'])) {
   150             $options_strings[
'charset'] = 
'DEFAULT CHARACTER SET ' . $options[
'charset'];
   151             if (!empty($options[
'collate'])) {
   152                 $options_strings[
'charset'] .= 
' COLLATE ' . $options[
'collate'];
   157         if (!empty($options[
'type'])) {
   158             $type = $options[
'type'];
   161             $options_strings[] = 
"ENGINE = $type";
   164         $query = 
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
   166         if (!empty($options_strings)) {
   167             $query .= 
' ' . implode(
' ', $options_strings);
   169         $this->pdo->exec($query);
   175         $query = 
"INSERT INTO $sequence_name ($seqcol_name) VALUES (" . ($start - 1) . 
')';
   176         $this->pdo->exec($query);
   188         foreach (array_keys($changes) as $change_name) {
   189             switch ($change_name) {
   206         if (!empty($changes[
'name'])) {
   207             $change_name = $db->quoteIdentifier($changes[
'name']);
   208             $query .= 
'RENAME TO ' . $change_name;
   211         if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
   212             foreach ($changes[
'add'] as $field_name => $field) {
   216                 $fd = $db->getFieldDefinition();
   218                     $query .= 
'ADD ' . $fd->getDeclaration($field[
'type'], $field_name, $field);
   223         if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
   224             foreach (array_keys($changes[
'remove']) as $field_name) {
   228                 $field_name = $db->quoteIdentifier($field_name);
   229                 $query .= 
'DROP ' . $field_name;
   234         if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
   235             foreach ($changes[
'rename'] as $field_name => $field) {
   236                 $rename[$field[
'name']] = $field_name;
   240         if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
   241             foreach ($changes[
'change'] as $field_name => $field) {
   245                 if (isset($rename[$field_name])) {
   246                     $old_field_name = $rename[$field_name];
   247                     unset($rename[$field_name]);
   249                     $old_field_name = $field_name;
   251                 $old_field_name = $db->quoteIdentifier($old_field_name);
   252                 $fd = $this->db_instance->getFieldDefinition();
   254                     $query .= 
"CHANGE $old_field_name " . $fd
   256                                 $field[
'definition'][
'type'],
   264         if (!empty($rename) && is_array($rename)) {
   265             foreach ($rename as $renamed_field) {
   269                 $field = $changes[
'rename'][$renamed_field];
   270                 $renamed_field = $db->quoteIdentifier($renamed_field);
   271                 $fd = $this->db_instance->getFieldDefinition();
   273                     $query .= 
'CHANGE ' . $renamed_field . 
' ' . $fd
   275                                 $field[
'definition'][
'type'],
   287         $name = $db->quoteIdentifier($name, 
true);
   289         $statement = 
"ALTER TABLE $name $query";
   291         return (
bool) $this->pdo->exec($statement);
   294     public function createTable(
string $name, array $fields, array $options = array()): bool
   296         $options[
'type'] = $this->db_instance->getStorageEngine();
   303         return $this->db_instance->getIndexName($idx);
   308         return $this->db_instance->getSequenceName($sqn);
   313         $table = $this->db_instance->quoteIdentifier($table);
   314         $query = 
"SHOW COLUMNS FROM $table";
   315         $result = $this->db_instance->query($query);
   317         while (
$data = $this->db_instance->fetchObject($result)) {
   318             $return[] = 
$data->Field;
   329         $key_name = 
'Key_name';
   330         $non_unique = 
'Non_unique';
   333         if ($db->options[
'portability'] ?? null) {
   334             if ($db->options[
'field_case'] == CASE_LOWER) {
   335                 $key_name = strtolower($key_name);
   336                 $non_unique = strtolower($non_unique);
   338                 $key_name = strtoupper($key_name);
   339                 $non_unique = strtoupper($non_unique);
   343         $table = $this->db_instance->quoteIdentifier($table);
   344         $query = 
"SHOW INDEX FROM $table";
   345         $result_set = $this->db_instance->query($query);
   348         while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
   349             if (!$index_data[$non_unique]) {
   350                 $index = $index_data[$key_name] !== 
'PRIMARY' ? $this->
fixIndexName($index_data[$key_name]) : 
'PRIMARY';
   351                 if (!empty($index)) {
   352                     $index = strtolower($index);
   353                     $result[$index] = 
true;
   358         if ($this->db_instance->options[
'portability'] ?? null) {
   359             $result = array_change_key_case($result, $this->db_instance->options[
'field_case']);
   362         return array_keys($result);
   370         $key_name = 
'Key_name';
   371         $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;
   396         if ($this->db_instance->options[
'portability'] ?? null) {
   397             $result = array_change_key_case($result, $this->db_instance->options[
'field_case']);
   400         return array_keys($result);
   406         $idx_name = preg_replace($idx_pattern, 
'\\1', $idx);
   407         if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
   414     public function createIndex(
string $table, 
string $name, array $definition): bool
   416         $table = $this->db_instance->quoteIdentifier($table, 
true);
   417         $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name), 
true);
   418         $query = 
"CREATE INDEX $name ON $table";
   420         foreach ($definition[
'fields'] as $field => $fieldinfo) {
   421             if (!empty($fieldinfo[
'length'])) {
   422                 $fields[] = $this->db_instance->quoteIdentifier($field, 
true) . 
'(' . $fieldinfo[
'length'] . 
')';
   424                 $fields[] = $this->db_instance->quoteIdentifier($field, 
true);
   427         $query .= 
' (' . implode(
', ', $fields) . 
')';
   429         return (
bool) $this->pdo->exec($query);
   432     public function dropIndex(
string $table, 
string $name): bool
   434         $table = $this->db_instance->quoteIdentifier($table, 
true);
   435         $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name), 
true);
   437         return (
bool) $this->pdo->exec(
"DROP INDEX $name ON $table");
   442         $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
   444         return (
bool) $this->pdo->exec(
"DROP TABLE $sequence_name");
   452         return $this->
getQueryUtils()->createTable($name, $fields, $options);
   455     public function dropConstraint(
string $table, 
string $name, 
bool $primary = 
false): bool
   458         $table = $db->quoteIdentifier($table, 
true);
   459         if ($primary || strtolower($name) === 
'primary') {
   460             $query = 
"ALTER TABLE $table DROP PRIMARY KEY";
   462             $name = $db->quoteIdentifier($db->getIndexName($name), 
true);
   463             $query = 
"ALTER TABLE $table DROP INDEX $name";
   466         return (
bool) $this->pdo->exec($query);
   472         $name = $db->quoteIdentifier($name, 
true);
   474         return (
bool) $this->pdo->exec(
"DROP TABLE $name");
   482         string $foreign_key_name,
   485         array $reference_field_names,
   486         string $reference_table,
   490         $table = $this->db_instance->quoteIdentifier($table_name, 
true);
   491         $reference_table = $this->db_instance->quoteIdentifier($reference_table, 
true);
   492         $field_names = implode(
",", $field_names);
   493         $field_names = $this->db_instance->quoteIdentifier($field_names, 
true);
   494         $reference_field_names = implode(
",", $reference_field_names);
   495         $reference_field_names = $this->db_instance->quoteIdentifier($reference_field_names, 
true);
   496         $foreign_key_name = $this->db_instance->quoteIdentifier($foreign_key_name, 
true);
   499             $on_update = $on_update->value;
   500             $update = 
"ON UPDATE $on_update";
   504             $on_delete = $on_delete->value;
   505             $delete = 
"ON DELETE $on_delete";
   507         $query = 
"ALTER TABLE   508                     $table ADD CONSTRAINT   509                     $foreign_key_name FOREIGN KEY ($field_names)   510                     REFERENCES $reference_table ($reference_field_names)   515         return (
bool) $this->pdo->exec($query);
   518     public function dropForeignKey(
string $foreign_key_name, 
string $table_name): bool
   520         $table = $this->db_instance->quoteIdentifier($table_name, 
true);
   521         $name = $this->db_instance->quoteIdentifier($foreign_key_name, 
true);
   522         $query = 
"ALTER TABLE $table DROP FOREIGN KEY $name;";
   524         return (
bool) $this->pdo->exec($query);
   529         $query = 
"SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='FOREIGN KEY';";
   530         $result_set = $this->db_instance->query($query);
   531         while ($foreign_data = $this->db_instance->fetchAssoc($result_set)) {
   532             if (array_key_exists(
   535             ) && $foreign_data[
'CONSTRAINT_NAME'] === $foreign_key_name) {
 
dropSequence(string $seq_name)
 
dropForeignKey(string $foreign_key_name, string $table_name)
 
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
 
fixIndexName(string $idx)
 
getTableCreationQuery(string $name, array $fields, array $options=[])
 
dropConstraint(string $table, string $name, bool $primary=false)
 
createTable(string $name, array $fields, array $options=array())
 
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
 
listTableIndexes(string $table)
 
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
 
getIndexName(string $idx)
 
fixSequenceName(string $sqn, bool $check=false)
 
alterTable(string $name, array $changes, bool $check)
 
dropIndex(string $table, string $name)
 
createSequence(string $seq_name, int $start=1, array $options=[])
 
listTables(?string $database=null)
 
foreignKeyExists(string $foreign_key_name, string $table_name)
 
listTableFields(string $table)
 
quoteIdentifier(string $identifier, bool $check_option=false)
 
createConstraint(string $table, string $name, array $definition)
 
getSequenceName(string $sqn)
 
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
 
listSequences(string $database=null)
 
__construct(\PDO $pdo, ilDBPdo $db_instance)
ilDBPdoManager constructor. 
 
ilQueryUtils $query_utils
 
const SEQUENCE_COLUMNS_NAME
 
createIndex(string $table, string $name, array $definition)
 
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)
 
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
 
listTableConstraints(string $table)