From 5243122e6aadc7d350c4638b7644a8e2e53a4ae8 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 27 Aug 2007 22:38:14 +0000 Subject: [PATCH] * support for big int & big floats --- dibi/dibi.php | 18 +++++++++--------- dibi/libs/driver.php | 2 +- dibi/libs/resultset.php | 2 +- dibi/libs/translator.php | 26 ++++++++++++++++++++++---- examples/sql-builder.php | 10 ++++++++++ version.txt | 2 +- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 1e5278db..d090d7af 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -14,7 +14,7 @@ * @author David Grudl * @copyright Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz) * @license New BSD License - * @version 0.8d (Revision: $WCREV$, Date: $WCDATE$) + * @version 0.8e (Revision: $WCREV$, Date: $WCDATE$) * @category Database * @package Dibi * @link http://dibi.texy.info/ @@ -87,7 +87,7 @@ class dibi FIELD_COUNTER = 'c', // counter or autoincrement, is integer // dibi version - VERSION = '0.8d (Revision: $WCREV$, Date: $WCDATE$)'; + VERSION = '0.8e (Revision: $WCREV$, Date: $WCDATE$)'; /** @@ -143,7 +143,7 @@ class dibi /** * Monostate class */ - private function __construct() + final private function __construct() {} @@ -168,18 +168,18 @@ class dibi } // include dibi driver - $className = "Dibi$config[driver]Driver"; - if (!class_exists($className)) { + $class = "Dibi$config[driver]Driver"; + if (!class_exists($class)) { include_once dirname(__FILE__) . "/drivers/$config[driver].php"; - if (!class_exists($className)) { - throw new DibiException("Unable to create instance of dibi driver class '$className'."); + if (!class_exists($class)) { + throw new DibiException("Unable to create instance of dibi driver class '$class'."); } } // create connection object and store in list - /** like $connection = $className::connect($config); */ - self::$connection = self::$registry[$name] = new $className($config); + /** like $connection = $class::connect($config); */ + self::$connection = self::$registry[$name] = new $class($config); if (dibi::$logAll) dibi::log("OK: connected to DB '$config[driver]'"); diff --git a/dibi/libs/driver.php b/dibi/libs/driver.php index e6778778..b26469fa 100644 --- a/dibi/libs/driver.php +++ b/dibi/libs/driver.php @@ -261,7 +261,7 @@ abstract class DibiDriver * Access to undeclared property * @throws Exception */ - private function __get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } + private function &__get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } private function __set($name, $value) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } private function __unset($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } /**#@-*/ diff --git a/dibi/libs/resultset.php b/dibi/libs/resultset.php index abda4b37..08e1c160 100644 --- a/dibi/libs/resultset.php +++ b/dibi/libs/resultset.php @@ -412,7 +412,7 @@ abstract class DibiResult implements IteratorAggregate, Countable * Access to undeclared property * @throws Exception */ - private function __get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } + private function &__get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } private function __set($name, $value) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } private function __unset($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } /**#@-*/ diff --git a/dibi/libs/translator.php b/dibi/libs/translator.php index b6f00ef0..a5265f11 100644 --- a/dibi/libs/translator.php +++ b/dibi/libs/translator.php @@ -209,27 +209,43 @@ final class DibiTranslator switch ($modifier) { case 's': // string return $this->driver->escape($value); + case 'sn': // string or NULL return $value == '' ? 'NULL' : $this->driver->escape($value); + case 'b': // boolean return $value ? $this->driver->formats['TRUE'] : $this->driver->formats['FALSE']; + case 'i': // signed int - case 'u': // unsigned int - return (string) (int) $value; + case 'u': // unsigned int, ignored + // support for numbers - keep them unchanged + if (is_string($value) && preg_match('#[+-]?\d+(e\d+)?$#A', $value)) { + return $value; + } + return (string) (int) ($value + 0); + case 'f': // float - return (string) (float) $value; // something like -9E-005 is accepted by SQL + // support for numbers - keep them unchanged + if (is_numeric($value) && (!is_string($value) || strpos($value, 'x') === FALSE)) { + return $value; // something like -9E-005 is accepted by SQL, HEX values is not + } + return (string) ($value + 0); + case 'd': // date return date($this->driver->formats['date'], is_string($value) ? strtotime($value) : $value); + case 't': // datetime return date($this->driver->formats['datetime'], is_string($value) ? strtotime($value) : $value); + case 'n': // identifier name return $this->delimite($value); + case 'sql':// preserve as SQL case 'p': // back compatibility $value = (string) $value; @@ -265,9 +281,11 @@ final class DibiTranslator case 'v': $this->hasError = TRUE; return "**Unexpected ".gettype($value)."**"; + case 'if': $this->hasError = TRUE; return "**The %$modifier is not allowed here**"; + default: $this->hasError = TRUE; return "**Unknown modifier %$modifier**"; @@ -391,7 +409,7 @@ final class DibiTranslator * Access to undeclared property * @throws Exception */ - private function __get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } + private function &__get($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } private function __set($name, $value) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } private function __unset($name) { throw new Exception("Access to undeclared property: " . get_class($this) . "::$$name"); } /**#@-*/ diff --git a/examples/sql-builder.php b/examples/sql-builder.php index 1e654954..33aedac3 100644 --- a/examples/sql-builder.php +++ b/examples/sql-builder.php @@ -69,3 +69,13 @@ dibi::test("UPDATE [mytable] SET", $array4, " WHERE [id]=%i", $n); // array with modifier %a - assoc dibi::test("UPDATE [mytable] SET%a", $array4, " WHERE [id]=%i", $n); + + +// long numbers +dibi::test("SELECT %i", '-123456789123456789123456789'); + +// long float numbers +dibi::test("SELECT %f", '-.12345678912345678912345678e10'); + +// hex numbers +dibi::test("SELECT %i", '0x11'); diff --git a/version.txt b/version.txt index 45c49708..67c87732 100644 --- a/version.txt +++ b/version.txt @@ -1,4 +1,4 @@ -Dibi version 0.8d +Dibi version 0.8e Revision: $WCREV$ Date: $WCDATE$