diff --git a/dibi/dibi.php b/dibi/dibi.php index ddb1507f..5a61df90 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -147,6 +147,11 @@ class dibi extends NClass */ 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); } diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 2c8f83a8..32dd9eee 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -246,14 +246,12 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) { - if (!mssql_data_seek($this->resultset, $row)) { - throw new DibiDatabaseException('Unable to seek to row ' . $row); - } + return mssql_data_seek($this->resultset, $row); } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index c9fbd9ed..a88b2959 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -290,7 +290,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) @@ -299,9 +299,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface throw new DibiDatabaseException('Cannot seek an unbuffered result set'); } - if (!mysql_data_seek($this->resultset, $row)) { - throw new DibiDatabaseException('Unable to seek to row ' . $row); - } + return mysql_data_seek($this->resultset, $row); } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 43916725..4b294984 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -43,14 +43,14 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface /** * Connection resource - * @var resource + * @var mysqli */ private $connection; /** * Resultset resource - * @var resource + * @var mysqli_result */ private $resultset; @@ -275,7 +275,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) @@ -283,9 +283,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface if (!$this->buffered) { throw new DibiDatabaseException('Cannot seek an unbuffered result set'); } - if (!mysqli_data_seek($this->resultset, $row)) { - throw new DibiDatabaseException('Unable to seek to row ' . $row); - } + return mysqli_data_seek($this->resultset, $row); } @@ -356,7 +354,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface /** * Returns the connection resource * - * @return mixed + * @return mysqli */ public function getResource() { @@ -368,7 +366,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface /** * Returns the resultset resource * - * @return mixed + * @return mysqli_result */ public function getResultResource() { diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index bef822b4..2ae30032 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -259,12 +259,13 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) { $this->row = $row; + return TRUE; } diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index c10fae5f..4c10bec1 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -253,7 +253,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index e9f869ab..0b288b1b 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -37,14 +37,14 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface /** * Connection resource - * @var resource + * @var PDO */ private $connection; /** * Resultset resource - * @var resource + * @var PDOStatement */ private $resultset; @@ -220,7 +220,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) @@ -261,7 +261,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface /** * Returns the connection resource * - * @return mixed + * @return PDO */ public function getResource() { @@ -273,7 +273,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface /** * Returns the resultset resource * - * @return mixed + * @return PDOStatement */ public function getResultResource() { diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 6b6366c2..94f15844 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -258,14 +258,12 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) { - if (!pg_result_seek($this->resultset, $row)) { - throw new DibiDatabaseException('Unable to seek to row ' . $row); - } + return pg_result_seek($this->resultset, $row); } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 42e75f15..630eb9cf 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -248,7 +248,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface * Moves cursor position without fetching row * * @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 */ public function seek($row) @@ -256,9 +256,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface if (!$this->buffered) { throw new DibiDatabaseException('Cannot seek an unbuffered result set'); } - if (!sqlite_seek($this->resultset, $row)) { - throw new DibiDatabaseException('Unable to seek to row ' . $row); - } + return sqlite_seek($this->resultset, $row); } diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index cbf119d6..bc7587f3 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -104,9 +104,11 @@ class DibiConnection extends NObject */ final protected function connect() { - $this->driver->connect($this->config); - $this->connected = TRUE; - dibi::notify($this, 'connected'); + if (!$this->connected) { + $this->driver->connect($this->config); + $this->connected = TRUE; + dibi::notify($this, 'connected'); + } } @@ -232,7 +234,7 @@ class DibiConnection extends NObject */ final public function nativeQuery($sql) { - if (!$this->connected) $this->connect(); + $this->connect(); dibi::$numOfQueries++; dibi::$sql = $sql; @@ -285,7 +287,7 @@ class DibiConnection extends NObject */ public function begin() { - if (!$this->connected) $this->connect(); + $this->connect(); $this->driver->begin(); dibi::notify($this, 'begin'); } @@ -298,7 +300,7 @@ class DibiConnection extends NObject */ public function commit() { - if (!$this->connected) $this->connect(); + $this->connect(); $this->driver->commit(); dibi::notify($this, 'commit'); } @@ -311,7 +313,7 @@ class DibiConnection extends NObject */ public function rollback() { - if (!$this->connected) $this->connect(); + $this->connect(); $this->driver->rollback(); dibi::notify($this, 'rollback'); } diff --git a/dibi/libs/DibiDriverInterface.php b/dibi/libs/DibiDriverInterface.php index 39cca6ab..a717138a 100644 --- a/dibi/libs/DibiDriverInterface.php +++ b/dibi/libs/DibiDriverInterface.php @@ -138,8 +138,8 @@ interface DibiDriverInterface /** * Moves cursor position without fetching row * - * @param int the 0-based cursor pos to seek to - * @return void + * @param int the 0-based cursor pos to seek to + * @return boolean TRUE on success, FALSE if unable to seek to specified record * @throws DibiException */ function seek($row); diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index f06fc774..2a119b99 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -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(); + if (ini_get('html_errors')) { + $errstr = strip_tags($errstr); + } throw new self($errstr, $errno); } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 52997e22..62c7566a 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -93,7 +93,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable */ public function __destruct() { - @$this->driver->free(); + @$this->free(); } @@ -105,7 +105,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable */ 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 * * @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 */ final public function seek($row) { 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() { - return $this->driver->rowCount(); + return $this->getDriver()->rowCount(); } @@ -145,7 +145,10 @@ class DibiResult extends NObject implements IteratorAggregate, Countable */ 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() { - $row = $this->driver->fetch(); + $row = $this->getDriver()->fetch(); if (!is_array($row)) return FALSE; $this->fetched = TRUE; @@ -183,7 +186,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable */ final function fetchSingle() { - $row = $this->driver->fetch(); + $row = $this->getDriver()->fetch(); if (!is_array($row)) return FALSE; $this->fetched = TRUE; @@ -434,7 +437,7 @@ class DibiResult extends NObject implements IteratorAggregate, Countable final protected function buildMeta() { if ($this->meta === NULL) { - $this->meta = $this->driver->buildMeta(); + $this->meta = $this->getDriver()->buildMeta(); foreach ($this->meta as $name => $info) { $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; + } + + } diff --git a/dibi/libs/DibiResultIterator.php b/dibi/libs/DibiResultIterator.php index fe233ee8..6d987344 100644 --- a/dibi/libs/DibiResultIterator.php +++ b/dibi/libs/DibiResultIterator.php @@ -45,12 +45,20 @@ */ final class DibiResultIterator implements Iterator { - private - $result, - $offset, - $limit, - $row, - $pointer; + /** @var DibiResult */ + private $result; + + /** @var int */ + private $offset; + + /** @var int */ + private $limit; + + /** @var int */ + private $row; + + /** @var int */ + private $pointer; /**