47 require_once
'MDB2/Driver/Manager/Common.php';
74 $name = $db->quoteIdentifier($name,
true);
75 return $db->standaloneQuery(
"CREATE DATABASE $name", null,
true);
95 $name = $db->quoteIdentifier($name,
true);
96 return $db->standaloneQuery(
"DROP DATABASE $name", null,
true);
199 foreach ($changes as $change_name => $change) {
200 switch ($change_name) {
209 'change type "'.$change_name.
'\" not yet supported', __FUNCTION__);
217 if (!empty($changes[
'add']) && is_array($changes[
'add'])) {
218 foreach ($changes[
'add'] as $field_name => $field) {
219 $query =
'ADD ' . $db->getDeclaration($field[
'type'], $field_name, $field);
220 $result = $db->exec(
"ALTER TABLE $name $query");
227 if (!empty($changes[
'remove']) && is_array($changes[
'remove'])) {
228 foreach ($changes[
'remove'] as $field_name => $field) {
229 $field_name = $db->quoteIdentifier($field_name,
true);
230 $query =
'DROP ' . $field_name;
231 $result = $db->exec(
"ALTER TABLE $name $query");
238 if (!empty($changes[
'change']) && is_array($changes[
'change'])) {
239 foreach ($changes[
'change'] as $field_name => $field) {
240 $field_name = $db->quoteIdentifier($field_name,
true);
241 if (!empty($field[
'type'])) {
242 $server_info = $db->getServerVersion();
246 if (is_array($server_info) && $server_info[
'major'] < 8) {
248 'changing column type for "'.$change_name.
'\" requires PostgreSQL 8.0 or above', __FUNCTION__);
250 $db->loadModule(
'Datatype', null,
true);
251 $query =
"ALTER $field_name TYPE ".$db->datatype->getTypeDeclaration($field[
'definition']);
252 $result = $db->exec(
"ALTER TABLE $name $query");
257 if (array_key_exists(
'default', $field)) {
258 $query =
"ALTER $field_name SET DEFAULT ".$db->quote($field[
'definition'][
'default'], $field[
'definition'][
'type']);
259 $result = $db->exec(
"ALTER TABLE $name $query");
264 if (!empty($field[
'notnull'])) {
265 $query =
"ALTER $field_name ".($field[
'definition'][
'notnull'] ?
"SET" :
"DROP").
' NOT NULL';
266 $result = $db->exec(
"ALTER TABLE $name $query");
274 if (!empty($changes[
'rename']) && is_array($changes[
'rename'])) {
275 foreach ($changes[
'rename'] as $field_name => $field) {
276 $field_name = $db->quoteIdentifier($field_name,
true);
277 $result = $db->exec(
"ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field[
'name'],
true));
285 $name = $db->quoteIdentifier($name,
true);
286 if (!empty($changes[
'name'])) {
288 foreach ($indexes as $index) {
289 $new_name = str_replace($name_orig, $changes[
'name'], $index);
290 $result = $db->exec(
"ALTER INDEX {$index}_idx RENAME TO {$new_name}_idx");
297 foreach ($constraints as $constraint) {
298 $new_name = str_replace($name_orig, $changes[
'name'], $constraint);
299 $result = $db->exec(
"ALTER TABLE {$name_orig} RENAME CONSTRAINT {$constraint}_idx TO {$new_name}_idx;");
305 $change_name = $db->quoteIdentifier($changes[
'name'],
true);
306 $result = $db->exec(
"ALTER TABLE $name RENAME TO ".$change_name);
331 $query =
'SELECT datname FROM pg_database';
332 $result2 = $db->standaloneQuery(
$query,
array(
'text'),
false);
337 $result = $result2->fetchCol();
343 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
364 $query =
'SELECT usename FROM pg_user';
365 $result2 = $db->standaloneQuery(
$query,
array(
'text'),
false);
370 $result = $result2->fetchCol();
393 WHERE schemaname NOT IN ('pg_catalog', 'information_schema') 394 AND viewname !~ '^pg_'";
400 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
422 $query =
'SELECT viewname FROM pg_views NATURAL JOIN pg_tables';
423 $query.=
' WHERE tablename ='.$db->quote($table,
'text');
429 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
457 tp.oid = pr.prorettype 458 AND pr.proisagg = FALSE 459 AND tp.typname <> 'trigger' 460 AND pr.pronamespace IN 461 (SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
467 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
489 $query =
'SELECT trg.tgname AS trigger_name 492 WHERE trg.tgrelid = tbl.oid';
493 if (!is_null($table)) {
494 $table = $db->quote(strtoupper($table),
'text');
495 $query .=
" AND tbl.relname = $table";
502 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
524 $query =
'SELECT c.relname AS "Name"' 525 .
' FROM pg_class c, pg_user u' 526 .
' WHERE c.relowner = u.usesysid' 527 .
" AND c.relkind = 'r'" 529 .
' (SELECT 1 FROM pg_views' 530 .
' WHERE viewname = c.relname)' 531 .
" AND c.relname !~ '^(pg_|sql_)'" 533 .
' SELECT c.relname AS "Name"' 535 .
" WHERE c.relkind = 'r'" 537 .
' (SELECT 1 FROM pg_views' 538 .
' WHERE viewname = c.relname)' 540 .
' (SELECT 1 FROM pg_user' 541 .
' WHERE usesysid = c.relowner)' 542 .
" AND c.relname !~ '^pg_'";
548 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
571 $table = $db->quoteIdentifier($table,
true);
573 $result2 = $db->query(
"SELECT * FROM $table");
577 $result = $result2->getColumnNames();
602 $table = $db->quote($table,
'text');
603 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
604 $subquery.=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
605 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
606 $indexes = $db->queryCol(
$query,
'text');
612 foreach ($indexes as $index) {
614 if (!empty($index)) {
620 $result = array_change_key_case(
$result, $db->options[
'field_case']);
642 $table = $db->quote($table,
'text');
643 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
644 $subquery.=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
645 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
646 $constraints = $db->queryCol(
$query);
652 foreach ($constraints as $constraint) {
654 if (!empty($constraint)) {
660 && $db->options[
'field_case'] == CASE_LOWER
662 $result = array_change_key_case(
$result, $db->options[
'field_case']);
685 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
686 return $db->exec(
"CREATE SEQUENCE $sequence_name INCREMENT 1".
687 (
$start < 1 ?
" MINVALUE $start" :
'').
" START $start");
707 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
708 return $db->exec(
"DROP SEQUENCE $sequence_name");
727 $query =
"SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
728 $query.=
"(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
729 $table_names = $db->queryCol(
$query);
734 foreach ($table_names as $table_name) {
738 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
const MDB2_OK(!class_exists('PEAR'))
The method mapErrorCode in each MDB2_dbtype implementation maps native error codes to one of these...
listUsers()
list all users
listTableConstraints($table)
list all constraints in a table
listTableFields($table)
list all fields in a table in the current database
const MDB2_ERROR_CANNOT_ALTER
listTableViews($table)
list the views in the database that reference a given table
listViews()
list all views in the current database
listFunctions()
list all functions in the current database
createDatabase($name)
create a new database
listSequences()
list all sequences in the current database
dropDatabase($name)
drop an existing database
alterTable($name, $changes, $check)
alter an existing table
listTableTriggers($table=null)
list all triggers in the database that reference a given table
isResultCommon($value)
Tell whether a value is a MDB2 result implementing the common interface.
listTableIndexes($table)
list all indexes in a table
_fixSequenceName($sqn, $check=false)
Removes any formatting in an sequence name using the 'seqname_format' option.
Create styles array
The data for the language used.
_fixIndexName($idx)
Removes any formatting in an index name using the 'idxname_format' option.
listDatabases()
list all databases
dropSequence($seq_name)
drop existing sequence
& getDBInstance()
Get the instance of MDB2 associated with the module instance.
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.
createSequence($seq_name, $start=1)
create sequence
listTables()
list all tables in the current database