1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-18 11:51:18 +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

@@ -31,8 +31,7 @@ abstract class DibiDriver
* Current connection configuration
* @var array
*/
protected
$config;
protected $config;
/**
* 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
*
* @param string SQL statement.
* @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();
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
* @return int|bool int on success or FALSE on failure
@@ -104,24 +167,28 @@ abstract class DibiDriver
abstract public function insertId();
/**
* Begins a transaction (if supported).
*/
abstract public function begin();
/**
* Commits statements in a transaction.
*/
abstract public function commit();
/**
* Rollback changes in a transaction.
*/
abstract public function rollback();
/**
* Returns last error
* @return array with items 'message' and 'code'
@@ -129,6 +196,7 @@ abstract class DibiDriver
abstract public function errorInfo();
/**
* Escapes the string
* @param string unescaped string
@@ -138,6 +206,7 @@ abstract class DibiDriver
abstract public function escape($value, $appendQuotes = FALSE);
/**
* Quotes SQL identifier (table's or column's name, etc.)
* @param string identifier
@@ -155,6 +224,7 @@ abstract class DibiDriver
abstract public function getMetaData();
/**
* Experimental - injects LIMIT/OFFSET to the SQL query
* @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);
/**
* Undefined property usage prevention
*/

View File

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

View File

@@ -29,29 +29,26 @@ class DibiTranslator
{
private
$driver,
$subK, $subV,
$modifier,
$hasError,
$comment,
$ifLevel,
$ifLevelStart;
public $sql;
public function __construct($driver, $subst)
public function __construct($driver)
{
$this->driver = $driver;
$this->subK = array_keys($subst);
$this->subV = array_values($subst);
}
/**
* Generates SQL
*
* @param array
* @return string
* @return string|FALSE
* @throw DibiException
*/
public function translate($args)
@@ -112,16 +109,30 @@ class DibiTranslator
// TODO: check !!!
$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)
{
// array processing (with or without modifier)
@@ -266,7 +277,6 @@ class DibiTranslator
/**
* PREG callback for @see self::formatValue()
* @param array
@@ -351,11 +361,7 @@ class DibiTranslator
*/
private function quote($value)
{
// apply substitutions
if ($this->subK && (strpos($value, ':') !== FALSE))
return str_replace($this->subK, $this->subV, $value);
return $this->driver->quoteName($value);
return $this->driver->quoteName( dibi::substitute($value) );
}