~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Db/Adapter/Pdo/Pgsql.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Db
 
17
 * @subpackage Adapter
 
18
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
19
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
20
 * @version    $Id: Pgsql.php 9101 2008-03-30 19:54:38Z thomas $
 
21
 */
 
22
 
 
23
 
 
24
/**
 
25
 * @see Zend_Db_Adapter_Pdo_Abstract
 
26
 */
 
27
require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
 
28
 
 
29
 
 
30
/**
 
31
 * Class for connecting to PostgreSQL databases and performing common operations.
 
32
 *
 
33
 * @category   Zend
 
34
 * @package    Zend_Db
 
35
 * @subpackage Adapter
 
36
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
37
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
38
 */
 
39
class Zend_Db_Adapter_Pdo_Pgsql extends Zend_Db_Adapter_Pdo_Abstract
 
40
{
 
41
 
 
42
    /**
 
43
     * PDO type.
 
44
     *
 
45
     * @var string
 
46
     */
 
47
    protected $_pdoType = 'pgsql';
 
48
 
 
49
    /**
 
50
     * Keys are UPPERCASE SQL datatypes or the constants
 
51
     * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
 
52
     *
 
53
     * Values are:
 
54
     * 0 = 32-bit integer
 
55
     * 1 = 64-bit integer
 
56
     * 2 = float or decimal
 
57
     *
 
58
     * @var array Associative array of datatypes to values 0, 1, or 2.
 
59
     */
 
60
    protected $_numericDataTypes = array(
 
61
        Zend_Db::INT_TYPE    => Zend_Db::INT_TYPE,
 
62
        Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
 
63
        Zend_Db::FLOAT_TYPE  => Zend_Db::FLOAT_TYPE,
 
64
        'INTEGER'            => Zend_Db::INT_TYPE,
 
65
        'SERIAL'             => Zend_Db::INT_TYPE,
 
66
        'SMALLINT'           => Zend_Db::INT_TYPE,
 
67
        'BIGINT'             => Zend_Db::BIGINT_TYPE,
 
68
        'BIGSERIAL'          => Zend_Db::BIGINT_TYPE,
 
69
        'DECIMAL'            => Zend_Db::FLOAT_TYPE,
 
70
        'DOUBLE PRECISION'   => Zend_Db::FLOAT_TYPE,
 
71
        'NUMERIC'            => Zend_Db::FLOAT_TYPE,
 
72
        'REAL'               => Zend_Db::FLOAT_TYPE
 
73
    );
 
74
 
 
75
    /**
 
76
     * Returns a list of the tables in the database.
 
77
     *
 
78
     * @return array
 
79
     */
 
80
    public function listTables()
 
81
    {
 
82
        // @todo use a better query with joins instead of subqueries
 
83
        $sql = "SELECT c.relname AS table_name "
 
84
             . "FROM pg_class c, pg_user u "
 
85
             . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
 
86
             . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
 
87
             . "AND c.relname !~ '^(pg_|sql_)' "
 
88
             . "UNION "
 
89
             . "SELECT c.relname AS table_name "
 
90
             . "FROM pg_class c "
 
91
             . "WHERE c.relkind = 'r' "
 
92
             . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
 
93
             . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
 
94
             . "AND c.relname !~ '^pg_'";
 
95
 
 
96
        return $this->fetchCol($sql);
 
97
    }
 
98
 
 
99
    /**
 
100
     * Returns the column descriptions for a table.
 
101
     *
 
102
     * The return value is an associative array keyed by the column name,
 
103
     * as returned by the RDBMS.
 
104
     *
 
105
     * The value of each array element is an associative array
 
106
     * with the following keys:
 
107
     *
 
108
     * SCHEMA_NAME      => string; name of database or schema
 
109
     * TABLE_NAME       => string;
 
110
     * COLUMN_NAME      => string; column name
 
111
     * COLUMN_POSITION  => number; ordinal position of column in table
 
112
     * DATA_TYPE        => string; SQL datatype name of column
 
113
     * DEFAULT          => string; default expression of column, null if none
 
114
     * NULLABLE         => boolean; true if column can have nulls
 
115
     * LENGTH           => number; length of CHAR/VARCHAR
 
116
     * SCALE            => number; scale of NUMERIC/DECIMAL
 
117
     * PRECISION        => number; precision of NUMERIC/DECIMAL
 
118
     * UNSIGNED         => boolean; unsigned property of an integer type
 
119
     * PRIMARY          => boolean; true if column is part of the primary key
 
120
     * PRIMARY_POSITION => integer; position of column in primary key
 
121
     * IDENTITY         => integer; true if column is auto-generated with unique values
 
122
     *
 
123
     * @todo Discover integer unsigned property.
 
124
     *
 
125
     * @param  string $tableName
 
126
     * @param  string $schemaName OPTIONAL
 
127
     * @return array
 
128
     */
 
129
    public function describeTable($tableName, $schemaName = null)
 
130
    {
 
131
        $sql = "SELECT
 
132
                a.attnum,
 
133
                n.nspname,
 
134
                c.relname,
 
135
                a.attname AS colname,
 
136
                t.typname AS type,
 
137
                a.atttypmod,
 
138
                FORMAT_TYPE(a.atttypid, a.atttypmod) AS complete_type,
 
139
                d.adsrc AS default_value,
 
140
                a.attnotnull AS notnull,
 
141
                a.attlen AS length,
 
142
                co.contype,
 
143
                ARRAY_TO_STRING(co.conkey, ',') AS conkey
 
144
            FROM pg_attribute AS a
 
145
                JOIN pg_class AS c ON a.attrelid = c.oid
 
146
                JOIN pg_namespace AS n ON c.relnamespace = n.oid
 
147
                JOIN pg_type AS t ON a.atttypid = t.oid
 
148
                LEFT OUTER JOIN pg_constraint AS co ON (co.conrelid = c.oid
 
149
                    AND a.attnum = ANY(co.conkey) AND co.contype = 'p')
 
150
                LEFT OUTER JOIN pg_attrdef AS d ON d.adrelid = c.oid AND d.adnum = a.attnum
 
151
            WHERE a.attnum > 0 AND c.relname = ".$this->quote($tableName);
 
152
        if ($schemaName) {
 
153
            $sql .= " AND n.nspname = ".$this->quote($schemaName);
 
154
        }
 
155
        $sql .= ' ORDER BY a.attnum';
 
156
 
 
157
        $stmt = $this->query($sql);
 
158
 
 
159
        // Use FETCH_NUM so we are not dependent on the CASE attribute of the PDO connection
 
160
        $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
 
161
 
 
162
        $attnum        = 0;
 
163
        $nspname       = 1;
 
164
        $relname       = 2;
 
165
        $colname       = 3;
 
166
        $type          = 4;
 
167
        $atttypemod    = 5;
 
168
        $complete_type = 6;
 
169
        $default_value = 7;
 
170
        $notnull       = 8;
 
171
        $length        = 9;
 
172
        $contype       = 10;
 
173
        $conkey        = 11;
 
174
 
 
175
        $desc = array();
 
176
        foreach ($result as $key => $row) {
 
177
            if ($row[$type] == 'varchar') {
 
178
                if (preg_match('/character varying(?:\((\d+)\))?/', $row[$complete_type], $matches)) {
 
179
                    if (isset($matches[1])) {
 
180
                        $row[$length] = $matches[1];
 
181
                    } else {
 
182
                        $row[$length] = null; // unlimited
 
183
                    }
 
184
                }
 
185
            }
 
186
            list($primary, $primaryPosition, $identity) = array(false, null, false);
 
187
            if ($row[$contype] == 'p') {
 
188
                $primary = true;
 
189
                $primaryPosition = array_search($row[$attnum], explode(',', $row[$conkey])) + 1;
 
190
                $identity = (bool) (preg_match('/^nextval/', $row[$default_value]));
 
191
            }
 
192
            $desc[$this->foldCase($row[$colname])] = array(
 
193
                'SCHEMA_NAME'      => $this->foldCase($row[$nspname]),
 
194
                'TABLE_NAME'       => $this->foldCase($row[$relname]),
 
195
                'COLUMN_NAME'      => $this->foldCase($row[$colname]),
 
196
                'COLUMN_POSITION'  => $row[$attnum],
 
197
                'DATA_TYPE'        => $row[$type],
 
198
                'DEFAULT'          => $row[$default_value],
 
199
                'NULLABLE'         => (bool) ($row[$notnull] != 't'),
 
200
                'LENGTH'           => $row[$length],
 
201
                'SCALE'            => null, // @todo
 
202
                'PRECISION'        => null, // @todo
 
203
                'UNSIGNED'         => null, // @todo
 
204
                'PRIMARY'          => $primary,
 
205
                'PRIMARY_POSITION' => $primaryPosition,
 
206
                'IDENTITY'         => $identity
 
207
            );
 
208
        }
 
209
        return $desc;
 
210
    }
 
211
 
 
212
 
 
213
    /**
 
214
     * Adds an adapter-specific LIMIT clause to the SELECT statement.
 
215
     *
 
216
     * @param string $sql
 
217
     * @param integer $count
 
218
     * @param integer $offset OPTIONAL
 
219
     * @return string
 
220
     */
 
221
    public function limit($sql, $count, $offset = 0)
 
222
    {
 
223
        $count = intval($count);
 
224
        if ($count <= 0) {
 
225
            /**
 
226
             * @see Zend_Db_Adapter_Exception
 
227
             */
 
228
            require_once 'Zend/Db/Adapter/Exception.php';
 
229
            throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
 
230
        }
 
231
 
 
232
        $offset = intval($offset);
 
233
        if ($offset < 0) {
 
234
            /**
 
235
             * @see Zend_Db_Adapter_Exception
 
236
             */
 
237
            require_once 'Zend/Db/Adapter/Exception.php';
 
238
            throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
 
239
        }
 
240
 
 
241
        $sql .= " LIMIT $count";
 
242
        if ($offset > 0) {
 
243
            $sql .= " OFFSET $offset";
 
244
        }
 
245
 
 
246
        return $sql;
 
247
    }
 
248
 
 
249
    /**
 
250
     * Return the most recent value from the specified sequence in the database.
 
251
     * This is supported only on RDBMS brands that support sequences
 
252
     * (e.g. Oracle, PostgreSQL, DB2).  Other RDBMS brands return null.
 
253
     *
 
254
     * @param string $sequenceName
 
255
     * @return string
 
256
     */
 
257
    public function lastSequenceId($sequenceName)
 
258
    {
 
259
        $this->_connect();
 
260
        $value = $this->fetchOne("SELECT CURRVAL(".$this->quote($sequenceName).")");
 
261
        return $value;
 
262
    }
 
263
 
 
264
    /**
 
265
     * Generate a new value from the specified sequence in the database, and return it.
 
266
     * This is supported only on RDBMS brands that support sequences
 
267
     * (e.g. Oracle, PostgreSQL, DB2).  Other RDBMS brands return null.
 
268
     *
 
269
     * @param string $sequenceName
 
270
     * @return string
 
271
     */
 
272
    public function nextSequenceId($sequenceName)
 
273
    {
 
274
        $this->_connect();
 
275
        $value = $this->fetchOne("SELECT NEXTVAL(".$this->quote($sequenceName).")");
 
276
        return $value;
 
277
    }
 
278
 
 
279
    /**
 
280
     * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
 
281
     *
 
282
     * As a convention, on RDBMS brands that support sequences
 
283
     * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence
 
284
     * from the arguments and returns the last id generated by that sequence.
 
285
     * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method
 
286
     * returns the last value generated for such a column, and the table name
 
287
     * argument is disregarded.
 
288
     *
 
289
     * @param string $tableName   OPTIONAL Name of table.
 
290
     * @param string $primaryKey  OPTIONAL Name of primary key column.
 
291
     * @return string
 
292
     */
 
293
    public function lastInsertId($tableName = null, $primaryKey = null)
 
294
    {
 
295
        if ($tableName !== null) {
 
296
            $sequenceName = $tableName;
 
297
            if ($primaryKey) {
 
298
                $sequenceName .= "_$primaryKey";
 
299
            }
 
300
            $sequenceName .= '_seq';
 
301
            return $this->lastSequenceId($sequenceName);
 
302
        }
 
303
        return $this->_connection->lastInsertId($tableName);
 
304
    }
 
305
 
 
306
}