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.
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 $
25
* @see Zend_Db_Adapter_Pdo_Abstract
27
require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
31
* Class for connecting to PostgreSQL databases and performing common operations.
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
39
class Zend_Db_Adapter_Pdo_Pgsql extends Zend_Db_Adapter_Pdo_Abstract
47
protected $_pdoType = 'pgsql';
50
* Keys are UPPERCASE SQL datatypes or the constants
51
* Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
56
* 2 = float or decimal
58
* @var array Associative array of datatypes to values 0, 1, or 2.
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
76
* Returns a list of the tables in the database.
80
public function listTables()
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_)' "
89
. "SELECT c.relname AS table_name "
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_'";
96
return $this->fetchCol($sql);
100
* Returns the column descriptions for a table.
102
* The return value is an associative array keyed by the column name,
103
* as returned by the RDBMS.
105
* The value of each array element is an associative array
106
* with the following keys:
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
123
* @todo Discover integer unsigned property.
125
* @param string $tableName
126
* @param string $schemaName OPTIONAL
129
public function describeTable($tableName, $schemaName = null)
135
a.attname AS colname,
138
FORMAT_TYPE(a.atttypid, a.atttypmod) AS complete_type,
139
d.adsrc AS default_value,
140
a.attnotnull AS notnull,
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);
153
$sql .= " AND n.nspname = ".$this->quote($schemaName);
155
$sql .= ' ORDER BY a.attnum';
157
$stmt = $this->query($sql);
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);
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];
182
$row[$length] = null; // unlimited
186
list($primary, $primaryPosition, $identity) = array(false, null, false);
187
if ($row[$contype] == 'p') {
189
$primaryPosition = array_search($row[$attnum], explode(',', $row[$conkey])) + 1;
190
$identity = (bool) (preg_match('/^nextval/', $row[$default_value]));
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
214
* Adds an adapter-specific LIMIT clause to the SELECT statement.
217
* @param integer $count
218
* @param integer $offset OPTIONAL
221
public function limit($sql, $count, $offset = 0)
223
$count = intval($count);
226
* @see Zend_Db_Adapter_Exception
228
require_once 'Zend/Db/Adapter/Exception.php';
229
throw new Zend_Db_Adapter_Exception("LIMIT argument count=$count is not valid");
232
$offset = intval($offset);
235
* @see Zend_Db_Adapter_Exception
237
require_once 'Zend/Db/Adapter/Exception.php';
238
throw new Zend_Db_Adapter_Exception("LIMIT argument offset=$offset is not valid");
241
$sql .= " LIMIT $count";
243
$sql .= " OFFSET $offset";
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.
254
* @param string $sequenceName
257
public function lastSequenceId($sequenceName)
260
$value = $this->fetchOne("SELECT CURRVAL(".$this->quote($sequenceName).")");
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.
269
* @param string $sequenceName
272
public function nextSequenceId($sequenceName)
275
$value = $this->fetchOne("SELECT NEXTVAL(".$this->quote($sequenceName).")");
280
* Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column.
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.
289
* @param string $tableName OPTIONAL Name of table.
290
* @param string $primaryKey OPTIONAL Name of primary key column.
293
public function lastInsertId($tableName = null, $primaryKey = null)
295
if ($tableName !== null) {
296
$sequenceName = $tableName;
298
$sequenceName .= "_$primaryKey";
300
$sequenceName .= '_seq';
301
return $this->lastSequenceId($sequenceName);
303
return $this->_connection->lastInsertId($tableName);