1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-29 16:59:49 +02:00

* DibiDriver::query -> DibiDriver::nativeQuery

* Dibi::query moved to DibiDriver::query
* methods getFields(), detectTypes(), getMetaData() moved to base class DibiDriver
* added PDO driver (not tested)
This commit is contained in:
David Grudl
2007-03-27 23:12:36 +00:00
parent f64a5d5251
commit 48ea525b04
12 changed files with 578 additions and 373 deletions

View File

@@ -24,7 +24,8 @@ if (!defined('DIBI')) die();
* The dibi driver for MySQL database
*
*/
class DibiMySqlDriver extends DibiDriver {
class DibiMySqlDriver extends DibiDriver
{
private
$conn,
$insertId = FALSE,
@@ -105,7 +106,7 @@ class DibiMySqlDriver extends DibiDriver {
public function query($sql)
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
$res = @mysql_query($sql, $this->conn);
@@ -216,9 +217,7 @@ class DibiMySqlDriver extends DibiDriver {
class DibiMySqlResult extends DibiResult
{
private
$resource,
$meta;
private $resource;
public function __construct($resource)
@@ -251,37 +250,8 @@ class DibiMySqlResult extends DibiResult
}
public function getFields()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return array_keys($this->meta);
}
protected function detectTypes()
{
if ($this->meta === NULL)
$this->createMeta();
}
/** this is experimental */
public function getMetaData($field)
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
protected function buildMeta()
{
static $types = array(
'ENUM' => dibi::FIELD_TEXT, // eventually dibi::FIELD_INTEGER

View File

@@ -24,7 +24,8 @@ if (!defined('DIBI')) die();
* The dibi driver for MySQLi database
*
*/
class DibiMySqliDriver extends DibiDriver {
class DibiMySqliDriver extends DibiDriver
{
private
$conn,
$insertId = FALSE,
@@ -73,7 +74,7 @@ class DibiMySqliDriver extends DibiDriver {
public function query($sql)
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
$res = @mysqli_query($this->conn, $sql);
@@ -184,9 +185,7 @@ class DibiMySqliDriver extends DibiDriver {
class DibiMySqliResult extends DibiResult
{
private
$resource,
$meta;
private $resource;
public function __construct($resource)
@@ -219,36 +218,8 @@ class DibiMySqliResult extends DibiResult
}
public function getFields()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return array_keys($this->meta);
}
protected function detectTypes()
{
if ($this->meta === NULL)
$this->createMeta();
}
/** this is experimental */
public function getMetaData($field)
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
protected function buildMeta()
{
static $types = array(
MYSQLI_TYPE_FLOAT => dibi::FIELD_FLOAT,

View File

@@ -24,7 +24,8 @@ if (!defined('DIBI')) die();
* The dibi driver interacting with databases via ODBC connections
*
*/
class DibiOdbcDriver extends DibiDriver {
class DibiOdbcDriver extends DibiDriver
{
private
$conn,
$affectedRows = FALSE;
@@ -76,7 +77,7 @@ class DibiOdbcDriver extends DibiDriver {
public function query($sql)
public function nativeQuery($sql)
{
$this->affectedRows = FALSE;
@@ -182,10 +183,8 @@ class DibiOdbcDriver extends DibiDriver {
class DibiOdbcResult extends DibiResult
{
private
$resource,
$meta,
$row = 0;
private $resource;
private $row = 0;
public function __construct($resource)
@@ -219,36 +218,8 @@ class DibiOdbcResult extends DibiResult
}
public function getFields()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return array_keys($this->meta);
}
protected function detectTypes()
{
if ($this->meta === NULL)
$this->createMeta();
}
/** this is experimental */
public function getMetaData($field)
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
protected function buildMeta()
{
// cache
if ($this->meta !== NULL)

219
dibi/drivers/pdo.php Normal file
View File

@@ -0,0 +1,219 @@
<?php
/**
* dibi - Database Abstraction Layer according to dgx
* --------------------------------------------------
*
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2007 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database
* @version $Revision$ $Date$
*/
// security - include dibi.php, not this file
if (!defined('DIBI')) die();
/**
* The dibi driver for PDO
*
*/
class DibiPdoDriver extends DibiDriver
{
/** @var PDO */
private $conn;
private $affectedRows = FALSE,
private $errorMsg;
public
$formats = array(
'TRUE' => "1",
'FALSE' => "0",
'date' => "'Y-m-d'",
'datetime' => "'Y-m-d H:i:s'",
);
public static function connect($config)
{
if (!extension_loaded('pdo'))
throw new DibiException("PHP extension 'pdo' is not loaded");
if (empty($config['dsn']))
throw new DibiException("DSN must be specified");
if (empty($config['username'])) $config['username'] = NULL;
if (empty($config['password'])) $config['password'] = NULL;
$conn = new PDO($config['dsn'], $config['username'], $config['password']);
$obj = new self($config);
$obj->conn = $conn;
return $obj;
}
public function nativeQuery($sql)
{
$this->affectedRows = FALSE;
$this->errorMsg = '';
$res = $this->conn->query($sql);
if ($res === FALSE) return FALSE;
//$this->affectedRows = 0;
//if ($this->affectedRows < 0) $this->affectedRows = FALSE;
if (is_resource($res))
return new DibiSqliteResult($res);
return TRUE;
}
public function affectedRows()
{
return $this->affectedRows;
}
public function insertId()
{
return $this->conn->lastInsertId();
}
public function begin()
{
return $this->conn->beginTransaction();
}
public function commit()
{
return $this->conn->commit();
}
public function rollback()
{
return $this->conn->rollBack();
}
public function errorInfo()
{
$error = $this->conn->errorInfo();
return array(
'message' => $error[2],
'code' => $error[1],
'SQLSTATE '=> $error[0],
);
}
public function escape($value, $appendQuotes = FALSE)
{
return $appendQuotes
? $this->conn->quote($value)
: FALSE; // error
}
public function quoteName($value)
{
return FALSE; // error
}
public function getMetaData()
{
trigger_error('Meta is not implemented yet.', E_USER_WARNING);
}
/**
* @see DibiDriver::applyLimit()
*/
public function applyLimit(&$sql, $limit, $offset = 0)
{
if ($limit < 0 && $offset < 1) return;
$sql .= ' LIMIT ' . $limit . ($offset > 0 ? ' OFFSET ' . (int) $offset : '');
}
} // class DibiPdoDriver
class DibiPdoResult extends DibiResult
{
/** @var PDOStatement */
private $resource;
private $row = 0;
public function __construct($resource)
{
$this->resource = $resource;
}
public function rowCount()
{
return $this->resource->rowCount();
}
protected function doFetch()
{
return $this->resource->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT, $this->row++);
}
public function seek($row)
{
$this->row = $row;
}
protected function free()
{
}
/** this is experimental */
protected function buildMeta()
{
$count = $this->resource->columnCount();
$this->meta = $this->convert = array();
for ($index = 0; $index < $count; $index++) {
$meta = $this->resource->getColumnMeta($i);
$meta['type'] = dibi::FIELD_UNKNOWN;
$name = $meta['name'];
$this->meta[$name] = $meta;
$this->convert[$name] = $meta['type'];
}
}
} // class DibiPdoResult

View File

@@ -24,7 +24,8 @@ if (!defined('DIBI')) die();
* The dibi driver for PostgreSql database
*
*/
class DibiPostgreDriver extends DibiDriver {
class DibiPostgreDriver extends DibiDriver
{
private
$conn,
$affectedRows = FALSE;
@@ -71,7 +72,7 @@ class DibiPostgreDriver extends DibiDriver {
public function query($sql)
public function nativeQuery($sql)
{
$this->affectedRows = FALSE;
@@ -175,9 +176,7 @@ class DibiPostgreDriver extends DibiDriver {
class DibiPostgreResult extends DibiResult
{
private
$resource,
$meta;
private $resource;
public function __construct($resource)
@@ -210,36 +209,8 @@ class DibiPostgreResult extends DibiResult
}
public function getFields()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return array_keys($this->meta);
}
protected function detectTypes()
{
if ($this->meta === NULL)
$this->createMeta();
}
/** this is experimental */
public function getMetaData($field)
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
protected function buildMeta()
{
static $types = array(
'bool' => dibi::FIELD_BOOL,

View File

@@ -24,7 +24,8 @@ if (!defined('DIBI')) die();
* The dibi driver for SQlite database
*
*/
class DibiSqliteDriver extends DibiDriver {
class DibiSqliteDriver extends DibiDriver
{
private
$conn,
$insertId = FALSE,
@@ -70,7 +71,7 @@ class DibiSqliteDriver extends DibiDriver {
public function query($sql)
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
@@ -174,9 +175,7 @@ class DibiSqliteDriver extends DibiDriver {
class DibiSqliteResult extends DibiResult
{
private
$resource,
$meta;
private $resource;
public function __construct($resource)
@@ -208,36 +207,8 @@ class DibiSqliteResult extends DibiResult
}
public function getFields()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return array_keys($this->meta);
}
protected function detectTypes()
{
if ($this->meta === NULL)
$this->createMeta();
}
/** this is experimental */
public function getMetaData($field)
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
protected function buildMeta()
{
$count = sqlite_num_fields($this->resource);
$this->meta = $this->convert = array();