1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-08 07:06:52 +02:00

* renamed some files libs

* added doc comments to drivers
* DibiDriver::prepare() renamed to config()
* fixed connection error handling in Postgre driver
This commit is contained in:
David Grudl
2007-11-09 02:28:27 +00:00
parent 6492fe10b6
commit 8a6d664876
18 changed files with 978 additions and 154 deletions

View File

@@ -33,11 +33,12 @@ if (version_compare(PHP_VERSION , '5.1.0', '<')) {
// libraries // libraries
require_once __FILE__ . '/../libs/NObject.php'; require_once __FILE__ . '/../libs/NObject.php';
require_once __FILE__ . '/../libs/driver.php'; require_once __FILE__ . '/../libs/DibiException.php';
require_once __FILE__ . '/../libs/resultset.php'; require_once __FILE__ . '/../libs/DibiDriver.php';
require_once __FILE__ . '/../libs/translator.php'; require_once __FILE__ . '/../libs/DibiResult.php';
require_once __FILE__ . '/../libs/exception.php'; require_once __FILE__ . '/../libs/DibiResultIterator.php';
require_once __FILE__ . '/../libs/logger.php'; require_once __FILE__ . '/../libs/DibiTranslator.php';
require_once __FILE__ . '/../libs/DibiLogger.php';

View File

@@ -27,6 +27,10 @@
*/ */
class DibiMsSqlDriver extends DibiDriver class DibiMsSqlDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "-1", 'TRUE' => "-1",
'FALSE' => "0", 'FALSE' => "0",
@@ -36,18 +40,27 @@ class DibiMsSqlDriver extends DibiDriver
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'username', 'user'); self::config($config, 'username', 'user');
self::prepare($config, 'password', 'pass'); self::config($config, 'password', 'pass');
self::prepare($config, 'host'); self::config($config, 'host');
parent::__construct($config); parent::__construct($config);
} }
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('mssql')) { if (!extension_loaded('mssql')) {
@@ -76,6 +89,13 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$res = @mssql_query($sql, $this->getConnection()); $res = @mssql_query($sql, $this->getConnection());
@@ -89,6 +109,11 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
$rows = mssql_rows_affected($this->getConnection()); $rows = mssql_rows_affected($this->getConnection());
@@ -97,6 +122,11 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -104,6 +134,10 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$this->doQuery('BEGIN TRANSACTION'); $this->doQuery('BEGIN TRANSACTION');
@@ -112,6 +146,10 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$this->doQuery('COMMIT'); $this->doQuery('COMMIT');
@@ -120,6 +158,10 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$this->doQuery('ROLLBACK'); $this->doQuery('ROLLBACK');
@@ -128,6 +170,11 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
return array( return array(
@@ -138,6 +185,13 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
$value = str_replace("'", "''", $value); $value = str_replace("'", "''", $value);
@@ -148,6 +202,12 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
return '[' . str_replace('.', '].[', $value) . ']'; return '[' . str_replace('.', '].[', $value) . ']';
@@ -155,6 +215,11 @@ class DibiMsSqlDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -163,7 +228,12 @@ class DibiMsSqlDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -191,6 +261,11 @@ class DibiMsSqlDriver extends DibiDriver
class DibiMSSqlResult extends DibiResult class DibiMSSqlResult extends DibiResult
{ {
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return mssql_num_rows($this->resource); return mssql_num_rows($this->resource);
@@ -198,6 +273,12 @@ class DibiMSSqlResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return mssql_fetch_assoc($this->resource); return mssql_fetch_assoc($this->resource);
@@ -205,6 +286,12 @@ class DibiMSSqlResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
return mssql_data_seek($this->resource, $row); return mssql_data_seek($this->resource, $row);
@@ -212,6 +299,11 @@ class DibiMSSqlResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
mssql_free_result($this->resource); mssql_free_result($this->resource);

View File

@@ -27,6 +27,10 @@
*/ */
class DibiMySqlDriver extends DibiDriver class DibiMySqlDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "1", 'TRUE' => "1",
'FALSE' => "0", 'FALSE' => "0",
@@ -36,13 +40,15 @@ class DibiMySqlDriver extends DibiDriver
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'username', 'user'); self::config($config, 'username', 'user');
self::prepare($config, 'password', 'pass'); self::config($config, 'password', 'pass');
// default values // default values
if ($config['username'] === NULL) $config['username'] = ini_get('mysql.default_user'); if ($config['username'] === NULL) $config['username'] = ini_get('mysql.default_user');
@@ -58,6 +64,12 @@ class DibiMySqlDriver extends DibiDriver
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('mysql')) { if (!extension_loaded('mysql')) {
@@ -110,6 +122,13 @@ class DibiMySqlDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -124,6 +143,11 @@ class DibiMySqlDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
$rows = mysql_affected_rows($this->getConnection()); $rows = mysql_affected_rows($this->getConnection());
@@ -132,6 +156,11 @@ class DibiMySqlDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
$id = mysql_insert_id($this->getConnection()); $id = mysql_insert_id($this->getConnection());
@@ -140,6 +169,10 @@ class DibiMySqlDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$this->doQuery('BEGIN'); $this->doQuery('BEGIN');
@@ -148,6 +181,10 @@ class DibiMySqlDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$this->doQuery('COMMIT'); $this->doQuery('COMMIT');
@@ -156,6 +193,10 @@ class DibiMySqlDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$this->doQuery('ROLLBACK'); $this->doQuery('ROLLBACK');
@@ -164,6 +205,11 @@ class DibiMySqlDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -175,6 +221,13 @@ class DibiMySqlDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -185,6 +238,12 @@ class DibiMySqlDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
return '`' . str_replace('.', '`.`', $value) . '`'; return '`' . str_replace('.', '`.`', $value) . '`';
@@ -192,6 +251,11 @@ class DibiMySqlDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -200,7 +264,12 @@ class DibiMySqlDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -225,6 +294,11 @@ class DibiMySqlDriver extends DibiDriver
class DibiMySqlResult extends DibiResult class DibiMySqlResult extends DibiResult
{ {
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return mysql_num_rows($this->resource); return mysql_num_rows($this->resource);
@@ -232,6 +306,12 @@ class DibiMySqlResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return mysql_fetch_assoc($this->resource); return mysql_fetch_assoc($this->resource);
@@ -239,6 +319,12 @@ class DibiMySqlResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
return mysql_data_seek($this->resource, $row); return mysql_data_seek($this->resource, $row);
@@ -246,6 +332,11 @@ class DibiMySqlResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
mysql_free_result($this->resource); mysql_free_result($this->resource);

View File

@@ -27,6 +27,10 @@
*/ */
class DibiMySqliDriver extends DibiDriver class DibiMySqliDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "1", 'TRUE' => "1",
'FALSE' => "0", 'FALSE' => "0",
@@ -35,16 +39,17 @@ class DibiMySqliDriver extends DibiDriver
); );
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'username', 'user'); self::config($config, 'username', 'user');
self::prepare($config, 'password', 'pass'); self::config($config, 'password', 'pass');
self::prepare($config, 'database'); self::config($config, 'database');
// default values // default values
if ($config['username'] === NULL) $config['username'] = ini_get('mysqli.default_user'); if ($config['username'] === NULL) $config['username'] = ini_get('mysqli.default_user');
@@ -60,6 +65,12 @@ class DibiMySqliDriver extends DibiDriver
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('mysqli')) { if (!extension_loaded('mysqli')) {
@@ -84,6 +95,13 @@ class DibiMySqliDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -98,6 +116,11 @@ class DibiMySqliDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
$rows = mysqli_affected_rows($this->getConnection()); $rows = mysqli_affected_rows($this->getConnection());
@@ -106,6 +129,11 @@ class DibiMySqliDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
$id = mysqli_insert_id($this->getConnection()); $id = mysqli_insert_id($this->getConnection());
@@ -114,6 +142,10 @@ class DibiMySqliDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -125,6 +157,10 @@ class DibiMySqliDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -137,6 +173,10 @@ class DibiMySqliDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -149,6 +189,11 @@ class DibiMySqliDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -160,6 +205,13 @@ class DibiMySqliDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -170,6 +222,12 @@ class DibiMySqliDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
return '`' . str_replace('.', '`.`', $value) . '`'; return '`' . str_replace('.', '`.`', $value) . '`';
@@ -177,6 +235,11 @@ class DibiMySqliDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -185,7 +248,12 @@ class DibiMySqliDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -210,6 +278,11 @@ class DibiMySqliDriver extends DibiDriver
class DibiMySqliResult extends DibiResult class DibiMySqliResult extends DibiResult
{ {
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return mysqli_num_rows($this->resource); return mysqli_num_rows($this->resource);
@@ -217,6 +290,12 @@ class DibiMySqliResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return mysqli_fetch_assoc($this->resource); return mysqli_fetch_assoc($this->resource);
@@ -224,6 +303,12 @@ class DibiMySqliResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
return mysqli_data_seek($this->resource, $row); return mysqli_data_seek($this->resource, $row);
@@ -231,6 +316,11 @@ class DibiMySqliResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
mysqli_free_result($this->resource); mysqli_free_result($this->resource);

View File

@@ -27,6 +27,10 @@
*/ */
class DibiOdbcDriver extends DibiDriver class DibiOdbcDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "-1", 'TRUE' => "-1",
'FALSE' => "0", 'FALSE' => "0",
@@ -43,14 +47,16 @@ class DibiOdbcDriver extends DibiDriver
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'username', 'user'); self::config($config, 'username', 'user');
self::prepare($config, 'password', 'pass'); self::config($config, 'password', 'pass');
self::prepare($config, 'database'); self::config($config, 'database');
// default values // default values
if ($config['username'] === NULL) $config['username'] = ini_get('odbc.default_user'); if ($config['username'] === NULL) $config['username'] = ini_get('odbc.default_user');
@@ -62,6 +68,12 @@ class DibiOdbcDriver extends DibiDriver
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('odbc')) { if (!extension_loaded('odbc')) {
@@ -86,6 +98,13 @@ class DibiOdbcDriver extends DibiDriver
/**
* Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiException
*/
public function nativeQuery($sql) public function nativeQuery($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = FALSE;
@@ -99,6 +118,13 @@ class DibiOdbcDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -113,6 +139,11 @@ class DibiOdbcDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
return $this->affectedRows; return $this->affectedRows;
@@ -120,6 +151,11 @@ class DibiOdbcDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -127,6 +163,10 @@ class DibiOdbcDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -138,6 +178,10 @@ class DibiOdbcDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -150,6 +194,10 @@ class DibiOdbcDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -162,6 +210,11 @@ class DibiOdbcDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -173,6 +226,13 @@ class DibiOdbcDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
$value = str_replace("'", "''", $value); $value = str_replace("'", "''", $value);
@@ -183,6 +243,12 @@ class DibiOdbcDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
return '[' . str_replace('.', '].[', $value) . ']'; return '[' . str_replace('.', '].[', $value) . ']';
@@ -190,6 +256,11 @@ class DibiOdbcDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -198,7 +269,12 @@ class DibiOdbcDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -225,6 +301,11 @@ class DibiOdbcResult extends DibiResult
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
// will return -1 with many drivers :-( // will return -1 with many drivers :-(
@@ -233,6 +314,12 @@ class DibiOdbcResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return odbc_fetch_array($this->resource, $this->row++); return odbc_fetch_array($this->resource, $this->row++);
@@ -240,6 +327,12 @@ class DibiOdbcResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
$this->row = $row; $this->row = $row;
@@ -247,6 +340,11 @@ class DibiOdbcResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
odbc_free_result($this->resource); odbc_free_result($this->resource);

View File

@@ -27,6 +27,10 @@
*/ */
class DibiOracleDriver extends DibiDriver class DibiOracleDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "1", 'TRUE' => "1",
'FALSE' => "0", 'FALSE' => "0",
@@ -39,20 +43,28 @@ class DibiOracleDriver extends DibiDriver
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'username', 'user'); self::config($config, 'username', 'user');
self::prepare($config, 'password', 'pass'); self::config($config, 'password', 'pass');
self::prepare($config, 'database', 'db'); self::config($config, 'database', 'db');
self::prepare($config, 'charset'); self::config($config, 'charset');
parent::__construct($config); parent::__construct($config);
} }
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('oci8')) { if (!extension_loaded('oci8')) {
@@ -73,6 +85,13 @@ class DibiOracleDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -95,6 +114,11 @@ class DibiOracleDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -102,6 +126,11 @@ class DibiOracleDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -109,6 +138,10 @@ class DibiOracleDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$this->autocommit = FALSE; $this->autocommit = FALSE;
@@ -116,6 +149,10 @@ class DibiOracleDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -129,6 +166,10 @@ class DibiOracleDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -142,6 +183,11 @@ class DibiOracleDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
return oci_error($this->getConnection()); return oci_error($this->getConnection());
@@ -149,6 +195,13 @@ class DibiOracleDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
return $appendQuotes return $appendQuotes
@@ -158,6 +211,12 @@ class DibiOracleDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
return '[' . str_replace('.', '].[', $value) . ']'; return '[' . str_replace('.', '].[', $value) . ']';
@@ -165,6 +224,11 @@ class DibiOracleDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -173,7 +237,12 @@ class DibiOracleDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -194,6 +263,11 @@ class DibiOracleDriver extends DibiDriver
class DibiOracleResult extends DibiResult class DibiOracleResult extends DibiResult
{ {
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return oci_num_rows($this->resource); return oci_num_rows($this->resource);
@@ -201,6 +275,12 @@ class DibiOracleResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return oci_fetch_assoc($this->resource); return oci_fetch_assoc($this->resource);
@@ -208,6 +288,12 @@ class DibiOracleResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
//throw new BadMethodCallException(__METHOD__ . ' is not implemented'); //throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -215,6 +301,11 @@ class DibiOracleResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
oci_free_statement($this->resource); oci_free_statement($this->resource);

View File

@@ -27,6 +27,10 @@
*/ */
class DibiPdoDriver extends DibiDriver class DibiPdoDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "1", 'TRUE' => "1",
'FALSE' => "0", 'FALSE' => "0",
@@ -35,21 +39,28 @@ class DibiPdoDriver extends DibiDriver
); );
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'username', 'user'); self::config($config, 'username', 'user');
self::prepare($config, 'password', 'pass'); self::config($config, 'password', 'pass');
self::prepare($config, 'dsn'); self::config($config, 'dsn');
parent::__construct($config); parent::__construct($config);
} }
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('pdo')) { if (!extension_loaded('pdo')) {
@@ -66,6 +77,13 @@ class DibiPdoDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$res = $this->getConnection()->query($sql); $res = $this->getConnection()->query($sql);
@@ -74,6 +92,11 @@ class DibiPdoDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -81,6 +104,11 @@ class DibiPdoDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
return $this->getConnection()->lastInsertId(); return $this->getConnection()->lastInsertId();
@@ -88,6 +116,10 @@ class DibiPdoDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$this->getConnection()->beginTransaction(); $this->getConnection()->beginTransaction();
@@ -96,6 +128,10 @@ class DibiPdoDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$this->getConnection()->commit(); $this->getConnection()->commit();
@@ -104,6 +140,10 @@ class DibiPdoDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$this->getConnection()->rollBack(); $this->getConnection()->rollBack();
@@ -112,6 +152,11 @@ class DibiPdoDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
$error = $this->getConnection()->errorInfo(); $error = $this->getConnection()->errorInfo();
@@ -124,6 +169,13 @@ class DibiPdoDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
if (!$appendQuotes) { if (!$appendQuotes) {
@@ -134,6 +186,12 @@ class DibiPdoDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
// quoting is not supported by PDO // quoting is not supported by PDO
@@ -142,6 +200,11 @@ class DibiPdoDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -150,7 +213,12 @@ class DibiPdoDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -172,6 +240,11 @@ class DibiPdoResult extends DibiResult
private $row = 0; private $row = 0;
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return $this->resource->rowCount(); return $this->resource->rowCount();
@@ -179,6 +252,12 @@ class DibiPdoResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return $this->resource->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT, $this->row++); return $this->resource->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT, $this->row++);
@@ -186,6 +265,12 @@ class DibiPdoResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
$this->row = $row; $this->row = $row;
@@ -193,6 +278,11 @@ class DibiPdoResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
} }

View File

@@ -27,6 +27,10 @@
*/ */
class DibiPostgreDriver extends DibiDriver class DibiPostgreDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "TRUE", 'TRUE' => "TRUE",
'FALSE' => "FALSE", 'FALSE' => "FALSE",
@@ -43,18 +47,26 @@ class DibiPostgreDriver extends DibiDriver
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
self::prepare($config, 'database', 'string'); self::config($config, 'database', 'string');
self::prepare($config, 'type'); self::config($config, 'type');
parent::__construct($config); parent::__construct($config);
} }
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('pgsql')) { if (!extension_loaded('pgsql')) {
@@ -63,14 +75,25 @@ class DibiPostgreDriver extends DibiDriver
$config = $this->getConfig(); $config = $this->getConfig();
// some errors aren't handled. Must use $php_errormsg
if (function_exists('ini_set')) {
$save = ini_set('track_errors', TRUE);
}
$php_errormsg = '';
if (isset($config['persistent'])) { if (isset($config['persistent'])) {
$connection = @pg_connect($config['database'], $config['type']); $connection = @pg_connect($config['database'], $config['type']);
} else { } else {
$connection = @pg_pconnect($config['database'], $config['type']); $connection = @pg_pconnect($config['database'], $config['type']);
} }
if (function_exists('ini_set')) {
ini_set('track_errors', $save);
}
if (!is_resource($connection)) { if (!is_resource($connection)) {
throw new DibiDatabaseException(pg_last_error()); throw new DibiDatabaseException($php_errormsg);
} }
if (isset($config['charset'])) { if (isset($config['charset'])) {
@@ -84,6 +107,13 @@ class DibiPostgreDriver extends DibiDriver
/**
* Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiException
*/
public function nativeQuery($sql) public function nativeQuery($sql)
{ {
$this->affectedRows = FALSE; $this->affectedRows = FALSE;
@@ -97,6 +127,13 @@ class DibiPostgreDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -111,6 +148,11 @@ class DibiPostgreDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
return $this->affectedRows; return $this->affectedRows;
@@ -118,6 +160,11 @@ class DibiPostgreDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId($sequence = NULL) public function insertId($sequence = NULL)
{ {
if ($sequence === NULL) { if ($sequence === NULL) {
@@ -138,6 +185,10 @@ class DibiPostgreDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$this->doQuery('BEGIN'); $this->doQuery('BEGIN');
@@ -146,6 +197,10 @@ class DibiPostgreDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$this->doQuery('COMMIT'); $this->doQuery('COMMIT');
@@ -154,6 +209,10 @@ class DibiPostgreDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$this->doQuery('ROLLBACK'); $this->doQuery('ROLLBACK');
@@ -162,6 +221,11 @@ class DibiPostgreDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
return array( return array(
@@ -172,6 +236,13 @@ class DibiPostgreDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
return $appendQuotes return $appendQuotes
@@ -181,6 +252,12 @@ class DibiPostgreDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
$value = str_replace('"', '""', $value); $value = str_replace('"', '""', $value);
@@ -189,6 +266,11 @@ class DibiPostgreDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -197,7 +279,12 @@ class DibiPostgreDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -222,6 +309,11 @@ class DibiPostgreDriver extends DibiDriver
class DibiPostgreResult extends DibiResult class DibiPostgreResult extends DibiResult
{ {
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return pg_num_rows($this->resource); return pg_num_rows($this->resource);
@@ -229,6 +321,12 @@ class DibiPostgreResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return pg_fetch_array($this->resource, NULL, PGSQL_ASSOC); return pg_fetch_array($this->resource, NULL, PGSQL_ASSOC);
@@ -236,6 +334,12 @@ class DibiPostgreResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
return pg_result_seek($this->resource, $row); return pg_result_seek($this->resource, $row);
@@ -243,6 +347,11 @@ class DibiPostgreResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
pg_free_result($this->resource); pg_free_result($this->resource);

View File

@@ -27,6 +27,10 @@
*/ */
class DibiSqliteDriver extends DibiDriver class DibiSqliteDriver extends DibiDriver
{ {
/**
* Describes how convert some datatypes to SQL command
* @var array
*/
public $formats = array( public $formats = array(
'TRUE' => "1", 'TRUE' => "1",
'FALSE' => "0", 'FALSE' => "0",
@@ -35,20 +39,27 @@ class DibiSqliteDriver extends DibiDriver
); );
/** /**
* Creates object and (optionally) connects to a database
*
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct($config)
{ {
self::prepare($config, 'database', 'file'); self::config($config, 'database', 'file');
if (!isset($config['mode'])) $config['mode'] = 0666; if (!isset($config['mode'])) $config['mode'] = 0666;
parent::__construct($config); parent::__construct($config);
} }
/**
* Connects to a database
*
* @throws DibiException
* @return resource
*/
protected function connect() protected function connect()
{ {
if (!extension_loaded('sqlite')) { if (!extension_loaded('sqlite')) {
@@ -74,6 +85,13 @@ class DibiSqliteDriver extends DibiDriver
/**
* Internal: Executes the SQL query
*
* @param string SQL statement.
* @return DibiResult|TRUE Result set object
* @throws DibiDatabaseException
*/
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
@@ -88,6 +106,11 @@ class DibiSqliteDriver extends DibiDriver
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
*/
public function affectedRows() public function affectedRows()
{ {
$rows = sqlite_changes($this->getConnection()); $rows = sqlite_changes($this->getConnection());
@@ -96,6 +119,11 @@ class DibiSqliteDriver extends DibiDriver
/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
*
* @return int|FALSE int on success or FALSE on failure
*/
public function insertId() public function insertId()
{ {
$id = sqlite_last_insert_rowid($this->getConnection()); $id = sqlite_last_insert_rowid($this->getConnection());
@@ -104,6 +132,10 @@ class DibiSqliteDriver extends DibiDriver
/**
* Begins a transaction (if supported).
* @return void
*/
public function begin() public function begin()
{ {
$this->doQuery('BEGIN'); $this->doQuery('BEGIN');
@@ -112,6 +144,10 @@ class DibiSqliteDriver extends DibiDriver
/**
* Commits statements in a transaction.
* @return void
*/
public function commit() public function commit()
{ {
$this->doQuery('COMMIT'); $this->doQuery('COMMIT');
@@ -120,6 +156,10 @@ class DibiSqliteDriver extends DibiDriver
/**
* Rollback changes in a transaction.
* @return void
*/
public function rollback() public function rollback()
{ {
$this->doQuery('ROLLBACK'); $this->doQuery('ROLLBACK');
@@ -128,6 +168,11 @@ class DibiSqliteDriver extends DibiDriver
/**
* Returns last error
*
* @return array with items 'message' and 'code'
*/
public function errorInfo() public function errorInfo()
{ {
$code = sqlite_last_error($this->getConnection()); $code = sqlite_last_error($this->getConnection());
@@ -139,6 +184,13 @@ class DibiSqliteDriver extends DibiDriver
/**
* Escapes the string
*
* @param string unescaped string
* @param bool quote string?
* @return string escaped and optionally quoted string
*/
public function escape($value, $appendQuotes = TRUE) public function escape($value, $appendQuotes = TRUE)
{ {
return $appendQuotes return $appendQuotes
@@ -148,6 +200,12 @@ class DibiSqliteDriver extends DibiDriver
/**
* Delimites identifier (table's or column's name, etc.)
*
* @param string identifier
* @return string delimited identifier
*/
public function delimite($value) public function delimite($value)
{ {
return '[' . str_replace('.', '].[', $value) . ']'; return '[' . str_replace('.', '].[', $value) . ']';
@@ -155,6 +213,11 @@ class DibiSqliteDriver extends DibiDriver
/**
* Gets a information of the current database.
*
* @return DibiMetaData
*/
public function getMetaData() public function getMetaData()
{ {
throw new BadMethodCallException(__METHOD__ . ' is not implemented'); throw new BadMethodCallException(__METHOD__ . ' is not implemented');
@@ -163,7 +226,12 @@ class DibiSqliteDriver extends DibiDriver
/** /**
* @see DibiDriver::applyLimit() * 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
*/ */
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
@@ -184,6 +252,11 @@ class DibiSqliteDriver extends DibiDriver
class DibiSqliteResult extends DibiResult class DibiSqliteResult extends DibiResult
{ {
/**
* Returns the number of rows in a result set
*
* @return int
*/
public function rowCount() public function rowCount()
{ {
return sqlite_num_rows($this->resource); return sqlite_num_rows($this->resource);
@@ -191,6 +264,12 @@ class DibiSqliteResult extends DibiResult
/**
* Fetches the row at current position and moves the internal cursor to the next position
* internal usage only
*
* @return array|FALSE array on success, FALSE if no next record
*/
protected function doFetch() protected function doFetch()
{ {
return sqlite_fetch_array($this->resource, SQLITE_ASSOC); return sqlite_fetch_array($this->resource, SQLITE_ASSOC);
@@ -198,6 +277,12 @@ class DibiSqliteResult extends DibiResult
/**
* Moves cursor position without fetching row
*
* @param int the 0-based cursor pos to seek to
* @return boolean TRUE on success, FALSE if unable to seek to specified record
*/
public function seek($row) public function seek($row)
{ {
return sqlite_seek($this->resource, $row); return sqlite_seek($this->resource, $row);
@@ -205,6 +290,11 @@ class DibiSqliteResult extends DibiResult
/**
* Frees the resources allocated for this result set
*
* @return void
*/
protected function free() protected function free()
{ {
} }

View File

@@ -59,7 +59,7 @@ abstract class DibiDriver extends NObject
* @param array connect configuration * @param array connect configuration
* @throws DibiException * @throws DibiException
*/ */
public function __construct($config) public function __construct(array $config)
{ {
$this->config = $config; $this->config = $config;
@@ -183,7 +183,7 @@ abstract class DibiDriver extends NObject
* @param string alias key * @param string alias key
* @return void * @return void
*/ */
protected static function prepare(&$config, $key, $alias=NULL) protected static function config(&$config, $key, $alias=NULL)
{ {
if (isset($config[$key])) return; if (isset($config[$key])) return;
@@ -290,7 +290,7 @@ abstract class DibiDriver extends NObject
/** /**
* Experimental - injects LIMIT/OFFSET to the SQL query * 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.
* @param int $limit * @param int $limit

View File

@@ -22,7 +22,7 @@
/** /**
* dibi basic logger & profiler * dibi basic logger & profiler (experimental)
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */

View File

@@ -26,10 +26,14 @@
* *
* <code> * <code>
* $result = dibi::query('SELECT * FROM [table]'); * $result = dibi::query('SELECT * FROM [table]');
*
* $row = $result->fetch();
* $value = $result->fetchSingle(); * $value = $result->fetchSingle();
* $all = $result->fetchAll(); * $table = $result->fetchAll();
* $pairs = $result->fetchPairs();
* $assoc = $result->fetchAssoc('id'); * $assoc = $result->fetchAssoc('id');
* $assoc = $result->fetchAssoc('active', 'id'); * $assoc = $result->fetchAssoc('active', 'id');
*
* unset($result); * unset($result);
* </code> * </code>
* *
@@ -443,7 +447,7 @@ abstract class DibiResult extends NObject implements IteratorAggregate, Countabl
/** /**
* Displays complete result-set as HTML table * Displays complete result-set as HTML table for debug purposes
* *
* @return void * @return void
*/ */
@@ -475,108 +479,27 @@ abstract class DibiResult extends NObject implements IteratorAggregate, Countabl
/** these are the required IteratorAggregate functions */ /**
final public function getIterator($offset = NULL, $count = NULL) * Required by the IteratorAggregate interface
* @param int offset
* @param int limit
* @return ArrayIterator
*/
final public function getIterator($offset = NULL, $limit = NULL)
{ {
return new DibiResultIterator($this, $offset, $count); return new DibiResultIterator($this, $offset, $limit);
} }
/** end required IteratorAggregate functions */
/** these are the required Countable functions */ /**
* Required by the Countable interface
* @return int
*/
final public function count() final public function count()
{ {
return $this->rowCount(); return $this->rowCount();
} }
/** end required Countable functions */
} // class DibiResult } // class DibiResult
/**
* Basic Result set iterator.
*
* This can be returned by DibiResult::getIterator() method or directly using foreach:
* <code>
* $result = dibi::query('SELECT * FROM table');
* foreach ($result as $fields) {
* print_r($fields);
* }
* unset($result);
* </code>
*
* Optionally you can specify offset and limit:
* <code>
* foreach ($result->getIterator(2, 3) as $fields) {
* print_r($fields);
* }
* </code>
*/
final class DibiResultIterator implements Iterator
{
private
$result,
$offset,
$count,
$row,
$pointer;
public function __construct(DibiResult $result, $offset = NULL, $count = NULL)
{
$this->result = $result;
$this->offset = (int) $offset;
$this->count = $count === NULL ? 2147483647 /*PHP_INT_MAX till 5.0.5 */ : (int) $count;
}
/** these are the required Iterator functions */
public function rewind()
{
$this->pointer = 0;
@$this->result->seek($this->offset);
$this->row = $this->result->fetch();
}
public function key()
{
return $this->pointer;
}
public function current()
{
return $this->row;
}
public function next()
{
$this->row = $this->result->fetch();
$this->pointer++;
}
public function valid()
{
return is_array($this->row) && ($this->pointer < $this->count);
}
/** end required Iterator functions */
} // class DibiResultIterator

View File

@@ -0,0 +1,125 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
*
* This source file is subject to the "dibi license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://php7.org/dibi/
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl
* @license http://php7.org/dibi/license (dibi license)
* @category Database
* @package Dibi
* @link http://php7.org/dibi/
*/
/**
* External result set iterator
*
* This can be returned by DibiResult::getIterator() method or using foreach
* <code>
* $result = dibi::query('SELECT * FROM table');
* foreach ($result as $row) {
* print_r($row);
* }
* unset($result);
* </code>
*
* Optionally you can specify offset and limit:
* <code>
* foreach ($result->getIterator(2, 3) as $row) {
* print_r($row);
* }
* </code>
*/
final class DibiResultIterator implements Iterator
{
private
$result,
$offset,
$limit,
$row,
$pointer;
/**
* Required by the Iterator interface
* @param int offset
* @param int limit
*/
public function __construct(DibiResult $result, $offset, $limit)
{
$this->result = $result;
$this->offset = (int) $offset;
$this->limit = $limit === NULL ? -1 : (int) $limit;
}
/**
* Rewinds the Iterator to the first element
* @return void
*/
public function rewind()
{
$this->pointer = 0;
@$this->result->seek($this->offset);
$this->row = $this->result->fetch();
}
/**
* Returns the key of the current element.
* @return mixed
*/
public function key()
{
return $this->pointer;
}
/**
* Returns the current element.
* @return mixed
*/
public function current()
{
return $this->row;
}
/**
* Moves forward to next element.
* @return void
*/
public function next()
{
$this->row = $this->result->fetch();
$this->pointer++;
}
/**
* Checks if there is a current element after calls to rewind() or next().
* @return bool
*/
public function valid()
{
return is_array($this->row) && ($this->limit < 0 || $this->pointer < $this->limit);
}
} // class DibiResultIterator

View File

@@ -22,7 +22,7 @@
/** /**
* dibi translator * dibi SQL translator
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
@@ -54,7 +54,7 @@ final class DibiTranslator extends NObject
public function __construct($driver) public function __construct(DibiDriver $driver)
{ {
$this->driver = $driver; $this->driver = $driver;
} }
@@ -67,7 +67,7 @@ final class DibiTranslator extends NObject
* @param array * @param array
* @return bool * @return bool
*/ */
public function translate($args) public function translate(array $args)
{ {
$this->hasError = FALSE; $this->hasError = FALSE;
$commandIns = NULL; $commandIns = NULL;
@@ -144,6 +144,12 @@ final class DibiTranslator extends NObject
/**
* Apply modifier to single value
* @param mixed
* @param string
* @return string
*/
private function formatValue($value, $modifier) private function formatValue($value, $modifier)
{ {
// array processing (with or without modifier) // array processing (with or without modifier)
@@ -412,4 +418,4 @@ final class DibiTranslator extends NObject
} }
} // class DibiParser } // class DibiTranslator

View File

@@ -21,6 +21,12 @@
/**
* compatiblity
*/
if (!class_exists('NObject', FALSE)) {
/** /**
* NObject is the ultimate ancestor of all instantiable classes. * NObject is the ultimate ancestor of all instantiable classes.
* *
@@ -227,3 +233,6 @@ abstract class NObject
class NPropertyException extends Exception class NPropertyException extends Exception
{ {
} }
}

View File

@@ -60,12 +60,8 @@ class MyDateTime implements DibiVariableInterface
// CHANGE TO REAL PARAMETERS! // CHANGE TO REAL PARAMETERS!
dibi::connect(array( dibi::connect(array(
'driver' => 'mysql', 'driver' => 'sqlite',
'host' => 'localhost', 'database' => 'sample.sdb',
'username' => 'root',
'password' => 'xxx',
'database' => 'dibi',
'charset' => 'utf8',
)); ));

View File

@@ -58,6 +58,11 @@ foreach ($res as $row => $fields) {
echo '<hr>'; echo '<hr>';
// fetch row by row with defined offset
foreach ($res->getIterator(2) as $row => $fields) {
print_r($fields);
}
// fetch row by row with defined offset and limit // fetch row by row with defined offset and limit
foreach ($res->getIterator(2, 1) as $row => $fields) { foreach ($res->getIterator(2, 1) as $row => $fields) {
print_r($fields); print_r($fields);
@@ -74,3 +79,11 @@ INNER JOIN [customers] USING ([customer_id])
$assoc = $res->fetchAssoc('customers.name,products.title'); // key $assoc = $res->fetchAssoc('customers.name,products.title'); // key
print_r($assoc); print_r($assoc);
echo '<hr>'; echo '<hr>';
$assoc = $res->fetchAssoc('customers.name,*,products.title'); // key
print_r($assoc);
echo '<hr>';
$assoc = $res->fetchAssoc('customers.name,#,products.title'); // key
print_r($assoc);
echo '<hr>';