2 require_once(
'./Services/Database/interfaces/interface.ilDBManager.php');
46 if (!$this->query_utils) {
69 $str =
'SHOW TABLES ' . ($database ?
' IN ' . $database :
'');
70 $r = $this->pdo->query($str);
73 $sequence_identifier =
"_seq";
74 while (
$data =
$r->fetchColumn()) {
75 if (!preg_match(
"/{$sequence_identifier}$/um",
$data)) {
92 $seq_name = preg_replace($seq_pattern,
'\\1', $sqn);
93 if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
111 if (!is_null($database)) {
112 $query .=
" FROM $database";
118 while ($table_name = $this->db_instance->fetchAssoc(
$res)) {
123 if ($this->db_instance->options[
'portability']) {
124 $result = array_map(($this->db_instance->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
143 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
144 $query =
"ALTER TABLE $table ADD CONSTRAINT $name";
145 if (!empty($definition[
'primary'])) {
147 } elseif (!empty($definition[
'unique'])) {
151 foreach (array_keys($definition[
'fields']) as $field) {
152 $fields[] = $db->quoteIdentifier($field,
true);
154 $query .=
' (' . implode(
', ', $fields) .
')';
156 return $this->pdo->exec(
$query);
168 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
171 $options_strings = array();
174 $options_strings[
'comment'] =
'COMMENT = ' . $this->db_instance->quote(
$options[
'comment'],
'text');
178 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET ' .
$options[
'charset'];
180 $options_strings[
'charset'] .=
' COLLATE ' .
$options[
'collate'];
189 $options_strings[] =
"ENGINE = $type";
192 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
194 if (!empty($options_strings)) {
195 $query .=
' ' . implode(
' ', $options_strings);
203 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (" . (
$start - 1) .
')';
221 foreach ($changes as $change_name => $change) {
222 switch ($change_name) {
239 if (!empty($changes[
'name'])) {
240 $change_name = $db->quoteIdentifier($changes[
'name']);
241 $query .=
'RENAME TO ' . $change_name;
244 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
245 foreach ($changes[
'add'] as $field_name => $field) {
249 $query .=
'ADD ' . $db->getFieldDefinition()->getDeclaration($field[
'type'], $field_name, $field);
253 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
254 foreach ($changes[
'remove'] as $field_name => $field) {
258 $field_name = $db->quoteIdentifier($field_name);
259 $query .=
'DROP ' . $field_name;
264 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
265 foreach ($changes[
'rename'] as $field_name => $field) {
266 $rename[$field[
'name']] = $field_name;
270 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
271 foreach ($changes[
'change'] as $field_name => $field) {
275 if (isset($rename[$field_name])) {
276 $old_field_name = $rename[$field_name];
277 unset($rename[$field_name]);
279 $old_field_name = $field_name;
281 $old_field_name = $db->quoteIdentifier($old_field_name);
282 $query .=
"CHANGE $old_field_name " . $this->db_instance->getFieldDefinition()
283 ->getDeclaration($field[
'definition'][
'type'], $field_name, $field[
'definition']);
287 if (!empty($rename) && is_array($rename)) {
288 foreach ($rename as $rename_name => $renamed_field) {
292 $field = $changes[
'rename'][$renamed_field];
293 $renamed_field = $db->quoteIdentifier($renamed_field);
294 $query .=
'CHANGE ' . $renamed_field .
' ' . $this->db_instance->getFieldDefinition()
295 ->getDeclaration($field[
'definition'][
'type'], $field[
'name'], $field[
'definition']);
305 $statement =
"ALTER TABLE $name $query";
307 return $this->pdo->exec($statement);
319 $options[
'type'] = $this->db_instance->getStorageEngine();
337 return $this->db_instance->getIndexName($idx);
347 return $this->db_instance->getSequenceName($sqn);
359 $query =
"SHOW COLUMNS FROM $table";
362 while (
$data = $this->db_instance->fetchObject(
$result)) {
363 $return[] =
$data->Field;
377 $key_name =
'Key_name';
378 $non_unique =
'Non_unique';
381 if ($db->options[
'portability']) {
382 if ($db->options[
'field_case'] == CASE_LOWER) {
383 $key_name = strtolower($key_name);
384 $non_unique = strtolower($non_unique);
386 $key_name = strtoupper($key_name);
387 $non_unique = strtoupper($non_unique);
392 $query =
"SHOW INDEX FROM $table";
393 $result_set = $this->db_instance->query(
$query);
396 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
397 if (!$index_data[$non_unique]) {
398 if ($index_data[$key_name] !==
'PRIMARY') {
410 if ($this->db_instance->options[
'portability']) {
411 $result = array_change_key_case(
$result, $this->db_instance->options[
'field_case']);
425 $key_name =
'Key_name';
426 $non_unique =
'Non_unique';
427 if ($this->db_instance->options[
'portability']) {
428 if ($this->db_instance->options[
'field_case'] == CASE_LOWER) {
429 $key_name = strtolower($key_name);
430 $non_unique = strtolower($non_unique);
432 $key_name = strtoupper($key_name);
433 $non_unique = strtoupper($non_unique);
438 $query =
"SHOW INDEX FROM $table";
439 $result_set = $this->db_instance->query(
$query);
441 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
442 $indexes[] = $index_data;
445 foreach ($indexes as $index_data) {
446 if ($index_data[$non_unique] && (
$index = $this->
fixIndexName($index_data[$key_name]))) {
451 if ($this->db_instance->options[
'portability']) {
452 $result = array_change_key_case(
$result, $this->db_instance->options[
'field_case']);
466 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
467 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
483 $table = $this->db_instance->quoteIdentifier(
$table,
true);
484 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName(
$name),
true);
485 $query =
"CREATE INDEX $name ON $table";
487 foreach ($definition[
'fields'] as $field => $fieldinfo) {
488 if (!empty($fieldinfo[
'length'])) {
489 $fields[] = $this->db_instance->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
491 $fields[] = $this->db_instance->quoteIdentifier($field,
true);
494 $query .=
' (' . implode(
', ', $fields) .
')';
496 return $this->pdo->exec(
$query);
507 $table = $this->db_instance->quoteIdentifier(
$table,
true);
508 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName(
$name),
true);
510 return $this->pdo->exec(
"DROP INDEX $name ON $table");
520 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($table_name));
522 return $this->pdo->exec(
"DROP TABLE $sequence_name");
549 if ($primary || strtolower(
$name) ==
'primary') {
550 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
552 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
553 $query =
"ALTER TABLE $table DROP INDEX $name";
556 return $this->pdo->exec(
$query);
569 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())
foreach($_POST as $key=> $value) $res
dropConstraint($table, $name, $primary=false)
createIndex($table, $name, $definition)
__construct(\PDO $pdo, ilDBPdo $db_instance)
ilDBPdoManager constructor.
const SEQUENCE_COLUMNS_NAME
if(empty($password)) $table
createSequence($seq_name, $start=1, $options=array())