Source for file BaseConnector.php
Documentation is available at BaseConnector.php
///////////////////////////////////////////////////////////////////////////////
* This file contains the abstract BaseConnector class.
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* The Connector library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* {@link http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License}
* @author Per Egil Roksvaag
* @copyright 2009 Per Egil Roksvaag
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
///////////////////////////////////////////////////////////////////////////////
* Include the {@link IConnector} interface.
require_once("IConnector.php");
///////////////////////////////////////////////////////////////////////////////
* Include the {@link TypeValidator} class and the global function
require_once("TypeValidator.php");
///////////////////////////////////////////////////////////////////////////////
* The BaseConnector implements common tasks for all IConnector
* classes in this package.
* You can't create an instance of this class (it's abstract).
* <b>Extend</b> it to create <b>new IConnector classes</b>,
* e.g. for PHP database extensions not contained in this package.
* @tutorial connector.pkg#extend
///////////////////////////////////////////////////////////////////////////
* @var resource The PHP database link identifier.
///////////////////////////////////////////////////////////////////////////
* @var array An associated array of cached query results.
///////////////////////////////////////////////////////////////////////////
* @var array Default values for all global options.
* @tutorial connector.pkg#options.element
self::CHAR_APPLICATION =>
"UTF-8",
self::CHAR_DATABASE =>
null,
self::LOG_DEBUG =>
false,
self::PARAM_CAST_NUMERIC =>
true,
self::PARAM_NAMED =>
false,
self::PARAM_PREFIX =>
"?",
self::PARAM_QUERIES =>
false,
self::PARAM_STRIP_TAGS =>
false,
self::PARAM_STRIP_MAGIC =>
false,
self::PARAM_TRIM =>
false,
self::RESULT_CACHE =>
true
// self::RESULT_LENGTH only allowed as a local select() option.
// self::RESULT_OFFSET only allowed as a local select() option.
// self::RESULT_KEY_FIELD only allowed as a local select() option.
///////////////////////////////////////////////////////////////////////////
* Call this method from descendent constructors to set global options.
* You can't create an instance of this class (it's abstract).
* @param array $options An associative array of options, see the {@tutorial connector.pkg#options.element}.
///////////////////////////////////////////////////////////////////////////
* Begins a transaction on the current connection.
* The current transaction includes all statements on the connection that
* were executed after the call to transaction() and before any calls
* to rollback() or commit().
* @see IConnector::transaction().
* @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
* @return bool true if the transaction was successfully begun, false otherwise.
///////////////////////////////////////////////////////////////////////////
* Commits the current transaction on the current connection.
* The current transaction includes all statements on the connection that
* were executed after the call to transaction() and before any calls
* to rollback() or commit().
* @see IConnector::commit().
* @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
* @return bool true if the transaction was successfully committed, false otherwise.
public function commit($options =
array())
///////////////////////////////////////////////////////////////////////////
* Rolls back the current transaction on the current connection.
* The current transaction includes all statements on the connection that
* were executed after the call to transaction() and before any calls
* to rollback() or commit().
* @see IConnector::rollback().
* @param array $options An associated array of options, see the {@tutorial connector.pkg#options.element}.
* @return bool true if the transaction was successfully rolled back, false otherwise.
public function rollback($options =
array())
///////////////////////////////////////////////////////////////////////////
* Get the database link identifier used by the IConnector instance.
* @return resource A PHP database link identifier.
///////////////////////////////////////////////////////////////////////////
* Get an array of all global options or the value of the global
* option defined in the optional $element parameter.
* @param string $element An option constant, see the {@tutorial connector.pkg#options.element}.
* @return mixed All options as an associated array or the value of a single option.
///////////////////////////////////////////////////////////////////////////
* Set one or more global options.
* Some options are only valid as constructor or function parameters.
* @param mixed $options Multiple options as an associated array or the value of a single option.
* @param string $element An option constant, see the {@tutorial connector.pkg#options.element}.
* @return mixed The previous option value(s).
public function setOptions($options, $element =
null)
$this->options[$element] =
$options;
foreach($options as $element =>
$value)
///////////////////////////////////////////////////////////////////////////
* Calculate the md5 hash value of the query string and - if necessary -
* the parameters of a parameterized query.
* @param string $query The modified SQL query string after binding.
* @param array $stack An array of parameters to be used in a parameterized query.
* @param array $options An associated array of options.
* @return string The md5 hash value.
protected function getHash($query, $stack =
array(), $options =
array())
return $stack &&
$this->lookup(self::PARAM_QUERIES)
///////////////////////////////////////////////////////////////////////////
* Check if a maching query result exists in the result cache.
* If true, set the <var>$table</var> argument to the cached query result,
* otherwise <var>$table</var> is set to an empty array.
* @param string $hash A hash value of a query, see {@link getHash()}.
* @param array &$table If found, return the cached query result, otherwise an empty array.
* @param array $options An associated array of options.
* @return bool true if a maching query result is found, false otherwise.
protected function getCache($hash, &$table, $options =
array())
if($this->lookup(self::RESULT_CACHE, $options))
$table =
$this->cache[$hash];
///////////////////////////////////////////////////////////////////////////
* Add a new query result to the result cache.
* @param string $hash A hash value of a query, see {@link getHash()}.
* @param array $table The query result to cache.
* @param array $options An associated array of options.
* @return int The number of cached query results.
protected function setCache($hash, $table, $options =
array())
if($this->lookup(self::RESULT_CACHE, $options))
$this->cache[$hash] =
$table;
///////////////////////////////////////////////////////////////////////////
* Get an option value. Any options in the local $options array
* overrides the global options.
* @param string $element An option constant, see the {@tutorial connector.pkg#options.element}.
* @param array $options An associated array of options.
* @param mixed $default The default value to return if no global or local options are set.
* @return mixed The value of a single option.
protected function lookup($element, $options =
array(), $default =
null)
return $options[$element];
///////////////////////////////////////////////////////////////////////////
* Replace the query placeholders with matching parameter values or - if
* parameterized queries are enabled - the database specific placeholder.
* Parameter values are converted, stripped, escaped, encoded and quoted
* according to the option values. The $stack returns an array of values
* for each placeholder to be used in parameterized queries.
* @param string $query A SQL query string.
* @param array $param An associated array of values to replace the $query placeholders.
* @param array &$stack Returns an array of values for use in parameterized queries.
* @param array $options An associated array of options.
* @return string The modified SQL query.
protected function bind($query, $param, &$stack =
array(), $options =
array())
for($i =
0; $i <
$count; $i++
)
list
($match, $name) =
$matches[$i];
$sql =
$this->cast($param[$name], $name, $stack, $options);
$result.=
$sql !==
null ?
$sql :
$match;
///////////////////////////////////////////////////////////////////////////
* Push a parameter on the stack and return a query placeholder.
* @param mixed $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Push parameters on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL placeholder.
protected function push($value, $name, &$stack, $options =
array())
if($this->lookup(self::PARAM_NAMED, $options))
$prefix =
$this->lookup(self::PARAM_PREFIX, $options);
$prefix =
$this->lookup(self::PARAM_PREFIX, $options);
///////////////////////////////////////////////////////////////////////////
* Cast a parameter to a valid SQL string or placeholder. The parameter is
* also pushed on the $stack - and if necessary converted - to be used in
* @param mixed $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Push parameters on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function cast($value, $name, &$stack, $options =
array())
if(is_null($value)) return $this->castNull($value, $name, $stack, $options);
if(is_bool($value)) return $this->castBool($value, $name, $stack, $options);
if(is_int($value)) return $this->castInt($value, $name, $stack, $options);
if(is_date($value)) return $this->castDate($value, $name, $stack, $options);
///////////////////////////////////////////////////////////////////////////
* Cast null to a valid SQL string.
* @param null $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Push parameters on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string.
protected function castNull($value, $name, &$stack, $options =
array())
///////////////////////////////////////////////////////////////////////////
* Cast a boolean parameter to a valid SQL string or placeholder.
* The parameter is also pushed on the $stack and converted
* to be used in parameterized queries.
* @param bool $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
///////////////////////////////////////////////////////////////////////////
protected function castBool($value, $name, &$stack, $options =
array())
return $this->lookup(self::PARAM_QUERIES, $options)
?
$this->push((int)
$value, $name, $stack, $options)
///////////////////////////////////////////////////////////////////////////
* Cast an integer parameter to a valid SQL string or placeholder.
* The parameter is also pushed on the $stack to be used in parameterized
* @param int $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castInt($value, $name, &$stack, $options =
array())
return $this->lookup(self::PARAM_QUERIES, $options)
?
$this->push($value, $name, $stack, $options)
///////////////////////////////////////////////////////////////////////////
* Cast a float parameter to a valid SQL string or placeholder.
* The parameter is also pushed on the $stack to be used in parameterized
* @param float $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castFloat($value, $name, &$stack, $options =
array())
return $this->lookup(self::PARAM_QUERIES, $options)
?
$this->push($value, $name, $stack, $options)
///////////////////////////////////////////////////////////////////////////
* Cast a DateTime parameter to a valid SQL string or placeholder.
* The parameter is also pushed on the $stack to be used in parameterized
* @param DateTime $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castDate($value, $name, &$stack, $options =
array())
if($this->lookup(self::PARAM_QUERIES, $options))
return $this->push($value, $name, $stack, $options);
///////////////////////////////////////////////////////////////////////////
* Cast a numeric string parameter to a valid SQL string or placeholder.
* The parameter is also pushed on the $stack and converted to be used in
* @param string $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castNumeric($value, $name, &$stack, $options =
array())
if($this->lookup(self::PARAM_CAST_NUMERIC, $options))
if((string)
$int ===
$value)
return $this->castInt($int, $name, $stack, $options);
if((string)
$float ===
$value)
return $this->castFloat($float, $name, $stack, $options);
return $this->castString($value, $name, $stack, $options);
///////////////////////////////////////////////////////////////////////////
* Cast a string parameter to a valid SQL string or placeholder.
* The parameter is also pushed on the $stack and modified to be used in
* String values are converted, stripped, escaped, encoded and quoted
* according to the option values.
* @param string $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castString($value, $name, &$stack, $options =
array())
$value =
$this->strStrip($value, $options);
if($this->lookup(self::PARAM_QUERIES, $options))
return $this->push($value, $name, $stack, $options);
return $this->strQuote($value, $options);
///////////////////////////////////////////////////////////////////////////
* With the exception of DataTime values, objects are not supported as
* parameters. Override this method to serialize your objects as
* @param object $value The parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castObject($value, $name, &$stack, $options =
array())
///////////////////////////////////////////////////////////////////////////
* Arrays are converted to a comma separated list of values.
* Strings are escaped and quoted. No placeholders are returend.
* Any arrays within the array are ignored. If the array is empty,
* the string "NULL" is returned.
* @param array $list An array of parameter to replace the query placeholder.
* @param string $name The name of the query placeholder.
* @param array &$stack Parameters are pushed on this array for use in parameterized queries.
* @param array $options An associated array of options.
* @return string A valid SQL string or placeholder.
protected function castArray($list, $name, &$stack, $options =
array())
$options[self::PARAM_QUERIES] =
false;
if(is_null($value)) $sql[] =
$this->castNull($value, $name, $stack, $options);
else if(is_bool($value)) $sql[] =
$this->castBool($value, $name, $stack, $options);
else if(is_int($value)) $sql[] =
$this->castInt($value, $name, $stack, $options);
else if(is_float($value)) $sql[] =
$this->castFloat($value, $name, $stack, $options);
else if(is_date($value)) $sql[] =
$this->castDate($value, $name, $stack, $options);
///////////////////////////////////////////////////////////////////////////
* Strip spare or insecure parts from a string parameter
* according to the current options.
* @param string $value The string that will be stripped.
* @param array $options An associated array of options.
* @return string The stripped string.
protected function strStrip($value, $options =
array())
if($this->lookup(self::PARAM_STRIP_MAGIC, $options))
if($this->lookup(self::PARAM_STRIP_TAGS, $options))
if($this->lookup(self::PARAM_TRIM, $options))
///////////////////////////////////////////////////////////////////////////
* Escape special characters in a string parameter.
* @param string $value The string to be escaped.
* @param array $options An associated array of options.
* @return string The escaped string.
protected function strEscape($value, $options =
array())
///////////////////////////////////////////////////////////////////////////
* Add surrounding quotes to a string parameter.
* @param string $value The string to be quoted.
* @param array $options An associated array of options.
* @return string The quoted string.
protected function strQuote($value, $options =
array())
///////////////////////////////////////////////////////////////////////////
* Converts character encoding of string parameters to the character
* set of the database according to the current options.
* @param string $value The string that will be converted.
* @param array $options An associated array of options.
* @return string The converted string.
protected function strEncode($value, $options =
array())
$db =
$this->lookup(self::CHAR_DATABASE, $options);
$app =
$this->lookup(self::CHAR_APPLICATION, $options);
if($db &&
$app &&
$db !=
$app)
return iconv($app, $db, $value);
foreach($value as $key =>
$cell)
$value[$key] =
iconv($app, $db, $cell);
///////////////////////////////////////////////////////////////////////////
* Converts character encoding of from the character
* set of the database according to the current options.
* @param string $value The string that will be converted.
* @param array $options An associated array of options.
* @return string The converted string.
protected function strDecode($value, $options =
array())
$db =
$this->lookup(self::CHAR_DATABASE, $options);
$app =
$this->lookup(self::CHAR_APPLICATION, $options);
if($db &&
$app &&
$db !=
$app)
return iconv($db, $app, $value);
foreach($value as $key =>
$cell)
$value[$key] =
iconv($db, $app, $cell);
///////////////////////////////////////////////////////////////////////////
Documentation generated on Wed, 03 Jun 2009 12:41:50 +0200 by phpDocumentor 1.4.1