1
0
mirror of https://github.com/dg/dibi.git synced 2025-10-24 19:26:10 +02:00
Files
php-dibi/dibi/libs/driver.php
2007-10-13 22:50:29 +00:00

284 lines
6.3 KiB
PHP

<?php
/**
* This file is part of the "dibi" project (http://php7.org/dibi/)
*
* @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
*/
/**
* dibi Common Driver
*
*/
abstract class DibiDriver
{
/**
* Current connection configuration
* @var array
*/
private $config;
/**
* Connection resource
* @var resource
*/
private $connection;
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array(
'TRUE' => "1", // boolean true
'FALSE' => "0", // boolean false
'date' => "'Y-m-d'", // format used by date()
'datetime' => "'Y-m-d H:i:s'", // format used by date()
);
/**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration
* @throws DibiException
*/
public function __construct($config)
{
$this->config = $config;
if (empty($config['lazy'])) {
$this->connection = $this->connect();
}
}
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
abstract protected function connect();
/**
* Returns configuration variable. If no $key is passed, returns the entire array.
*
* @see DibiDriver::__construct
* @param string
* @param mixed default value to use if key not found
* @return mixed
*/
final public function getConfig($key = NULL, $default = NULL)
{
if ($key === NULL) {
return $this->config;
} elseif (isset($this->config[$key])) {
return $this->config[$key];
} else {
return $default;
}
}
/**
* Returns the connection resource
*
* @return resource
*/
final public function getConnection()
{
if (!$this->connection) {
$this->connection = $this->connect();
}
return $this->connection;
}
/**
* Generates (translates) and executes SQL query
*
* @param array|mixed one or more arguments
* @return DibiResult|TRUE
* @throws DibiException
*/
final public function query($args)
{
if (!is_array($args)) $args = func_get_args();
$trans = new DibiTranslator($this);
if ($trans->translate($args)) {
return $this->nativeQuery($trans->sql);
} else {
throw new DibiException('SQL translate error: ' . $trans->sql);
}
}
/**
* Generates and prints SQL query
*
* @param array|mixed one or more arguments
* @return bool
*/
final public function test($args)
{
if (!is_array($args)) $args = func_get_args();
$trans = new DibiTranslator($this);
$ok = $trans->translate($args);
dibi::dump($trans->sql);
return $ok;
}
/**
* Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiException
*/
public function nativeQuery($sql)
{
dibi::notify('beforeQuery', $this, $sql);
$res = $this->doQuery($sql);
dibi::notify('afterQuery', $this, $res);
return $res;
}
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
abstract protected function doQuery($sql);
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
abstract public function affectedRows();
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
abstract public function insertId();
/**
* Begins a transaction (if supported).
* @return void
*/
abstract public function begin();
/**
* Commits statements in a transaction.
* @return void
*/
abstract public function commit();
/**
* Rollback changes in a transaction.
* @return void
*/
abstract public function rollback();
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
abstract public function errorInfo();
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
abstract public function escape($value, $appendQuotes = TRUE);
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
abstract public function delimite($value);
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
abstract public function getMetaData();
/**
* Experimental - injects LIMIT/OFFSET to the SQL query
*
* @param string &$sql The SQL query that will be modified.
* @param int $limit
* @param int $offset
* @return void
*/
abstract public function applyLimit(&$sql, $limit, $offset = 0);
/**#@+
* Access to undeclared property
* @throws Exception
*/
private function &__get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); }
private function __set($name, $value) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); }
private function __unset($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); }
/**#@-*/
} // class DibiDriver