mirror of
https://github.com/dg/dibi.git
synced 2025-08-18 20:02:20 +02:00
- smarter handling of substitutions :subst: outside of brackets []
This commit is contained in:
@@ -134,7 +134,7 @@ class dibi
|
|||||||
public static $substs = array();
|
public static $substs = array();
|
||||||
|
|
||||||
/** @var callback Substitution fallback */
|
/** @var callback Substitution fallback */
|
||||||
public static $substFallBack;
|
public static $substFallBack = array(__CLASS__, 'defaultSubstFallback');
|
||||||
|
|
||||||
/** @var array @see addHandler */
|
/** @var array @see addHandler */
|
||||||
private static $handlers = array();
|
private static $handlers = array();
|
||||||
@@ -647,6 +647,18 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default substitution fallback handler.
|
||||||
|
* @param string
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function defaultSubstFallback($expr)
|
||||||
|
{
|
||||||
|
throw new InvalidStateException("Missing substitution for '$expr' expression.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* misc tools ****************d*g**/
|
/********************* misc tools ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
@@ -121,14 +121,14 @@ final class DibiTranslator extends DibiObject
|
|||||||
(?:
|
(?:
|
||||||
`(.+?)`| ## 1) `identifier`
|
`(.+?)`| ## 1) `identifier`
|
||||||
\[(.+?)\]| ## 2) [identifier]
|
\[(.+?)\]| ## 2) [identifier]
|
||||||
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
|
(\')((?:\'\'|[^\'])*)\'| ## 3,4) 'string'
|
||||||
(")((?:""|[^"])*)"| ## 5,6) "string"
|
(")((?:""|[^"])*)"| ## 5,6) "string"
|
||||||
(\'|")| ## 7) lone quote
|
(\'|")| ## 7) lone quote
|
||||||
:(\S*?:)| ## 8) substitution
|
:(\S*?:)([a-zA-Z0-9._]?)| ## 8,9) substitution
|
||||||
%([a-zA-Z]{1,4})(?![a-zA-Z]) ## 9) modifier
|
%([a-zA-Z]{1,4})(?![a-zA-Z]) ## 10) modifier
|
||||||
)/xs',
|
)/xs',
|
||||||
*/ // note: this can change $this->args & $this->cursor & ...
|
*/ // note: this can change $this->args & $this->cursor & ...
|
||||||
. preg_replace_callback('/(?=[`[\'":%])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
|
. preg_replace_callback('/(?=[`[\'":%])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?)|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
|
||||||
array($this, 'cb'),
|
array($this, 'cb'),
|
||||||
substr($arg, $toSkip)
|
substr($arg, $toSkip)
|
||||||
);
|
);
|
||||||
@@ -357,7 +357,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
} else {
|
} else {
|
||||||
return substr($value, 0, $toSkip)
|
return substr($value, 0, $toSkip)
|
||||||
. preg_replace_callback(
|
. preg_replace_callback(
|
||||||
'/(?=[`[\'":])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:))/s',
|
'/(?=[`[\'":])(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|:(\S*?:)([a-zA-Z0-9._]?))/s',
|
||||||
array($this, 'cb'),
|
array($this, 'cb'),
|
||||||
substr($value, $toSkip)
|
substr($value, $toSkip)
|
||||||
);
|
);
|
||||||
@@ -418,10 +418,11 @@ final class DibiTranslator extends DibiObject
|
|||||||
// [6] => string
|
// [6] => string
|
||||||
// [7] => lone-quote
|
// [7] => lone-quote
|
||||||
// [8] => substitution
|
// [8] => substitution
|
||||||
// [9] => modifier (when called from self::translate())
|
// [9] => substitution flag
|
||||||
|
// [10] => modifier (when called from self::translate())
|
||||||
|
|
||||||
if (!empty($matches[9])) { // modifier
|
if (!empty($matches[10])) { // modifier
|
||||||
$mod = $matches[9];
|
$mod = $matches[10];
|
||||||
$cursor = & $this->cursor;
|
$cursor = & $this->cursor;
|
||||||
|
|
||||||
if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') {
|
if ($cursor >= count($this->args) && $mod !== 'else' && $mod !== 'end') {
|
||||||
@@ -501,7 +502,9 @@ final class DibiTranslator extends DibiObject
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($matches[8]) { // SQL identifier substitution
|
if ($matches[8]) { // SQL identifier substitution
|
||||||
return $this->subCb(array(1 => substr($matches[8], 0, -1)));
|
$m = substr($matches[8], 0, -1);
|
||||||
|
$m = isset(dibi::$substs[$m]) ? dibi::$substs[$m] : call_user_func(dibi::$substFallBack, $m);
|
||||||
|
return $matches[9] == '' ? $this->formatValue($m, FALSE) : $m . $matches[9]; // value or identifier
|
||||||
}
|
}
|
||||||
|
|
||||||
die('this should be never executed');
|
die('this should be never executed');
|
||||||
@@ -536,15 +539,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
private static function subCb($m)
|
private static function subCb($m)
|
||||||
{
|
{
|
||||||
$m = $m[1];
|
$m = $m[1];
|
||||||
if (isset(dibi::$substs[$m])) {
|
return isset(dibi::$substs[$m]) ? dibi::$substs[$m] : call_user_func(dibi::$substFallBack, $m);
|
||||||
return dibi::$substs[$m];
|
|
||||||
|
|
||||||
} elseif (dibi::$substFallBack) {
|
|
||||||
return dibi::$substs[$m] = call_user_func(dibi::$substFallBack, $m);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return $m;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -36,10 +36,14 @@ dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
|
|||||||
// create substitution fallback
|
// create substitution fallback
|
||||||
function substFallBack($expr)
|
function substFallBack($expr)
|
||||||
{
|
{
|
||||||
|
if (defined($expr)) {
|
||||||
|
return constant($expr);
|
||||||
|
} else {
|
||||||
return 'the_' . $expr;
|
return 'the_' . $expr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dibi::setSubstFallBack('substFallBack');
|
dibi::setSubstFallBack('substFallBack');
|
||||||
|
|
||||||
dibi::test("UPDATE [:account:user] SET [name]='John Doe'");
|
dibi::test("UPDATE [:account:user] SET [name]='John Doe', [active]=:true:");
|
||||||
// -> UPDATE [the_accountuser] SET [name]='John Doe'
|
// -> UPDATE [the_accountuser] SET [name]='John Doe', [active]=1
|
Reference in New Issue
Block a user