From 7f995a558b495597c2e8c82b279aa5abf57fac5f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 23 Aug 2007 17:12:58 +0000 Subject: [PATCH] * fixed odbc_num_rows and pg_affected_rows --- dibi/dibi.php | 21 +++++++++++--------- dibi/drivers/mssql.php | 26 +++++++++++++++---------- dibi/drivers/mysql.php | 41 +++++++++++++++++++++++----------------- dibi/drivers/mysqli.php | 16 ++++++++++------ dibi/drivers/odbc.php | 28 +++++++++++++++++---------- dibi/drivers/pdo.php | 9 ++++++--- dibi/drivers/postgre.php | 17 +++++++++++------ dibi/drivers/sqlite.php | 20 ++++++++++++-------- dibi/libs/driver.php | 13 +++++++++---- dibi/libs/exception.php | 9 +++++++-- dibi/libs/resultset.php | 40 +++++++++++++++++++++++---------------- dibi/libs/translator.php | 34 ++++++++++++++++++++++----------- 12 files changed, 172 insertions(+), 102 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 4ee601e1..1e5278db 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -158,20 +158,23 @@ class dibi public static function connect($config, $name = 0) { // DSN string - if (is_string($config)) + if (is_string($config)) { parse_str($config, $config); + } // config['driver'] is required - if (empty($config['driver'])) + if (empty($config['driver'])) { throw new DibiException('Driver is not specified.'); + } // include dibi driver $className = "Dibi$config[driver]Driver"; if (!class_exists($className)) { include_once dirname(__FILE__) . "/drivers/$config[driver].php"; - if (!class_exists($className)) + if (!class_exists($className)) { throw new DibiException("Unable to create instance of dibi driver class '$className'."); + } } // create connection object and store in list @@ -207,14 +210,16 @@ class dibi public static function getConnection($name = NULL) { if ($name === NULL) { - if (!self::$connection) + if (!self::$connection) { throw new DibiException('Dibi is not connected to database'); + } return self::$connection; } - if (!isset(self::$registry[$name])) + if (!isset(self::$registry[$name])) { throw new DibiException("There is no connection named '$name'."); + } return self::$registry[$name]; } @@ -245,8 +250,7 @@ class dibi public static function query($args) { // receive arguments - if (!is_array($args)) - $args = func_get_args(); + if (!is_array($args)) $args = func_get_args(); return self::getConnection()->query($args); } @@ -262,8 +266,7 @@ class dibi public static function test($args) { // receive arguments - if (!is_array($args)) - $args = func_get_args(); + if (!is_array($args)) $args = func_get_args(); // and generate SQL $trans = new DibiTranslator(self::getConnection()); diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index ea92b85c..f86c6448 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -40,8 +40,9 @@ class DibiMSSqlDriver extends DibiDriver */ public function __construct($config) { - if (!extension_loaded('mssql')) + if (!extension_loaded('mssql')) { throw new DibiException("PHP extension 'mssql' is not loaded"); + } if (!isset($config['host'])) $config['host'] = NULL; if (!isset($config['username'])) $config['username'] = NULL; @@ -56,17 +57,18 @@ class DibiMSSqlDriver extends DibiDriver { $config = $this->config; - if (empty($config['persistent'])) + if (empty($config['persistent'])) { $connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE); - else + } else { $connection = @mssql_pconnect($config['host'], $config['username'], $config['password']); + } - if (!is_resource($connection)) + if (!is_resource($connection)) { throw new DibiException("Connecting error (driver mssql)'"); + } - if (!empty($config['database'])) { - if (!@mssql_select_db($config['database'], $connection)) - throw new DibiException("Connecting error (driver mssql)"); + if (!empty($config['database']) && !@mssql_select_db($config['database'], $connection)) { + throw new DibiException("Connecting error (driver mssql)"); } return $connection; @@ -85,8 +87,9 @@ class DibiMSSqlDriver extends DibiDriver $this->affectedRows = mssql_rows_affected($connection); if ($this->affectedRows < 0) $this->affectedRows = FALSE; - if (is_resource($res)) + if (is_resource($res)) { return new DibiMSSqlResult($res); + } return TRUE; } @@ -168,10 +171,13 @@ class DibiMSSqlDriver extends DibiDriver public function applyLimit(&$sql, $limit, $offset = 0) { // offset suppot is missing... - if ($limit >= 0) + if ($limit >= 0) { $sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')'; + } - if ($offset) throw new DibiException('Offset is not implemented in driver odbc'); + if ($offset) { + throw new DibiException('Offset is not implemented in driver odbc'); + } } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 5a821db7..ae5a2107 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -41,8 +41,9 @@ class DibiMySqlDriver extends DibiDriver */ public function __construct($config) { - if (!extension_loaded('mysql')) + if (!extension_loaded('mysql')) { throw new DibiException("PHP extension 'mysql' is not loaded"); + } // default values if (empty($config['username'])) $config['username'] = ini_get('mysql.default_user'); @@ -62,30 +63,36 @@ class DibiMySqlDriver extends DibiDriver { $config = $this->config; - if (isset($config['protocol']) && $config['protocol'] === 'unix') // host can be socket + if (isset($config['protocol']) && $config['protocol'] === 'unix') { // host can be socket $host = ':' . $config['host']; - else + } else { $host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']); + } // some errors aren't handled. Must use $php_errormsg - if (function_exists('ini_set')) + if (function_exists('ini_set')) { $save = ini_set('track_errors', TRUE); + } + $php_errormsg = ''; - if (empty($config['persistent'])) + if (empty($config['persistent'])) { $connection = @mysql_connect($host, $config['username'], $config['password'], TRUE); - else + } else { $connection = @mysql_pconnect($host, $config['username'], $config['password']); + } - if (function_exists('ini_set')) + if (function_exists('ini_set')) { ini_set('track_errors', $save); + } - if (!is_resource($connection)) + if (!is_resource($connection)) { throw new DibiException("Connecting error (driver mysql)'", array( 'message' => mysql_error() ? mysql_error() : $php_errormsg, 'code' => mysql_errno(), )); + } if (!empty($config['charset'])) { @@ -94,12 +101,11 @@ class DibiMySqlDriver extends DibiDriver } - if (!empty($config['database'])) { - if (!@mysql_select_db($config['database'], $connection)) - throw new DibiException("Connecting error (driver mysql)", array( - 'message' => mysql_error($connection), - 'code' => mysql_errno($connection), - )); + if (!empty($config['database']) && !@mysql_select_db($config['database'], $connection)) { + throw new DibiException("Connecting error (driver mysql)", array( + 'message' => mysql_error($connection), + 'code' => mysql_errno($connection), + )); } return $connection; @@ -121,8 +127,9 @@ class DibiMySqlDriver extends DibiDriver $this->insertId = mysql_insert_id($connection); if ($this->insertId < 1) $this->insertId = FALSE; - if (is_resource($res)) + if (is_resource($res)) { return new DibiMySqlResult($res); + } return TRUE; } @@ -309,9 +316,9 @@ class DibiMySqlResult extends DibiResult $info['length'] = mysql_field_len($this->resource, $index); $info['table'] = mysql_field_table($this->resource, $index); - if (in_array('auto_increment', $info['flags'])) // or 'primary_key' ? + if (in_array('auto_increment', $info['flags'])) { // or 'primary_key' ? $info['type'] = dibi::FIELD_COUNTER; - else { + } else { $info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN; // if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255) diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 284c8298..ee836f28 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -42,8 +42,9 @@ class DibiMySqliDriver extends DibiDriver */ public function __construct($config) { - if (!extension_loaded('mysqli')) + if (!extension_loaded('mysqli')) { throw new DibiException("PHP extension 'mysqli' is not loaded"); + } // default values if (empty($config['username'])) $config['username'] = ini_get('mysqli.default_user'); @@ -66,14 +67,16 @@ class DibiMySqliDriver extends DibiDriver $connection = @mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']); - if (!$connection) + if (!$connection) { throw new DibiException("Connecting error (driver mysqli)", array( 'message' => mysqli_connect_error(), 'code' => mysqli_connect_errno(), )); + } - if (!empty($config['charset'])) + if (!empty($config['charset'])) { mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'"); + } return $connection; } @@ -94,8 +97,9 @@ class DibiMySqliDriver extends DibiDriver $this->insertId = mysqli_insert_id($connection); if ($this->insertId < 1) $this->insertId = FALSE; - if (is_object($res)) + if (is_object($res)) { return new DibiMySqliResult($res); + } return TRUE; } @@ -278,9 +282,9 @@ class DibiMySqliResult extends DibiResult $info = (array) mysqli_fetch_field_direct($this->resource, $index); $native = $info['native'] = $info['type']; - if ($info['flags'] & MYSQLI_AUTO_INCREMENT_FLAG) // or 'primary_key' ? + if ($info['flags'] & MYSQLI_AUTO_INCREMENT_FLAG) { // or 'primary_key' ? $info['type'] = dibi::FIELD_COUNTER; - else { + } else { $info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN; // if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255) // $info['type'] = dibi::FIELD_LONG_TEXT; diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index be9c60ba..51793fb6 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -49,14 +49,17 @@ class DibiOdbcDriver extends DibiDriver if (empty($config['password'])) $config['password'] = ini_get('odbc.default_pw'); if (empty($config['database'])) $config['database'] = ini_get('odbc.default_db'); - if (empty($config['username'])) + if (empty($config['username'])) { throw new DibiException("Username must be specified (driver odbc)"); + } - if (empty($config['password'])) + if (empty($config['password'])) { throw new DibiException("Password must be specified (driver odbc)"); + } - if (empty($config['database'])) + if (empty($config['database'])) { throw new DibiException("Database must be specified (driver odbc)"); + } parent::__construct($config); } @@ -67,16 +70,18 @@ class DibiOdbcDriver extends DibiDriver { $config = $this->config; - if (empty($config['persistent'])) + if (empty($config['persistent'])) { $connection = @odbc_connect($config['database'], $config['username'], $config['password']); - else + } else { $connection = @odbc_pconnect($config['database'], $config['username'], $config['password']); + } - if (!is_resource($connection)) + if (!is_resource($connection)) { throw new DibiException("Connecting error (driver odbc)", array( 'message' => odbc_errormsg(), 'code' => odbc_error(), )); + } return $connection; } @@ -92,11 +97,12 @@ class DibiOdbcDriver extends DibiDriver if ($res === FALSE) return FALSE; - $this->affectedRows = odbc_num_rows($connection); + $this->affectedRows = odbc_num_rows($res); if ($this->affectedRows < 0) $this->affectedRows = FALSE; - if (is_resource($res)) + if (is_resource($res)) { return new DibiOdbcResult($res); + } return TRUE; } @@ -185,8 +191,9 @@ class DibiOdbcDriver extends DibiDriver public function applyLimit(&$sql, $limit, $offset = 0) { // offset suppot is missing... - if ($limit >= 0) + if ($limit >= 0) { $sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')'; + } if ($offset) throw new DibiException('Offset is not implemented in driver odbc'); } @@ -246,8 +253,9 @@ class DibiOdbcResult extends DibiResult protected function buildMeta() { // cache - if ($this->meta !== NULL) + if ($this->meta !== NULL) { return $this->meta; + } static $types = array( 'CHAR' => dibi::FIELD_TEXT, diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 95cebc56..937e7ee8 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -40,11 +40,13 @@ class DibiPdoDriver extends DibiDriver */ public function __construct($config) { - if (!extension_loaded('pdo')) + if (!extension_loaded('pdo')) { throw new DibiException("PHP extension 'pdo' is not loaded"); + } - if (empty($config['dsn'])) + if (empty($config['dsn'])) { throw new DibiException("DSN must be specified (driver odbc)"); + } if (empty($config['username'])) $config['username'] = NULL; if (empty($config['password'])) $config['password'] = NULL; @@ -70,8 +72,9 @@ class DibiPdoDriver extends DibiDriver if ($res === FALSE) return FALSE; - if ($res instanceof PDOStatement) + if ($res instanceof PDOStatement) { return new DibiPdoResult($res); + } return TRUE; } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index f21681fc..af666c30 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -41,11 +41,13 @@ class DibiPostgreDriver extends DibiDriver */ public function __construct($config) { - if (!extension_loaded('pgsql')) + if (!extension_loaded('pgsql')) { throw new DibiException("PHP extension 'pgsql' is not loaded"); + } - if (empty($config['string'])) + if (empty($config['string'])) { throw new DibiException("Connection string must be specified (driver postgre)"); + } if (empty($config['type'])) $config['type'] = NULL; @@ -58,15 +60,17 @@ class DibiPostgreDriver extends DibiDriver { $config = $this->config; - if (isset($config['persistent'])) + if (isset($config['persistent'])) { $connection = @pg_connect($config['string'], $config['type']); - else + } else { $connection = @pg_pconnect($config['string'], $config['type']); + } - if (!is_resource($connection)) + if (!is_resource($connection)) { throw new DibiException("Connecting error (driver postgre)", array( 'message' => pg_last_error(), )); + } if (!empty($config['charset'])) { @pg_set_client_encoding($connection, $config['charset']); @@ -89,8 +93,9 @@ class DibiPostgreDriver extends DibiDriver $this->affectedRows = pg_affected_rows($connection); if ($this->affectedRows < 0) $this->affectedRows = FALSE; - if (is_resource($res)) + if (is_resource($res)) { return new DibiPostgreResult($res); + } return TRUE; } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index a5ea1196..6005ec2e 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -42,14 +42,15 @@ class DibiSqliteDriver extends DibiDriver */ public function __construct($config) { - if (!extension_loaded('sqlite')) + if (!extension_loaded('sqlite')) { throw new DibiException("PHP extension 'sqlite' is not loaded"); + } - if (empty($config['database'])) + if (empty($config['database'])) { throw new DibiException("Database must be specified (driver sqlite)"); + } - if (!isset($config['mode'])) - $config['mode'] = 0666; + if (!isset($config['mode'])) $config['mode'] = 0666; parent::__construct($config); } @@ -61,15 +62,17 @@ class DibiSqliteDriver extends DibiDriver $config = $this->config; $errorMsg = ''; - if (empty($config['persistent'])) + if (empty($config['persistent'])) { $connection = @sqlite_open($config['database'], $config['mode'], $errorMsg); - else + } else { $connection = @sqlite_popen($config['database'], $config['mode'], $errorMsg); + } - if (!$connection) + if (!$connection) { throw new DibiException("Connecting error (driver sqlite)", array( 'message' => $errorMsg, )); + } return $connection; } @@ -91,8 +94,9 @@ class DibiSqliteDriver extends DibiDriver $this->insertId = sqlite_last_insert_rowid($connection); if ($this->insertId < 1) $this->insertId = FALSE; - if (is_resource($res)) + if (is_resource($res)) { return new DibiSqliteResult($res); + } return TRUE; } diff --git a/dibi/libs/driver.php b/dibi/libs/driver.php index 5cc3fd1f..e6778778 100644 --- a/dibi/libs/driver.php +++ b/dibi/libs/driver.php @@ -105,8 +105,7 @@ abstract class DibiDriver final public function query($args) { // receive arguments - if (!is_array($args)) - $args = func_get_args(); + if (!is_array($args)) $args = func_get_args(); // and generate SQL $trans = new DibiTranslator($this); @@ -121,7 +120,10 @@ abstract class DibiDriver if ($res === FALSE) { // query error if (dibi::$logFile) { // log to file $info = $this->errorInfo(); - if ($info['code']) $info['message'] = "[$info[code]] $info[message]"; + if ($info['code']) { + $info['message'] = "[$info[code]] $info[message]"; + } + dibi::log( "ERROR: $info[message]" . "\n-- SQL: " . $sql @@ -135,7 +137,10 @@ abstract class DibiDriver throw new DibiException('Query error (driver ' . $this->config['driver'] . ')', $info, $sql); } else { $info = $this->errorInfo(); - if ($info['code']) $info['message'] = "[$info[code]] $info[message]"; + if ($info['code']) { + $info['message'] = "[$info[code]] $info[message]"; + } + trigger_error("dibi: $info[message]", E_USER_WARNING); return FALSE; } diff --git a/dibi/libs/exception.php b/dibi/libs/exception.php index e2507bd1..9333701e 100644 --- a/dibi/libs/exception.php +++ b/dibi/libs/exception.php @@ -37,12 +37,14 @@ class DibiException extends Exception } + final public function getSql() { return $this->sql; } + final public function getDbError() { return $this->dbError; @@ -56,13 +58,16 @@ class DibiException extends Exception if ($this->dbError) { $s .= "\n\nDatabase error: "; - if (isset($this->dbError['code'])) + if (isset($this->dbError['code'])) { $s .= "[" . $this->dbError['code'] . "] "; + } $s .= $this->dbError['message']; } - if ($this->sql) $s .= "\nSQL: " . $this->sql; + if ($this->sql) { + $s .= "\nSQL: " . $this->sql; + } return $s; } diff --git a/dibi/libs/resultset.php b/dibi/libs/resultset.php index 071eecdd..abda4b37 100644 --- a/dibi/libs/resultset.php +++ b/dibi/libs/resultset.php @@ -107,14 +107,14 @@ abstract class DibiResult implements IteratorAggregate, Countable final public function fetch() { $row = $this->doFetch(); - if (!is_array($row)) - return FALSE; + if (!is_array($row)) return FALSE; // types-converting? if ($t = $this->convert) { // little speed-up foreach ($row as $key => $value) { - if (isset($t[$key])) + if (isset($t[$key])) { $row[$key] = $this->convert($value, $t[$key]); + } } } @@ -130,8 +130,7 @@ abstract class DibiResult implements IteratorAggregate, Countable final function fetchSingle() { $row = $this->doFetch(); - if (!is_array($row)) - return FALSE; + if (!is_array($row)) return FALSE; // types-converting? if ($t = $this->convert) { // little speed-up @@ -155,8 +154,7 @@ abstract class DibiResult implements IteratorAggregate, Countable { @$this->seek(0); $row = $this->fetch(); - if (!$row) - return array(); // empty resultset + if (!$row) return array(); // empty resultset $data = array(); if (count($row) === 1) { @@ -296,14 +294,15 @@ abstract class DibiResult implements IteratorAggregate, Countable final public function setType($field, $type = NULL) { - if ($field === TRUE) + if ($field === TRUE) { $this->detectTypes(); - elseif (is_array($field)) + } elseif (is_array($field)) { $this->convert = $field; - else + } else { $this->convert[$field] = $type; + } } @@ -318,19 +317,22 @@ abstract class DibiResult implements IteratorAggregate, Countable final public function convert($value, $type) { - if ($value === NULL || $value === FALSE) + if ($value === NULL || $value === FALSE) { return $value; + } if (isset(self::$types[$type])) { settype($value, self::$types[$type]); return $value; } - if ($type === dibi::FIELD_DATE) + if ($type === dibi::FIELD_DATE) { return strtotime($value); // !!! not good + } - if ($type === dibi::FIELD_DATETIME) + if ($type === dibi::FIELD_DATETIME) { return strtotime($value); // !!! not good + } return $value; } @@ -344,7 +346,9 @@ abstract class DibiResult implements IteratorAggregate, Countable final public function getFields() { // lazy init - if ($this->meta === NULL) $this->buildMeta(); + if ($this->meta === NULL) { + $this->buildMeta(); + } return array_keys($this->meta); } @@ -358,7 +362,9 @@ abstract class DibiResult implements IteratorAggregate, Countable final public function getMetaData($field) { // lazy init - if ($this->meta === NULL) $this->buildMeta(); + if ($this->meta === NULL) { + $this->buildMeta(); + } return isset($this->meta[$field]) ? $this->meta[$field] : FALSE; } @@ -370,7 +376,9 @@ abstract class DibiResult implements IteratorAggregate, Countable */ final protected function detectTypes() { - if ($this->meta === NULL) $this->buildMeta(); + if ($this->meta === NULL) { + $this->buildMeta(); + } } diff --git a/dibi/libs/translator.php b/dibi/libs/translator.php index 0737eea3..b6f00ef0 100644 --- a/dibi/libs/translator.php +++ b/dibi/libs/translator.php @@ -102,7 +102,9 @@ final class DibiTranslator } // default processing - if (!$comment) $sql[] = $this->formatValue($arg, $mod); + if (!$comment) { + $sql[] = $this->formatValue($arg, $mod); + } $mod = FALSE; } // foreach @@ -116,16 +118,18 @@ final class DibiTranslator // error handling if ($this->hasError) { - if (dibi::$logFile) // log to file + if (dibi::$logFile) { // log to file dibi::log( "ERROR: SQL generate error" . "\n-- SQL: " . $sql . ";\n-- " . date('Y-m-d H:i:s ') ); + } - if (dibi::$throwExceptions) + if (dibi::$throwExceptions) { throw new DibiException('SQL generate error', NULL, $sql); - else { + + } else { trigger_error("dibi: SQL generate error: $sql", E_USER_WARNING); return FALSE; } @@ -164,19 +168,23 @@ final class DibiTranslator $pair = explode('%', $k, 2); // generate arrays - if ($kx !== NULL) $kx[] = $this->delimite($pair[0]); + if ($kx !== NULL) { + $kx[] = $this->delimite($pair[0]); + } $vx[] = $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE); } - if ($kx === NULL) + if ($kx === NULL) { return '(' . implode(', ', $vx) . ')'; - else + } else { return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')'; + } default: - foreach ($value as $v) + foreach ($value as $v) { $vx[] = $this->formatValue($v, $modifier); + } return implode(', ', $vx); } @@ -185,10 +193,13 @@ final class DibiTranslator // with modifier procession if ($modifier) { - if ($value === NULL) return 'NULL'; + if ($value === NULL) { + return 'NULL'; + } - if ($value instanceof DibiVariableInterface) + if ($value instanceof DibiVariableInterface) { return $value->toSql($this->driver, $modifier); + } if (!is_scalar($value)) { // array is already processed $this->hasError = TRUE; @@ -226,8 +237,9 @@ final class DibiTranslator // speed-up - is regexp required? $toSkip = strcspn($value, '`[\'"%'); - if (strlen($value) === $toSkip) // needn't be translated + if (strlen($value) === $toSkip) { // needn't be translated return $value; + } // note: only this can change $this->modifier return substr($value, 0, $toSkip)