diff --git a/dibi/dibi.php b/dibi/dibi.php index aa1043d..052e68b 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -199,7 +199,7 @@ class dibi */ public static function isConnected() { - return (bool) self::$connection; + return (self::$connection !== NULL) && self::$connection->isConnected(); } @@ -214,7 +214,7 @@ class dibi public static function getConnection($name = NULL) { if ($name === NULL) { - if (!self::$connection) { + if (self::$connection === NULL) { throw new DibiException('Dibi is not connected to database.'); } diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 7180f90..9de24c1 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -77,16 +77,16 @@ class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver DibiConnection::alias($config, 'host'); if (empty($config['persistent'])) { - $this->connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE); + $this->connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE); // intentionally @ } else { - $this->connection = @mssql_pconnect($config['host'], $config['username'], $config['password']); + $this->connection = @mssql_pconnect($config['host'], $config['username'], $config['password']); // intentionally @ } if (!is_resource($this->connection)) { throw new DibiDriverException("Can't connect to DB."); } - if (isset($config['database']) && !@mssql_select_db($config['database'], $this->connection)) { + if (isset($config['database']) && !@mssql_select_db($config['database'], $this->connection)) { // intentionally @ throw new DibiDriverException("Can't select DB '$config[database]'."); } } @@ -114,7 +114,7 @@ class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver */ public function query($sql) { - $this->resultset = @mssql_query($sql, $this->connection); + $this->resultset = @mssql_query($sql, $this->connection); // intentionally @ if ($this->resultset === FALSE) { throw new DibiDriverException('Query error', 0, $sql); diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index dfdb677..9e82b10 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -110,9 +110,9 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver } if (empty($config['persistent'])) { - $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']); + $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']); // intentionally @ } else { - $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']); + $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']); // intentionally @ } if (!is_resource($this->connection)) { @@ -123,18 +123,18 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver $ok = FALSE; if (function_exists('mysql_set_charset')) { // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.2.3) - $ok = @mysql_set_charset($config['charset'], $this->connection); + $ok = @mysql_set_charset($config['charset'], $this->connection); // intentionally @ } - if (!$ok) $ok = @mysql_query("SET NAMES '$config[charset]'", $this->connection); + if (!$ok) $ok = @mysql_query("SET NAMES '$config[charset]'", $this->connection); // intentionally @ if (!$ok) $this->throwException(); } if (isset($config['database'])) { - @mysql_select_db($config['database'], $this->connection) or $this->throwException(); + @mysql_select_db($config['database'], $this->connection) or $this->throwException(); // intentionally @ } if (isset($config['sqlmode'])) { - if (!@mysql_query("SET sql_mode='$config[sqlmode]'", $this->connection)) $this->throwException(); + if (!@mysql_query("SET sql_mode='$config[sqlmode]'", $this->connection)) $this->throwException(); // intentionally @ } $this->buffered = empty($config['unbuffered']); @@ -164,9 +164,9 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver public function query($sql) { if ($this->buffered) { - $this->resultset = @mysql_query($sql, $this->connection); + $this->resultset = @mysql_query($sql, $this->connection); // intentionally @ } else { - $this->resultset = @mysql_unbuffered_query($sql, $this->connection); + $this->resultset = @mysql_unbuffered_query($sql, $this->connection); // intentionally @ } if (mysql_errno($this->connection)) { diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 3cbaa34..bfbdd66 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -101,7 +101,7 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver } $this->connection = mysqli_init(); - @mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['options']); + @mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['options']); // intentionally @ if ($errno = mysqli_connect_errno()) { throw new DibiDriverException(mysqli_connect_error(), $errno); @@ -111,14 +111,14 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver $ok = FALSE; if (version_compare(PHP_VERSION , '5.1.5', '>=')) { // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5) - $ok = @mysqli_set_charset($this->connection, $config['charset']); + $ok = @mysqli_set_charset($this->connection, $config['charset']); // intentionally @ } - if (!$ok) $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); + if (!$ok) $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @ if (!$ok) $this->throwException(); } if (isset($config['sqlmode'])) { - if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) $this->throwException(); + if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) $this->throwException(); // intentionally @ } $this->buffered = empty($config['unbuffered']); @@ -147,7 +147,7 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver */ public function query($sql) { - $this->resultset = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); + $this->resultset = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @ if (mysqli_errno($this->connection)) { $this->throwException($sql); diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 1e90ab8..87d46b1 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -87,9 +87,9 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db'); if (empty($config['persistent'])) { - $this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); + $this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); // intentionally @ } else { - $this->connection = @odbc_pconnect($config['dsn'], $config['username'], $config['password']); + $this->connection = @odbc_pconnect($config['dsn'], $config['username'], $config['password']); // intentionally @ } if (!is_resource($this->connection)) { @@ -120,7 +120,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver */ public function query($sql) { - $this->resultset = @odbc_exec($this->connection, $sql); + $this->resultset = @odbc_exec($this->connection, $sql); // intentionally @ if ($this->resultset === FALSE) { $this->throwException($sql); diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index b21c3a9..6292a39 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -82,7 +82,7 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver DibiConnection::alias($config, 'database', 'db'); DibiConnection::alias($config, 'charset'); - $this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); + $this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @ if (!$this->connection) { $err = oci_error(); diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 7133bea..3acddd0 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -316,7 +316,7 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver $meta = array(); for ($i = 0; $i < $count; $i++) { // items 'name' and 'table' are required - $info = @$this->resultset->getColumnsMeta($i); + $info = @$this->resultset->getColumnsMeta($i); // intentionally @ if ($info === FALSE) { throw new DibiDriverException('Driver does not support meta data.'); } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 9181ea9..4f6fcb4 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -58,13 +58,6 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver private $escMethod = FALSE; - /** - * Affected rows. - * @var int|FALSE - */ - private $affectedRows = FALSE; - - /** * @throws DibiException @@ -148,16 +141,13 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver */ public function query($sql) { - $this->resultset = @pg_query($this->connection, $sql); + $this->resultset = @pg_query($this->connection, $sql); // intentionally @ if ($this->resultset === FALSE) { - $this->affectedRows = FALSE; throw new DibiDriverException(pg_last_error($this->connection), 0, $sql); } - $this->affectedRows = pg_affected_rows($this->resultset); // retrieve immediately due PHP bug - - return is_resource($this->resultset); + return is_resource($this->resultset) && pg_num_fields($this->resultset); } @@ -169,7 +159,7 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver */ public function affectedRows() { - return $this->affectedRows; + return pg_affected_rows($this->resultset); } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 75ce413..ec35519 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -92,9 +92,9 @@ class DibiSqliteDriver extends /*Nette::*/Object implements IDibiDriver $errorMsg = ''; if (empty($config['persistent'])) { - $this->connection = @sqlite_open($config['database'], 0666, $errorMsg); + $this->connection = @sqlite_open($config['database'], 0666, $errorMsg); // intentionally @ } else { - $this->connection = @sqlite_popen($config['database'], 0666, $errorMsg); + $this->connection = @sqlite_popen($config['database'], 0666, $errorMsg); // intentionally @ } if (!$this->connection) { diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 8c2d86d..1982be1 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -90,7 +90,7 @@ class DibiConnection extends /*Nette::*/Object } if (isset($config['result:objects'])) { - // normalize + // normalize $val = $config['result:objects']; $config['result:objects'] = is_string($val) && !is_numeric($val) ? $val : (bool) $val; } @@ -154,6 +154,18 @@ class DibiConnection extends /*Nette::*/Object + /** + * Returns TRUE when connection was established. + * + * @return bool + */ + final public function isConnected() + { + return $this->connected; + } + + + /** * Returns configuration variable. If no $key is passed, returns the entire array. * @@ -411,9 +423,9 @@ class DibiConnection extends /*Nette::*/Object { $this->connect(); - @set_time_limit(0); + @set_time_limit(0); // intentionally @ - $handle = @fopen($file, 'r'); + $handle = @fopen($file, 'r'); // intentionally @ if (!$handle) { throw new FileNotFoundException("Cannot open file '$file'."); } diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 002f4b0..6d559ae 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -117,7 +117,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource */ public function __destruct() { - @$this->free(); + @$this->free(); // intentionally @ } diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index fc009c2..471e7c8 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -131,9 +131,9 @@ final class DibiTranslator extends /*Nette::*/Object )/xs', */ // note: this can change $this->args & $this->cursor & ... . preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s', - array($this, 'cb'), - substr($arg, $toSkip) - ); + array($this, 'cb'), + substr($arg, $toSkip) + ); } continue; @@ -302,9 +302,9 @@ final class DibiTranslator extends /*Nette::*/Object } else { return substr($value, 0, $toSkip) . preg_replace_callback('/(?=`|\[|\'|")(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"(\'|"))/s', - array($this, 'cb'), - substr($value, $toSkip) - ); + array($this, 'cb'), + substr($value, $toSkip) + ); } case 'a': diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index 2034e25..bbf5894 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -85,6 +85,15 @@ interface IDibiDriver + /** + * Returns TRUE when connection was established. + * + * @return bool + */ + function isConnected(); + + + /** * Internal: Executes the SQL query. *