mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 13:47:33 +02:00
- added dibi::setSubstFallBack()
- added DibiFluent::fetch()
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -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'");
|
||||
|
Reference in New Issue
Block a user