2 require_once(
'./Services/Database/interfaces/interface.ilDBManager.php');
43 if (!$this->query_utils) {
64 $str =
'SHOW TABLES ' . ($database ?
' IN ' . $database :
'');
65 $r = $this->pdo->query($str);
68 $sequence_identifier =
"_seq";
69 while (
$data =
$r->fetchColumn()) {
70 if (!preg_match(
"/{$sequence_identifier}$/um",
$data)) {
86 $seq_name = preg_replace($seq_pattern,
'\\1', $sqn);
87 if ($seq_name && !strcasecmp($sqn, $this->db_instance->getSequenceName($seq_name))) {
104 if (!is_null($database)) {
105 $query .=
" FROM $database";
111 while ($table_name = $this->db_instance->fetchAssoc(
$res)) {
116 if ($this->db_instance->options[
'portability']) {
117 $result = array_map(($this->db_instance->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
134 $table = $db->quoteIdentifier($table,
true);
135 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
136 $query =
"ALTER TABLE $table ADD CONSTRAINT $name";
137 if (!empty($definition[
'primary'])) {
139 } elseif (!empty($definition[
'unique'])) {
143 foreach (array_keys($definition[
'fields']) as $field) {
144 $fields[] = $db->quoteIdentifier($field,
true);
146 $query .=
' (' . implode(
', ', $fields) .
')';
148 return $this->pdo->exec(
$query);
159 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($seq_name));
162 $options_strings =
array();
165 $options_strings[
'comment'] =
'COMMENT = ' . $this->db_instance->quote(
$options[
'comment'],
'text');
169 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET ' .
$options[
'charset'];
171 $options_strings[
'charset'] .=
' COLLATE ' .
$options[
'collate'];
180 $options_strings[] =
"ENGINE = $type";
183 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
185 if (!empty($options_strings)) {
186 $query .=
' ' . implode(
' ', $options_strings);
194 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (" . (
$start - 1) .
')';
211 foreach ($changes as $change_name => $change) {
212 switch ($change_name) {
229 if (!empty($changes[
'name'])) {
230 $change_name = $db->quoteIdentifier($changes[
'name']);
231 $query .=
'RENAME TO ' . $change_name;
234 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
235 foreach ($changes[
'add'] as $field_name => $field) {
239 $query .=
'ADD ' . $db->getFieldDefinition()->getDeclaration($field[
'type'], $field_name, $field);
243 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
244 foreach ($changes[
'remove'] as $field_name => $field) {
248 $field_name = $db->quoteIdentifier($field_name);
249 $query .=
'DROP ' . $field_name;
254 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
255 foreach ($changes[
'rename'] as $field_name => $field) {
256 $rename[$field[
'name']] = $field_name;
260 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
261 foreach ($changes[
'change'] as $field_name => $field) {
265 if (isset($rename[$field_name])) {
266 $old_field_name = $rename[$field_name];
267 unset($rename[$field_name]);
269 $old_field_name = $field_name;
271 $old_field_name = $db->quoteIdentifier($old_field_name);
272 $query .=
"CHANGE $old_field_name " . $this->db_instance->getFieldDefinition()
273 ->getDeclaration($field[
'definition'][
'type'], $field_name, $field[
'definition']);
277 if (!empty($rename) && is_array($rename)) {
278 foreach ($rename as $rename_name => $renamed_field) {
282 $field = $changes[
'rename'][$renamed_field];
283 $renamed_field = $db->quoteIdentifier($renamed_field);
284 $query .=
'CHANGE ' . $renamed_field .
' ' . $this->db_instance->getFieldDefinition()
285 ->getDeclaration($field[
'definition'][
'type'], $field[
'name'], $field[
'definition']);
293 $name = $db->quoteIdentifier($name,
true);
295 $statement =
"ALTER TABLE $name $query";
297 return $this->pdo->exec($statement);
308 $options[
'type'] = $this->db_instance->getStorageEngine();
325 return $this->db_instance->getIndexName($idx);
334 return $this->db_instance->getSequenceName($sqn);
344 $table = $this->db_instance->quoteIdentifier($table);
345 $query =
"SHOW COLUMNS FROM $table";
348 while (
$data = $this->db_instance->fetchObject(
$result)) {
349 $return[] =
$data->Field;
362 $key_name =
'Key_name';
363 $non_unique =
'Non_unique';
366 if ($db->options[
'portability']) {
367 if ($db->options[
'field_case'] == CASE_LOWER) {
368 $key_name = strtolower($key_name);
369 $non_unique = strtolower($non_unique);
371 $key_name = strtoupper($key_name);
372 $non_unique = strtoupper($non_unique);
376 $table = $this->db_instance->quoteIdentifier($table);
377 $query =
"SHOW INDEX FROM $table";
378 $result_set = $this->db_instance->query(
$query);
381 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
382 if (!$index_data[$non_unique]) {
383 if ($index_data[$key_name] !==
'PRIMARY') {
388 if (!empty($index)) {
389 $index = strtolower($index);
395 if ($this->db_instance->options[
'portability']) {
396 $result = array_change_key_case(
$result, $this->db_instance->options[
'field_case']);
409 $key_name =
'Key_name';
410 $non_unique =
'Non_unique';
411 if ($this->db_instance->options[
'portability']) {
412 if ($this->db_instance->options[
'field_case'] == CASE_LOWER) {
413 $key_name = strtolower($key_name);
414 $non_unique = strtolower($non_unique);
416 $key_name = strtoupper($key_name);
417 $non_unique = strtoupper($non_unique);
421 $table = $this->db_instance->quoteIdentifier($table);
422 $query =
"SHOW INDEX FROM $table";
423 $result_set = $this->db_instance->query(
$query);
425 while ($index_data = $this->db_instance->fetchAssoc($result_set)) {
426 $indexes[] = $index_data;
429 foreach ($indexes as $index_data) {
430 if ($index_data[$non_unique] && ($index = $this->
fixIndexName($index_data[$key_name]))) {
435 if ($this->db_instance->options[
'portability']) {
436 $result = array_change_key_case(
$result, $this->db_instance->options[
'field_case']);
449 $idx_name = preg_replace($idx_pattern,
'\\1', $idx);
450 if ($idx_name && !strcasecmp($idx, $this->db_instance->getIndexName($idx_name))) {
465 $table = $this->db_instance->quoteIdentifier($table,
true);
466 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name),
true);
467 $query =
"CREATE INDEX $name ON $table";
469 foreach ($definition[
'fields'] as $field => $fieldinfo) {
470 if (!empty($fieldinfo[
'length'])) {
471 $fields[] = $this->db_instance->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
473 $fields[] = $this->db_instance->quoteIdentifier($field,
true);
476 $query .=
' (' . implode(
', ', $fields) .
')';
478 return $this->pdo->exec(
$query);
488 $table = $this->db_instance->quoteIdentifier($table,
true);
489 $name = $this->db_instance->quoteIdentifier($this->db_instance->getIndexName($name),
true);
491 return $this->pdo->exec(
"DROP INDEX $name ON $table");
500 $sequence_name = $this->db_instance->quoteIdentifier($this->db_instance->getSequenceName($table_name));
502 return $this->pdo->exec(
"DROP TABLE $sequence_name");
526 $table = $db->quoteIdentifier($table,
true);
527 if ($primary || strtolower($name) ==
'primary') {
528 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
530 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
531 $query =
"ALTER TABLE $table DROP INDEX $name";
534 return $this->pdo->exec(
$query);
544 $name = $db->quoteIdentifier($name,
true);
546 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())
if(!is_array($argv)) $options
dropConstraint($table, $name, $primary=false)
createIndex($table, $name, $definition)
Create styles array
The data for the language used.
__construct(\PDO $pdo, ilDBPdo $db_instance)
ilDBPdoManager constructor.
const SEQUENCE_COLUMNS_NAME
createSequence($seq_name, $start=1, $options=array())