48 require_once
'MDB2/Driver/Manager/Common.php';
77 $name = $db->quoteIdentifier($name,
true);
78 $query =
"CREATE DATABASE $name";
103 $name = $db->quoteIdentifier($name,
true);
104 $query =
"DROP DATABASE $name";
162 $options_strings =
array();
165 $options_strings[
'comment'] =
'COMMENT = '.$db->quote(
$options[
'comment'],
'text');
169 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET '.$options[
'charset'];
171 $options_strings[
'charset'].=
' COLLATE '.$options[
'collate'];
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);
206 $options_strings =
array();
209 $options_strings[
'comment'] =
'COMMENT = '.$db->quote(
$options[
'comment'],
'text');
213 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET '.$options[
'charset'];
215 $options_strings[
'charset'].=
' COLLATE '.$options[
'collate'];
222 } elseif ($db->options[
'default_table_type']) {
223 $type = $db->options[
'default_table_type'];
226 $options_strings[] =
"ENGINE = $type";
229 if (!empty($options_strings)) {
230 $query .=
' '.implode(
' ', $options_strings);
336 foreach ($changes as $change_name => $change) {
337 switch ($change_name) {
346 'change type "'.$change_name.
'" not yet supported', __FUNCTION__);
355 if (!empty($changes[
'name'])) {
356 $change_name = $db->quoteIdentifier($changes[
'name'],
true);
357 $query .=
'RENAME TO ' . $change_name;
360 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
361 foreach ($changes[
'add'] as $field_name => $field) {
365 $query.=
'ADD ' . $db->getDeclaration($field[
'type'], $field_name, $field);
369 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
370 foreach ($changes[
'remove'] as $field_name => $field) {
374 $field_name = $db->quoteIdentifier($field_name,
true);
375 $query.=
'DROP ' . $field_name;
380 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
381 foreach ($changes[
'rename'] as $field_name => $field) {
382 $rename[$field[
'name']] = $field_name;
386 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
387 foreach ($changes[
'change'] as $field_name => $field) {
391 if (isset($rename[$field_name])) {
392 $old_field_name = $rename[$field_name];
393 unset($rename[$field_name]);
395 $old_field_name = $field_name;
397 $old_field_name = $db->quoteIdentifier($old_field_name,
true);
398 $query.=
"CHANGE $old_field_name " . $db->getDeclaration($field[
'definition'][
'type'], $field_name, $field[
'definition']);
402 if (!empty($rename) && is_array($rename)) {
403 foreach ($rename as $rename_name => $renamed_field) {
407 $field = $changes[
'rename'][$renamed_field];
408 $renamed_field = $db->quoteIdentifier($renamed_field,
true);
409 $query.=
'CHANGE ' . $renamed_field .
' ' . $db->getDeclaration($field[
'definition'][
'type'], $field[
'name'], $field[
'definition']);
417 $name = $db->quoteIdentifier($name,
true);
418 return $db->exec(
"ALTER TABLE $name $query");
437 $result = $db->queryCol(
'SHOW DATABASES');
442 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
463 return $db->queryCol(
'SELECT DISTINCT USER FROM mysql.USER');
482 $query =
"SELECT name FROM mysql.proc";
493 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
516 if (!is_null($table)) {
517 $table = $db->quote($table,
'text');
525 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
547 $query =
"SHOW /*!50002 FULL*/ TABLES";
548 if (!is_null($database)) {
549 $query .=
" FROM $database";
551 $query.=
"/*!50002 WHERE Table_type = 'BASE TABLE'*/";
559 foreach ($table_names as $table) {
565 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
587 $query =
'SHOW FULL TABLES';
588 if (!is_null($database)) {
589 $query.=
" FROM $database";
591 $query.=
" WHERE Table_type = 'VIEW'";
599 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
621 $table = $db->quoteIdentifier($table,
true);
622 $result = $db->queryCol(
"SHOW COLUMNS FROM $table");
627 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
676 $table = $db->quoteIdentifier($table,
true);
677 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
678 $query =
"CREATE INDEX $name ON $table";
680 foreach ($definition[
'fields'] as $field => $fieldinfo) {
681 if (!empty($fieldinfo[
'length'])) {
682 $fields[] = $db->quoteIdentifier($field,
true) .
'(' . $fieldinfo[
'length'] .
')';
684 $fields[] = $db->quoteIdentifier($field,
true);
687 $query .=
' ('. implode(
', ', $fields) .
')';
709 $table = $db->quoteIdentifier($table,
true);
710 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
711 return $db->exec(
"DROP INDEX $name ON $table");
731 $key_name =
'Key_name';
732 $non_unique =
'Non_unique';
734 if ($db->options[
'field_case'] == CASE_LOWER) {
735 $key_name = strtolower($key_name);
736 $non_unique = strtolower($non_unique);
738 $key_name = strtoupper($key_name);
739 $non_unique = strtoupper($non_unique);
743 $table = $db->quoteIdentifier($table,
true);
744 $query =
"SHOW INDEX FROM $table";
751 foreach ($indexes as $index_data) {
752 if ($index_data[$non_unique] && ($index = $this->
_fixIndexName($index_data[$key_name]))) {
758 $result = array_change_key_case(
$result, $db->options[
'field_case']);
796 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
797 if (!empty($definition[
'primary'])) {
800 } elseif (!empty($definition[
'unique'])) {
805 'invalid definition, could not create constraint', __FUNCTION__);
808 $table = $db->quoteIdentifier($table,
true);
809 $query =
"ALTER TABLE $table ADD $type $name";
811 foreach (array_keys($definition[
'fields']) as $field) {
812 $fields[] = $db->quoteIdentifier($field,
true);
814 $query .=
' ('. implode(
', ', $fields) .
')';
837 $table = $db->quoteIdentifier($table,
true);
838 if ($primary || strtolower($name) ==
'primary') {
839 $query =
"ALTER TABLE $table DROP PRIMARY KEY";
841 $name = $db->quoteIdentifier($db->getIndexName($name),
true);
842 $query =
"ALTER TABLE $table DROP INDEX $name";
864 $key_name =
'Key_name';
865 $non_unique =
'Non_unique';
867 if ($db->options[
'field_case'] == CASE_LOWER) {
868 $key_name = strtolower($key_name);
869 $non_unique = strtolower($non_unique);
871 $key_name = strtoupper($key_name);
872 $non_unique = strtoupper($non_unique);
876 $table = $db->quoteIdentifier($table,
true);
877 $query =
"SHOW INDEX FROM $table";
884 foreach ($indexes as $index_data) {
885 if (!$index_data[$non_unique]) {
886 if ($index_data[$key_name] !==
'PRIMARY') {
891 if (!empty($index)) {
898 $result = array_change_key_case(
$result, $db->options[
'field_case']);
928 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
929 $seqcol_name = $db->quoteIdentifier($db->options[
'seqcol_name'],
true);
931 $options_strings =
array();
934 $options_strings[
'comment'] =
'COMMENT = '.$db->quote(
$options[
'comment'],
'text');
938 $options_strings[
'charset'] =
'DEFAULT CHARACTER SET '.$options[
'charset'];
940 $options_strings[
'charset'].=
' COLLATE '.$options[
'collate'];
947 } elseif ($db->options[
'default_table_type']) {
948 $type = $db->options[
'default_table_type'];
951 $options_strings[] =
"ENGINE = $type";
954 if (!empty($options_strings)) {
955 $query.=
' '.implode(
' ', $options_strings);
958 $query =
"CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
959 if (!empty($options_strings)) {
960 $query .=
' '.implode(
' ', $options_strings);
971 $query =
"INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).
')';
978 $result = $db->exec(
"DROP TABLE $sequence_name");
980 return $db->raiseError(
$result, null, null,
981 'could not drop inconsistent sequence table', __FUNCTION__);
984 return $db->raiseError(
$res, null, null,
985 'could not create sequence table', __FUNCTION__);
1005 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
1006 return $db->exec(
"DROP TABLE $sequence_name");
1027 if (!is_null($database)) {
1028 $query .=
" FROM $database";
1030 $table_names = $db->queryCol(
$query);
1032 return $table_names;
1036 foreach ($table_names as $table_name) {
1042 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
const MDB2_FETCHMODE_ASSOC
Column data indexed by column names.
const MDB2_OK(!class_exists('PEAR'))
The method mapErrorCode in each MDB2_dbtype implementation maps native error codes to one of these...
dropDatabase($name)
drop an existing database
dropConstraint($table, $name, $primary=false)
drop existing constraint
createTable($name, $fields, $options=array())
create a new table
listTableFields($table)
list all fields in a table in the current database
createDatabase($name)
create a new database
const MDB2_ERROR_CANNOT_ALTER
listViews($database=null)
list all views in the current database
listDatabases()
list all databases
listTables($database=null)
list all tables in the current database
listTableIndexes($table)
list all indexes in a table
const MDB2_FETCHMODE_ORDERED
Column data indexed by numbers, ordered from 0 and up.
if(!is_array($argv)) $options
alterTable($name, $changes, $check)
alter an existing table
listSequences($database=null)
list all sequences in the current database
listTableConstraints($table)
list all constraints in a table
const MDB2_ERROR_NEED_MORE_DATA
createSequence($seq_name, $start=1, $options=array())
create sequence
_fixSequenceName($sqn, $check=false)
Removes any formatting in an sequence name using the 'seqname_format' option.
createConstraint($table, $name, $definition)
create a constraint on a table
dropIndex($table, $name)
drop existing index
createIndex($table, $name, $definition)
Get the stucture of a field into an array.
listUsers()
list all users
Create styles array
The data for the language used.
_fixIndexName($idx)
Removes any formatting in an index name using the 'idxname_format' option.
dropSequence($seq_name)
drop existing sequence
& getDBInstance()
Get the instance of MDB2 associated with the module instance.
listTableTriggers($table=null)
list all triggers in the database that reference a given table
_getCreateTableQuery($name, $fields, $options=array())
Create a basic SQL query for a new table creation.
getTableCreationQuery($name, $fields, $options=array())
PATCH: For Testcases Only.
listFunctions()
list all functions in the current database
const MDB2_PORTABILITY_FIX_CASE
Portability: convert names of tables and fields to case defined in the "field_case" option when using...
isError($data, $code=null)
Tell whether a value is a PEAR error.