2
// +----------------------------------------------------------------------+
3
// | PHP versions 4 and 5 |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
6
// | Stig. S. Bakken, Lukas Smith |
7
// | All rights reserved. |
8
// +----------------------------------------------------------------------+
9
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
10
// | API as well as database abstraction for PHP applications. |
11
// | This LICENSE is in the BSD license style. |
13
// | Redistribution and use in source and binary forms, with or without |
14
// | modification, are permitted provided that the following conditions |
17
// | Redistributions of source code must retain the above copyright |
18
// | notice, this list of conditions and the following disclaimer. |
20
// | Redistributions in binary form must reproduce the above copyright |
21
// | notice, this list of conditions and the following disclaimer in the |
22
// | documentation and/or other materials provided with the distribution. |
24
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
25
// | Lukas Smith nor the names of his contributors may be used to endorse |
26
// | or promote products derived from this software without specific prior|
27
// | written permission. |
29
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
30
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
31
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
32
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
33
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
34
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
35
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
36
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
37
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
38
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
39
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
40
// | POSSIBILITY OF SUCH DAMAGE. |
41
// +----------------------------------------------------------------------+
42
// | Author: Paul Cooper <pgc@ucecom.com> |
43
// +----------------------------------------------------------------------+
45
// $Id: pgsql.php,v 1.70 2007/04/10 20:35:49 nrf Exp $
47
require_once 'MDB2/Driver/Manager/Common.php';
50
* MDB2 MySQL driver for the management modules
54
* @author Paul Cooper <pgc@ucecom.com>
56
class MDB2_Driver_Manager_pgsql extends MDB2_Driver_Manager_Common
58
// {{{ createDatabase()
61
* create a new database
63
* @param string $name name of the database that should be created
64
* @return mixed MDB2_OK on success, a MDB2 error on failure
67
function createDatabase($name)
69
$db =& $this->getDBInstance();
70
if (PEAR::isError($db)) {
74
$name = $db->quoteIdentifier($name, true);
75
return $db->standaloneQuery("CREATE DATABASE $name", null, true);
82
* drop an existing database
84
* @param string $name name of the database that should be dropped
85
* @return mixed MDB2_OK on success, a MDB2 error on failure
88
function dropDatabase($name)
90
$db =& $this->getDBInstance();
91
if (PEAR::isError($db)) {
95
$name = $db->quoteIdentifier($name, true);
96
return $db->standaloneQuery("DROP DATABASE $name", null, true);
103
* alter an existing table
105
* @param string $name name of the table that is intended to be changed.
106
* @param array $changes associative array that contains the details of each type
107
* of change that is intended to be performed. The types of
108
* changes that are currently supported are defined as follows:
112
* New name for the table.
116
* Associative array with the names of fields to be added as
117
* indexes of the array. The value of each entry of the array
118
* should be set to another associative array with the properties
119
* of the fields to be added. The properties of the fields should
120
* be the same as defined by the MDB2 parser.
125
* Associative array with the names of fields to be removed as indexes
126
* of the array. Currently the values assigned to each entry are ignored.
127
* An empty array should be used for future compatibility.
131
* Associative array with the names of fields to be renamed as indexes
132
* of the array. The value of each entry of the array should be set to
133
* another associative array with the entry named name with the new
134
* field name and the entry named Declaration that is expected to contain
135
* the portion of the field declaration already in DBMS specific SQL code
136
* as it is used in the CREATE TABLE statement.
140
* Associative array with the names of the fields to be changed as indexes
141
* of the array. Keep in mind that if it is intended to change either the
142
* name of a field and any other properties, the change array entries
143
* should have the new names of the fields as array indexes.
145
* The value of each entry of the array should be set to another associative
146
* array with the properties of the fields to that are meant to be changed as
147
* array entries. These entries should be assigned to the new values of the
148
* respective properties. The properties of the fields should be the same
149
* as defined by the MDB2 parser.
153
* 'name' => 'userlist',
156
* 'type' => 'integer',
161
* 'file_limit' => array(),
162
* 'time_limit' => array()
167
* 'definition' => array(
175
* 'name' => 'gender',
176
* 'definition' => array(
185
* @param boolean $check indicates whether the function should just check if the DBMS driver
186
* can perform the requested table alterations if the value is true or
187
* actually perform them otherwise.
190
* @return mixed MDB2_OK on success, a MDB2 error on failure
192
function alterTable($name, $changes, $check)
194
$db =& $this->getDBInstance();
195
if (PEAR::isError($db)) {
199
foreach ($changes as $change_name => $change) {
200
switch ($change_name) {
208
return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
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");
221
if (PEAR::isError($result)) {
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");
232
if (PEAR::isError($result)) {
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();
243
if (PEAR::isError($server_info)) {
246
if (is_array($server_info) && $server_info['major'] < 8) {
247
return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
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");
253
if (PEAR::isError($result)) {
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");
260
if (PEAR::isError($result)) {
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");
267
if (PEAR::isError($result)) {
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));
278
if (PEAR::isError($result)) {
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);
288
if (PEAR::isError($result)) {
297
// {{{ listDatabases()
302
* @return mixed array of database names on success, a MDB2 error on failure
305
function listDatabases()
307
$db =& $this->getDBInstance();
308
if (PEAR::isError($db)) {
312
$query = 'SELECT datname FROM pg_database';
313
$result2 = $db->standaloneQuery($query, array('text'), false);
314
if (!MDB2::isResultCommon($result2)) {
318
$result = $result2->fetchCol();
320
if (PEAR::isError($result)) {
323
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
324
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
335
* @return mixed array of user names on success, a MDB2 error on failure
340
$db =& $this->getDBInstance();
341
if (PEAR::isError($db)) {
345
$query = 'SELECT usename FROM pg_user';
346
$result2 = $db->standaloneQuery($query, array('text'), false);
347
if (!MDB2::isResultCommon($result2)) {
351
$result = $result2->fetchCol();
360
* list all views in the current database
362
* @return mixed array of view names on success, a MDB2 error on failure
367
$db =& $this->getDBInstance();
368
if (PEAR::isError($db)) {
372
$query = "SELECT viewname
374
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
375
AND viewname !~ '^pg_'";
376
$result = $db->queryCol($query);
377
if (PEAR::isError($result)) {
380
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
381
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
387
// {{{ listTableViews()
390
* list the views in the database that reference a given table
392
* @param string table for which all referenced views should be found
393
* @return mixed array of view names on success, a MDB2 error on failure
396
function listTableViews($table)
398
$db =& $this->getDBInstance();
399
if (PEAR::isError($db)) {
403
$query = 'SELECT viewname FROM pg_views NATURAL JOIN pg_tables';
404
$query.= ' WHERE tablename ='.$db->quote($table, 'text');
405
$result = $db->queryCol($query);
406
if (PEAR::isError($result)) {
409
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
410
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
416
// {{{ listFunctions()
419
* list all functions in the current database
421
* @return mixed array of function names on success, a MDB2 error on failure
424
function listFunctions()
426
$db =& $this->getDBInstance();
427
if (PEAR::isError($db)) {
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')";
443
$result = $db->queryCol($query);
444
if (PEAR::isError($result)) {
447
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
448
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
454
// {{{ listTableTriggers()
457
* list all triggers in the database that reference a given table
459
* @param string table for which all referenced triggers should be found
460
* @return mixed array of trigger names on success, a MDB2 error on failure
463
function listTableTriggers($table = null)
465
$db =& $this->getDBInstance();
466
if (PEAR::isError($db)) {
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";
478
$result = $db->queryCol($query);
479
if (PEAR::isError($result)) {
482
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
483
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
492
* list all tables in the current database
494
* @return mixed array of table names on success, a MDB2 error on failure
497
function listTables()
499
$db =& $this->getDBInstance();
500
if (PEAR::isError($db)) {
504
// gratuitously stolen from PEAR DB _getSpecialQuery in pgsql.php
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_'";
524
$result = $db->queryCol($query);
525
if (PEAR::isError($result)) {
528
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
529
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
535
// {{{ listTableFields()
538
* list all fields in a table in the current database
540
* @param string $table name of table that should be used in method
541
* @return mixed array of field names on success, a MDB2 error on failure
544
function listTableFields($table)
546
$db =& $this->getDBInstance();
547
if (PEAR::isError($db)) {
551
$table = $db->quoteIdentifier($table, true);
553
$result2 = $db->query("SELECT * FROM $table");
554
if (PEAR::isError($result2)) {
557
$result = $result2->getColumnNames();
559
if (PEAR::isError($result)) {
562
return array_flip($result);
566
// {{{ listTableIndexes()
569
* list all indexes in a table
571
* @param string $table name of table that should be used in method
572
* @return mixed array of index names on success, a MDB2 error on failure
575
function listTableIndexes($table)
577
$db =& $this->getDBInstance();
578
if (PEAR::isError($db)) {
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');
587
if (PEAR::isError($indexes)) {
592
foreach ($indexes as $index) {
593
$index = $this->_fixIndexName($index);
594
if (!empty($index)) {
595
$result[$index] = true;
599
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
600
$result = array_change_key_case($result, $db->options['field_case']);
602
return array_keys($result);
606
// {{{ listTableConstraints()
609
* list all constraints in a table
611
* @param string $table name of table that should be used in method
612
* @return mixed array of constraint names on success, a MDB2 error on failure
615
function listTableConstraints($table)
617
$db =& $this->getDBInstance();
618
if (PEAR::isError($db)) {
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);
627
if (PEAR::isError($constraints)) {
632
foreach ($constraints as $constraint) {
633
$constraint = $this->_fixIndexName($constraint);
634
if (!empty($constraint)) {
635
$result[$constraint] = true;
639
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE
640
&& $db->options['field_case'] == CASE_LOWER
642
$result = array_change_key_case($result, $db->options['field_case']);
644
return array_keys($result);
648
// {{{ createSequence()
653
* @param string $seq_name name of the sequence to be created
654
* @param string $start start value of the sequence; default is 1
655
* @return mixed MDB2_OK on success, a MDB2 error on failure
658
function createSequence($seq_name, $start = 1)
660
$db =& $this->getDBInstance();
661
if (PEAR::isError($db)) {
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");
671
// {{{ dropSequence()
674
* drop existing sequence
676
* @param string $seq_name name of the sequence to be dropped
677
* @return mixed MDB2_OK on success, a MDB2 error on failure
680
function dropSequence($seq_name)
682
$db =& $this->getDBInstance();
683
if (PEAR::isError($db)) {
687
$sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
688
return $db->exec("DROP SEQUENCE $sequence_name");
692
// {{{ listSequences()
695
* list all sequences in the current database
697
* @return mixed array of sequence names on success, a MDB2 error on failure
700
function listSequences()
702
$db =& $this->getDBInstance();
703
if (PEAR::isError($db)) {
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);
710
if (PEAR::isError($table_names)) {
714
foreach ($table_names as $table_name) {
715
$result[] = $this->_fixSequenceName($table_name);
717
if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
718
$result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);