mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 05:37:39 +02:00
- substitution is performed by DibiTranslator
- fixed bug in DibiTranslator
This commit is contained in:
@@ -131,10 +131,10 @@ class dibi
|
|||||||
private static $connection;
|
private static $connection;
|
||||||
|
|
||||||
/** @var array Substitutions for identifiers */
|
/** @var array Substitutions for identifiers */
|
||||||
private static $substs = array();
|
public static $substs = array();
|
||||||
|
|
||||||
/** @var callback Substitution fallback */
|
/** @var callback Substitution fallback */
|
||||||
private static $substFallBack;
|
public static $substFallBack;
|
||||||
|
|
||||||
/** @var array @see addHandler */
|
/** @var array @see addHandler */
|
||||||
private static $handlers = array();
|
private static $handlers = array();
|
||||||
@@ -590,22 +590,6 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove substitution pair.
|
* Remove substitution pair.
|
||||||
* @param mixed from or TRUE
|
* @param mixed from or TRUE
|
||||||
@@ -623,39 +607,17 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides substitution.
|
* Sets substitution fallback handler.
|
||||||
* @param string
|
* @param callback
|
||||||
* @return string
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function substitute($value)
|
public static function setSubstFallback($callback)
|
||||||
{
|
{
|
||||||
if (strpos($value, ':') === FALSE) {
|
if (!is_callable($callback)) {
|
||||||
return $value;
|
throw new InvalidArgumentException("Invalid callback.");
|
||||||
|
|
||||||
} else {
|
|
||||||
return preg_replace_callback('#:(.*):#U', array('dibi', 'subCb'), $value);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
self::$substFallBack = $callback;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -123,8 +123,8 @@ final class DibiTranslator extends DibiObject
|
|||||||
\[(.+?)\]| ## 2) [identifier]
|
\[(.+?)\]| ## 2) [identifier]
|
||||||
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
|
(\')((?:\'\'|[^\'])*)\'| ## 3,4) string
|
||||||
(")((?:""|[^"])*)"| ## 5,6) "string"
|
(")((?:""|[^"])*)"| ## 5,6) "string"
|
||||||
(\'|") ## 7) lone-quote
|
(\'|")| ## 7) lone quote
|
||||||
%([a-zA-Z]{1,4})(?![a-zA-Z])|## 8) modifier
|
%([a-zA-Z]{1,4})(?![a-zA-Z]) ## 8) modifier
|
||||||
)/xs',
|
)/xs',
|
||||||
*/ // note: this can change $this->args & $this->cursor & ...
|
*/ // note: this can change $this->args & $this->cursor & ...
|
||||||
. preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
|
. preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|")|%([a-zA-Z]{1,4})(?![a-zA-Z]))/s',
|
||||||
@@ -351,7 +351,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
return $value;
|
return $value;
|
||||||
} else {
|
} else {
|
||||||
return substr($value, 0, $toSkip)
|
return substr($value, 0, $toSkip)
|
||||||
. preg_replace_callback('/(?=`|\[|\'|")(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"(\'|"))/s',
|
. preg_replace_callback('/(?=`|\[|\'|")(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|(\'|"))/s',
|
||||||
array($this, 'cb'),
|
array($this, 'cb'),
|
||||||
substr($value, $toSkip)
|
substr($value, $toSkip)
|
||||||
);
|
);
|
||||||
@@ -505,8 +505,35 @@ final class DibiTranslator extends DibiObject
|
|||||||
*/
|
*/
|
||||||
private function delimite($value)
|
private function delimite($value)
|
||||||
{
|
{
|
||||||
return $value === '*' ? '*' : $this->driver->escape(dibi::substitute($value), dibi::IDENTIFIER);
|
if ($value === '*') {
|
||||||
|
return '*';
|
||||||
|
|
||||||
|
} elseif (strpos($value, ':') !== FALSE) { // provide substitution
|
||||||
|
$value = preg_replace_callback('#:(.*):#U', array(__CLASS__, 'subCb'), $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->driver->escape($value, dibi::IDENTIFIER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // class DibiTranslator
|
|
||||||
|
/**
|
||||||
|
* Substitution callback.
|
||||||
|
* @param array
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private static function subCb($m)
|
||||||
|
{
|
||||||
|
$m = $m[1];
|
||||||
|
if (isset(dibi::$substs[$m])) {
|
||||||
|
return dibi::$substs[$m];
|
||||||
|
|
||||||
|
} elseif (dibi::$substFallBack) {
|
||||||
|
return dibi::$substs[$m] = call_user_func(dibi::$substFallBack, $m);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return $m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user