mirror of
https://github.com/dg/dibi.git
synced 2025-08-10 16:14:57 +02:00
Substitution moved from class dibi to DibiConnection
This commit is contained in:
@@ -147,9 +147,6 @@ class dibi
|
|||||||
/** @var DibiConnection Current connection */
|
/** @var DibiConnection Current connection */
|
||||||
private static $connection;
|
private static $connection;
|
||||||
|
|
||||||
/** @var DibiLazyStorage Substitutions for identifiers */
|
|
||||||
public static $substs;
|
|
||||||
|
|
||||||
/** @var array @see addHandler */
|
/** @var array @see addHandler */
|
||||||
private static $handlers = array();
|
private static $handlers = array();
|
||||||
|
|
||||||
@@ -610,6 +607,17 @@ class dibi
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns substitution hashmap - Monostate for DibiConnection::getSubstitutes().
|
||||||
|
* @return DibiLazyStorage
|
||||||
|
*/
|
||||||
|
public static function getSubstitutes()
|
||||||
|
{
|
||||||
|
return self::getConnection()->getSubstitutes();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new substitution pair for indentifiers.
|
* Create a new substitution pair for indentifiers.
|
||||||
* @param string from
|
* @param string from
|
||||||
@@ -618,7 +626,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function addSubst($expr, $subst)
|
public static function addSubst($expr, $subst)
|
||||||
{
|
{
|
||||||
self::$substs->$expr = $subst;
|
self::getSubstitutes()->$expr = $subst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -630,10 +638,13 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function removeSubst($expr)
|
public static function removeSubst($expr)
|
||||||
{
|
{
|
||||||
|
$substitutes = self::getSubstitutes();
|
||||||
if ($expr === TRUE) {
|
if ($expr === TRUE) {
|
||||||
self::$substs = new DibiLazyStorage(self::$substs->getCallback());
|
foreach ($substitutes as $expr => $foo) {
|
||||||
|
unset($substitutes->$expr);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
unset(self::$substs->$expr);
|
unset($substitutes->$expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -646,19 +657,7 @@ class dibi
|
|||||||
*/
|
*/
|
||||||
public static function setSubstFallback($callback)
|
public static function setSubstFallback($callback)
|
||||||
{
|
{
|
||||||
self::$substs->setCallback($callback);
|
self::getSubstitutes()->setCallback($callback);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default substitution fallback handler.
|
|
||||||
* @param string
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function defaultSubstFallback($expr)
|
|
||||||
{
|
|
||||||
return ":$expr:";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -730,8 +729,3 @@ class dibi
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// static constructor
|
|
||||||
dibi::$substs = new DibiLazyStorage(array('dibi', 'defaultSubstFallback'));
|
|
@@ -42,6 +42,9 @@ class DibiConnection extends DibiObject
|
|||||||
/** @var bool Is connected? */
|
/** @var bool Is connected? */
|
||||||
private $connected = FALSE;
|
private $connected = FALSE;
|
||||||
|
|
||||||
|
/** @var DibiLazyStorage Substitutes for identifiers */
|
||||||
|
private $substitutes;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,7 +102,7 @@ class DibiConnection extends DibiObject
|
|||||||
$config['name'] = $name;
|
$config['name'] = $name;
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
$this->driver = new $class;
|
$this->driver = new $class;
|
||||||
$this->translator = new DibiTranslator($this->driver);
|
$this->translator = new DibiTranslator($this);
|
||||||
|
|
||||||
// profiler
|
// profiler
|
||||||
$profilerCfg = & $config['profiler'];
|
$profilerCfg = & $config['profiler'];
|
||||||
@@ -119,9 +122,10 @@ class DibiConnection extends DibiObject
|
|||||||
$this->setProfiler(new $class($profilerCfg));
|
$this->setProfiler(new $class($profilerCfg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->substitutes = new DibiLazyStorage(create_function('$expr', 'return ":$expr:";'));
|
||||||
if (!empty($config['substitutes'])) {
|
if (!empty($config['substitutes'])) {
|
||||||
foreach ($config['substitutes'] as $key => $value) {
|
foreach ($config['substitutes'] as $key => $value) {
|
||||||
dibi::addSubst($key, $value);
|
$this->substitutes->$key = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,6 +586,42 @@ class DibiConnection extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********************* substitutions ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns substitution hashmap.
|
||||||
|
* @return DibiLazyStorage
|
||||||
|
*/
|
||||||
|
public function getSubstitutes()
|
||||||
|
{
|
||||||
|
return $this->substitutes;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides substitution.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function substitute($value)
|
||||||
|
{
|
||||||
|
return strpos($value, ':') === FALSE ? $value : preg_replace_callback('#:([^:\s]*):#', array($this, 'subCb'), $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Substitution callback.
|
||||||
|
*/
|
||||||
|
private function subCb($m)
|
||||||
|
{
|
||||||
|
return $this->substitutes->{$m[1]};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/********************* shortcuts ****************d*g**/
|
/********************* shortcuts ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
@@ -19,6 +19,9 @@
|
|||||||
*/
|
*/
|
||||||
final class DibiTranslator extends DibiObject
|
final class DibiTranslator extends DibiObject
|
||||||
{
|
{
|
||||||
|
/** @var DibiConnection */
|
||||||
|
private $connection;
|
||||||
|
|
||||||
/** @var IDibiDriver */
|
/** @var IDibiDriver */
|
||||||
private $driver;
|
private $driver;
|
||||||
|
|
||||||
@@ -51,9 +54,9 @@ final class DibiTranslator extends DibiObject
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function __construct(IDibiDriver $driver)
|
public function __construct(DibiConnection $connection)
|
||||||
{
|
{
|
||||||
$this->driver = $driver;
|
$this->connection = $connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -67,6 +70,8 @@ final class DibiTranslator extends DibiObject
|
|||||||
public function translate(array $args)
|
public function translate(array $args)
|
||||||
{
|
{
|
||||||
$this->identifiers = new DibiLazyStorage(array($this, 'delimite'));
|
$this->identifiers = new DibiLazyStorage(array($this, 'delimite'));
|
||||||
|
$this->driver = $this->connection->getDriver();
|
||||||
|
|
||||||
$args = array_values($args);
|
$args = array_values($args);
|
||||||
while (count($args) === 1 && is_array($args[0])) { // implicit array expansion
|
while (count($args) === 1 && is_array($args[0])) { // implicit array expansion
|
||||||
$args = array_values($args[0]);
|
$args = array_values($args[0]);
|
||||||
@@ -302,7 +307,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
|
|
||||||
case 'ex':
|
case 'ex':
|
||||||
case 'sql':
|
case 'sql':
|
||||||
$translator = new self($this->driver);
|
$translator = new self($this->connection);
|
||||||
return $translator->translate($value);
|
return $translator->translate($value);
|
||||||
|
|
||||||
default: // value, value, value - all with the same modifier
|
default: // value, value, value - all with the same modifier
|
||||||
@@ -555,7 +560,8 @@ final class DibiTranslator extends DibiObject
|
|||||||
|
|
||||||
if ($matches[8]) { // SQL identifier substitution
|
if ($matches[8]) { // SQL identifier substitution
|
||||||
$m = substr($matches[8], 0, -1);
|
$m = substr($matches[8], 0, -1);
|
||||||
return $matches[9] == '' ? $this->formatValue(dibi::$substs->$m, FALSE) : dibi::$substs->$m . $matches[9]; // value or identifier
|
$m = $this->connection->getSubstitutes()->$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');
|
||||||
@@ -571,7 +577,7 @@ final class DibiTranslator extends DibiObject
|
|||||||
*/
|
*/
|
||||||
public function delimite($value)
|
public function delimite($value)
|
||||||
{
|
{
|
||||||
$value = self::substitute($value);
|
$value = $this->connection->substitute($value);
|
||||||
$parts = explode('.', $value);
|
$parts = explode('.', $value);
|
||||||
foreach ($parts as & $v) {
|
foreach ($parts as & $v) {
|
||||||
if ($v !== '*') $v = $this->driver->escape($v, dibi::IDENTIFIER);
|
if ($v !== '*') $v = $this->driver->escape($v, dibi::IDENTIFIER);
|
||||||
@@ -579,30 +585,4 @@ final class DibiTranslator extends DibiObject
|
|||||||
return implode('.', $parts);
|
return implode('.', $parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides substitution.
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public static function substitute($value)
|
|
||||||
{
|
|
||||||
if (strpos($value, ':') !== FALSE) { // provide substitution
|
|
||||||
return preg_replace_callback('#:([^:\s]*):#', array(__CLASS__, 'subCb'), $value);
|
|
||||||
}
|
|
||||||
return $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Substitution callback.
|
|
||||||
* @param array
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private static function subCb($m)
|
|
||||||
{
|
|
||||||
return dibi::$substs->{$m[1]};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user