1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-12 17:14:16 +02:00

substitutes moved from DibiDriver to Dibi

changed "comments" behavior in DibiParser
This commit is contained in:
David Grudl
2006-09-23 06:34:44 +00:00
parent da70be27a8
commit da608c2db2
14 changed files with 113 additions and 201 deletions

View File

@@ -34,13 +34,6 @@ abstract class DibiDriver
protected
$config;
/**
* Substitutions for identifiers
* @var array
*/
protected
$substs = array();
/**
* Describes how convert some datatypes to SQL command
* @var array
@@ -159,52 +152,4 @@ abstract class DibiDriver
/**
* Create a new substitution pair for indentifiers
* @param string from
* @param string to
* @return void
*/
public function addSubst($expr, $subst)
{
$this->substs[':'.$expr.':'] = $subst;
}
/**
* Remove substitution pair
* @param string from
* @return void
*/
public function removeSubst($expr)
{
unset($this->substs[':'.$expr.':']);
}
/**
* Apply substitutions to indentifier
* @param string indentifier
* @return string
*/
protected function applySubsts($value)
{
// apply substitutions
if ($this->substs && (strpos($value, ':') !== FALSE))
return strtr($value, $this->substs);
return $value;
}
} // class DibiDriver
?>

View File

@@ -65,6 +65,3 @@ function is_error($var)
{
return ($var === FALSE) || ($var instanceof Exception);
}
?>

View File

@@ -28,22 +28,30 @@ if (!defined('DIBI')) die();
class DibiParser
{
private
$driver,
$subK, $subV,
$modifier,
$hasError,
$driver,
$ifLevel,
$ifLevelStart;
public function __construct($driver, $subst)
{
$this->driver = $driver;
$this->subK = array_keys($subst);
$this->subV = array_values($subst);
}
/**
* Generates SQL
*
* @param array
* @return string
*/
public function parse($driver, $args)
public function parse($args)
{
$this->driver = $driver;
$this->hasError = FALSE;
$command = null;
$mod = & $this->modifier; // shortcut
@@ -72,7 +80,7 @@ class DibiParser
}
// simple string means SQL
if (is_string($arg) && (!$mod || 'p'==$mod)) {
if (is_string($arg) && (!$mod || 'p' == $mod)) {
$mod = FALSE;
// will generate new mod
$sql[] = $this->formatValue($arg, 'p');
@@ -88,9 +96,7 @@ class DibiParser
}
// default processing
$sql[] = $comment
? '...'
: $this->formatValue($arg, $mod);
if (!$comment) $sql[] = $this->formatValue($arg, $mod);
$mod = FALSE;
} // foreach
@@ -98,6 +104,9 @@ class DibiParser
$sql = implode(' ', $sql);
// remove comments
$sql = preg_replace('#\/\*.*?\*\/#s', '', $sql);
if ($this->hasError)
return new DibiException('Errors during generating SQL', array('sql' => $sql));
@@ -131,7 +140,7 @@ class DibiParser
} else $mod = FALSE;
// generate array
$vx[] = $this->driver->quoteName($pair[0]) . '=' . $this->formatValue($v, $mod);
$vx[] = $this->quoteName($pair[0]) . '=' . $this->formatValue($v, $mod);
}
return implode(', ', $vx);
@@ -151,7 +160,7 @@ class DibiParser
} else $mod = FALSE;
// generate arrays
$kx[] = $this->driver->quoteName($pair[0]);
$kx[] = $this->quoteName($pair[0]);
$vx[] = $this->formatValue($v, $mod);
}
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
@@ -197,7 +206,7 @@ class DibiParser
? strtotime($value)
: $value);
case 'n': // identifier name
return $this->driver->quoteName($value);
return $this->quoteName($value);
case 'p': // preserve as SQL
$value = (string) $value;
@@ -209,6 +218,7 @@ class DibiParser
// note: only this can change $this->modifier
return substr($value, 0, $toSkip)
/*
. preg_replace_callback('/
(?=`|\[|\'|"|%) ## speed-up
(?:
@@ -220,9 +230,11 @@ class DibiParser
%([a-zA-Z]{1,2})$| ## 8) right modifier
(\'|") ## 9) lone-quote
)/xs',
array($this, 'callback'),
substr($value, $toSkip)
);
*/
. preg_replace_callback('/(?=`|\[|\'|"|%)(?:`(.+?)`|\[(.+?)\]|(\')((?:\'\'|[^\'])*)\'|(")((?:""|[^"])*)"|%(else|end)|%([a-zA-Z]{1,2})$|(\'|"))/s',
array($this, 'cb'),
substr($value, $toSkip)
);
case 'a':
case 'v':
@@ -264,11 +276,11 @@ class DibiParser
/**
* PREG callback for @see self::translate()
* PREG callback for @see self::formatValue()
* @param array
* @return string
*/
private function callback($matches)
private function cb($matches)
{
// [1] => `ident`
// [2] => [ident]
@@ -280,26 +292,10 @@ class DibiParser
// [8] => right modifier
// [9] => lone-quote
if ($matches[1]) // SQL identifiers: `ident`
return $this->driver->quoteName($matches[1]);
if ($matches[2]) // SQL identifiers: [ident]
return $this->driver->quoteName($matches[2]);
if ($matches[3]) // SQL strings: '....'
return $this->comment
? '...'
: $this->driver->escape( strtr($matches[4], array("''" => "'")), TRUE);
if ($matches[5]) // SQL strings: "..."
return $this->comment
? '...'
: $this->driver->escape( strtr($matches[6], array('""' => '"')), TRUE);
if ($matches[7]) { // %end | %else
if (!empty($matches[7])) { // %end | %else
if (!$this->ifLevel) {
$this->hasError = TRUE;
return "**Unexpected condition $matches[8]**";
return "**Unexpected condition $matches[7]**";
}
if ('end' == $matches[7]) {
@@ -325,11 +321,26 @@ class DibiParser
}
}
if ($matches[8]) { // modifier
if (!empty($matches[8])) { // modifier
$this->modifier = $matches[8];
return '';
}
if ($this->comment) return '';
if ($matches[1]) // SQL identifiers: `ident`
return $this->quoteName($matches[1]);
if ($matches[2]) // SQL identifiers: [ident]
return $this->quoteName($matches[2]);
if ($matches[3]) // SQL strings: '....'
return $this->driver->escape( str_replace("''", "'", $matches[4]), TRUE);
if ($matches[5]) // SQL strings: "..."
return $this->driver->escape( str_replace('""', '"', $matches[6]), TRUE);
if ($matches[9]) { // string quote
$this->hasError = TRUE;
@@ -341,14 +352,21 @@ class DibiParser
/**
* Apply substitutions to indentifier and quotes it
* @param string indentifier
* @return string
*/
private function quoteName($value)
{
// apply substitutions
if ($this->subK && (strpos($value, ':') !== FALSE))
return str_replace($this->subK, $this->subV, $value);
return $this->driver->quoteName($value);
}
} // class DibiParser
?>

View File

@@ -373,7 +373,3 @@ class DibiResultIterator implements Iterator
} // class DibiResultIterator
?>