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));
284 $name = $db->quoteIdentifier($name,
true);
285 if (!empty($changes[
'name'])) {
286 $change_name = $db->quoteIdentifier($changes[
'name'],
true);
287 $result = $db->exec(
"ALTER TABLE $name RENAME TO ".$change_name);
312 $query =
'SELECT datname FROM pg_database';
313 $result2 = $db->standaloneQuery(
$query, array(
'text'),
false);
318 $result = $result2->fetchCol();
324 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
345 $query =
'SELECT usename FROM pg_user';
346 $result2 = $db->standaloneQuery(
$query, array(
'text'),
false);
351 $result = $result2->fetchCol();
374 WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
375 AND viewname !~ '^pg_'";
381 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
403 $query =
'SELECT viewname FROM pg_views NATURAL JOIN pg_tables';
404 $query.=
' WHERE tablename ='.$db->quote($table,
'text');
410 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
438 tp.oid = pr.prorettype
439 AND pr.proisagg = FALSE
440 AND tp.typname <> 'trigger'
441 AND pr.pronamespace IN
442 (SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
448 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
470 $query =
'SELECT trg.tgname AS trigger_name
473 WHERE trg.tgrelid = tbl.oid';
474 if (!is_null($table)) {
475 $table = $db->quote(strtoupper($table),
'text');
476 $query .=
" AND tbl.relname = $table";
483 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
505 $query =
'SELECT c.relname AS "Name"'
506 .
' FROM pg_class c, pg_user u'
507 .
' WHERE c.relowner = u.usesysid'
508 .
" AND c.relkind = 'r'"
510 .
' (SELECT 1 FROM pg_views'
511 .
' WHERE viewname = c.relname)'
512 .
" AND c.relname !~ '^(pg_|sql_)'"
514 .
' SELECT c.relname AS "Name"'
516 .
" WHERE c.relkind = 'r'"
518 .
' (SELECT 1 FROM pg_views'
519 .
' WHERE viewname = c.relname)'
521 .
' (SELECT 1 FROM pg_user'
522 .
' WHERE usesysid = c.relowner)'
523 .
" AND c.relname !~ '^pg_'";
529 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);
551 $table = $db->quoteIdentifier($table,
true);
553 $result2 = $db->query(
"SELECT * FROM $table");
557 $result = $result2->getColumnNames();
582 $table = $db->quote($table,
'text');
583 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
584 $subquery.=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND indisunique != 't' AND indisprimary != 't'";
585 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
586 $indexes = $db->queryCol(
$query,
'text');
592 foreach ($indexes as $index) {
594 if (!empty($index)) {
600 $result = array_change_key_case(
$result, $db->options[
'field_case']);
622 $table = $db->quote($table,
'text');
623 $subquery =
"SELECT indexrelid FROM pg_index, pg_class";
624 $subquery.=
" WHERE pg_class.relname=$table AND pg_class.oid=pg_index.indrelid AND (indisunique = 't' OR indisprimary = 't')";
625 $query =
"SELECT relname FROM pg_class WHERE oid IN ($subquery)";
626 $constraints = $db->queryCol(
$query);
632 foreach ($constraints as $constraint) {
634 if (!empty($constraint)) {
640 && $db->options[
'field_case'] == CASE_LOWER
642 $result = array_change_key_case(
$result, $db->options[
'field_case']);
665 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
666 return $db->exec(
"CREATE SEQUENCE $sequence_name INCREMENT 1".
667 ($start < 1 ?
" MINVALUE $start" :
'').
" START $start");
687 $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name),
true);
688 return $db->exec(
"DROP SEQUENCE $sequence_name");
707 $query =
"SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN";
708 $query.=
"(SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%' AND nspname != 'information_schema')";
709 $table_names = $db->queryCol(
$query);
714 foreach ($table_names as $table_name) {
718 $result = array_map(($db->options[
'field_case'] == CASE_LOWER ?
'strtolower' :
'strtoupper'),
$result);