48 require_once
'MDB2/Driver/Manager/Common.php';
78 $query =
"CREATE DATABASE $name";
104 $query =
"DROP DATABASE $name";
162 $options_strings = array();
164 if (!empty($options[
'comment'])) {
165 $options_strings[
'comment'] =
'COMMENT = '.$db->quote($options[
'comment'],
'text');
168 if (!empty($options[
'charset'])) {
169 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET '.$options[
'charset'];
170 if (!empty($options[
'collate'])) {
171 $options_strings[
'charset'].=
' COLLATE '.$options[
'collate'];
176 if (!empty($options[
'type'])) {
177 $type = $options[
'type'];
178 }
elseif ($db->options[
'default_table_type']) {
179 $type = $db->options[
'default_table_type'];
182 $options_strings[] =
"ENGINE = $type";
185 if (!empty($options_strings)) {
186 $query .=
' '.implode(
' ', $options_strings);
291 foreach ($changes as $change_name => $change) {
292 switch ($change_name) {
301 'change type "'.$change_name.
'" not yet supported', __FUNCTION__);
310 if (!empty($changes[
'name'])) {
311 $change_name = $db->quoteIdentifier($changes[
'name'],
true);
312 $query .=
'RENAME TO ' . $change_name;
315 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
316 foreach ($changes[
'add'] as $field_name => $field) {
320 $query.=
'ADD ' . $db->getDeclaration($field[
'type'], $field_name, $field);
324 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
325 foreach ($changes[
'remove'] as $field_name => $field) {
329 $field_name = $db->quoteIdentifier($field_name,
true);
330 $query.=
'DROP ' . $field_name;
335 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
336 foreach ($changes[
'rename'] as $field_name => $field) {
337 $rename[$field[
'name']] = $field_name;
341 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
342 foreach ($changes[
'change'] as $field_name => $field) {
346 if (isset($rename[$field_name])) {
347 $old_field_name = $rename[$field_name];
348 unset($rename[$field_name]);
350 $old_field_name = $field_name;
352 $old_field_name = $db->quoteIdentifier($old_field_name,
true);
353 $query.=
"CHANGE $old_field_name " . $db->getDeclaration($field[
'definition'][
'type'], $field_name, $field[
'definition']);
357 if (!empty($rename) && is_array($rename)) {
358 foreach ($rename as $rename_name => $renamed_field) {
362 $field = $changes[
'rename'][$renamed_field];
363 $renamed_field = $db->quoteIdentifier($renamed_field,
true);
364 $query.=
'CHANGE ' . $renamed_field .
' ' . $db->getDeclaration($field[
'definition'][
'type'], $field[
'name'], $field[
'definition']);
373 return $db->exec(
"ALTER TABLE $name $query");
392 $result = $db->queryCol(
'SHOW DATABASES');
397 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
418 return $db->queryCol(
'SELECT DISTINCT USER FROM mysql.USER');
437 $query =
"SELECT name FROM mysql.proc";
448 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
471 if (!is_null($table)) {
472 $table = $db->quote($table,
'text');
480 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
502 $query =
"SHOW /*!50002 FULL*/ TABLES";
503 if (!is_null($database)) {
504 $query .=
" FROM $database";
506 $query.=
"/*!50002 WHERE Table_type = 'BASE TABLE'*/";
514 foreach ($table_names as $table) {
520 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
542 $query =
'SHOW FULL TABLES';
543 if (!is_null($database)) {
544 $query.=
" FROM $database";
546 $query.=
" WHERE Table_type = 'VIEW'";
554 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
576 $table = $db->quoteIdentifier($table,
true);
577 $result = $db->queryCol(
"SHOW COLUMNS FROM $table");
582 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
631 $table = $db->quoteIdentifier($table,
true);
632 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
633 $query =
"CREATE INDEX $name ON $table";
635 foreach ($definition[
'fields'] as $field => $fieldinfo) {
636 if (!empty($fieldinfo[
'length'])) {
637 $fields[] = $db->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
639 $fields[] = $db->quoteIdentifier($field,
true);
642 $query .=
' ('. implode(
', ', $fields) .
')';
664 $table = $db->quoteIdentifier($table,
true);
665 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
666 return $db->exec(
"DROP INDEX $name ON $table");
686 $key_name =
'Key_name';
687 $non_unique =
'Non_unique';
689 if ($db->options[
'field_case'] == CASE_LOWER) {
690 $key_name = strtolower($key_name);
691 $non_unique = strtolower($non_unique);
693 $key_name = strtoupper($key_name);
694 $non_unique = strtoupper($non_unique);
698 $table = $db->quoteIdentifier($table,
true);
699 $query =
"SHOW INDEX FROM $table";
706 foreach ($indexes as $index_data) {
707 if ($index_data[$non_unique] && ($index = $this->
_fixIndexName($index_data[$key_name]))) {
713 $result = array_change_key_case(
$result, $db->options[
'field_case']);
751 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
752 if (!empty($definition[
'primary'])) {
755 }
elseif (!empty($definition[
'unique'])) {
760 'invalid definition, could not create constraint', __FUNCTION__);
763 $table = $db->quoteIdentifier($table,
true);
764 $query =
"ALTER TABLE $table ADD $type $name";
766 foreach (array_keys($definition[
'fields']) as $field) {
767 $fields[] = $db->quoteIdentifier($field,
true);
769 $query .=
' ('. implode(
', ', $fields) .
')';
792 $table = $db->quoteIdentifier($table,
true);
793 if ($primary || strtolower(
$name) ==
'primary') {
794 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
796 $name = $db->quoteIdentifier($db->getIndexName(
$name),
true);
797 $query =
"ALTER TABLE $table DROP INDEX $name";
819 $key_name =
'Key_name';
820 $non_unique =
'Non_unique';
822 if ($db->options[
'field_case'] == CASE_LOWER) {
823 $key_name = strtolower($key_name);
824 $non_unique = strtolower($non_unique);
826 $key_name = strtoupper($key_name);
827 $non_unique = strtoupper($non_unique);
831 $table = $db->quoteIdentifier($table,
true);
832 $query =
"SHOW INDEX FROM $table";
839 foreach ($indexes as $index_data) {
840 if (!$index_data[$non_unique]) {
841 if ($index_data[$key_name] !==
'PRIMARY') {
846 if (!empty($index)) {
853 $result = array_change_key_case(
$result, $db->options[
'field_case']);
883 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
884 $seqcol_name = $db->quoteIdentifier($db->options[
'seqcol_name'],
true);
886 $options_strings = array();
888 if (!empty($options[
'comment'])) {
889 $options_strings[
'comment'] =
'COMMENT = '.$db->quote($options[
'comment'],
'text');
892 if (!empty($options[
'charset'])) {
893 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET '.$options[
'charset'];
894 if (!empty($options[
'collate'])) {
895 $options_strings[
'charset'].=
' COLLATE '.$options[
'collate'];
900 if (!empty($options[
'type'])) {
901 $type = $options[
'type'];
902 }
elseif ($db->options[
'default_table_type']) {
903 $type = $db->options[
'default_table_type'];
906 $options_strings[] =
"ENGINE = $type";
909 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
910 if (!empty($options_strings)) {
911 $query .=
' '.implode(
' ', $options_strings);
923 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).
')';
930 $result = $db->exec(
"DROP TABLE $sequence_name");
932 return $db->raiseError(
$result, null, null,
933 'could not drop inconsistent sequence table', __FUNCTION__);
936 return $db->raiseError(
$res, null, null,
937 'could not create sequence table', __FUNCTION__);
957 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
958 return $db->exec(
"DROP TABLE $sequence_name");
979 if (!is_null($database)) {
980 $query .=
" FROM $database";
982 $table_names = $db->queryCol(
$query);
988 foreach ($table_names as $table_name) {
994 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);