1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-17 04:21:17 +02:00

* update DibiVariableInterface

* some bugs fixed
This commit is contained in:
David Grudl
2007-11-12 07:33:23 +00:00
parent 89c53395c1
commit 46850aa588
15 changed files with 53 additions and 53 deletions

View File

@@ -132,11 +132,6 @@ class dibi extends NClass
*/ */
public static $defaultDriver = 'mysql'; 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) * Event notification (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback)
* *
* @param string event name
* @param DibiConnection * @param DibiConnection
* @param string event name
* @param mixed * @param mixed
* @return void * @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) { foreach (self::$handlers as $handler) {
call_user_func($handler, $event, $conn, $arg); call_user_func($handler, $connection, $event, $arg);
} }
} }

View File

@@ -54,8 +54,8 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -180,6 +180,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? -1 : 0;
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value); 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); 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) public function seek($row)
{ {
if (!mssql_data_seek($this->resultset, $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);
} }
} }

View File

@@ -65,8 +65,8 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -224,6 +224,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? 1 : 0;
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value); 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); 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'); throw new BadMethodCallException(__METHOD__ . ' is not allowed for unbuffered queries');
} }
if (!mysql_data_seek($this->resultset, $row)) { 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);
} }
} }

View File

@@ -65,8 +65,8 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -208,6 +208,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? 1 : 0;
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value); 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); 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'); throw new BadMethodCallException(__METHOD__ . ' is not allowed for unbuffered queries');
} }
if (!mysqli_data_seek($this->resultset, $row)) { 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);
} }
} }

View File

@@ -60,8 +60,8 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -195,6 +195,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? -1 : 0;
if ($type === dibi::FIELD_DATE) return date("#m/d/Y#", $value); 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); 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');
} }

View File

@@ -59,8 +59,8 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -193,6 +193,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? 1 : 0;
if ($type === dibi::FIELD_DATE) return date("U", $value); if ($type === dibi::FIELD_DATE) return date("U", $value);
if ($type === dibi::FIELD_DATETIME) 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');
} }

View File

@@ -59,8 +59,8 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -168,6 +168,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? 1 : 0;
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value); 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); 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');
} }

View File

@@ -52,8 +52,8 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -194,6 +194,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? 'TRUE' : 'FALSE';
if ($type === dibi::FIELD_DATE) return date("'Y-m-d'", $value); 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); 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) public function seek($row)
{ {
if (!pg_result_seek($this->resultset, $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);
} }
} }

View File

@@ -58,8 +58,8 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
/** /**
* Connects to a database * Connects to a database
* *
* @throws DibiException
* @return void * @return void
* @throws DibiException
*/ */
public function connect(array &$config) public function connect(array &$config)
{ {
@@ -185,6 +185,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
* @param string value * @param string value
* @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER) * @param string type (dibi::FIELD_TEXT, dibi::FIELD_BOOL, dibi::FIELD_DATE, dibi::FIELD_DATETIME, dibi::IDENTIFIER)
* @return string formatted value * @return string formatted value
* @throws InvalidArgumentException
*/ */
public function format($value, $type) 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_BOOL) return $value ? 1 : 0;
if ($type === dibi::FIELD_DATE) return date("U", $value); if ($type === dibi::FIELD_DATE) return date("U", $value);
if ($type === dibi::FIELD_DATETIME) 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() public function free()
{ {
$this->resultset = NULL;
} }

View File

@@ -106,7 +106,7 @@ class DibiConnection extends NObject
{ {
$this->driver->connect($this->config); $this->driver->connect($this->config);
$this->connected = TRUE; $this->connected = TRUE;
dibi::notify('connected'); dibi::notify($this, 'connected');
} }
@@ -121,7 +121,7 @@ class DibiConnection extends NObject
if ($this->connected) { if ($this->connected) {
$this->driver->disconnect(); $this->driver->disconnect();
$this->connected = FALSE; $this->connected = FALSE;
dibi::notify('disconnected'); dibi::notify($this, 'disconnected');
} }
} }
@@ -234,12 +234,19 @@ class DibiConnection extends NObject
{ {
if (!$this->connected) $this->connect(); 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 = $this->driver->query($sql);
$res = $res ? new DibiResult(clone $this->driver) : TRUE; // backward compatibility - will be changed to NULL $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; return $res;
} }
@@ -280,7 +287,7 @@ class DibiConnection extends NObject
{ {
if (!$this->connected) $this->connect(); if (!$this->connected) $this->connect();
$this->driver->begin(); $this->driver->begin();
dibi::notify('begin', $this); dibi::notify($this, 'begin');
} }
@@ -293,7 +300,7 @@ class DibiConnection extends NObject
{ {
if (!$this->connected) $this->connect(); if (!$this->connected) $this->connect();
$this->driver->commit(); $this->driver->commit();
dibi::notify('commit', $this); dibi::notify($this, 'commit');
} }
@@ -306,7 +313,7 @@ class DibiConnection extends NObject
{ {
if (!$this->connected) $this->connect(); if (!$this->connected) $this->connect();
$this->driver->rollback(); $this->driver->rollback();
dibi::notify('rollback', $this); dibi::notify($this, 'rollback');
} }

View File

@@ -209,5 +209,5 @@ interface DibiVariableInterface
* @param string optional modifier * @param string optional modifier
* @return string SQL code * @return string SQL code
*/ */
public function toSQL($driver, $modifier = NULL); public function toSql(DibiDriverInterface $driver, $modifier);
} }

View File

@@ -53,7 +53,7 @@ class DibiDatabaseException extends DibiException
{ {
parent::__construct($message); parent::__construct($message);
$this->sql = $sql; $this->sql = $sql;
dibi::notify('exception', NULL, $this); dibi::notify(NULL, 'exception', $this);
} }

View File

@@ -53,17 +53,17 @@ final class DibiLogger extends NObject
/** /**
* Event handler (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback) * Event handler (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback)
* *
* @param DibiConnection
* @param string event name * @param string event name
* @param mixed * @param mixed
* @param mixed
* @return void * @return void
*/ */
public function handler($event, $connection, $arg) public function handler($connection, $event, $arg)
{ {
if ($event === 'afterQuery' && $this->logQueries) { if ($event === 'afterQuery' && $this->logQueries) {
$this->write( $this->write(
"OK: " . dibi::$sql "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-- takes: " . sprintf('%0.3f', dibi::$elapsedTime * 1000) . ' ms'
. "\n-- driver: " . $connection->getConfig('driver') . "\n-- driver: " . $connection->getConfig('driver')
. "\n-- " . date('Y-m-d H:i:s') . "\n-- " . date('Y-m-d H:i:s')

View File

@@ -312,7 +312,7 @@ final class DibiTranslator extends NObject
return 'NULL'; return 'NULL';
if ($value instanceof DibiVariableInterface) if ($value instanceof DibiVariableInterface)
return $value->toSql($this->driver); return $value->toSql($this->driver, NULL);
$this->hasError = TRUE; $this->hasError = TRUE;
return '**Unexpected ' . gettype($value) . '**'; return '**Unexpected ' . gettype($value) . '**';

View File

@@ -44,7 +44,7 @@ class MyDateTime implements DibiVariableInterface
* @param string optional modifier * @param string optional modifier
* @return string * @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. return $driver->format($this->time, dibi::FIELD_DATETIME); // format according to driver's spec.
} }