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

- removed DibiPostgreDriver workaround (there is no bug in pg_affected_rows)

- added IDibiConnection::isConnected()
This commit is contained in:
David Grudl
2008-05-16 17:56:24 +00:00
parent b27b0193f1
commit a8acce6d59
13 changed files with 60 additions and 49 deletions

View File

@@ -199,7 +199,7 @@ class dibi
*/ */
public static function isConnected() 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) public static function getConnection($name = NULL)
{ {
if ($name === NULL) { if ($name === NULL) {
if (!self::$connection) { if (self::$connection === NULL) {
throw new DibiException('Dibi is not connected to database.'); throw new DibiException('Dibi is not connected to database.');
} }

View File

@@ -77,16 +77,16 @@ class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver
DibiConnection::alias($config, 'host'); DibiConnection::alias($config, 'host');
if (empty($config['persistent'])) { 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 { } 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)) { if (!is_resource($this->connection)) {
throw new DibiDriverException("Can't connect to DB."); 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]'."); throw new DibiDriverException("Can't select DB '$config[database]'.");
} }
} }
@@ -114,7 +114,7 @@ class DibiMsSqlDriver extends /*Nette::*/Object implements IDibiDriver
*/ */
public function query($sql) public function query($sql)
{ {
$this->resultset = @mssql_query($sql, $this->connection); $this->resultset = @mssql_query($sql, $this->connection); // intentionally @
if ($this->resultset === FALSE) { if ($this->resultset === FALSE) {
throw new DibiDriverException('Query error', 0, $sql); throw new DibiDriverException('Query error', 0, $sql);

View File

@@ -110,9 +110,9 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
} }
if (empty($config['persistent'])) { 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 { } 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)) { if (!is_resource($this->connection)) {
@@ -123,18 +123,18 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
$ok = FALSE; $ok = FALSE;
if (function_exists('mysql_set_charset')) { 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) // 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 (!$ok) $this->throwException();
} }
if (isset($config['database'])) { 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 (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']); $this->buffered = empty($config['unbuffered']);
@@ -164,9 +164,9 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
public function query($sql) public function query($sql)
{ {
if ($this->buffered) { if ($this->buffered) {
$this->resultset = @mysql_query($sql, $this->connection); $this->resultset = @mysql_query($sql, $this->connection); // intentionally @
} else { } else {
$this->resultset = @mysql_unbuffered_query($sql, $this->connection); $this->resultset = @mysql_unbuffered_query($sql, $this->connection); // intentionally @
} }
if (mysql_errno($this->connection)) { if (mysql_errno($this->connection)) {

View File

@@ -101,7 +101,7 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
} }
$this->connection = mysqli_init(); $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()) { if ($errno = mysqli_connect_errno()) {
throw new DibiDriverException(mysqli_connect_error(), $errno); throw new DibiDriverException(mysqli_connect_error(), $errno);
@@ -111,14 +111,14 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
$ok = FALSE; $ok = FALSE;
if (version_compare(PHP_VERSION , '5.1.5', '>=')) { 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) // 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 (!$ok) $this->throwException();
} }
if (isset($config['sqlmode'])) { 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']); $this->buffered = empty($config['unbuffered']);
@@ -147,7 +147,7 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
*/ */
public function query($sql) 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)) { if (mysqli_errno($this->connection)) {
$this->throwException($sql); $this->throwException($sql);

View File

@@ -87,9 +87,9 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db'); if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db');
if (empty($config['persistent'])) { 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 { } 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)) { if (!is_resource($this->connection)) {
@@ -120,7 +120,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
*/ */
public function query($sql) public function query($sql)
{ {
$this->resultset = @odbc_exec($this->connection, $sql); $this->resultset = @odbc_exec($this->connection, $sql); // intentionally @
if ($this->resultset === FALSE) { if ($this->resultset === FALSE) {
$this->throwException($sql); $this->throwException($sql);

View File

@@ -82,7 +82,7 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
DibiConnection::alias($config, 'database', 'db'); DibiConnection::alias($config, 'database', 'db');
DibiConnection::alias($config, 'charset'); 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) { if (!$this->connection) {
$err = oci_error(); $err = oci_error();

View File

@@ -316,7 +316,7 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
$meta = array(); $meta = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
// items 'name' and 'table' are required // items 'name' and 'table' are required
$info = @$this->resultset->getColumnsMeta($i); $info = @$this->resultset->getColumnsMeta($i); // intentionally @
if ($info === FALSE) { if ($info === FALSE) {
throw new DibiDriverException('Driver does not support meta data.'); throw new DibiDriverException('Driver does not support meta data.');
} }

View File

@@ -58,13 +58,6 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
private $escMethod = FALSE; private $escMethod = FALSE;
/**
* Affected rows.
* @var int|FALSE
*/
private $affectedRows = FALSE;
/** /**
* @throws DibiException * @throws DibiException
@@ -148,16 +141,13 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
*/ */
public function query($sql) public function query($sql)
{ {
$this->resultset = @pg_query($this->connection, $sql); $this->resultset = @pg_query($this->connection, $sql); // intentionally @
if ($this->resultset === FALSE) { if ($this->resultset === FALSE) {
$this->affectedRows = FALSE;
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql); 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) && pg_num_fields($this->resultset);
return is_resource($this->resultset);
} }
@@ -169,7 +159,7 @@ class DibiPostgreDriver extends /*Nette::*/Object implements IDibiDriver
*/ */
public function affectedRows() public function affectedRows()
{ {
return $this->affectedRows; return pg_affected_rows($this->resultset);
} }

View File

@@ -92,9 +92,9 @@ class DibiSqliteDriver extends /*Nette::*/Object implements IDibiDriver
$errorMsg = ''; $errorMsg = '';
if (empty($config['persistent'])) { if (empty($config['persistent'])) {
$this->connection = @sqlite_open($config['database'], 0666, $errorMsg); $this->connection = @sqlite_open($config['database'], 0666, $errorMsg); // intentionally @
} else { } else {
$this->connection = @sqlite_popen($config['database'], 0666, $errorMsg); $this->connection = @sqlite_popen($config['database'], 0666, $errorMsg); // intentionally @
} }
if (!$this->connection) { if (!$this->connection) {

View File

@@ -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. * Returns configuration variable. If no $key is passed, returns the entire array.
* *
@@ -411,9 +423,9 @@ class DibiConnection extends /*Nette::*/Object
{ {
$this->connect(); $this->connect();
@set_time_limit(0); @set_time_limit(0); // intentionally @
$handle = @fopen($file, 'r'); $handle = @fopen($file, 'r'); // intentionally @
if (!$handle) { if (!$handle) {
throw new FileNotFoundException("Cannot open file '$file'."); throw new FileNotFoundException("Cannot open file '$file'.");
} }

View File

@@ -117,7 +117,7 @@ class DibiResult extends /*Nette::*/Object implements IDataSource
*/ */
public function __destruct() public function __destruct()
{ {
@$this->free(); @$this->free(); // intentionally @
} }

View File

@@ -85,6 +85,15 @@ interface IDibiDriver
/**
* Returns TRUE when connection was established.
*
* @return bool
*/
function isConnected();
/** /**
* Internal: Executes the SQL query. * Internal: Executes the SQL query.
* *