mirror of
https://github.com/dg/dibi.git
synced 2025-02-21 09:23:57 +01:00
* update DibiVariableInterface
* some bugs fixed
This commit is contained in:
parent
89c53395c1
commit
46850aa588
@ -132,11 +132,6 @@ class dibi extends NClass
|
||||
*/
|
||||
public static $defaultDriver = 'mysql';
|
||||
|
||||
/**
|
||||
* Start time
|
||||
* @var int
|
||||
*/
|
||||
private static $time;
|
||||
|
||||
|
||||
|
||||
@ -396,26 +391,15 @@ class dibi extends NClass
|
||||
/**
|
||||
* Event notification (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback)
|
||||
*
|
||||
* @param string event name
|
||||
* @param DibiConnection
|
||||
* @param string event name
|
||||
* @param mixed
|
||||
* @return void
|
||||
*/
|
||||
public static function notify($event, DibiConnection $conn = NULL, $arg = NULL)
|
||||
public static function notify(DibiConnection $connection = NULL, $event, $arg = NULL)
|
||||
{
|
||||
if ($event === 'beforeQuery') {
|
||||
self::$numOfQueries++;
|
||||
self::$elapsedTime = FALSE;
|
||||
self::$time = -microtime(TRUE);
|
||||
self::$sql = $arg;
|
||||
|
||||
} elseif ($event === 'afterQuery') {
|
||||
self::$elapsedTime = self::$time + microtime(TRUE);
|
||||
self::$totalTime += self::$elapsedTime;
|
||||
}
|
||||
|
||||
foreach (self::$handlers as $handler) {
|
||||
call_user_func($handler, $event, $conn, $arg);
|
||||
call_user_func($handler, $connection, $event, $arg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,8 +54,8 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -180,6 +180,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -188,7 +189,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? -1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
@ -251,7 +252,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
|
||||
public function seek($row)
|
||||
{
|
||||
if (!mssql_data_seek($this->resultset, $row)) {
|
||||
throw new DibiDriverException('Unable to seek to row ' . $row);
|
||||
throw new DibiDatabaseException('Unable to seek to row ' . $row);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,8 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -224,6 +224,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -232,7 +233,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
@ -298,7 +299,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
|
||||
throw new BadMethodCallException(__METHOD__ . ' is not allowed for unbuffered queries');
|
||||
}
|
||||
if (!mysql_data_seek($this->resultset, $row)) {
|
||||
throw new DibiDriverException('Unable to seek to row ' . $row);
|
||||
throw new DibiDatabaseException('Unable to seek to row ' . $row);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,8 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -208,6 +208,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -216,7 +217,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
@ -283,7 +284,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
|
||||
throw new BadMethodCallException(__METHOD__ . ' is not allowed for unbuffered queries');
|
||||
}
|
||||
if (!mysqli_data_seek($this->resultset, $row)) {
|
||||
throw new DibiDriverException('Unable to seek to row ' . $row);
|
||||
throw new DibiDatabaseException('Unable to seek to row ' . $row);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -60,8 +60,8 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -195,6 +195,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -203,7 +204,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? -1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("#m/d/Y#", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("#m/d/Y H:i:s#", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
|
@ -59,8 +59,8 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -193,6 +193,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -201,7 +202,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("U", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("U", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
|
@ -59,8 +59,8 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -168,6 +168,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -176,7 +177,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
|
@ -52,8 +52,8 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -194,6 +194,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -202,7 +203,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? 'TRUE' : 'FALSE';
|
||||
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("'Y-m-d H:i:s'", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
@ -263,7 +264,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
|
||||
public function seek($row)
|
||||
{
|
||||
if (!pg_result_seek($this->resultset, $row)) {
|
||||
throw new DibiDriverException('Unable to seek to row ' . $row);
|
||||
throw new DibiDatabaseException('Unable to seek to row ' . $row);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,8 +58,8 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function connect(array &$config)
|
||||
{
|
||||
@ -185,6 +185,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
|
||||
* @param string value
|
||||
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
|
||||
* @return string formatted value
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function format($value, $type)
|
||||
{
|
||||
@ -193,7 +194,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
|
||||
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
|
||||
if ($type === dibi::FIELD_DATE) return date("U", $value);
|
||||
if ($type === dibi::FIELD_DATETIME) return date("U", $value);
|
||||
throw new DibiException('Invalid formatting type');
|
||||
throw new InvalidArgumentException('Unsupported formatting type');
|
||||
}
|
||||
|
||||
|
||||
@ -269,6 +270,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
|
||||
*/
|
||||
public function free()
|
||||
{
|
||||
$this->resultset = NULL;
|
||||
}
|
||||
|
||||
|
||||
|
@ -106,7 +106,7 @@ class DibiConnection extends NObject
|
||||
{
|
||||
$this->driver->connect($this->config);
|
||||
$this->connected = TRUE;
|
||||
dibi::notify('connected');
|
||||
dibi::notify($this, 'connected');
|
||||
}
|
||||
|
||||
|
||||
@ -121,7 +121,7 @@ class DibiConnection extends NObject
|
||||
if ($this->connected) {
|
||||
$this->driver->disconnect();
|
||||
$this->connected = FALSE;
|
||||
dibi::notify('disconnected');
|
||||
dibi::notify($this, 'disconnected');
|
||||
}
|
||||
}
|
||||
|
||||
@ -234,12 +234,19 @@ class DibiConnection extends NObject
|
||||
{
|
||||
if (!$this->connected) $this->connect();
|
||||
|
||||
dibi::notify('beforeQuery', $this, $sql);
|
||||
dibi::$numOfQueries++;
|
||||
dibi::$sql = $sql;
|
||||
dibi::$elapsedTime = FALSE;
|
||||
$time = -microtime(TRUE);
|
||||
dibi::notify($this, 'beforeQuery', $sql);
|
||||
|
||||
$res = $this->driver->query($sql);
|
||||
$res = $res ? new DibiResult(clone $this->driver) : TRUE; // backward compatibility - will be changed to NULL
|
||||
|
||||
dibi::notify('afterQuery', $this, $res);
|
||||
$time += microtime(TRUE);
|
||||
dibi::$elapsedTime = $time;
|
||||
dibi::$totalTime += $time;
|
||||
dibi::notify($this, 'afterQuery', $res);
|
||||
|
||||
return $res;
|
||||
}
|
||||
@ -280,7 +287,7 @@ class DibiConnection extends NObject
|
||||
{
|
||||
if (!$this->connected) $this->connect();
|
||||
$this->driver->begin();
|
||||
dibi::notify('begin', $this);
|
||||
dibi::notify($this, 'begin');
|
||||
}
|
||||
|
||||
|
||||
@ -293,7 +300,7 @@ class DibiConnection extends NObject
|
||||
{
|
||||
if (!$this->connected) $this->connect();
|
||||
$this->driver->commit();
|
||||
dibi::notify('commit', $this);
|
||||
dibi::notify($this, 'commit');
|
||||
}
|
||||
|
||||
|
||||
@ -306,7 +313,7 @@ class DibiConnection extends NObject
|
||||
{
|
||||
if (!$this->connected) $this->connect();
|
||||
$this->driver->rollback();
|
||||
dibi::notify('rollback', $this);
|
||||
dibi::notify($this, 'rollback');
|
||||
}
|
||||
|
||||
|
||||
|
@ -209,5 +209,5 @@ interface DibiVariableInterface
|
||||
* @param string optional modifier
|
||||
* @return string SQL code
|
||||
*/
|
||||
public function toSQL($driver, $modifier = NULL);
|
||||
public function toSql(DibiDriverInterface $driver, $modifier);
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class DibiDatabaseException extends DibiException
|
||||
{
|
||||
parent::__construct($message);
|
||||
$this->sql = $sql;
|
||||
dibi::notify('exception', NULL, $this);
|
||||
dibi::notify(NULL, 'exception', $this);
|
||||
}
|
||||
|
||||
|
||||
|
@ -53,17 +53,17 @@ final class DibiLogger extends NObject
|
||||
/**
|
||||
* Event handler (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback)
|
||||
*
|
||||
* @param DibiConnection
|
||||
* @param string event name
|
||||
* @param mixed
|
||||
* @param mixed
|
||||
* @return void
|
||||
*/
|
||||
public function handler($event, $connection, $arg)
|
||||
public function handler($connection, $event, $arg)
|
||||
{
|
||||
if ($event === 'afterQuery' && $this->logQueries) {
|
||||
$this->write(
|
||||
"OK: " . dibi::$sql
|
||||
. ($arg instanceof DibiResult ? ";\n-- rows: " . $arg->rowCount() : '')
|
||||
. ($arg instanceof DibiResult ? ";\n-- rows: " . count($arg) : '')
|
||||
. "\n-- takes: " . sprintf('%0.3f', dibi::$elapsedTime * 1000) . ' ms'
|
||||
. "\n-- driver: " . $connection->getConfig('driver')
|
||||
. "\n-- " . date('Y-m-d H:i:s')
|
||||
|
@ -312,7 +312,7 @@ final class DibiTranslator extends NObject
|
||||
return 'NULL';
|
||||
|
||||
if ($value instanceof DibiVariableInterface)
|
||||
return $value->toSql($this->driver);
|
||||
return $value->toSql($this->driver, NULL);
|
||||
|
||||
$this->hasError = TRUE;
|
||||
return '**Unexpected ' . gettype($value) . '**';
|
||||
|
@ -44,7 +44,7 @@ class MyDateTime implements DibiVariableInterface
|
||||
* @param string optional modifier
|
||||
* @return string
|
||||
*/
|
||||
public function toSQL($driver, $modifier = NULL)
|
||||
public function toSql(DibiDriverInterface $driver, $modifier)
|
||||
{
|
||||
return $driver->format($this->time, dibi::FIELD_DATETIME); // format according to driver's spec.
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user