1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 21:28:02 +02:00

* fixed odbc_num_rows and pg_affected_rows

This commit is contained in:
David Grudl
2007-08-23 17:12:58 +00:00
parent 05b8c0ad43
commit 7f995a558b
12 changed files with 172 additions and 102 deletions

View File

@@ -158,20 +158,23 @@ class dibi
public static function connect($config, $name = 0) public static function connect($config, $name = 0)
{ {
// DSN string // DSN string
if (is_string($config)) if (is_string($config)) {
parse_str($config, $config); parse_str($config, $config);
}
// config['driver'] is required // config['driver'] is required
if (empty($config['driver'])) if (empty($config['driver'])) {
throw new DibiException('Driver is not specified.'); throw new DibiException('Driver is not specified.');
}
// include dibi driver // include dibi driver
$className = "Dibi$config[driver]Driver"; $className = "Dibi$config[driver]Driver";
if (!class_exists($className)) { if (!class_exists($className)) {
include_once dirname(__FILE__) . "/drivers/$config[driver].php"; 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'."); throw new DibiException("Unable to create instance of dibi driver class '$className'.");
}
} }
// create connection object and store in list // create connection object and store in list
@@ -207,14 +210,16 @@ 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) {
throw new DibiException('Dibi is not connected to database'); throw new DibiException('Dibi is not connected to database');
}
return self::$connection; return self::$connection;
} }
if (!isset(self::$registry[$name])) if (!isset(self::$registry[$name])) {
throw new DibiException("There is no connection named '$name'."); throw new DibiException("There is no connection named '$name'.");
}
return self::$registry[$name]; return self::$registry[$name];
} }
@@ -245,8 +250,7 @@ class dibi
public static function query($args) public static function query($args)
{ {
// receive arguments // receive arguments
if (!is_array($args)) if (!is_array($args)) $args = func_get_args();
$args = func_get_args();
return self::getConnection()->query($args); return self::getConnection()->query($args);
} }
@@ -262,8 +266,7 @@ class dibi
public static function test($args) public static function test($args)
{ {
// receive arguments // receive arguments
if (!is_array($args)) if (!is_array($args)) $args = func_get_args();
$args = func_get_args();
// and generate SQL // and generate SQL
$trans = new DibiTranslator(self::getConnection()); $trans = new DibiTranslator(self::getConnection());

View File

@@ -40,8 +40,9 @@ class DibiMSSqlDriver extends DibiDriver
*/ */
public function __construct($config) public function __construct($config)
{ {
if (!extension_loaded('mssql')) if (!extension_loaded('mssql')) {
throw new DibiException("PHP extension 'mssql' is not loaded"); throw new DibiException("PHP extension 'mssql' is not loaded");
}
if (!isset($config['host'])) $config['host'] = NULL; if (!isset($config['host'])) $config['host'] = NULL;
if (!isset($config['username'])) $config['username'] = NULL; if (!isset($config['username'])) $config['username'] = NULL;
@@ -56,17 +57,18 @@ class DibiMSSqlDriver extends DibiDriver
{ {
$config = $this->config; $config = $this->config;
if (empty($config['persistent'])) if (empty($config['persistent'])) {
$connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE); $connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE);
else } else {
$connection = @mssql_pconnect($config['host'], $config['username'], $config['password']); $connection = @mssql_pconnect($config['host'], $config['username'], $config['password']);
}
if (!is_resource($connection)) if (!is_resource($connection)) {
throw new DibiException("Connecting error (driver mssql)'"); throw new DibiException("Connecting error (driver mssql)'");
}
if (!empty($config['database'])) { if (!empty($config['database']) && !@mssql_select_db($config['database'], $connection)) {
if (!@mssql_select_db($config['database'], $connection)) throw new DibiException("Connecting error (driver mssql)");
throw new DibiException("Connecting error (driver mssql)");
} }
return $connection; return $connection;
@@ -85,8 +87,9 @@ class DibiMSSqlDriver extends DibiDriver
$this->affectedRows = mssql_rows_affected($connection); $this->affectedRows = mssql_rows_affected($connection);
if ($this->affectedRows < 0) $this->affectedRows = FALSE; if ($this->affectedRows < 0) $this->affectedRows = FALSE;
if (is_resource($res)) if (is_resource($res)) {
return new DibiMSSqlResult($res); return new DibiMSSqlResult($res);
}
return TRUE; return TRUE;
} }
@@ -168,10 +171,13 @@ class DibiMSSqlDriver extends DibiDriver
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
// offset suppot is missing... // offset suppot is missing...
if ($limit >= 0) if ($limit >= 0) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')'; $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');
}
} }

View File

@@ -41,8 +41,9 @@ class DibiMySqlDriver extends DibiDriver
*/ */
public function __construct($config) public function __construct($config)
{ {
if (!extension_loaded('mysql')) if (!extension_loaded('mysql')) {
throw new DibiException("PHP extension 'mysql' is not loaded"); throw new DibiException("PHP extension 'mysql' is not loaded");
}
// default values // default values
if (empty($config['username'])) $config['username'] = ini_get('mysql.default_user'); if (empty($config['username'])) $config['username'] = ini_get('mysql.default_user');
@@ -62,30 +63,36 @@ class DibiMySqlDriver extends DibiDriver
{ {
$config = $this->config; $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']; $host = ':' . $config['host'];
else } else {
$host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']); $host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']);
}
// some errors aren't handled. Must use $php_errormsg // 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); $save = ini_set('track_errors', TRUE);
}
$php_errormsg = ''; $php_errormsg = '';
if (empty($config['persistent'])) if (empty($config['persistent'])) {
$connection = @mysql_connect($host, $config['username'], $config['password'], TRUE); $connection = @mysql_connect($host, $config['username'], $config['password'], TRUE);
else } else {
$connection = @mysql_pconnect($host, $config['username'], $config['password']); $connection = @mysql_pconnect($host, $config['username'], $config['password']);
}
if (function_exists('ini_set')) if (function_exists('ini_set')) {
ini_set('track_errors', $save); ini_set('track_errors', $save);
}
if (!is_resource($connection)) if (!is_resource($connection)) {
throw new DibiException("Connecting error (driver mysql)'", array( throw new DibiException("Connecting error (driver mysql)'", array(
'message' => mysql_error() ? mysql_error() : $php_errormsg, 'message' => mysql_error() ? mysql_error() : $php_errormsg,
'code' => mysql_errno(), 'code' => mysql_errno(),
)); ));
}
if (!empty($config['charset'])) { if (!empty($config['charset'])) {
@@ -94,12 +101,11 @@ class DibiMySqlDriver extends DibiDriver
} }
if (!empty($config['database'])) { if (!empty($config['database']) && !@mysql_select_db($config['database'], $connection)) {
if (!@mysql_select_db($config['database'], $connection)) throw new DibiException("Connecting error (driver mysql)", array(
throw new DibiException("Connecting error (driver mysql)", array( 'message' => mysql_error($connection),
'message' => mysql_error($connection), 'code' => mysql_errno($connection),
'code' => mysql_errno($connection), ));
));
} }
return $connection; return $connection;
@@ -121,8 +127,9 @@ class DibiMySqlDriver extends DibiDriver
$this->insertId = mysql_insert_id($connection); $this->insertId = mysql_insert_id($connection);
if ($this->insertId < 1) $this->insertId = FALSE; if ($this->insertId < 1) $this->insertId = FALSE;
if (is_resource($res)) if (is_resource($res)) {
return new DibiMySqlResult($res); return new DibiMySqlResult($res);
}
return TRUE; return TRUE;
} }
@@ -309,9 +316,9 @@ class DibiMySqlResult extends DibiResult
$info['length'] = mysql_field_len($this->resource, $index); $info['length'] = mysql_field_len($this->resource, $index);
$info['table'] = mysql_field_table($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; $info['type'] = dibi::FIELD_COUNTER;
else { } else {
$info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN; $info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN;
// if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255) // if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255)

View File

@@ -42,8 +42,9 @@ class DibiMySqliDriver extends DibiDriver
*/ */
public function __construct($config) public function __construct($config)
{ {
if (!extension_loaded('mysqli')) if (!extension_loaded('mysqli')) {
throw new DibiException("PHP extension 'mysqli' is not loaded"); throw new DibiException("PHP extension 'mysqli' is not loaded");
}
// default values // default values
if (empty($config['username'])) $config['username'] = ini_get('mysqli.default_user'); 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']); $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( throw new DibiException("Connecting error (driver mysqli)", array(
'message' => mysqli_connect_error(), 'message' => mysqli_connect_error(),
'code' => mysqli_connect_errno(), 'code' => mysqli_connect_errno(),
)); ));
}
if (!empty($config['charset'])) if (!empty($config['charset'])) {
mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'"); mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'");
}
return $connection; return $connection;
} }
@@ -94,8 +97,9 @@ class DibiMySqliDriver extends DibiDriver
$this->insertId = mysqli_insert_id($connection); $this->insertId = mysqli_insert_id($connection);
if ($this->insertId < 1) $this->insertId = FALSE; if ($this->insertId < 1) $this->insertId = FALSE;
if (is_object($res)) if (is_object($res)) {
return new DibiMySqliResult($res); return new DibiMySqliResult($res);
}
return TRUE; return TRUE;
} }
@@ -278,9 +282,9 @@ class DibiMySqliResult extends DibiResult
$info = (array) mysqli_fetch_field_direct($this->resource, $index); $info = (array) mysqli_fetch_field_direct($this->resource, $index);
$native = $info['native'] = $info['type']; $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; $info['type'] = dibi::FIELD_COUNTER;
else { } else {
$info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN; $info['type'] = isset($types[$native]) ? $types[$native] : dibi::FIELD_UNKNOWN;
// if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255) // if ($info['type'] === dibi::FIELD_TEXT && $info['length'] > 255)
// $info['type'] = dibi::FIELD_LONG_TEXT; // $info['type'] = dibi::FIELD_LONG_TEXT;

View File

@@ -49,14 +49,17 @@ class DibiOdbcDriver extends DibiDriver
if (empty($config['password'])) $config['password'] = ini_get('odbc.default_pw'); 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['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)"); 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)"); 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)"); throw new DibiException("Database must be specified (driver odbc)");
}
parent::__construct($config); parent::__construct($config);
} }
@@ -67,16 +70,18 @@ class DibiOdbcDriver extends DibiDriver
{ {
$config = $this->config; $config = $this->config;
if (empty($config['persistent'])) if (empty($config['persistent'])) {
$connection = @odbc_connect($config['database'], $config['username'], $config['password']); $connection = @odbc_connect($config['database'], $config['username'], $config['password']);
else } else {
$connection = @odbc_pconnect($config['database'], $config['username'], $config['password']); $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( throw new DibiException("Connecting error (driver odbc)", array(
'message' => odbc_errormsg(), 'message' => odbc_errormsg(),
'code' => odbc_error(), 'code' => odbc_error(),
)); ));
}
return $connection; return $connection;
} }
@@ -92,11 +97,12 @@ class DibiOdbcDriver extends DibiDriver
if ($res === FALSE) return FALSE; 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 ($this->affectedRows < 0) $this->affectedRows = FALSE;
if (is_resource($res)) if (is_resource($res)) {
return new DibiOdbcResult($res); return new DibiOdbcResult($res);
}
return TRUE; return TRUE;
} }
@@ -185,8 +191,9 @@ class DibiOdbcDriver extends DibiDriver
public function applyLimit(&$sql, $limit, $offset = 0) public function applyLimit(&$sql, $limit, $offset = 0)
{ {
// offset suppot is missing... // offset suppot is missing...
if ($limit >= 0) if ($limit >= 0) {
$sql = 'SELECT TOP ' . (int) $limit . ' * FROM (' . $sql . ')'; $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');
} }
@@ -246,8 +253,9 @@ class DibiOdbcResult extends DibiResult
protected function buildMeta() protected function buildMeta()
{ {
// cache // cache
if ($this->meta !== NULL) if ($this->meta !== NULL) {
return $this->meta; return $this->meta;
}
static $types = array( static $types = array(
'CHAR' => dibi::FIELD_TEXT, 'CHAR' => dibi::FIELD_TEXT,

View File

@@ -40,11 +40,13 @@ class DibiPdoDriver extends DibiDriver
*/ */
public function __construct($config) public function __construct($config)
{ {
if (!extension_loaded('pdo')) if (!extension_loaded('pdo')) {
throw new DibiException("PHP extension 'pdo' is not loaded"); 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)"); throw new DibiException("DSN must be specified (driver odbc)");
}
if (empty($config['username'])) $config['username'] = NULL; if (empty($config['username'])) $config['username'] = NULL;
if (empty($config['password'])) $config['password'] = NULL; if (empty($config['password'])) $config['password'] = NULL;
@@ -70,8 +72,9 @@ class DibiPdoDriver extends DibiDriver
if ($res === FALSE) return FALSE; if ($res === FALSE) return FALSE;
if ($res instanceof PDOStatement) if ($res instanceof PDOStatement) {
return new DibiPdoResult($res); return new DibiPdoResult($res);
}
return TRUE; return TRUE;
} }

View File

@@ -41,11 +41,13 @@ class DibiPostgreDriver extends DibiDriver
*/ */
public function __construct($config) public function __construct($config)
{ {
if (!extension_loaded('pgsql')) if (!extension_loaded('pgsql')) {
throw new DibiException("PHP extension 'pgsql' is not loaded"); 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)"); throw new DibiException("Connection string must be specified (driver postgre)");
}
if (empty($config['type'])) $config['type'] = NULL; if (empty($config['type'])) $config['type'] = NULL;
@@ -58,15 +60,17 @@ class DibiPostgreDriver extends DibiDriver
{ {
$config = $this->config; $config = $this->config;
if (isset($config['persistent'])) if (isset($config['persistent'])) {
$connection = @pg_connect($config['string'], $config['type']); $connection = @pg_connect($config['string'], $config['type']);
else } else {
$connection = @pg_pconnect($config['string'], $config['type']); $connection = @pg_pconnect($config['string'], $config['type']);
}
if (!is_resource($connection)) if (!is_resource($connection)) {
throw new DibiException("Connecting error (driver postgre)", array( throw new DibiException("Connecting error (driver postgre)", array(
'message' => pg_last_error(), 'message' => pg_last_error(),
)); ));
}
if (!empty($config['charset'])) { if (!empty($config['charset'])) {
@pg_set_client_encoding($connection, $config['charset']); @pg_set_client_encoding($connection, $config['charset']);
@@ -89,8 +93,9 @@ class DibiPostgreDriver extends DibiDriver
$this->affectedRows = pg_affected_rows($connection); $this->affectedRows = pg_affected_rows($connection);
if ($this->affectedRows < 0) $this->affectedRows = FALSE; if ($this->affectedRows < 0) $this->affectedRows = FALSE;
if (is_resource($res)) if (is_resource($res)) {
return new DibiPostgreResult($res); return new DibiPostgreResult($res);
}
return TRUE; return TRUE;
} }

View File

@@ -42,14 +42,15 @@ class DibiSqliteDriver extends DibiDriver
*/ */
public function __construct($config) public function __construct($config)
{ {
if (!extension_loaded('sqlite')) if (!extension_loaded('sqlite')) {
throw new DibiException("PHP extension 'sqlite' is not loaded"); 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)"); throw new DibiException("Database must be specified (driver sqlite)");
}
if (!isset($config['mode'])) if (!isset($config['mode'])) $config['mode'] = 0666;
$config['mode'] = 0666;
parent::__construct($config); parent::__construct($config);
} }
@@ -61,15 +62,17 @@ class DibiSqliteDriver extends DibiDriver
$config = $this->config; $config = $this->config;
$errorMsg = ''; $errorMsg = '';
if (empty($config['persistent'])) if (empty($config['persistent'])) {
$connection = @sqlite_open($config['database'], $config['mode'], $errorMsg); $connection = @sqlite_open($config['database'], $config['mode'], $errorMsg);
else } else {
$connection = @sqlite_popen($config['database'], $config['mode'], $errorMsg); $connection = @sqlite_popen($config['database'], $config['mode'], $errorMsg);
}
if (!$connection) if (!$connection) {
throw new DibiException("Connecting error (driver sqlite)", array( throw new DibiException("Connecting error (driver sqlite)", array(
'message' => $errorMsg, 'message' => $errorMsg,
)); ));
}
return $connection; return $connection;
} }
@@ -91,8 +94,9 @@ class DibiSqliteDriver extends DibiDriver
$this->insertId = sqlite_last_insert_rowid($connection); $this->insertId = sqlite_last_insert_rowid($connection);
if ($this->insertId < 1) $this->insertId = FALSE; if ($this->insertId < 1) $this->insertId = FALSE;
if (is_resource($res)) if (is_resource($res)) {
return new DibiSqliteResult($res); return new DibiSqliteResult($res);
}
return TRUE; return TRUE;
} }

View File

@@ -105,8 +105,7 @@ abstract class DibiDriver
final public function query($args) final public function query($args)
{ {
// receive arguments // receive arguments
if (!is_array($args)) if (!is_array($args)) $args = func_get_args();
$args = func_get_args();
// and generate SQL // and generate SQL
$trans = new DibiTranslator($this); $trans = new DibiTranslator($this);
@@ -121,7 +120,10 @@ abstract class DibiDriver
if ($res === FALSE) { // query error if ($res === FALSE) { // query error
if (dibi::$logFile) { // log to file if (dibi::$logFile) { // log to file
$info = $this->errorInfo(); $info = $this->errorInfo();
if ($info['code']) $info['message'] = "[$info[code]] $info[message]"; if ($info['code']) {
$info['message'] = "[$info[code]] $info[message]";
}
dibi::log( dibi::log(
"ERROR: $info[message]" "ERROR: $info[message]"
. "\n-- SQL: " . $sql . "\n-- SQL: " . $sql
@@ -135,7 +137,10 @@ abstract class DibiDriver
throw new DibiException('Query error (driver ' . $this->config['driver'] . ')', $info, $sql); throw new DibiException('Query error (driver ' . $this->config['driver'] . ')', $info, $sql);
} else { } else {
$info = $this->errorInfo(); $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); trigger_error("dibi: $info[message]", E_USER_WARNING);
return FALSE; return FALSE;
} }

View File

@@ -37,12 +37,14 @@ class DibiException extends Exception
} }
final public function getSql() final public function getSql()
{ {
return $this->sql; return $this->sql;
} }
final public function getDbError() final public function getDbError()
{ {
return $this->dbError; return $this->dbError;
@@ -56,13 +58,16 @@ class DibiException extends Exception
if ($this->dbError) { if ($this->dbError) {
$s .= "\n\nDatabase error: "; $s .= "\n\nDatabase error: ";
if (isset($this->dbError['code'])) if (isset($this->dbError['code'])) {
$s .= "[" . $this->dbError['code'] . "] "; $s .= "[" . $this->dbError['code'] . "] ";
}
$s .= $this->dbError['message']; $s .= $this->dbError['message'];
} }
if ($this->sql) $s .= "\nSQL: " . $this->sql; if ($this->sql) {
$s .= "\nSQL: " . $this->sql;
}
return $s; return $s;
} }

View File

@@ -107,14 +107,14 @@ abstract class DibiResult implements IteratorAggregate, Countable
final public function fetch() final public function fetch()
{ {
$row = $this->doFetch(); $row = $this->doFetch();
if (!is_array($row)) if (!is_array($row)) return FALSE;
return FALSE;
// types-converting? // types-converting?
if ($t = $this->convert) { // little speed-up if ($t = $this->convert) { // little speed-up
foreach ($row as $key => $value) { foreach ($row as $key => $value) {
if (isset($t[$key])) if (isset($t[$key])) {
$row[$key] = $this->convert($value, $t[$key]); $row[$key] = $this->convert($value, $t[$key]);
}
} }
} }
@@ -130,8 +130,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
final function fetchSingle() final function fetchSingle()
{ {
$row = $this->doFetch(); $row = $this->doFetch();
if (!is_array($row)) if (!is_array($row)) return FALSE;
return FALSE;
// types-converting? // types-converting?
if ($t = $this->convert) { // little speed-up if ($t = $this->convert) { // little speed-up
@@ -155,8 +154,7 @@ abstract class DibiResult implements IteratorAggregate, Countable
{ {
@$this->seek(0); @$this->seek(0);
$row = $this->fetch(); $row = $this->fetch();
if (!$row) if (!$row) return array(); // empty resultset
return array(); // empty resultset
$data = array(); $data = array();
if (count($row) === 1) { if (count($row) === 1) {
@@ -296,14 +294,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
final public function setType($field, $type = NULL) final public function setType($field, $type = NULL)
{ {
if ($field === TRUE) if ($field === TRUE) {
$this->detectTypes(); $this->detectTypes();
elseif (is_array($field)) } elseif (is_array($field)) {
$this->convert = $field; $this->convert = $field;
else } else {
$this->convert[$field] = $type; $this->convert[$field] = $type;
}
} }
@@ -318,19 +317,22 @@ abstract class DibiResult implements IteratorAggregate, Countable
final public function convert($value, $type) final public function convert($value, $type)
{ {
if ($value === NULL || $value === FALSE) if ($value === NULL || $value === FALSE) {
return $value; return $value;
}
if (isset(self::$types[$type])) { if (isset(self::$types[$type])) {
settype($value, self::$types[$type]); settype($value, self::$types[$type]);
return $value; return $value;
} }
if ($type === dibi::FIELD_DATE) if ($type === dibi::FIELD_DATE) {
return strtotime($value); // !!! not good return strtotime($value); // !!! not good
}
if ($type === dibi::FIELD_DATETIME) if ($type === dibi::FIELD_DATETIME) {
return strtotime($value); // !!! not good return strtotime($value); // !!! not good
}
return $value; return $value;
} }
@@ -344,7 +346,9 @@ abstract class DibiResult implements IteratorAggregate, Countable
final public function getFields() final public function getFields()
{ {
// lazy init // lazy init
if ($this->meta === NULL) $this->buildMeta(); if ($this->meta === NULL) {
$this->buildMeta();
}
return array_keys($this->meta); return array_keys($this->meta);
} }
@@ -358,7 +362,9 @@ abstract class DibiResult implements IteratorAggregate, Countable
final public function getMetaData($field) final public function getMetaData($field)
{ {
// lazy init // lazy init
if ($this->meta === NULL) $this->buildMeta(); if ($this->meta === NULL) {
$this->buildMeta();
}
return isset($this->meta[$field]) ? $this->meta[$field] : FALSE; return isset($this->meta[$field]) ? $this->meta[$field] : FALSE;
} }
@@ -370,7 +376,9 @@ abstract class DibiResult implements IteratorAggregate, Countable
*/ */
final protected function detectTypes() final protected function detectTypes()
{ {
if ($this->meta === NULL) $this->buildMeta(); if ($this->meta === NULL) {
$this->buildMeta();
}
} }

View File

@@ -102,7 +102,9 @@ final class DibiTranslator
} }
// default processing // default processing
if (!$comment) $sql[] = $this->formatValue($arg, $mod); if (!$comment) {
$sql[] = $this->formatValue($arg, $mod);
}
$mod = FALSE; $mod = FALSE;
} // foreach } // foreach
@@ -116,16 +118,18 @@ final class DibiTranslator
// error handling // error handling
if ($this->hasError) { if ($this->hasError) {
if (dibi::$logFile) // log to file if (dibi::$logFile) { // log to file
dibi::log( dibi::log(
"ERROR: SQL generate error" "ERROR: SQL generate error"
. "\n-- SQL: " . $sql . "\n-- SQL: " . $sql
. ";\n-- " . date('Y-m-d H:i:s ') . ";\n-- " . date('Y-m-d H:i:s ')
); );
}
if (dibi::$throwExceptions) if (dibi::$throwExceptions) {
throw new DibiException('SQL generate error', NULL, $sql); throw new DibiException('SQL generate error', NULL, $sql);
else {
} else {
trigger_error("dibi: SQL generate error: $sql", E_USER_WARNING); trigger_error("dibi: SQL generate error: $sql", E_USER_WARNING);
return FALSE; return FALSE;
} }
@@ -164,19 +168,23 @@ final class DibiTranslator
$pair = explode('%', $k, 2); $pair = explode('%', $k, 2);
// generate arrays // 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); $vx[] = $this->formatValue($v, isset($pair[1]) ? $pair[1] : FALSE);
} }
if ($kx === NULL) if ($kx === NULL) {
return '(' . implode(', ', $vx) . ')'; return '(' . implode(', ', $vx) . ')';
else } else {
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')'; return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
}
default: default:
foreach ($value as $v) foreach ($value as $v) {
$vx[] = $this->formatValue($v, $modifier); $vx[] = $this->formatValue($v, $modifier);
}
return implode(', ', $vx); return implode(', ', $vx);
} }
@@ -185,10 +193,13 @@ final class DibiTranslator
// with modifier procession // with modifier procession
if ($modifier) { 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); return $value->toSql($this->driver, $modifier);
}
if (!is_scalar($value)) { // array is already processed if (!is_scalar($value)) { // array is already processed
$this->hasError = TRUE; $this->hasError = TRUE;
@@ -226,8 +237,9 @@ final class DibiTranslator
// speed-up - is regexp required? // speed-up - is regexp required?
$toSkip = strcspn($value, '`[\'"%'); $toSkip = strcspn($value, '`[\'"%');
if (strlen($value) === $toSkip) // needn't be translated if (strlen($value) === $toSkip) { // needn't be translated
return $value; return $value;
}
// note: only this can change $this->modifier // note: only this can change $this->modifier
return substr($value, 0, $toSkip) return substr($value, 0, $toSkip)