3 declare(strict_types=1);
42 if ($this->query_utils === null) {
57 public function listTables(?
string $database = null): array
59 $str =
'SHOW TABLES ' . ($database ?
' IN ' . $database :
'');
60 $r = $this->pdo->query($str);
63 $sequence_identifier =
"_seq";
64 while (
$data = $r->fetchColumn()) {
65 if (!preg_match(
"/$sequence_identifier$/um",
$data)) {
76 $seq_name = preg_replace($seq_pattern,
'\\1', $sqn);
77 if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
89 $query =
"SHOW TABLES LIKE '%_seq'";
90 if (!is_null($database)) {
91 $query .=
" FROM $database";
97 while ($table_name = $this->db_instance->fetchAssoc(
$res)) {
102 if ($this->db_instance->options[
'portability'] ?? null) {
104 ($this->db_instance->options[
'field_case'] === CASE_LOWER ?
'strtolower' :
'strtoupper'),
120 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
121 $query =
"ALTER TABLE $table ADD CONSTRAINT $name";
122 if (!empty($definition[
'primary'])) {
124 } elseif (!empty($definition[
'unique'])) {
128 foreach (array_keys($definition[
'fields']) as $field) {
129 $fields[] = $db->quoteIdentifier($field,
true);
131 $query .=
' (' . implode(
', ', $fields) .
')';
133 return (
bool) $this->pdo->exec(
$query);
136 public function createSequence(
string $seq_name,
int $start = 1, array $options = []): bool
138 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
141 $options_strings = array();
143 if (!empty($options[
'comment'])) {
144 $options_strings[
'comment'] =
'COMMENT = ' . $this->db_instance->quote($options[
'comment'],
'text');
147 if (!empty($options[
'charset'])) {
148 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET ' . $options[
'charset'];
149 if (!empty($options[
'collate'])) {
150 $options_strings[
'charset'] .=
' COLLATE ' . $options[
'collate'];
155 if (!empty($options[
'type'])) {
156 $type = $options[
'type'];
159 $options_strings[] =
"ENGINE = $type";
162 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
164 if (!empty($options_strings)) {
165 $query .=
' ' . implode(
' ', $options_strings);
173 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (" . ($start - 1) .
')';
186 foreach (array_keys($changes) as $change_name) {
187 switch ($change_name) {
204 if (!empty($changes[
'name'])) {
205 $change_name = $db->quoteIdentifier($changes[
'name']);
206 $query .=
'RENAME TO ' . $change_name;
209 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
210 foreach ($changes[
'add'] as $field_name => $field) {
214 $fd = $db->getFieldDefinition();
216 $query .=
'ADD ' . $fd->getDeclaration($field[
'type'], $field_name, $field);
221 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
222 foreach (array_keys($changes[
'remove']) as $field_name) {
226 $field_name = $db->quoteIdentifier($field_name);
227 $query .=
'DROP ' . $field_name;
232 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
233 foreach ($changes[
'rename'] as $field_name => $field) {
234 $rename[$field[
'name']] = $field_name;
238 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
239 foreach ($changes[
'change'] as $field_name => $field) {
243 if (isset($rename[$field_name])) {
244 $old_field_name = $rename[$field_name];
245 unset($rename[$field_name]);
247 $old_field_name = $field_name;
249 $old_field_name = $db->quoteIdentifier($old_field_name);
250 $fd = $this->db_instance->getFieldDefinition();
252 $query .=
"CHANGE $old_field_name " . $fd
254 $field[
'definition'][
'type'],
262 if (!empty($rename) && is_array($rename)) {
263 foreach ($rename as $renamed_field) {
267 $field = $changes[
'rename'][$renamed_field];
268 $renamed_field = $db->quoteIdentifier($renamed_field);
269 $fd = $this->db_instance->getFieldDefinition();
271 $query .=
'CHANGE ' . $renamed_field .
' ' . $fd
273 $field[
'definition'][
'type'],
285 $name = $db->quoteIdentifier($name,
true);
287 $statement =
"ALTER TABLE $name $query";
289 return (
bool) $this->pdo->exec($statement);
294 $options[
'type'] = $this->db_instance->getStorageEngine();
301 return $this->db_instance->getIndexName($idx);
306 return $this->db_instance->getSequenceName($sqn);
311 $table = $this->db_instance->quoteIdentifier($table);
312 $query =
"SHOW COLUMNS FROM $table";
313 $result = $this->db_instance->query(
$query);
315 while (
$data = $this->db_instance->fetchObject($result)) {
316 $return[] =
$data->Field;
327 $key_name =
'Key_name';
328 $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';
356 if ($this->db_instance->options[
'portability'] ?? null) {
357 $result = array_change_key_case($result, $this->db_instance->options[
'field_case']);
360 return array_keys($result);
368 $key_name =
'Key_name';
369 $non_unique =
'Non_unique';
370 if ($this->db_instance->options[
'portability'] ?? null) {
371 if ($this->db_instance->options[
'field_case'] == CASE_LOWER) {
372 $key_name = strtolower($key_name);
373 $non_unique = strtolower($non_unique);
375 $key_name = strtoupper($key_name);
376 $non_unique = strtoupper($non_unique);
380 $table = $this->db_instance->quoteIdentifier($table);
381 $query =
"SHOW INDEX FROM $table";
382 $result_set = $this->db_instance->query(
$query);
384 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
385 $indexes[] = $index_data;
388 foreach ($indexes as $index_data) {
389 if ($index_data[$non_unique] && (
$index = $this->
fixIndexName($index_data[$key_name]))) {
394 if ($this->db_instance->options[
'portability'] ?? null) {
395 $result = array_change_key_case($result, $this->db_instance->options[
'field_case']);
398 return array_keys($result);
404 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
405 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
412 public function createIndex(
string $table,
string $name, array $definition): bool
414 $table = $this->db_instance->quoteIdentifier($table,
true);
415 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name),
true);
416 $query =
"CREATE INDEX $name ON $table";
418 foreach ($definition[
'fields'] as $field => $fieldinfo) {
419 if (!empty($fieldinfo[
'length'])) {
420 $fields[] = $this->db_instance->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
422 $fields[] = $this->db_instance->quoteIdentifier($field,
true);
425 $query .=
' (' . implode(
', ', $fields) .
')';
427 return (
bool) $this->pdo->exec(
$query);
430 public function dropIndex(
string $table,
string $name): bool
432 $table = $this->db_instance->quoteIdentifier($table,
true);
433 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name),
true);
435 return (
bool) $this->pdo->exec(
"DROP INDEX $name ON $table");
440 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
442 return (
bool) $this->pdo->exec(
"DROP TABLE $sequence_name");
450 return $this->
getQueryUtils()->createTable($name, $fields, $options);
453 public function dropConstraint(
string $table,
string $name,
bool $primary =
false): bool
456 $table = $db->quoteIdentifier($table,
true);
457 if ($primary || strtolower($name) ===
'primary') {
458 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
460 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
461 $query =
"ALTER TABLE $table DROP INDEX $name";
464 return (
bool) $this->pdo->exec(
$query);
470 $name = $db->quoteIdentifier($name,
true);
472 return (
bool) $this->pdo->exec(
"DROP TABLE $name");
dropSequence(string $seq_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)
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilQueryUtils $query_utils
const SEQUENCE_COLUMNS_NAME
createIndex(string $table, string $name, array $definition)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
listTableConstraints(string $table)