1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-09 07:37:08 +02:00

Substitution moved from class dibi to DibiConnection

This commit is contained in:
David Grudl
2011-01-25 17:41:44 +01:00
parent 88cccc0543
commit 26de1aebc0
3 changed files with 71 additions and 57 deletions

View File

@@ -147,9 +147,6 @@ class dibi
/** @var DibiConnection Current connection */
private static $connection;
/** @var DibiLazyStorage Substitutions for identifiers */
public static $substs;
/** @var array @see addHandler */
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.
* @param string from
@@ -618,7 +626,7 @@ class dibi
*/
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)
{
$substitutes = self::getSubstitutes();
if ($expr === TRUE) {
self::$substs = new DibiLazyStorage(self::$substs->getCallback());
foreach ($substitutes as $expr => $foo) {
unset($substitutes->$expr);
}
} else {
unset(self::$substs->$expr);
unset($substitutes->$expr);
}
}
@@ -646,19 +657,7 @@ class dibi
*/
public static function setSubstFallback($callback)
{
self::$substs->setCallback($callback);
}
/**
* Default substitution fallback handler.
* @param string
* @return mixed
*/
public static function defaultSubstFallback($expr)
{
return ":$expr:";
self::getSubstitutes()->setCallback($callback);
}
@@ -730,8 +729,3 @@ class dibi
}
}
// static constructor
dibi::$substs = new DibiLazyStorage(array('dibi', 'defaultSubstFallback'));

View File

@@ -42,6 +42,9 @@ class DibiConnection extends DibiObject
/** @var bool Is connected? */
private $connected = FALSE;
/** @var DibiLazyStorage Substitutes for identifiers */
private $substitutes;
/**
@@ -99,7 +102,7 @@ class DibiConnection extends DibiObject
$config['name'] = $name;
$this->config = $config;
$this->driver = new $class;
$this->translator = new DibiTranslator($this->driver);
$this->translator = new DibiTranslator($this);
// profiler
$profilerCfg = & $config['profiler'];
@@ -119,9 +122,10 @@ class DibiConnection extends DibiObject
$this->setProfiler(new $class($profilerCfg));
}
$this->substitutes = new DibiLazyStorage(create_function('$expr', 'return ":$expr:";'));
if (!empty($config['substitutes'])) {
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**/

View File

@@ -19,6 +19,9 @@
*/
final class DibiTranslator extends DibiObject
{
/** @var DibiConnection */
private $connection;
/** @var IDibiDriver */
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)
{
$this->identifiers = new DibiLazyStorage(array($this, 'delimite'));
$this->driver = $this->connection->getDriver();
$args = array_values($args);
while (count($args) === 1 && is_array($args[0])) { // implicit array expansion
$args = array_values($args[0]);
@@ -302,7 +307,7 @@ final class DibiTranslator extends DibiObject
case 'ex':
case 'sql':
$translator = new self($this->driver);
$translator = new self($this->connection);
return $translator->translate($value);
default: // value, value, value - all with the same modifier
@@ -555,7 +560,8 @@ final class DibiTranslator extends DibiObject
if ($matches[8]) { // SQL identifier substitution
$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');
@@ -571,7 +577,7 @@ final class DibiTranslator extends DibiObject
*/
public function delimite($value)
{
$value = self::substitute($value);
$value = $this->connection->substitute($value);
$parts = explode('.', $value);
foreach ($parts as & $v) {
if ($v !== '*') $v = $this->driver->escape($v, dibi::IDENTIFIER);
@@ -579,30 +585,4 @@ final class DibiTranslator extends DibiObject
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]};
}
}