1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 09:53:11 +01:00

renamed parser -> translator

added "Undefined property usage prevention"
This commit is contained in:
David Grudl 2006-10-26 13:09:56 +00:00
parent d705f4089d
commit 11b294be44
4 changed files with 54 additions and 20 deletions

View File

@ -28,14 +28,14 @@ if (version_compare(PHP_VERSION , '5.0.3', '<'))
// libraries
require_once dirname(__FILE__).'/libs/driver.php';
require_once dirname(__FILE__).'/libs/resultset.php';
require_once dirname(__FILE__).'/libs/parser.php';
require_once dirname(__FILE__).'/libs/translator.php';
require_once dirname(__FILE__).'/libs/exception.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set'))
date_default_timezone_set('Europe/Prague'); // or 'GMT'
// if (function_exists('date_default_timezone_set'))
// date_default_timezone_set('Europe/Prague'); // or 'GMT'
@ -125,6 +125,13 @@ class dibi
/**
* Monostate class
*/
private function __construct()
{}
/**
* Creates a new DibiDriver object and connects it to specified database
*
@ -243,8 +250,8 @@ class dibi
$args = func_get_args();
// and generate SQL
$parser = new DibiParser(self::$conn, self::$substs);
self::$sql = $parser->parse($args);
$trans = new DibiTranslator(self::$conn, self::$substs);
self::$sql = $trans->translate($args);
if (is_error(self::$sql)) return self::$sql; // reraise the exception
// execute SQL
@ -306,8 +313,8 @@ class dibi
$args = func_get_args();
// and generate SQL
$parser = new DibiParser(self::$conn, self::$substs);
$sql = $parser->parse($args);
$trans = new DibiTranslator(self::$conn, self::$substs);
$sql = $trans->translate($args);
$dump = TRUE; // !!!
if ($dump) {
if (is_error($sql))

View File

@ -58,9 +58,9 @@ abstract class DibiDriver
/**
* Driver initialization
*
* @param array connect configuration
* Protected constructor. Must be initialized using the factory method.
* @see DibiDriver::connect()
* @param array connect configuration
*/
protected function __construct($config)
{
@ -70,7 +70,7 @@ abstract class DibiDriver
/**
* Get the configuration descriptor used by connect() to connect to database.
* @see connect()
* @see DibiDriver::connect()
* @return array
*/
public function getConfig()
@ -160,4 +160,13 @@ abstract class DibiDriver
abstract public function applyLimit(&$sql, $limit, $offset = 0);
/**
* Undefined property usage prevention
*/
function __get($nm) { throw new Exception("Undefined property '" . get_class($this) . "::$$nm'"); }
function __set($nm, $val) { $this->__get($nm); }
private function __unset($nm) { $this->__get($nm); }
private function __isset($nm) { $this->__get($nm); }
} // class DibiDriver

View File

@ -291,6 +291,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
}
/** end required Countable functions */
/**
* Undefined property usage prevention
*/
function __get($nm) { throw new Exception("Undefined property '" . get_class($this) . "::$$nm'"); }
function __set($nm, $val) { $this->__get($nm); }
private function __unset($nm) { $this->__get($nm); }
private function __isset($nm) { $this->__get($nm); }
} // class DibiResult
@ -372,4 +381,5 @@ class DibiResultIterator implements Iterator
/** end required Iterator functions */
} // class DibiResultIterator

View File

@ -22,16 +22,17 @@ if (!defined('DIBI')) die();
/**
* dibi parser
* dibi translator
*
*/
class DibiParser
class DibiTranslator
{
private
$driver,
$subK, $subV,
$modifier,
$hasError,
$comment,
$ifLevel,
$ifLevelStart;
@ -50,7 +51,7 @@ class DibiParser
* @param array
* @return string
*/
public function parse($args)
public function translate($args)
{
$this->hasError = FALSE;
$command = null;
@ -140,7 +141,7 @@ class DibiParser
} else $mod = FALSE;
// generate array
$vx[] = $this->quoteName($pair[0]) . '=' . $this->formatValue($v, $mod);
$vx[] = $this->quote($pair[0]) . '=' . $this->formatValue($v, $mod);
}
return implode(', ', $vx);
@ -160,7 +161,7 @@ class DibiParser
} else $mod = FALSE;
// generate arrays
$kx[] = $this->quoteName($pair[0]);
$kx[] = $this->quote($pair[0]);
$vx[] = $this->formatValue($v, $mod);
}
return '(' . implode(', ', $kx) . ') VALUES (' . implode(', ', $vx) . ')';
@ -206,7 +207,7 @@ class DibiParser
? strtotime($value)
: $value);
case 'n': // identifier name
return $this->quoteName($value);
return $this->quote($value);
case 'p': // preserve as SQL
$value = (string) $value;
@ -330,10 +331,10 @@ class DibiParser
if ($matches[1]) // SQL identifiers: `ident`
return $this->quoteName($matches[1]);
return $this->quote($matches[1]);
if ($matches[2]) // SQL identifiers: [ident]
return $this->quoteName($matches[2]);
return $this->quote($matches[2]);
if ($matches[3]) // SQL strings: '....'
return $this->driver->escape( str_replace("''", "'", $matches[4]), TRUE);
@ -357,7 +358,7 @@ class DibiParser
* @param string indentifier
* @return string
*/
private function quoteName($value)
private function quote($value)
{
// apply substitutions
if ($this->subK && (strpos($value, ':') !== FALSE))
@ -368,5 +369,12 @@ class DibiParser
/**
* Undefined property usage prevention
*/
function __get($nm) { throw new Exception("Undefined property '" . get_class($this) . "::$$nm'"); }
function __set($nm, $val) { $this->__get($nm); }
private function __unset($nm) { $this->__get($nm); }
private function __isset($nm) { $this->__get($nm); }
} // class DibiParser