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

278 lines
6.0 KiB
PHP
Raw Normal View History

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
if (!class_exists('dibi', FALSE)) die();
2006-09-13 11:49:32 +00:00
/**
* The dibi driver for PostgreSql database
*
*/
class DibiPostgreDriver extends DibiDriver
{
2006-09-13 11:49:32 +00:00
private
$insertId = FALSE,
2006-09-13 11:49:32 +00:00
$affectedRows = FALSE;
public
$formats = array(
'TRUE' => "1",
'FALSE' => "0",
'date' => "'Y-m-d'",
'datetime' => "'Y-m-d H:i:s'",
);
/**
* @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
{
if (!extension_loaded('pgsql')) {
2006-11-13 06:32:16 +00:00
throw new DibiException("PHP extension 'pgsql' is not loaded");
}
2006-09-13 11:49:32 +00:00
if (empty($config['string'])) {
throw new DibiException("Connection string must be specified (driver postgre)");
}
2006-09-13 11:49:32 +00:00
if (empty($config['type'])) $config['type'] = NULL;
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
if (isset($config['persistent'])) {
2007-06-25 17:02:12 +00:00
$connection = @pg_connect($config['string'], $config['type']);
} else {
2007-06-25 17:02:12 +00:00
$connection = @pg_pconnect($config['string'], $config['type']);
}
2006-09-13 11:49:32 +00:00
if (!is_resource($connection)) {
throw new DibiException("Connecting error (driver postgre)", array(
2006-09-13 11:49:32 +00:00
'message' => pg_last_error(),
));
}
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
}
public function nativeQuery($sql)
2006-09-13 11:49:32 +00:00
{
$this->insertId = $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-08-28 21:41:15 +00:00
} elseif (is_resource($res)) {
$this->insertId = pg_last_oid($res);
if ($this->insertId < 0) $this->insertId = FALSE;
2007-08-28 21:41:15 +00:00
$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
2006-09-13 11:49:32 +00:00
public function insertId()
{
return $this->insertId;
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
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
{
return $value;
2006-09-13 11:49:32 +00:00
}
public function getMetaData()
{
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
{
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 */
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