From 4e41f1641ef72447eca0f315107c6080720426ef Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 8 Jun 2008 12:44:44 +0000 Subject: [PATCH] - added dibi::setSubstFallBack() - added DibiFluent::fetch() --- dibi/dibi.php | 61 +++++++++++++++++++++++++++++++++--- dibi/libs/DibiFluent.php | 19 ++++++++++- dibi/libs/DibiTranslator.php | 5 +-- examples/table-prefix.php | 18 +++++++++++ 4 files changed, 93 insertions(+), 10 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 864e8b06..0a237ca7 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -111,6 +111,12 @@ class dibi */ private static $substs = array(); + /** + * Substitution fallback. + * @var callback + */ + private static $substFallBack; + /** * @see addHandler * @var array @@ -545,7 +551,24 @@ class dibi */ public static function addSubst($expr, $subst) { - self::$substs[':'.$expr.':'] = $subst; + self::$substs[$expr] = $subst; + } + + + + /** + * Sets substitution fallback handler. + * + * @param callback + * @return void + */ + public static function setSubstFallback($callback) + { + if (!is_callable($callback)) { + throw new InvalidArgumentException("Invalid callback."); + } + + self::$substFallBack = $callback; } @@ -568,13 +591,41 @@ class dibi /** - * Returns substitution pairs. + * Provides substitution. * - * @return array + * @param string + * @return string */ - public static function getSubst() + public static function substitute($value) { - return self::$substs; + if (strpos($value, ':') === FALSE) { + return $value; + + } else { + return preg_replace_callback('#:(.*):#U', array('dibi', 'subCb'), $value); + } + } + + + + /** + * Substitution callback. + * + * @param array + * @return string + */ + private static function subCb($m) + { + $m = $m[1]; + if (isset(self::$substs[$m])) { + return self::$substs[$m]; + + } elseif (self::$substFallBack) { + return self::$substs[$m] = call_user_func(self::$substFallBack, $m); + + } else { + return $m; + } } diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index a04a8aea..d18a4e2d 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -27,7 +27,7 @@ * @package dibi * @version $Revision$ $Date$ */ -final class DibiFluent extends /*Nette::*/Object +class DibiFluent extends /*Nette::*/Object { /** @var array */ public static $masks = array( @@ -103,6 +103,7 @@ final class DibiFluent extends /*Nette::*/Object // special types or argument if (count($args) === 1) { $arg = $args[0]; + // TODO: really ignore TRUE? if ($arg === TRUE) { $args = array(); @@ -115,6 +116,7 @@ final class DibiFluent extends /*Nette::*/Object // append to clause $this->cursor = & $this->clauses[$clause]; + // TODO: really delete? if ($args === array(FALSE)) { $this->cursor = NULL; return $this; @@ -224,6 +226,21 @@ final class DibiFluent extends /*Nette::*/Object + /** + * Generates, executes SQL query and fetches the single row. + * @return array|FALSE array on success, FALSE if no next record + * @throws DibiException + */ + public function fetch() + { + if ($this->command === 'SELECT') { + $this->clauses['LIMIT'] = array(1); + } + return $this->connection->query($this->_export())->fetch(); + } + + + /** * Generates and prints SQL query or it's part. * @param string clause name diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index a22400e5..65c6821d 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -450,10 +450,7 @@ final class DibiTranslator extends /*Nette::*/Object */ private function delimite($value) { - if (strpos($value, ':') !== FALSE) { - $value = strtr($value, dibi::getSubst()); - } - return $this->driver->escape($value, dibi::IDENTIFIER); + return $this->driver->escape(dibi::substitute($value), dibi::IDENTIFIER); } diff --git a/examples/table-prefix.php b/examples/table-prefix.php index 00f42e2a..9a36ceae 100644 --- a/examples/table-prefix.php +++ b/examples/table-prefix.php @@ -18,9 +18,27 @@ dibi::addSubst('blog', 'wp_'); dibi::test("UPDATE [:blog:items] SET [text]='Hello World'"); + + // create new substitution :: (empty) ==> my_ dibi::addSubst('', 'my_'); // generate and dump SQL dibi::test("UPDATE [database.::table] SET [text]='Hello World'"); + + + + + +function substFallBack($expr) +{ + return 'the_' . $expr; +} + +// create substitution fallback +dibi::setSubstFallBack('substFallBack'); + + +// generate and dump SQL +dibi::test("UPDATE [:account:user] SET [name]='John Doe'");