2006-09-13 11:49:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2007-10-30 00:58:15 +00:00
|
|
|
* dibi - tiny'n'smart database abstraction layer
|
|
|
|
* ----------------------------------------------
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
|
|
|
*
|
|
|
|
* This source file is subject to the "dibi license" that is bundled
|
|
|
|
* with this package in the file license.txt.
|
|
|
|
*
|
|
|
|
* For more information please see http://php7.org/dibi/
|
2006-09-13 11:49:32 +00:00
|
|
|
*
|
2007-06-24 23:49:57 +00:00
|
|
|
* @author David Grudl
|
2007-10-30 00:58:15 +00:00
|
|
|
* @copyright Copyright (c) 2005, 2007 David Grudl
|
|
|
|
* @license http://php7.org/dibi/license (dibi license)
|
2007-06-24 23:49:57 +00:00
|
|
|
* @category Database
|
|
|
|
* @package Dibi
|
2007-10-30 00:58:15 +00:00
|
|
|
* @link http://php7.org/dibi/
|
2006-09-13 11:49:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dibi driver for PostgreSql database
|
|
|
|
*
|
2007-10-30 00:58:15 +00:00
|
|
|
* @version $Revision$ $Date$
|
2006-09-13 11:49:32 +00:00
|
|
|
*/
|
2007-03-27 23:12:36 +00:00
|
|
|
class DibiPostgreDriver extends DibiDriver
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
public $formats = array(
|
2007-10-28 18:04:23 +00:00
|
|
|
'TRUE' => "TRUE",
|
|
|
|
'FALSE' => "FALSE",
|
2007-09-29 07:53:25 +00:00
|
|
|
'date' => "'Y-m-d'",
|
|
|
|
'datetime' => "'Y-m-d H:i:s'",
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Affected rows
|
|
|
|
* @var mixed
|
|
|
|
*/
|
|
|
|
private $affectedRows = FALSE;
|
2006-09-13 11:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-04-25 06:18:06 +00:00
|
|
|
/**
|
|
|
|
* @param array connect configuration
|
2007-06-24 23:49:57 +00:00
|
|
|
* @throws DibiException
|
2007-04-25 06:18:06 +00:00
|
|
|
*/
|
|
|
|
public function __construct($config)
|
2006-09-13 11:49:32 +00:00
|
|
|
{
|
2007-10-26 17:44:24 +00:00
|
|
|
self::prepare($config, 'database', 'string');
|
|
|
|
self::prepare($config, 'type');
|
2007-04-25 06:18:06 +00:00
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function connect()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
if (!extension_loaded('pgsql')) {
|
|
|
|
throw new DibiException("PHP extension 'pgsql' is not loaded");
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = $this->getConfig();
|
2007-04-25 06:18:06 +00:00
|
|
|
|
2007-08-23 17:12:58 +00:00
|
|
|
if (isset($config['persistent'])) {
|
2007-10-26 17:44:24 +00:00
|
|
|
$connection = @pg_connect($config['database'], $config['type']);
|
2007-08-23 17:12:58 +00:00
|
|
|
} else {
|
2007-10-26 17:44:24 +00:00
|
|
|
$connection = @pg_pconnect($config['database'], $config['type']);
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-08-23 17:12:58 +00:00
|
|
|
if (!is_resource($connection)) {
|
2007-09-29 07:53:25 +00:00
|
|
|
throw new DibiDatabaseException(pg_last_error());
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-10-26 17:44:24 +00:00
|
|
|
if (isset($config['charset'])) {
|
2007-06-25 17:02:12 +00:00
|
|
|
@pg_set_client_encoding($connection, $config['charset']);
|
2006-09-13 11:49:32 +00:00
|
|
|
// don't handle this error...
|
|
|
|
}
|
2007-09-29 07:53:25 +00:00
|
|
|
|
|
|
|
dibi::notify('connected', $this);
|
2007-06-25 17:02:12 +00:00
|
|
|
return $connection;
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-27 23:12:36 +00:00
|
|
|
public function nativeQuery($sql)
|
2006-09-13 11:49:32 +00:00
|
|
|
{
|
2007-08-28 23:17:34 +00:00
|
|
|
$this->affectedRows = FALSE;
|
2007-09-29 07:53:25 +00:00
|
|
|
$res = parent::nativeQuery($sql);
|
|
|
|
if ($res instanceof DibiResult) {
|
|
|
|
$this->affectedRows = pg_affected_rows($res->getResource());
|
|
|
|
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
protected function doQuery($sql)
|
|
|
|
{
|
|
|
|
$connection = $this->getConnection();
|
|
|
|
$res = @pg_query($connection, $sql);
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-08-28 21:41:15 +00:00
|
|
|
if ($res === FALSE) {
|
2007-09-29 07:53:25 +00:00
|
|
|
throw new DibiDatabaseException(pg_last_error($connection), 0, $sql);
|
2007-08-28 21:41:15 +00:00
|
|
|
}
|
2007-10-01 05:34:50 +00:00
|
|
|
|
|
|
|
return is_resource($res) ? new DibiPostgreResult($res) : TRUE;
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
public function affectedRows()
|
|
|
|
{
|
|
|
|
return $this->affectedRows;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2007-08-28 23:17:34 +00:00
|
|
|
public function insertId($sequence = NULL)
|
2006-09-13 11:49:32 +00:00
|
|
|
{
|
2007-10-26 17:44:24 +00:00
|
|
|
if ($sequence === NULL) {
|
2007-08-28 23:17:34 +00:00
|
|
|
// PostgreSQL 8.1 is needed
|
2007-09-29 07:53:25 +00:00
|
|
|
$res = $this->doQuery("SELECT LASTVAL() AS seq");
|
2007-08-28 23:17:34 +00:00
|
|
|
} else {
|
2007-09-29 07:53:25 +00:00
|
|
|
$res = $this->doQuery("SELECT CURRVAL('$sequence') AS seq");
|
2007-08-28 23:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_resource($res)) {
|
|
|
|
$row = pg_fetch_assoc($res);
|
|
|
|
pg_free_result($res);
|
|
|
|
return $row['seq'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
public function begin()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
$this->doQuery('BEGIN');
|
|
|
|
dibi::notify('begin', $this);
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
public function commit()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
$this->doQuery('COMMIT');
|
|
|
|
dibi::notify('commit', $this);
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
public function rollback()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
$this->doQuery('ROLLBACK');
|
|
|
|
dibi::notify('rollback', $this);
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2007-01-29 05:08:52 +00:00
|
|
|
public function errorInfo()
|
|
|
|
{
|
|
|
|
return array(
|
2007-06-25 17:02:12 +00:00
|
|
|
'message' => pg_last_error($this->getConnection()),
|
2007-01-29 05:08:52 +00:00
|
|
|
'code' => NULL,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2007-08-09 03:31:34 +00:00
|
|
|
public function escape($value, $appendQuotes = TRUE)
|
2006-09-13 11:49:32 +00:00
|
|
|
{
|
|
|
|
return $appendQuotes
|
|
|
|
? "'" . pg_escape_string($value) . "'"
|
|
|
|
: pg_escape_string($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2007-04-16 03:01:55 +00:00
|
|
|
public function delimite($value)
|
2006-09-13 11:49:32 +00:00
|
|
|
{
|
2007-08-28 23:17:34 +00:00
|
|
|
$value = str_replace('"', '""', $value);
|
|
|
|
return '"' . str_replace('.', '"."', $value) . '"';
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getMetaData()
|
|
|
|
{
|
2007-11-08 03:32:37 +00:00
|
|
|
throw new BadMethodCallException(__METHOD__ . ' is not implemented');
|
2006-09-13 11:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-23 07:55:11 +00:00
|
|
|
/**
|
|
|
|
* @see DibiDriver::applyLimit()
|
|
|
|
*/
|
|
|
|
public function applyLimit(&$sql, $limit, $offset = 0)
|
|
|
|
{
|
|
|
|
if ($limit >= 0)
|
|
|
|
$sql .= ' LIMIT ' . (int) $limit;
|
|
|
|
|
|
|
|
if ($offset > 0)
|
|
|
|
$sql .= ' OFFSET ' . (int) $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
} // class DibiPostgreDriver
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DibiPostgreResult extends DibiResult
|
|
|
|
{
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
public function rowCount()
|
|
|
|
{
|
|
|
|
return pg_num_rows($this->resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
protected function doFetch()
|
|
|
|
{
|
|
|
|
return pg_fetch_array($this->resource, NULL, PGSQL_ASSOC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
public function seek($row)
|
|
|
|
{
|
|
|
|
return pg_result_seek($this->resource, $row);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
protected function free()
|
|
|
|
{
|
|
|
|
pg_free_result($this->resource);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-13 11:49:32 +00:00
|
|
|
/** this is experimental */
|
2007-03-27 23:12:36 +00:00
|
|
|
protected function buildMeta()
|
2006-09-13 11:49:32 +00:00
|
|
|
{
|
|
|
|
static $types = array(
|
|
|
|
'bool' => dibi::FIELD_BOOL,
|
|
|
|
'int2' => dibi::FIELD_INTEGER,
|
|
|
|
'int4' => dibi::FIELD_INTEGER,
|
|
|
|
'int8' => dibi::FIELD_INTEGER,
|
|
|
|
'numeric' => dibi::FIELD_FLOAT,
|
|
|
|
'float4' => dibi::FIELD_FLOAT,
|
|
|
|
'float8' => dibi::FIELD_FLOAT,
|
|
|
|
'timestamp' => dibi::FIELD_DATETIME,
|
|
|
|
'date' => dibi::FIELD_DATE,
|
|
|
|
'time' => dibi::FIELD_DATETIME,
|
|
|
|
'varchar' => dibi::FIELD_TEXT,
|
|
|
|
'bpchar' => dibi::FIELD_TEXT,
|
|
|
|
'inet' => dibi::FIELD_TEXT,
|
|
|
|
'money' => dibi::FIELD_FLOAT,
|
|
|
|
);
|
|
|
|
|
|
|
|
$count = pg_num_fields($this->resource);
|
|
|
|
$this->meta = $this->convert = array();
|
|
|
|
for ($index = 0; $index < $count; $index++) {
|
|
|
|
|
|
|
|
$info['native'] = $native = pg_field_type($this->resource, $index);
|
|
|
|
$info['length'] = pg_field_size($this->resource, $index);
|
|
|
|
$info['table'] = pg_field_table($this->resource, $index);
|
|
|
|
$info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN;
|
|
|
|
|
|
|
|
$name = pg_field_name($this->resource, $index);
|
|
|
|
$this->meta[$name] = $info;
|
|
|
|
$this->convert[$name] = $info['type'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // class DibiPostgreResult
|