1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-25 03:12:50 +01:00
php-dibi/dibi/drivers/postgre.php

290 lines
6.6 KiB
PHP
Raw Normal View History

2006-09-13 11:49:32 +00:00
<?php
/**
* 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
* @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
* @link http://php7.org/dibi/
2006-09-13 11:49:32 +00:00
*/
/**
* The dibi driver for PostgreSql database
*
* @version $Revision$ $Date$
2006-09-13 11:49:32 +00:00
*/
class DibiPostgreDriver extends DibiDriver
{
public $formats = array(
2007-10-28 18:04:23 +00:00
'TRUE' => "TRUE",
'FALSE' => "FALSE",
'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
/**
* @param array connect configuration
2007-06-24 23:49:57 +00:00
* @throws DibiException
*/
public function __construct($config)
2006-09-13 11:49:32 +00:00
{
self::prepare($config, 'database', 'string');
self::prepare($config, 'type');
parent::__construct($config);
}
protected function connect()
{
if (!extension_loaded('pgsql')) {
throw new DibiException("PHP extension 'pgsql' is not loaded");
}
$config = $this->getConfig();
if (isset($config['persistent'])) {
$connection = @pg_connect($config['database'], $config['type']);
} else {
$connection = @pg_pconnect($config['database'], $config['type']);
}
2006-09-13 11:49:32 +00:00
if (!is_resource($connection)) {
throw new DibiDatabaseException(pg_last_error());
}
2006-09-13 11:49:32 +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...
}
dibi::notify('connected', $this);
2007-06-25 17:02:12 +00:00
return $connection;
2006-09-13 11:49:32 +00:00
}
public function nativeQuery($sql)
2006-09-13 11:49:32 +00:00
{
$this->affectedRows = FALSE;
$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
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) {
throw new DibiDatabaseException(pg_last_error($connection), 0, $sql);
2007-08-28 21:41:15 +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
public function insertId($sequence = NULL)
2006-09-13 11:49:32 +00:00
{
if ($sequence === NULL) {
// PostgreSQL 8.1 is needed
$res = $this->doQuery("SELECT LASTVAL() AS seq");
} else {
$res = $this->doQuery("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()
{
$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()
{
$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()
{
$this->doQuery('ROLLBACK');
dibi::notify('rollback', $this);
2006-09-13 11:49:32 +00:00
}
2007-08-23 00:57:28 +00:00
public function errorInfo()
{
return array(
2007-06-25 17:02:12 +00:00
'message' => pg_last_error($this->getConnection()),
'code' => NULL,
);
}
2007-08-23 00:57:28 +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
public function delimite($value)
2006-09-13 11:49:32 +00:00
{
$value = str_replace('"', '""', $value);
return '"' . str_replace('.', '"."', $value) . '"';
2006-09-13 11:49:32 +00:00
}
public function getMetaData()
{
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 */
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