61 if (!$this->query_utils) {
84 $str =
'SHOW TABLES ' . ($database ?
' IN ' . $database :
'');
85 $r = $this->pdo->query($str);
88 $sequence_identifier =
"_seq";
89 while (
$data = $r->fetchColumn()) {
90 if (!preg_match(
"/{$sequence_identifier}$/um",
$data)) {
107 $seq_name = preg_replace($seq_pattern,
'\\1', $sqn);
108 if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
126 if (!is_null($database)) {
127 $query .=
" FROM $database";
133 while ($table_name = $this->db_instance->fetchAssoc(
$res)) {
138 if ($this->db_instance->options[
'portability'] ?? null) {
139 $result = array_map(($this->db_instance->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
157 $table = $db->quoteIdentifier($table,
true);
158 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
159 $query =
"ALTER TABLE $table ADD CONSTRAINT $name";
160 if (!empty($definition[
'primary'])) {
162 } elseif (!empty($definition[
'unique'])) {
166 foreach (array_keys($definition[
'fields']) as $field) {
167 $fields[] = $db->quoteIdentifier($field,
true);
169 $query .=
' (' . implode(
', ', $fields) .
')';
171 return $this->pdo->exec(
$query);
183 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
186 $options_strings = array();
188 if (!empty($options[
'comment'])) {
189 $options_strings[
'comment'] =
'COMMENT = ' . $this->db_instance->quote($options[
'comment'],
'text');
192 if (!empty($options[
'charset'])) {
193 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET ' . $options[
'charset'];
194 if (!empty($options[
'collate'])) {
195 $options_strings[
'charset'] .=
' COLLATE ' . $options[
'collate'];
200 if (!empty($options[
'type'])) {
201 $type = $options[
'type'];
204 $options_strings[] =
"ENGINE = $type";
207 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
209 if (!empty($options_strings)) {
210 $query .=
' ' . implode(
' ', $options_strings);
218 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (" . ($start - 1) .
')';
236 foreach ($changes as $change_name => $change) {
237 switch ($change_name) {
254 if (!empty($changes[
'name'])) {
255 $change_name = $db->quoteIdentifier($changes[
'name']);
256 $query .=
'RENAME TO ' . $change_name;
259 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
260 foreach ($changes[
'add'] as $field_name => $field) {
264 $query .=
'ADD ' . $db->getFieldDefinition()->getDeclaration($field[
'type'], $field_name, $field);
268 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
269 foreach ($changes[
'remove'] as $field_name => $field) {
273 $field_name = $db->quoteIdentifier($field_name);
274 $query .=
'DROP ' . $field_name;
279 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
280 foreach ($changes[
'rename'] as $field_name => $field) {
281 $rename[$field[
'name']] = $field_name;
285 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
286 foreach ($changes[
'change'] as $field_name => $field) {
290 if (isset($rename[$field_name])) {
291 $old_field_name = $rename[$field_name];
292 unset($rename[$field_name]);
294 $old_field_name = $field_name;
296 $old_field_name = $db->quoteIdentifier($old_field_name);
297 $query .=
"CHANGE $old_field_name " . $this->db_instance->getFieldDefinition()
298 ->getDeclaration($field[
'definition'][
'type'], $field_name, $field[
'definition']);
302 if (!empty($rename) && is_array($rename)) {
303 foreach ($rename as $rename_name => $renamed_field) {
307 $field = $changes[
'rename'][$renamed_field];
308 $renamed_field = $db->quoteIdentifier($renamed_field);
309 $query .=
'CHANGE ' . $renamed_field .
' ' . $this->db_instance->getFieldDefinition()
310 ->getDeclaration($field[
'definition'][
'type'], $field[
'name'], $field[
'definition']);
320 $statement =
"ALTER TABLE $name $query";
322 return $this->pdo->exec($statement);
334 $options[
'type'] = $this->db_instance->getStorageEngine();
352 return $this->db_instance->getIndexName($idx);
362 return $this->db_instance->getSequenceName($sqn);
373 $table = $this->db_instance->quoteIdentifier($table);
374 $query =
"SHOW COLUMNS FROM $table";
377 while (
$data = $this->db_instance->fetchObject(
$result)) {
378 $return[] =
$data->Field;
392 $key_name =
'Key_name';
393 $non_unique =
'Non_unique';
396 if ($db->options[
'portability'] ??
false) {
397 if (($db->options[
'field_case'] ?? null) == CASE_LOWER) {
398 $key_name = strtolower($key_name);
399 $non_unique = strtolower($non_unique);
401 $key_name = strtoupper($key_name);
402 $non_unique = strtoupper($non_unique);
406 $table = $this->db_instance->quoteIdentifier($table);
407 $query =
"SHOW INDEX FROM $table";
408 $result_set = $this->db_instance->query(
$query);
411 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
412 if (!$index_data[$non_unique]) {
413 if ($index_data[$key_name] !==
'PRIMARY') {
425 if ($this->db_instance->options[
'portability'] ??
false) {
426 $result = array_change_key_case(
$result, $this->db_instance->options[
'field_case'] ?? null);
440 $key_name =
'Key_name';
441 $non_unique =
'Non_unique';
442 if ($this->db_instance->options[
'portability'] ?? null) {
443 if ($this->db_instance->options[
'field_case'] == CASE_LOWER) {
444 $key_name = strtolower($key_name);
445 $non_unique = strtolower($non_unique);
447 $key_name = strtoupper($key_name);
448 $non_unique = strtoupper($non_unique);
452 $table = $this->db_instance->quoteIdentifier($table);
453 $query =
"SHOW INDEX FROM $table";
454 $result_set = $this->db_instance->query(
$query);
456 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
457 $indexes[] = $index_data;
460 foreach ($indexes as $index_data) {
461 if ($index_data[$non_unique] && (
$index = $this->
fixIndexName($index_data[$key_name]))) {
466 if ($this->db_instance->options[
'portability'] ?? null) {
467 $result = array_change_key_case(
$result, $this->db_instance->options[
'field_case']);
481 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
482 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
498 $table = $this->db_instance->quoteIdentifier($table,
true);
499 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName(
$name),
true);
500 $query =
"CREATE INDEX $name ON $table";
502 foreach ($definition[
'fields'] as $field => $fieldinfo) {
503 if (!empty($fieldinfo[
'length'])) {
504 $fields[] = $this->db_instance->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
506 $fields[] = $this->db_instance->quoteIdentifier($field,
true);
509 $query .=
' (' . implode(
', ', $fields) .
')';
511 return $this->pdo->exec(
$query);
522 $table = $this->db_instance->quoteIdentifier($table,
true);
523 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName(
$name),
true);
525 return $this->pdo->exec(
"DROP INDEX $name ON $table");
535 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($table_name));
537 return $this->pdo->exec(
"DROP TABLE $sequence_name");
563 $table = $db->quoteIdentifier($table,
true);
564 if ($primary || strtolower(
$name) ==
'primary') {
565 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
567 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
568 $query =
"ALTER TABLE $table DROP INDEX $name";
571 return $this->pdo->exec(
$query);
584 return $db->manipulate(
"DROP TABLE $name");
alterTable($name, $changes, $check)
listTables($database=null)
listTableConstraints($table)
listSequences($database=null)
fixSequenceName($sqn, $check=false)
dropTable($name)
Table-name mixed
dropSequence($table_name)
getTableCreationQuery($name, $fields, $options=array())
Interface ilDBPdoManagerInterface.
Class ilDatabaseException.
createConstraint($table, $name, $definition)
createTable($name, $fields, $options=array())
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
foreach($_POST as $key=> $value) $res
dropConstraint($table, $name, $primary=false)
createIndex($table, $name, $definition)
__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...
const SEQUENCE_COLUMNS_NAME
createSequence($seq_name, $start=1, $options=array())