1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-13 10:36:22 +02: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 // libraries
require_once dirname(__FILE__).'/libs/driver.php'; require_once dirname(__FILE__).'/libs/driver.php';
require_once dirname(__FILE__).'/libs/resultset.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'; require_once dirname(__FILE__).'/libs/exception.php';
// required since PHP 5.1.0 // required since PHP 5.1.0
if (function_exists('date_default_timezone_set')) // if (function_exists('date_default_timezone_set'))
date_default_timezone_set('Europe/Prague'); // or 'GMT' // 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 * Creates a new DibiDriver object and connects it to specified database
* *
@ -243,8 +250,8 @@ class dibi
$args = func_get_args(); $args = func_get_args();
// and generate SQL // and generate SQL
$parser = new DibiParser(self::$conn, self::$substs); $trans = new DibiTranslator(self::$conn, self::$substs);
self::$sql = $parser->parse($args); self::$sql = $trans->translate($args);
if (is_error(self::$sql)) return self::$sql; // reraise the exception if (is_error(self::$sql)) return self::$sql; // reraise the exception
// execute SQL // execute SQL
@ -306,8 +313,8 @@ class dibi
$args = func_get_args(); $args = func_get_args();
// and generate SQL // and generate SQL
$parser = new DibiParser(self::$conn, self::$substs); $trans = new DibiTranslator(self::$conn, self::$substs);
$sql = $parser->parse($args); $sql = $trans->translate($args);
$dump = TRUE; // !!! $dump = TRUE; // !!!
if ($dump) { if ($dump) {
if (is_error($sql)) if (is_error($sql))

View File

@ -58,9 +58,9 @@ abstract class DibiDriver
/** /**
* Driver initialization * Protected constructor. Must be initialized using the factory method.
* * @see DibiDriver::connect()
* @param array connect configuration * @param array connect configuration
*/ */
protected function __construct($config) protected function __construct($config)
{ {
@ -70,7 +70,7 @@ abstract class DibiDriver
/** /**
* Get the configuration descriptor used by connect() to connect to database. * Get the configuration descriptor used by connect() to connect to database.
* @see connect() * @see DibiDriver::connect()
* @return array * @return array
*/ */
public function getConfig() public function getConfig()
@ -160,4 +160,13 @@ abstract class DibiDriver
abstract public function applyLimit(&$sql, $limit, $offset = 0); 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 } // class DibiDriver

View File

@ -291,6 +291,15 @@ abstract class DibiResult implements IteratorAggregate, Countable
} }
/** end required Countable functions */ /** 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 } // class DibiResult
@ -372,4 +381,5 @@ class DibiResultIterator implements Iterator
/** end required Iterator functions */ /** end required Iterator functions */
} // class DibiResultIterator } // class DibiResultIterator

View File

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