2008-07-17 03:51:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dibi - tiny'n'smart database abstraction layer
|
|
|
|
* ----------------------------------------------
|
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
2008-07-17 03:51:29 +00:00
|
|
|
* @license http://dibiphp.com/license dibi license
|
|
|
|
* @link http://dibiphp.com
|
2010-02-24 07:33:16 +01:00
|
|
|
* @package dibi\drivers
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dibi driver for PostgreSQL database.
|
|
|
|
*
|
|
|
|
* Connection options:
|
|
|
|
* - 'host','hostaddr','port','dbname','user','password','connect_timeout','options','sslmode','service' - see PostgreSQL API
|
|
|
|
* - 'string' - or use connection string
|
|
|
|
* - 'persistent' - try to find a persistent link?
|
|
|
|
* - 'charset' - character encoding to set
|
|
|
|
* - 'schema' - the schema search path
|
|
|
|
* - 'lazy' - if TRUE, connection will be established only when required
|
2009-01-17 19:27:40 +00:00
|
|
|
* - 'resource' - connection resource (optional)
|
2008-07-17 03:51:29 +00:00
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
2010-02-24 07:33:16 +01:00
|
|
|
* @package dibi\drivers
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-09-05 05:35:15 +00:00
|
|
|
class DibiPostgreDriver extends DibiObject implements IDibiDriver
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var resource Connection resource */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $connection;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var resource Resultset resource */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $resultSet;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var bool Escape method */
|
2008-07-17 03:51:29 +00:00
|
|
|
private $escMethod = FALSE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (!extension_loaded('pgsql')) {
|
|
|
|
throw new DibiDriverException("PHP extension 'pgsql' is not loaded.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connects to a database.
|
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public function connect(array &$config)
|
|
|
|
{
|
2009-01-17 19:27:40 +00:00
|
|
|
if (isset($config['resource'])) {
|
|
|
|
$this->connection = $config['resource'];
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
} else {
|
2009-01-17 19:27:40 +00:00
|
|
|
if (isset($config['string'])) {
|
|
|
|
$string = $config['string'];
|
|
|
|
} else {
|
|
|
|
$string = '';
|
2009-08-21 13:48:16 +02:00
|
|
|
DibiConnection::alias($config, 'user', 'username');
|
2009-01-17 19:27:40 +00:00
|
|
|
foreach (array('host','hostaddr','port','dbname','user','password','connect_timeout','options','sslmode','service') as $key) {
|
|
|
|
if (isset($config[$key])) $string .= $key . '=' . $config[$key] . ' ';
|
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2009-01-17 19:27:40 +00:00
|
|
|
DibiDriverException::tryError();
|
|
|
|
if (empty($config['persistent'])) {
|
|
|
|
$this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
|
|
|
|
} else {
|
|
|
|
$this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);
|
|
|
|
}
|
|
|
|
if (DibiDriverException::catchError($msg)) {
|
|
|
|
throw new DibiDriverException($msg, 0);
|
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_resource($this->connection)) {
|
|
|
|
throw new DibiDriverException('Connecting error.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config['charset'])) {
|
|
|
|
DibiDriverException::tryError();
|
|
|
|
pg_set_client_encoding($this->connection, $config['charset']);
|
|
|
|
if (DibiDriverException::catchError($msg)) {
|
|
|
|
throw new DibiDriverException($msg, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config['schema'])) {
|
2009-02-02 17:50:02 +00:00
|
|
|
$this->query('SET search_path TO "' . $config['schema'] . '"');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->escMethod = version_compare(PHP_VERSION , '5.2.0', '>=');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects from a database.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function disconnect()
|
|
|
|
{
|
|
|
|
pg_close($this->connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the SQL query.
|
|
|
|
* @param string SQL statement.
|
|
|
|
* @param bool update affected rows?
|
|
|
|
* @return IDibiDriver|NULL
|
|
|
|
* @throws DibiDriverException
|
|
|
|
*/
|
|
|
|
public function query($sql)
|
|
|
|
{
|
|
|
|
$this->resultSet = @pg_query($this->connection, $sql); // intentionally @
|
|
|
|
|
|
|
|
if ($this->resultSet === FALSE) {
|
|
|
|
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_resource($this->resultSet) && pg_num_fields($this->resultSet) ? clone $this : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
|
|
|
|
* @return int|FALSE number of rows or FALSE on error
|
|
|
|
*/
|
2009-02-26 20:02:14 +00:00
|
|
|
public function getAffectedRows()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
return pg_affected_rows($this->resultSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
|
|
|
* @return int|FALSE int on success or FALSE on failure
|
|
|
|
*/
|
2009-02-26 20:02:14 +00:00
|
|
|
public function getInsertId($sequence)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
if ($sequence === NULL) {
|
|
|
|
// PostgreSQL 8.1 is needed
|
|
|
|
$has = $this->query("SELECT LASTVAL()");
|
|
|
|
} else {
|
|
|
|
$has = $this->query("SELECT CURRVAL('$sequence')");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$has) return FALSE;
|
|
|
|
|
|
|
|
$row = $this->fetch(FALSE);
|
|
|
|
$this->free();
|
|
|
|
return is_array($row) ? $row[0] : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins a transaction (if supported).
|
2009-06-30 14:08:49 +00:00
|
|
|
* @param string optional savepoint name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiDriverException
|
|
|
|
*/
|
2008-11-17 16:17:16 +00:00
|
|
|
public function begin($savepoint = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-11-17 16:17:16 +00:00
|
|
|
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits statements in a transaction.
|
2009-06-30 14:08:49 +00:00
|
|
|
* @param string optional savepoint name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiDriverException
|
|
|
|
*/
|
2008-11-17 16:17:16 +00:00
|
|
|
public function commit($savepoint = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-11-17 16:17:16 +00:00
|
|
|
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback changes in a transaction.
|
2009-06-30 14:08:49 +00:00
|
|
|
* @param string optional savepoint name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiDriverException
|
|
|
|
*/
|
2008-11-17 16:17:16 +00:00
|
|
|
public function rollback($savepoint = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-11-17 16:17:16 +00:00
|
|
|
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-01-11 16:56:54 +01:00
|
|
|
/**
|
|
|
|
* Is in transaction?
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function inTransaction()
|
|
|
|
{
|
|
|
|
throw new NotSupportedException('PostgreSQL driver does not support transaction testing.');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-02 17:13:43 +00:00
|
|
|
/**
|
|
|
|
* Returns the connection resource.
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getResource()
|
|
|
|
{
|
|
|
|
return $this->connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* SQL ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
2009-03-19 12:18:16 +00:00
|
|
|
* Encodes data for use in a SQL statement.
|
2009-08-20 23:42:50 +02:00
|
|
|
* @param mixed value
|
2009-03-16 06:48:27 +00:00
|
|
|
* @param string type (dibi::TEXT, dibi::BOOL, ...)
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return string encoded value
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function escape($value, $type)
|
|
|
|
{
|
|
|
|
switch ($type) {
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::TEXT:
|
2008-07-17 03:51:29 +00:00
|
|
|
if ($this->escMethod) {
|
|
|
|
return "'" . pg_escape_string($this->connection, $value) . "'";
|
|
|
|
} else {
|
|
|
|
return "'" . pg_escape_string($value) . "'";
|
|
|
|
}
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::BINARY:
|
2008-07-17 03:51:29 +00:00
|
|
|
if ($this->escMethod) {
|
|
|
|
return "'" . pg_escape_bytea($this->connection, $value) . "'";
|
|
|
|
} else {
|
|
|
|
return "'" . pg_escape_bytea($value) . "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
case dibi::IDENTIFIER:
|
2008-10-01 16:04:16 +00:00
|
|
|
// @see http://www.postgresql.org/docs/8.2/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
|
2008-07-17 03:51:29 +00:00
|
|
|
$a = strrpos($value, '.');
|
|
|
|
if ($a === FALSE) {
|
|
|
|
return '"' . str_replace('"', '""', $value) . '"';
|
|
|
|
} else {
|
|
|
|
// table.col delimite as table."col"
|
|
|
|
return substr($value, 0, $a) . '."' . str_replace('"', '""', substr($value, $a + 1)) . '"';
|
|
|
|
}
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::BOOL:
|
2008-07-17 03:51:29 +00:00
|
|
|
return $value ? 'TRUE' : 'FALSE';
|
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::DATE:
|
2009-08-20 23:42:50 +02:00
|
|
|
return $value instanceof DateTime ? $value->format("'Y-m-d'") : date("'Y-m-d'", $value);
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
case dibi::DATETIME:
|
2009-08-20 23:42:50 +02:00
|
|
|
return $value instanceof DateTime ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException('Unsupported type.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes data from result set.
|
|
|
|
* @param string value
|
2009-03-16 06:48:27 +00:00
|
|
|
* @param string type (dibi::BINARY)
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return string decoded value
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function unescape($value, $type)
|
|
|
|
{
|
2009-07-08 12:10:32 +00:00
|
|
|
if ($type === dibi::BINARY) {
|
2008-07-17 03:51:29 +00:00
|
|
|
return pg_unescape_bytea($value);
|
|
|
|
}
|
2009-07-08 12:10:32 +00:00
|
|
|
throw new InvalidArgumentException('Unsupported type.');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injects LIMIT/OFFSET to the SQL query.
|
|
|
|
* @param string &$sql The SQL query that will be modified.
|
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function applyLimit(&$sql, $limit, $offset)
|
|
|
|
{
|
|
|
|
if ($limit >= 0)
|
|
|
|
$sql .= ' LIMIT ' . (int) $limit;
|
|
|
|
|
|
|
|
if ($offset > 0)
|
|
|
|
$sql .= ' OFFSET ' . (int) $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-02 17:13:43 +00:00
|
|
|
/********************* result set ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* Returns the number of rows in a result set.
|
|
|
|
* @return int
|
|
|
|
*/
|
2009-02-26 20:02:14 +00:00
|
|
|
public function getRowCount()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
return pg_num_rows($this->resultSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the row at current position and moves the internal cursor to the next position.
|
|
|
|
* @param bool TRUE for associative array, FALSE for numeric
|
|
|
|
* @return array array on success, nonarray if no next record
|
2008-10-28 15:24:47 +00:00
|
|
|
* @internal
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-10-10 17:39:33 +00:00
|
|
|
public function fetch($assoc)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-10-10 17:39:33 +00:00
|
|
|
return pg_fetch_array($this->resultSet, NULL, $assoc ? PGSQL_ASSOC : PGSQL_NUM);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Moves cursor position without fetching row.
|
|
|
|
* @param int the 0-based cursor pos to seek to
|
|
|
|
* @return boolean TRUE on success, FALSE if unable to seek to specified record
|
|
|
|
*/
|
|
|
|
public function seek($row)
|
|
|
|
{
|
|
|
|
return pg_result_seek($this->resultSet, $row);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Frees the resources allocated for this result set.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function free()
|
|
|
|
{
|
|
|
|
pg_free_result($this->resultSet);
|
|
|
|
$this->resultSet = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns metadata for all columns in a result set.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumnsMeta()
|
|
|
|
{
|
|
|
|
$hasTable = version_compare(PHP_VERSION , '5.2.0', '>=');
|
|
|
|
$count = pg_num_fields($this->resultSet);
|
2008-10-28 01:03:50 +00:00
|
|
|
$res = array();
|
2008-07-17 03:51:29 +00:00
|
|
|
for ($i = 0; $i < $count; $i++) {
|
2008-10-28 01:03:50 +00:00
|
|
|
$row = array(
|
2008-07-17 03:51:29 +00:00
|
|
|
'name' => pg_field_name($this->resultSet, $i),
|
|
|
|
'table' => $hasTable ? pg_field_table($this->resultSet, $i) : NULL,
|
2008-10-02 17:13:43 +00:00
|
|
|
'nativetype'=> pg_field_type($this->resultSet, $i),
|
2008-07-17 03:51:29 +00:00
|
|
|
);
|
2008-10-28 01:03:50 +00:00
|
|
|
$row['fullname'] = $row['table'] ? $row['table'] . '.' . $row['name'] : $row['name'];
|
|
|
|
$res[] = $row;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
2008-10-28 01:03:50 +00:00
|
|
|
return $res;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-10-02 17:13:43 +00:00
|
|
|
* Returns the result set resource.
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2008-10-02 17:13:43 +00:00
|
|
|
public function getResultResource()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-10-02 17:13:43 +00:00
|
|
|
return $this->resultSet;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-02 17:13:43 +00:00
|
|
|
/********************* reflection ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
2008-10-02 17:13:43 +00:00
|
|
|
* Returns list of tables.
|
|
|
|
* @return array
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-10-02 17:13:43 +00:00
|
|
|
public function getTables()
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-10-28 19:31:32 +00:00
|
|
|
$version = pg_version($this->connection);
|
|
|
|
if ($version['server'] < 8) {
|
2009-02-26 20:02:14 +00:00
|
|
|
throw new DibiDriverException('Reflection requires PostgreSQL 8.');
|
2008-10-28 19:31:32 +00:00
|
|
|
}
|
|
|
|
|
2008-10-28 01:03:50 +00:00
|
|
|
$this->query("
|
|
|
|
SELECT table_name as name, CAST(table_type = 'VIEW' AS INTEGER) as view
|
|
|
|
FROM information_schema.tables
|
|
|
|
WHERE table_schema = current_schema()
|
|
|
|
");
|
|
|
|
$res = pg_fetch_all($this->resultSet);
|
|
|
|
$this->free();
|
2010-01-23 08:00:52 +01:00
|
|
|
return $res ? $res : array();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-10-02 17:13:43 +00:00
|
|
|
* Returns metadata for all columns in a table.
|
|
|
|
* @param string
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getColumns($table)
|
|
|
|
{
|
2009-03-16 06:48:27 +00:00
|
|
|
$_table = $this->escape($table, dibi::TEXT);
|
2008-10-28 12:27:21 +00:00
|
|
|
$this->query("
|
|
|
|
SELECT indkey
|
2008-10-28 19:31:32 +00:00
|
|
|
FROM pg_class
|
|
|
|
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid AND pg_index.indisprimary
|
|
|
|
WHERE pg_class.relname = $_table
|
2008-10-28 12:27:21 +00:00
|
|
|
");
|
|
|
|
$primary = (int) pg_fetch_object($this->resultSet)->indkey;
|
|
|
|
|
|
|
|
$this->query("
|
|
|
|
SELECT *
|
|
|
|
FROM information_schema.columns
|
2008-10-28 14:37:40 +00:00
|
|
|
WHERE table_name = $_table AND table_schema = current_schema()
|
2008-10-28 12:27:21 +00:00
|
|
|
ORDER BY ordinal_position
|
|
|
|
");
|
|
|
|
$res = array();
|
|
|
|
while ($row = $this->fetch(TRUE)) {
|
|
|
|
$size = (int) max($row['character_maximum_length'], $row['numeric_precision']);
|
|
|
|
$res[] = array(
|
|
|
|
'name' => $row['column_name'],
|
|
|
|
'table' => $table,
|
|
|
|
'nativetype' => strtoupper($row['udt_name']),
|
|
|
|
'size' => $size ? $size : NULL,
|
|
|
|
'nullable' => $row['is_nullable'] === 'YES',
|
|
|
|
'default' => $row['column_default'],
|
|
|
|
'autoincrement' => (int) $row['ordinal_position'] === $primary && substr($row['column_default'], 0, 7) === 'nextval',
|
2008-10-28 14:37:40 +00:00
|
|
|
'vendor' => $row,
|
|
|
|
);
|
2008-10-28 12:27:21 +00:00
|
|
|
}
|
|
|
|
$this->free();
|
|
|
|
return $res;
|
2008-10-02 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns metadata for all indexes in a table.
|
|
|
|
* @param string
|
|
|
|
* @return array
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2008-10-02 17:13:43 +00:00
|
|
|
public function getIndexes($table)
|
|
|
|
{
|
2009-03-16 06:48:27 +00:00
|
|
|
$_table = $this->escape($table, dibi::TEXT);
|
2008-10-28 19:31:32 +00:00
|
|
|
$this->query("
|
|
|
|
SELECT ordinal_position, column_name
|
|
|
|
FROM information_schema.columns
|
|
|
|
WHERE table_name = $_table AND table_schema = current_schema()
|
|
|
|
ORDER BY ordinal_position
|
|
|
|
");
|
|
|
|
|
|
|
|
$columns = array();
|
|
|
|
while ($row = $this->fetch(TRUE)) {
|
|
|
|
$columns[$row['ordinal_position']] = $row['column_name'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->query("
|
|
|
|
SELECT pg_class2.relname, indisunique, indisprimary, indkey
|
|
|
|
FROM pg_class
|
|
|
|
LEFT JOIN pg_index on pg_class.oid = pg_index.indrelid
|
|
|
|
INNER JOIN pg_class as pg_class2 on pg_class2.oid = pg_index.indexrelid
|
|
|
|
WHERE pg_class.relname = $_table
|
|
|
|
");
|
|
|
|
|
|
|
|
$res = array();
|
|
|
|
while ($row = $this->fetch(TRUE)) {
|
|
|
|
$res[$row['relname']]['name'] = $row['relname'];
|
|
|
|
$res[$row['relname']]['unique'] = $row['indisunique'] === 't';
|
|
|
|
$res[$row['relname']]['primary'] = $row['indisprimary'] === 't';
|
|
|
|
foreach (explode(' ', $row['indkey']) as $index) {
|
|
|
|
$res[$row['relname']]['columns'][] = $columns[$index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->free();
|
|
|
|
return array_values($res);
|
2008-10-02 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns metadata for all foreign keys in a table.
|
|
|
|
* @param string
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getForeignKeys($table)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException;
|
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
}
|