1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 19:30:30 +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

@@ -14,11 +14,11 @@
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @category Database
* @version 0.7e $Revision$ $Date$ * @version 0.7f $Revision$ $Date$
*/ */
define('DIBI', 'Version 0.7e $Revision$'); define('DIBI', 'Version 0.7f $Revision$');
if (version_compare(PHP_VERSION , '5.0.3', '<')) if (version_compare(PHP_VERSION , '5.0.3', '<'))
@@ -219,9 +219,6 @@ class dibi
/** /**
* Generates and executes SQL query * Generates and executes SQL query
* *
@@ -231,77 +228,12 @@ class dibi
*/ */
static public function query($args) static public function query($args)
{ {
$conn = self::getConnection(); $args = func_get_args();
return self::getConnection()->query($args);
// receive arguments
if (!is_array($args))
$args = func_get_args();
// and generate SQL
$trans = new DibiTranslator($conn, self::$substs);
if (!$trans->translate($args)) {
if (self::$logFile) // log to file
self::log(
"ERROR: SQL generate error"
. "\n-- SQL: " . $trans->sql
. ";\n-- " . date('Y-m-d H:i:s ')
);
if (dibi::$throwExceptions)
throw new DibiException('SQL generate error', NULL, $trans->sql);
else {
trigger_error("dibi: SQL generate error: $trans->sql", E_USER_WARNING);
return FALSE;
}
}
self::$sql = $trans->sql;
// execute SQL
$timer = -microtime(true);
$res = $conn->query(self::$sql);
if ($res === FALSE) { // query error
if (self::$logFile) { // log to file
$info = $conn->errorInfo();
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
self::log(
"ERROR: $info[message]"
. "\n-- SQL: " . self::$sql
. ";\n-- " . date('Y-m-d H:i:s ')
);
}
if (dibi::$throwExceptions) {
$info = $conn->errorInfo();
throw new DibiException('Query error', $info, self::$sql);
} else {
$info = $conn->errorInfo();
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
trigger_error("dibi: $info[message]", E_USER_WARNING);
return FALSE;
}
}
if (self::$logFile && self::$logAll) { // log success
$timer += microtime(true);
$msg = $res instanceof DibiResult ? 'object('.get_class($res).') rows: '.$res->rowCount() : 'OK';
self::log(
"OK: " . self::$sql
. ";\n-- result: $msg"
. "\n-- takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
. "\n-- " . date('Y-m-d H:i:s ')
);
}
return $res;
} }
/** /**
* Generates and prints SQL query * Generates and prints SQL query
* *
@@ -315,13 +247,18 @@ class dibi
$args = func_get_args(); $args = func_get_args();
// and generate SQL // and generate SQL
$trans = new DibiTranslator(self::getConnection(), self::$substs); $trans = new DibiTranslator(self::getConnection());
$ok = $trans->translate($args); try {
if (!$ok) echo 'ERROR: '; $sql = $trans->translate($args);
} catch (DibiException $e) {
return FALSE;
}
self::dump($trans->sql); if ($sql === FALSE) return FALSE;
return $ok; self::dump($sql);
return TRUE;
} }
@@ -366,6 +303,7 @@ class dibi
} }
/** /**
* Prints out a syntax highlighted version of the SQL command * Prints out a syntax highlighted version of the SQL command
* *
@@ -449,6 +387,18 @@ class dibi
} }
/**
* Process substitutions in string
* @param string
* @return string
*/
static public function substitute($s)
{
if (strpos($s, ':') === FALSE) return $s;
return strtr($s, self::$substs);
}
/** /**
* Error logging * Error logging
* EXPERIMENTAL * EXPERIMENTAL

View File

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

View File

@@ -24,7 +24,8 @@ if (!defined('DIBI')) die();
* The dibi driver for MySQLi database * The dibi driver for MySQLi database
* *
*/ */
class DibiMySqliDriver extends DibiDriver { class DibiMySqliDriver extends DibiDriver
{
private private
$conn, $conn,
$insertId = FALSE, $insertId = FALSE,
@@ -73,7 +74,7 @@ class DibiMySqliDriver extends DibiDriver {
public function query($sql) public function nativeQuery($sql)
{ {
$this->insertId = $this->affectedRows = FALSE; $this->insertId = $this->affectedRows = FALSE;
$res = @mysqli_query($this->conn, $sql); $res = @mysqli_query($this->conn, $sql);
@@ -184,9 +185,7 @@ class DibiMySqliDriver extends DibiDriver {
class DibiMySqliResult extends DibiResult class DibiMySqliResult extends DibiResult
{ {
private private $resource;
$resource,
$meta;
public function __construct($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 */ /** this is experimental */
public function getMetaData($field) protected function buildMeta()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
{ {
static $types = array( static $types = array(
MYSQLI_TYPE_FLOAT => dibi::FIELD_FLOAT, 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 * The dibi driver interacting with databases via ODBC connections
* *
*/ */
class DibiOdbcDriver extends DibiDriver { class DibiOdbcDriver extends DibiDriver
{
private private
$conn, $conn,
$affectedRows = FALSE; $affectedRows = FALSE;
@@ -76,7 +77,7 @@ class DibiOdbcDriver extends DibiDriver {
public function query($sql) public function nativeQuery($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = FALSE;
@@ -182,10 +183,8 @@ class DibiOdbcDriver extends DibiDriver {
class DibiOdbcResult extends DibiResult class DibiOdbcResult extends DibiResult
{ {
private private $resource;
$resource, private $row = 0;
$meta,
$row = 0;
public function __construct($resource) 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 */ /** this is experimental */
public function getMetaData($field) protected function buildMeta()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
{ {
// cache // cache
if ($this->meta !== NULL) 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 * The dibi driver for PostgreSql database
* *
*/ */
class DibiPostgreDriver extends DibiDriver { class DibiPostgreDriver extends DibiDriver
{
private private
$conn, $conn,
$affectedRows = FALSE; $affectedRows = FALSE;
@@ -71,7 +72,7 @@ class DibiPostgreDriver extends DibiDriver {
public function query($sql) public function nativeQuery($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = FALSE;
@@ -175,9 +176,7 @@ class DibiPostgreDriver extends DibiDriver {
class DibiPostgreResult extends DibiResult class DibiPostgreResult extends DibiResult
{ {
private private $resource;
$resource,
$meta;
public function __construct($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 */ /** this is experimental */
public function getMetaData($field) protected function buildMeta()
{
// cache
if ($this->meta === NULL)
$this->createMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/** this is experimental */
private function createMeta()
{ {
static $types = array( static $types = array(
'bool' => dibi::FIELD_BOOL, 'bool' => dibi::FIELD_BOOL,

View File

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

View File

@@ -31,8 +31,7 @@ abstract class DibiDriver
* Current connection configuration * Current connection configuration
* @var array * @var array
*/ */
protected protected $config;
$config;
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -80,13 +79,76 @@ abstract class DibiDriver
/**
* Generates and executes SQL query
*
* @param array|mixed one or more arguments
* @return int|DibiResult
* @throw DibiException
*/
public function query($args)
{
// receive arguments
if (!is_array($args))
$args = func_get_args();
// and generate SQL
$trans = new DibiTranslator($this);
dibi::$sql = $sql = $trans->translate($args);
if ($sql === FALSE) return FALSE;
// execute SQL
$timer = -microtime(true);
$res = $this->nativeQuery($sql);
if ($res === FALSE) { // query error
if (dibi::$logFile) { // log to file
$info = $this->errorInfo();
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
dibi::log(
"ERROR: $info[message]"
. "\n-- SQL: " . $sql
. ";\n-- " . date('Y-m-d H:i:s ')
);
}
if (dibi::$throwExceptions) {
$info = $this->errorInfo();
throw new DibiException('Query error', $info, $sql);
} else {
$info = $this->errorInfo();
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
trigger_error("dibi: $info[message]", E_USER_WARNING);
return FALSE;
}
}
if (dibi::$logFile && dibi::$logAll) { // log success
$timer += microtime(true);
$msg = $res instanceof DibiResult ? 'object('.get_class($res).') rows: '.$res->rowCount() : 'OK';
dibi::log(
"OK: " . $sql
. ";\n-- result: $msg"
. "\n-- takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
. "\n-- " . date('Y-m-d H:i:s ')
);
}
return $res;
}
/** /**
* Executes the SQL query * Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return object|bool Result set object or TRUE on success, FALSE on failure * @return object|bool Result set object or TRUE on success, FALSE on failure
*/ */
abstract public function query($sql); abstract public function nativeQuery($sql);
/** /**
@@ -97,6 +159,7 @@ abstract class DibiDriver
abstract public function affectedRows(); abstract public function affectedRows();
/** /**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
* @return int|bool int on success or FALSE on failure * @return int|bool int on success or FALSE on failure
@@ -104,24 +167,28 @@ abstract class DibiDriver
abstract public function insertId(); abstract public function insertId();
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
*/ */
abstract public function begin(); abstract public function begin();
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
*/ */
abstract public function commit(); abstract public function commit();
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
*/ */
abstract public function rollback(); abstract public function rollback();
/** /**
* Returns last error * Returns last error
* @return array with items 'message' and 'code' * @return array with items 'message' and 'code'
@@ -129,6 +196,7 @@ abstract class DibiDriver
abstract public function errorInfo(); abstract public function errorInfo();
/** /**
* Escapes the string * Escapes the string
* @param string unescaped string * @param string unescaped string
@@ -138,6 +206,7 @@ abstract class DibiDriver
abstract public function escape($value, $appendQuotes = FALSE); abstract public function escape($value, $appendQuotes = FALSE);
/** /**
* Quotes SQL identifier (table's or column's name, etc.) * Quotes SQL identifier (table's or column's name, etc.)
* @param string identifier * @param string identifier
@@ -155,6 +224,7 @@ abstract class DibiDriver
abstract public function getMetaData(); abstract public function getMetaData();
/** /**
* Experimental - injects LIMIT/OFFSET to the SQL query * Experimental - injects LIMIT/OFFSET to the SQL query
* @param string &$sql The SQL query that will be modified. * @param string &$sql The SQL query that will be modified.
@@ -165,6 +235,7 @@ abstract class DibiDriver
abstract public function applyLimit(&$sql, $limit, $offset = 0); abstract public function applyLimit(&$sql, $limit, $offset = 0);
/** /**
* Undefined property usage prevention * Undefined property usage prevention
*/ */

View File

@@ -51,8 +51,14 @@ abstract class DibiResult implements IteratorAggregate, Countable
*/ */
protected $convert; protected $convert;
/**
* Describes columns types
* @var array
*/
protected $meta;
static private $meta = array(
static private $types = array(
dibi::FIELD_TEXT => 'string', dibi::FIELD_TEXT => 'string',
dibi::FIELD_BINARY => 'string', dibi::FIELD_BINARY => 'string',
dibi::FIELD_BOOL => 'bool', dibi::FIELD_BOOL => 'bool',
@@ -70,30 +76,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
*/ */
abstract public function seek($row); abstract public function seek($row);
/** /**
* Returns the number of rows in a result set * Returns the number of rows in a result set
* @return int * @return int
*/ */
abstract public function rowCount(); abstract public function rowCount();
/**
* Gets an array of field names
* @return array
*/
abstract public function getFields();
/**
* Gets an array of meta informations about column
* @param string column name
* @return array
*/
abstract public function getMetaData($field);
/**
* Acquires ....
* @return void
*/
abstract protected function detectTypes();
/** /**
* Frees the resources allocated for this result set * Frees the resources allocated for this result set
@@ -101,6 +92,8 @@ abstract class DibiResult implements IteratorAggregate, Countable
*/ */
abstract protected function free(); abstract protected function free();
/** /**
* Fetches the row at current position and moves the internal cursor to the next position * Fetches the row at current position and moves the internal cursor to the next position
* internal usage only * internal usage only
@@ -109,6 +102,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
abstract protected function doFetch(); abstract protected function doFetch();
/** /**
* Fetches the row at current position, process optional type conversion * Fetches the row at current position, process optional type conversion
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
@@ -296,6 +290,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
} }
/** is this needed? */ /** is this needed? */
public function getType($field) public function getType($field)
{ {
@@ -303,13 +298,14 @@ abstract class DibiResult implements IteratorAggregate, Countable
} }
public function convert($value, $type) public function convert($value, $type)
{ {
if ($value === NULL || $value === FALSE) if ($value === NULL || $value === FALSE)
return $value; return $value;
if (isset(self::$meta[$type])) { if (isset(self::$types[$type])) {
settype($value, self::$meta[$type]); settype($value, self::$types[$type]);
return $value; return $value;
} }
@@ -323,6 +319,52 @@ abstract class DibiResult implements IteratorAggregate, Countable
} }
/**
* Gets an array of field names
* @return array
*/
public function getFields()
{
// lazy init
if ($this->meta === NULL) $this->buildMeta();
return array_keys($this->meta);
}
/**
* Gets an array of meta informations about column
* @param string column name
* @return array
*/
public function getMetaData($field)
{
// lazy init
if ($this->meta === NULL) $this->buildMeta();
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
}
/**
* Acquires ....
* @return void
*/
protected function detectTypes()
{
if ($this->meta === NULL) $this->buildMeta();
}
/**
* @return void
*/
abstract protected function buildMeta();
/** these are the required IteratorAggregate functions */ /** these are the required IteratorAggregate functions */
public function getIterator($offset = NULL, $count = NULL) public function getIterator($offset = NULL, $count = NULL)
{ {
@@ -331,6 +373,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** end required IteratorAggregate functions */ /** end required IteratorAggregate functions */
/** these are the required Countable functions */ /** these are the required Countable functions */
public function count() public function count()
{ {
@@ -339,6 +382,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
/** end required Countable functions */ /** end required Countable functions */
/** /**
* Undefined property usage prevention * Undefined property usage prevention
*/ */
@@ -393,6 +437,7 @@ class DibiResultIterator implements Iterator
} }
/** these are the required Iterator functions */ /** these are the required Iterator functions */
public function rewind() public function rewind()
{ {
@@ -402,18 +447,21 @@ class DibiResultIterator implements Iterator
} }
public function key() public function key()
{ {
return $this->row; return $this->row;
} }
public function current() public function current()
{ {
return $this->record; return $this->record;
} }
public function next() public function next()
{ {
$this->record = $this->result->fetch(); $this->record = $this->result->fetch();
@@ -421,6 +469,7 @@ class DibiResultIterator implements Iterator
} }
public function valid() public function valid()
{ {
return is_array($this->record) && ($this->row < $this->count); return is_array($this->record) && ($this->row < $this->count);

View File

@@ -29,29 +29,26 @@ class DibiTranslator
{ {
private private
$driver, $driver,
$subK, $subV,
$modifier, $modifier,
$hasError, $hasError,
$comment, $comment,
$ifLevel, $ifLevel,
$ifLevelStart; $ifLevelStart;
public $sql;
public function __construct($driver, $subst) public function __construct($driver)
{ {
$this->driver = $driver; $this->driver = $driver;
$this->subK = array_keys($subst);
$this->subV = array_values($subst);
} }
/** /**
* Generates SQL * Generates SQL
* *
* @param array * @param array
* @return string * @return string|FALSE
* @throw DibiException * @throw DibiException
*/ */
public function translate($args) public function translate($args)
@@ -112,16 +109,30 @@ class DibiTranslator
// TODO: check !!! // TODO: check !!!
$sql = preg_replace('#\x00.*?\x00#s', '', $sql); $sql = preg_replace('#\x00.*?\x00#s', '', $sql);
$this->sql = $sql; // error handling
if ($this->hasError) {
if (dibi::$logFile) // log to file
dibi::log(
"ERROR: SQL generate error"
. "\n-- SQL: " . $sql
. ";\n-- " . date('Y-m-d H:i:s ')
);
return !$this->hasError; if (dibi::$throwExceptions)
throw new DibiException('SQL generate error', NULL, $sql);
else {
trigger_error("dibi: SQL generate error: $sql", E_USER_WARNING);
return FALSE;
}
}
// OK
return $sql;
} }
private function formatValue($value, $modifier) private function formatValue($value, $modifier)
{ {
// array processing (with or without modifier) // array processing (with or without modifier)
@@ -266,7 +277,6 @@ class DibiTranslator
/** /**
* PREG callback for @see self::formatValue() * PREG callback for @see self::formatValue()
* @param array * @param array
@@ -351,11 +361,7 @@ class DibiTranslator
*/ */
private function quote($value) private function quote($value)
{ {
// apply substitutions return $this->driver->quoteName( dibi::substitute($value) );
if ($this->subK && (strpos($value, ':') !== FALSE))
return str_replace($this->subK, $this->subV, $value);
return $this->driver->quoteName($value);
} }

View File

@@ -1,84 +1,140 @@
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38; OK: connected to DB 'mysql'
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 178.920 ms OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- result: object(DibiMySqlResult) rows: 0
-- takes: 1.109 ms
-- 2007-02-02 04:38:34
Successfully connected to DB 'mysql'
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
Query error: Table 'test.nucleus_item' doesn't exist -- result: object(DibiMySqlResult) rows: 12
-- takes: 1.492 ms
SELECT * FROM `nucleus_item` WHERE `inumber` = 38; -- 2007-02-02 04:38:34
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.527 ms ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-02-02 04:38:34
Query error: Table 'test.nucleus_item' doesn't exist ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
SELECT * FROM `nucleus_item` WHERE `inumber` < 38; -- 2007-02-02 04:38:34
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 3.806 ms OK: connected to DB 'mysql'
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- result: object(DibiMySqlResult) rows: 0
Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22) -- takes: 1.565 ms
-- 2007-02-02 04:47:37
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38;
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22) OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
-- Takes: 5.017 ms -- result: object(DibiMySqlResult) rows: 12
-- takes: 1.347 ms
-- 2007-02-02 04:47:37
Successfully connected to DB 'mysql' ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
Query error: Table 'test.nucleus_item' doesn't exist -- 2007-02-02 04:47:37
SELECT * FROM `nucleus_item` WHERE `inumber` = 38; ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- Result: Query error: Table 'test.nucleus_item' doesn't exist -- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- Takes: 4.560 ms -- 2007-02-02 04:47:37
OK: connected to DB 'mysql'
Query error: Table 'test.nucleus_item' doesn't exist OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- result: object(DibiMySqlResult) rows: 0
SELECT * FROM `nucleus_item` WHERE `inumber` < 38; -- takes: 1.147 ms
-- Result: Query error: Table 'test.nucleus_item' doesn't exist -- 2007-02-02 04:47:56
-- Takes: 2.001 ms
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
-- result: object(DibiMySqlResult) rows: 12
-- takes: 1.085 ms
Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22) -- 2007-02-02 04:47:56
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38; ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22) -- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- Takes: 39.903 ms -- 2007-02-02 04:47:56
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
Successfully connected to DB 'mysql' -- 2007-02-02 04:47:57
Query error: Table 'test.nucleus_item' doesn't exist OK: connected to DB 'mysql'
SELECT * FROM `nucleus_item` WHERE `inumber` = 38; OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- Result: Query error: Table 'test.nucleus_item' doesn't exist -- result: object(DibiMySqlResult) rows: 0
-- Takes: 2.183 ms -- takes: 0.944 ms
-- 2007-02-02 04:48:23
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
Query error: Table 'test.nucleus_item' doesn't exist -- result: object(DibiMySqlResult) rows: 12
-- takes: 1.258 ms
SELECT * FROM `nucleus_item` WHERE `inumber` < 38; -- 2007-02-02 04:48:23
-- Result: Query error: Table 'test.nucleus_item' doesn't exist
-- Takes: 2.513 ms ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-02-02 04:48:23
Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22) ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
SELECT * FROM `*nucleus_item` WHERE `inumber` < 38; -- 2007-02-02 04:48:23
-- Result: Query error: Can't find file: '.\test\*nucleus_item.frm' (errno: 22)
-- Takes: 5.509 ms OK: connected to DB 'mysql'
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- result: object(DibiMySqlResult) rows: 0
-- takes: 1.039 ms
-- 2007-02-02 04:50:46
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
-- result: object(DibiMySqlResult) rows: 12
-- takes: 1.499 ms
-- 2007-02-02 04:50:46
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-02-02 04:50:46
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-02-02 04:50:46
OK: connected to DB 'mysql'
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- result: object(DibiMySqlResult) rows: 0
-- takes: 1.518 ms
-- 2007-03-28 00:45:27
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
-- result: object(DibiMySqlResult) rows: 12
-- takes: 6.385 ms
-- 2007-03-28 00:45:27
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-03-28 00:45:27
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-03-28 00:45:27
OK: connected to DB 'mysql'
OK: SELECT * FROM `mytable` WHERE `inumber` = 38;
-- result: object(DibiMySqlResult) rows: 0
-- takes: 1.124 ms
-- 2007-03-28 01:06:07
OK: SELECT * FROM `mytable` WHERE `inumber` < 38;
-- result: object(DibiMySqlResult) rows: 12
-- takes: 1.607 ms
-- 2007-03-28 01:06:07
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-03-28 01:06:07
ERROR: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `mytable` WHERE `inumber` < 38' at line 1
-- SQL: SELECT FROM `mytable` WHERE `inumber` < 38;
-- 2007-03-28 01:06:07

View File

@@ -1 +1 @@
Dibi Version 0.7e Dibi Version 0.7f