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

* DibiConnection::getConfig('name') returns name of connection

* seek() returns boolean again
* DibiDriver throws exception when is used after free()
This commit is contained in:
David Grudl
2007-11-13 01:51:44 +00:00
parent 40444c1341
commit 0eeff53fe8
14 changed files with 85 additions and 56 deletions

View File

@@ -147,6 +147,11 @@ class dibi extends NClass
*/ */
public static function connect($config = array(), $name = 0) public static function connect($config = array(), $name = 0)
{ {
if (is_array($config)) {
$config['name'] = $name;
} else {
$config .= '&name=' . urlencode($name);
}
return self::$connection = self::$registry[$name] = new DibiConnection($config); return self::$connection = self::$registry[$name] = new DibiConnection($config);
} }

View File

@@ -246,14 +246,12 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
{ {
if (!mssql_data_seek($this->resultset, $row)) { return mssql_data_seek($this->resultset, $row);
throw new DibiDatabaseException('Unable to seek to row ' . $row);
}
} }

View File

@@ -290,7 +290,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
@@ -299,9 +299,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
throw new DibiDatabaseException('Cannot seek an unbuffered result set'); throw new DibiDatabaseException('Cannot seek an unbuffered result set');
} }
if (!mysql_data_seek($this->resultset, $row)) { return mysql_data_seek($this->resultset, $row);
throw new DibiDatabaseException('Unable to seek to row ' . $row);
}
} }

View File

@@ -43,14 +43,14 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/** /**
* Connection resource * Connection resource
* @var resource * @var mysqli
*/ */
private $connection; private $connection;
/** /**
* Resultset resource * Resultset resource
* @var resource * @var mysqli_result
*/ */
private $resultset; private $resultset;
@@ -275,7 +275,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
@@ -283,9 +283,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
if (!$this->buffered) { if (!$this->buffered) {
throw new DibiDatabaseException('Cannot seek an unbuffered result set'); throw new DibiDatabaseException('Cannot seek an unbuffered result set');
} }
if (!mysqli_data_seek($this->resultset, $row)) { return mysqli_data_seek($this->resultset, $row);
throw new DibiDatabaseException('Unable to seek to row ' . $row);
}
} }
@@ -356,7 +354,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/** /**
* Returns the connection resource * Returns the connection resource
* *
* @return mixed * @return mysqli
*/ */
public function getResource() public function getResource()
{ {
@@ -368,7 +366,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/** /**
* Returns the resultset resource * Returns the resultset resource
* *
* @return mixed * @return mysqli_result
*/ */
public function getResultResource() public function getResultResource()
{ {

View File

@@ -259,12 +259,13 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
{ {
$this->row = $row; $this->row = $row;
return TRUE;
} }

View File

@@ -253,7 +253,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)

View File

@@ -37,14 +37,14 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/** /**
* Connection resource * Connection resource
* @var resource * @var PDO
*/ */
private $connection; private $connection;
/** /**
* Resultset resource * Resultset resource
* @var resource * @var PDOStatement
*/ */
private $resultset; private $resultset;
@@ -220,7 +220,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
@@ -261,7 +261,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/** /**
* Returns the connection resource * Returns the connection resource
* *
* @return mixed * @return PDO
*/ */
public function getResource() public function getResource()
{ {
@@ -273,7 +273,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/** /**
* Returns the resultset resource * Returns the resultset resource
* *
* @return mixed * @return PDOStatement
*/ */
public function getResultResource() public function getResultResource()
{ {

View File

@@ -258,14 +258,12 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
{ {
if (!pg_result_seek($this->resultset, $row)) { return pg_result_seek($this->resultset, $row);
throw new DibiDatabaseException('Unable to seek to row ' . $row);
}
} }

View File

@@ -248,7 +248,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
public function seek($row) public function seek($row)
@@ -256,9 +256,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
if (!$this->buffered) { if (!$this->buffered) {
throw new DibiDatabaseException('Cannot seek an unbuffered result set'); throw new DibiDatabaseException('Cannot seek an unbuffered result set');
} }
if (!sqlite_seek($this->resultset, $row)) { return sqlite_seek($this->resultset, $row);
throw new DibiDatabaseException('Unable to seek to row ' . $row);
}
} }

View File

@@ -104,9 +104,11 @@ class DibiConnection extends NObject
*/ */
final protected function connect() final protected function connect()
{ {
$this->driver->connect($this->config); if (!$this->connected) {
$this->connected = TRUE; $this->driver->connect($this->config);
dibi::notify($this, 'connected'); $this->connected = TRUE;
dibi::notify($this, 'connected');
}
} }
@@ -232,7 +234,7 @@ class DibiConnection extends NObject
*/ */
final public function nativeQuery($sql) final public function nativeQuery($sql)
{ {
if (!$this->connected) $this->connect(); $this->connect();
dibi::$numOfQueries++; dibi::$numOfQueries++;
dibi::$sql = $sql; dibi::$sql = $sql;
@@ -285,7 +287,7 @@ class DibiConnection extends NObject
*/ */
public function begin() public function begin()
{ {
if (!$this->connected) $this->connect(); $this->connect();
$this->driver->begin(); $this->driver->begin();
dibi::notify($this, 'begin'); dibi::notify($this, 'begin');
} }
@@ -298,7 +300,7 @@ class DibiConnection extends NObject
*/ */
public function commit() public function commit()
{ {
if (!$this->connected) $this->connect(); $this->connect();
$this->driver->commit(); $this->driver->commit();
dibi::notify($this, 'commit'); dibi::notify($this, 'commit');
} }
@@ -311,7 +313,7 @@ class DibiConnection extends NObject
*/ */
public function rollback() public function rollback()
{ {
if (!$this->connected) $this->connect(); $this->connect();
$this->driver->rollback(); $this->driver->rollback();
dibi::notify($this, 'rollback'); dibi::notify($this, 'rollback');
} }

View File

@@ -138,8 +138,8 @@ interface DibiDriverInterface
/** /**
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
function seek($row); function seek($row);

View File

@@ -72,9 +72,12 @@ class DibiDatabaseException extends DibiException
public static function _catchErrorHandler($errno, $errstr) public static function _catchErrorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{ {
self::restore(); self::restore();
if (ini_get('html_errors')) {
$errstr = strip_tags($errstr);
}
throw new self($errstr, $errno); throw new self($errstr, $errno);
} }

View File

@@ -93,7 +93,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/ */
public function __destruct() public function __destruct()
{ {
@$this->driver->free(); @$this->free();
} }
@@ -105,7 +105,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/ */
final public function getResource() final public function getResource()
{ {
return $this->driver->getResultResource(); return $this->getDriver()->getResultResource();
} }
@@ -114,13 +114,13 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
* Moves cursor position without fetching row * Moves cursor position without fetching row
* *
* @param int the 0-based cursor pos to seek to * @param int the 0-based cursor pos to seek to
* @return void * @return boolean TRUE on success, FALSE if unable to seek to specified record
* @throws DibiException * @throws DibiException
*/ */
final public function seek($row) final public function seek($row)
{ {
if ($row !== 0 || $this->fetched) { if ($row !== 0 || $this->fetched) {
$this->driver->seek($row); return (bool) $this->getDriver()->seek($row);
} }
} }
@@ -133,7 +133,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/ */
final public function rowCount() final public function rowCount()
{ {
return $this->driver->rowCount(); return $this->getDriver()->rowCount();
} }
@@ -145,7 +145,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/ */
final public function free() final public function free()
{ {
$this->driver->free(); if ($this->driver !== NULL) {
$this->driver->free();
$this->driver = NULL;
}
} }
@@ -158,7 +161,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/ */
final public function fetch() final public function fetch()
{ {
$row = $this->driver->fetch(); $row = $this->getDriver()->fetch();
if (!is_array($row)) return FALSE; if (!is_array($row)) return FALSE;
$this->fetched = TRUE; $this->fetched = TRUE;
@@ -183,7 +186,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
*/ */
final function fetchSingle() final function fetchSingle()
{ {
$row = $this->driver->fetch(); $row = $this->getDriver()->fetch();
if (!is_array($row)) return FALSE; if (!is_array($row)) return FALSE;
$this->fetched = TRUE; $this->fetched = TRUE;
@@ -434,7 +437,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
final protected function buildMeta() final protected function buildMeta()
{ {
if ($this->meta === NULL) { if ($this->meta === NULL) {
$this->meta = $this->driver->buildMeta(); $this->meta = $this->getDriver()->buildMeta();
foreach ($this->meta as $name => $info) { foreach ($this->meta as $name => $info) {
$this->convert[$name] = $info['type']; $this->convert[$name] = $info['type'];
} }
@@ -495,4 +498,21 @@ class DibiResult extends NObject implements IteratorAggregate, Countable
} }
/**
* Safe access to property $driver
*
* @return DibiDriverInterface
* @throws DibiException
*/
private function getDriver()
{
if ($this->driver === NULL) {
throw new DibiException('Resultset was released from memory');
}
return $this->driver;
}
} }

View File

@@ -45,12 +45,20 @@
*/ */
final class DibiResultIterator implements Iterator final class DibiResultIterator implements Iterator
{ {
private /** @var DibiResult */
$result, private $result;
$offset,
$limit, /** @var int */
$row, private $offset;
$pointer;
/** @var int */
private $limit;
/** @var int */
private $row;
/** @var int */
private $pointer;
/** /**