2006-09-13 11:49:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2007-04-11 18:30:30 +00:00
|
|
|
* This file is part of the "dibi" project (http://dibi.texy.info/)
|
2006-09-13 11:49:32 +00:00
|
|
|
*
|
2007-06-24 23:49:57 +00:00
|
|
|
* @author David Grudl
|
|
|
|
* @copyright Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
|
|
|
* @license New BSD License
|
|
|
|
* @version $Revision$ $Date$
|
|
|
|
* @category Database
|
|
|
|
* @package Dibi
|
2006-09-13 11:49:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// security - include dibi.php, not this file
|
2007-05-13 18:32:03 +00:00
|
|
|
if (!class_exists('dibi', FALSE)) die();
|
2006-09-13 11:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The dibi driver for PostgreSql database
|
|
|
|
*
|
|
|
|
*/
|
2007-03-27 23:12:36 +00:00
|
|
|
class DibiPostgreDriver extends DibiDriver
|
|
|
|
{
|
2006-09-13 11:49:32 +00:00
|
|
|
private
|
|
|
|
$affectedRows = FALSE;
|
|
|
|
|
|
|
|
public
|
|
|
|
$formats = array(
|
|
|
|
'TRUE' => "1",
|
|
|
|
'FALSE' => "0",
|
|
|
|
'date' => "'Y-m-d'",
|
|
|
|
'datetime' => "'Y-m-d H:i:s'",
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
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-08-23 17:12:58 +00:00
|
|
|
if (!extension_loaded('pgsql')) {
|
2006-11-13 06:32:16 +00:00
|
|
|
throw new DibiException("PHP extension 'pgsql' is not loaded");
|
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 (empty($config['string'])) {
|
2007-05-11 22:25:32 +00:00
|
|
|
throw new DibiException("Connection string must be specified (driver postgre)");
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-09-13 11:49:32 +00:00
|
|
|
|
|
|
|
if (empty($config['type'])) $config['type'] = NULL;
|
|
|
|
|
2007-04-25 06:18:06 +00:00
|
|
|
parent::__construct($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function connect()
|
|
|
|
{
|
|
|
|
$config = $this->config;
|
|
|
|
|
2007-08-23 17:12:58 +00:00
|
|
|
if (isset($config['persistent'])) {
|
2007-06-25 17:02:12 +00:00
|
|
|
$connection = @pg_connect($config['string'], $config['type']);
|
2007-08-23 17:12:58 +00:00
|
|
|
} else {
|
2007-06-25 17:02:12 +00:00
|
|
|
$connection = @pg_pconnect($config['string'], $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-05-11 22:25:32 +00:00
|
|
|
throw new DibiException("Connecting error (driver postgre)", array(
|
2006-09-13 11:49:32 +00:00
|
|
|
'message' => pg_last_error(),
|
|
|
|
));
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-09-13 11:49:32 +00:00
|
|
|
|
|
|
|
if (!empty($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-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;
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-08-28 21:41:15 +00:00
|
|
|
$res = @pg_query($this->getConnection(), $sql);
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-08-28 21:41:15 +00:00
|
|
|
if ($res === FALSE) {
|
|
|
|
return FALSE;
|
2007-01-29 05:08:52 +00:00
|
|
|
|
2007-08-28 21:41:15 +00:00
|
|
|
} elseif (is_resource($res)) {
|
|
|
|
$this->affectedRows = pg_affected_rows($res);
|
|
|
|
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
2006-09-13 11:49:32 +00:00
|
|
|
|
2007-03-26 06:22:53 +00:00
|
|
|
return new DibiPostgreResult($res);
|
|
|
|
|
2007-08-28 21:41:15 +00:00
|
|
|
} else {
|
|
|
|
return 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-08-28 23:17:34 +00:00
|
|
|
if (empty($sequence)) {
|
|
|
|
// PostgreSQL 8.1 is needed
|
|
|
|
$res = pg_query($this->getConnection(), "SELECT LASTVAL() AS seq");
|
|
|
|
} else {
|
|
|
|
$res = pg_query($this->getConnection(), "SELECT CURRVAL('$sequence') AS seq");
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-25 17:02:12 +00:00
|
|
|
return pg_query($this->getConnection(), 'BEGIN');
|
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-06-25 17:02:12 +00:00
|
|
|
return pg_query($this->getConnection(), 'COMMIT');
|
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-06-25 17:02:12 +00:00
|
|
|
return pg_query($this->getConnection(), 'ROLLBACK');
|
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-08-28 22:25:01 +00:00
|
|
|
throw new DibiException(__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-03-27 23:12:36 +00:00
|
|
|
private $resource;
|
2006-09-13 11:49:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
public function __construct($resource)
|
|
|
|
{
|
|
|
|
$this->resource = $resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|