Source for file Common.php
Documentation is available at Common.php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Lukas Smith <smith@pooteeweet.org> |
// +----------------------------------------------------------------------+
// $Id: Common.php 328137 2012-10-25 02:26:35Z danielc $
require_once 'MDB2/LOB.php';
* @author Lukas Smith <smith@pooteeweet.org>
* MDB2_Driver_Common: Base class that is extended by each MDB2 driver
* To load this module in the MDB2 object:
* $mdb->loadModule('Datatype');
* @author Lukas Smith <smith@pooteeweet.org>
'timestamp' => '1970-01-01 00:00:00',
* contains all LOB objects created with this MDB2 instance
* Get the list of valid types
* This function returns an array of valid types as keys with the values
* being possible default values for all native datatypes and mapped types
* @return mixed array on success, a MDB2 error on failure
if (!empty ($db->options ['datatype_map'])) {
foreach ($db->options ['datatype_map'] as $type => $mapped_type) {
$types[$type] = $types[$mapped_type];
} elseif (!empty ($db->options ['datatype_map_callback'][$type])) {
$parameter = array ('type' => $type, 'mapped_type' => $mapped_type);
$default = call_user_func_array($db->options ['datatype_map_callback'][$type], array (&$db, __FUNCTION__ , $parameter));
$types[$type] = $default;
// {{{ checkResultTypes()
* Define the list of types to be associated with the columns of a given
* This function may be called before invoking fetchRow(), fetchOne()
* fetchCole() and fetchAll() so that the necessary data type
* conversions are performed on the data to be retrieved by them. If this
* function is not called, the type of all result set columns is assumed
* to be text, thus leading to not perform any conversions.
* @param array $types array variable that lists the
* data types to be expected in the result set columns. If this array
* contains less types than the number of columns that are returned
* in the result set, the remaining columns are assumed to be of the
* type text. Currently, the types clob and blob are not fully
* @return mixed MDB2_OK on success, a MDB2 error on failure
$types = is_array($types) ? $types : array ($types);
foreach ($types as $key => $type) {
if (empty ($db->options ['datatype_map'][$type])) {
$type. ' for '. $key. ' is not a supported column type', __FUNCTION__ );
// {{{ _baseConvertResult()
* General type conversion method
* @param mixed $value reference to a value to be converted
* @param string $type specifies which type to convert to
* @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return object an MDB2 error on failure
$this->lobs[$lob_index]['lob_index'] = $lob_index;
'attempt to convert result value to an unknown type :' . $type, __FUNCTION__ );
* Convert a value to a RDBMS indipendent MDB2 type
* @param mixed $value value to be converted
* @param string $type specifies which type to convert to
* @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return mixed converted value
if (!empty ($db->options ['datatype_map'][$type])) {
$type = $db->options ['datatype_map'][$type];
if (!empty ($db->options ['datatype_map_callback'][$type])) {
$parameter = array ('type' => $type, 'value' => $value, 'rtrim' => $rtrim);
return call_user_func_array($db->options ['datatype_map_callback'][$type], array (&$db, __FUNCTION__ , $parameter));
// {{{ convertResultRow()
* @param array $row specifies the types to convert to
* @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
* @return mixed MDB2_OK on success, an MDB2 error on failure
//$types = $this->_sortResultFieldTypes(array_keys($row), $types);
foreach ($row as $key => $value) {
if (empty ($types[$key])) {
// {{{ _sortResultFieldTypes()
* @param array $row specifies the types to convert to
* @param bool $rtrim if to rtrim text values or not
* @return mixed MDB2_OK on success, a MDB2 error on failure
$n_cols = count($columns);
$n_types = count($types);
if ($n_cols > $n_types) {
for ($i= $n_cols - $n_types; $i >= 0; $i-- ) {
foreach ($columns as $col) {
$sorted_types[$col] = null;
foreach ($types as $name => $type) {
$sorted_types[$name] = $type;
// if there are left types in the array, fill the null values of the
// sorted array with them, in order.
if (null === $sorted_types[$k]) {
$sorted_types[$k] = current($types);
* Obtain DBMS specific SQL code portion needed to declare
* @param string $type type to which the value should be converted to
* @param string $name name the field to be declared.
* @param string $field definition of the field
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
if (!empty ($db->options ['datatype_map'][$type])) {
$type = $db->options ['datatype_map'][$type];
if (!empty ($db->options ['datatype_map_callback'][$type])) {
$parameter = array ('type' => $type, 'name' => $name, 'field' => $field);
return call_user_func_array($db->options ['datatype_map_callback'][$type], array (&$db, __FUNCTION__ , $parameter));
'type not defined: '. $type, __FUNCTION__ );
return $this->{" _get{$type}Declaration" }($name, $field);
// {{{ getTypeDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an text type
* field to be used in statements like CREATE TABLE.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Integer value that determines the maximum length of the text
* field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
* Text value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
switch ($field['type']) {
$length = !empty ($field['length']) ? $field['length'] : $db->options ['default_text_field_length'];
$fixed = !empty ($field['fixed']) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR('. $length. ')' : 'CHAR('. $db->options ['default_text_field_length']. ')')
: ($length ? 'VARCHAR('. $length. ')' : 'TEXT');
return 'CHAR ('. strlen('YYYY-MM-DD'). ')';
return 'CHAR ('. strlen('HH:MM:SS'). ')';
return 'CHAR ('. strlen('YYYY-MM-DD HH:MM:SS'). ')';
* Obtain DBMS specific SQL code portion needed to declare a generic type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Integer value that determines the maximum length of the text
* field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
* Text value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* Text value with the default CHARACTER SET for this field.
* Text value with the default COLLATION for this field.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field, or a MDB2_Error on failure
$name = $db->quoteIdentifier ($name, true );
return $declaration_options;
// {{{ _getDeclarationOptions()
* Obtain DBMS specific SQL code portion needed to declare a generic type
* field to be used in statement like CREATE TABLE, without the field name
* and type values (ie. just the character set, default value, if the
* field is permitted to be NULL or not, and the collation options).
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Text value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* Text value with the default CHARACTER SET for this field.
* Text value with the default COLLATION for this field.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field's options.
$charset = empty ($field['charset']) ? '' :
' '. $this->_getCharsetFieldDeclaration ($field['charset']);
$notnull = empty ($field['notnull']) ? '' : ' NOT NULL';
if ($field['default'] === '') {
$field['default'] = $valid_default_values[$field['type']];
if (null !== $field['default']) {
$default = ' DEFAULT ' . $this->quote($field['default'], $field['type']);
$collation = empty ($field['collation']) ? '' :
' '. $this->_getCollationFieldDeclaration ($field['collation']);
return $charset. $default. $notnull. $collation;
// {{{ _getCharsetFieldDeclaration()
* Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
* of a field declaration to be used in statements like CREATE TABLE.
* @param string $charset name of the charset
* @return string DBMS specific SQL code portion needed to set the CHARACTER SET
* of a field declaration.
function _getCharsetFieldDeclaration ($charset)
// {{{ _getCollationFieldDeclaration()
* Obtain DBMS specific SQL code portion needed to set the COLLATION
* of a field declaration to be used in statements like CREATE TABLE.
* @param string $collation name of the collation
* @return string DBMS specific SQL code portion needed to set the COLLATION
* of a field declaration.
function _getCollationFieldDeclaration ($collation)
// {{{ _getIntegerDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an integer type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Boolean flag that indicates whether the field should be
* declared as unsigned integer if possible.
* Integer value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
if (!empty ($field['unsigned'])) {
$db->warnings [] = " unsigned integer field \"$name\" is being declared as signed integer";
// {{{ _getTextDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an text type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Integer value that determines the maximum length of the text
* field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
* Text value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ _getCLOBDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an character
* large object type field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Integer value that determines the maximum length of the large
* object field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
$notnull = empty ($field['notnull']) ? '' : ' NOT NULL';
$name = $db->quoteIdentifier ($name, true );
// {{{ _getBLOBDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an binary large
* object type field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Integer value that determines the maximum length of the large
* object field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
$notnull = empty ($field['notnull']) ? '' : ' NOT NULL';
$name = $db->quoteIdentifier ($name, true );
// {{{ _getBooleanDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a boolean type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Boolean value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ _getDateDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a date type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Date value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ _getTimestampDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a timestamp
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Timestamp value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ _getTimeDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a time
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Time value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ _getFloatDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a float type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Float value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ _getDecimalDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a decimal type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param array $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Decimal value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
// {{{ compareDefinition()
* Obtain an array of changes that may need to applied
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
$type = !empty ($current['type']) ? $current['type'] : null;
if (!empty ($db->options ['datatype_map_callback'][$type])) {
$parameter = array ('current' => $current, 'previous' => $previous);
$change = call_user_func_array($db->options ['datatype_map_callback'][$type], array (&$db, __FUNCTION__ , $parameter));
'type "'. $current['type']. '" is not yet supported', __FUNCTION__ );
if (empty ($previous['type']) || $previous['type'] != $type) {
$change = $this->{" _compare{$type}Definition" }($current, $previous);
if ($previous['type'] != $type) {
$previous_notnull = !empty ($previous['notnull']) ? $previous['notnull'] : false;
$notnull = !empty ($current['notnull']) ? $current['notnull'] : false;
if ($previous_notnull != $notnull) {
$change['notnull'] = true;
$previous_default = array_key_exists('default', $previous) ? $previous['default'] :
if ($previous_default !== $default) {
$change['default'] = true;
// {{{ _compareIntegerDefinition()
* Obtain an array of changes that may need to applied to an integer field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
$previous_length = !empty ($previous['length']) ? $previous['length'] : 4;
$length = !empty ($current['length']) ? $current['length'] : 4;
if ($previous_length != $length) {
$change['length'] = $length;
$previous_unsigned = !empty ($previous['unsigned']) ? $previous['unsigned'] : false;
$unsigned = !empty ($current['unsigned']) ? $current['unsigned'] : false;
if ($previous_unsigned != $unsigned) {
$change['unsigned'] = true;
$previous_autoincrement = !empty ($previous['autoincrement']) ? $previous['autoincrement'] : false;
$autoincrement = !empty ($current['autoincrement']) ? $current['autoincrement'] : false;
if ($previous_autoincrement != $autoincrement) {
$change['autoincrement'] = true;
// {{{ _compareTextDefinition()
* Obtain an array of changes that may need to applied to an text field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
$previous_length = !empty ($previous['length']) ? $previous['length'] : 0;
$length = !empty ($current['length']) ? $current['length'] : 0;
if ($previous_length != $length) {
$change['length'] = true;
$previous_fixed = !empty ($previous['fixed']) ? $previous['fixed'] : 0;
$fixed = !empty ($current['fixed']) ? $current['fixed'] : 0;
if ($previous_fixed != $fixed) {
// {{{ _compareCLOBDefinition()
* Obtain an array of changes that may need to applied to an CLOB field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareBLOBDefinition()
* Obtain an array of changes that may need to applied to an BLOB field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareDateDefinition()
* Obtain an array of changes that may need to applied to an date field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareTimeDefinition()
* Obtain an array of changes that may need to applied to an time field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareTimestampDefinition()
* Obtain an array of changes that may need to applied to an timestamp field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareBooleanDefinition()
* Obtain an array of changes that may need to applied to an boolean field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareFloatDefinition()
* Obtain an array of changes that may need to applied to an float field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
// {{{ _compareDecimalDefinition()
* Obtain an array of changes that may need to applied to an decimal field
* @param array $current new definition
* @param array $previous old definition
* @return array containing all changes that will need to be applied
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param string $type type to which the value should be converted to
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function quote($value, $type = null , $quote = true , $escape_wildcards = false )
// todo: default to decimal as float is quite unusual
if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/', $value)) {
} elseif (preg_match('/^\d{2}:\d{2}$/', $value)) {
} elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
} elseif (!empty ($db->options ['datatype_map'][$type])) {
$type = $db->options ['datatype_map'][$type];
if (!empty ($db->options ['datatype_map_callback'][$type])) {
$parameter = array ('type' => $type, 'value' => $value, 'quote' => $quote, 'escape_wildcards' => $escape_wildcards);
return call_user_func_array($db->options ['datatype_map_callback'][$type], array (&$db, __FUNCTION__ , $parameter));
'type not defined: '. $type, __FUNCTION__ );
$value = $this->{" _quote{$type}" }($value, $quote, $escape_wildcards);
if ($quote && $escape_wildcards && $db->string_quoting ['escape_pattern']
&& $db->string_quoting ['escape'] !== $db->string_quoting ['escape_pattern']
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that already contains any DBMS specific
* escaped character sequences.
function _quoteText($value, $quote, $escape_wildcards)
$value = $db->escape ($value, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @return string text string that represents the given argument value in
* a DBMS specific format.
if (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
$value = @fopen($value, 'r');
$value.= @fread($fp, $db->options ['lob_buffer_length']);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function _quoteLOB($value, $quote, $escape_wildcards)
if ($db->options ['lob_allow_url_include']) {
return $this->_quoteText($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function _quoteCLOB($value, $quote, $escape_wildcards)
return $this->_quoteLOB($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function _quoteBLOB($value, $quote, $escape_wildcards)
return $this->_quoteLOB($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function _quoteDate($value, $quote, $escape_wildcards)
if ($value === 'CURRENT_DATE') {
if (isset ($db->function ) && is_object($this->function ) && is_a($db->function , 'MDB2_Driver_Function_Common')) {
return $db->function ->now ('date');
return $this->_quoteText($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
if ($value === 'CURRENT_TIMESTAMP') {
if (isset ($db->function ) && is_object($db->function ) && is_a($db->function , 'MDB2_Driver_Function_Common')) {
return $db->function ->now ('timestamp');
return 'CURRENT_TIMESTAMP';
return $this->_quoteText($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function _quoteTime($value, $quote, $escape_wildcards)
if ($value === 'CURRENT_TIME') {
if (isset ($db->function ) && is_object($this->function ) && is_a($db->function , 'MDB2_Driver_Function_Common')) {
return $db->function ->now ('time');
return $this->_quoteText($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
function _quoteFloat($value, $quote, $escape_wildcards)
if (preg_match('/^(.*)e([-+])(\d+)$/i', $value, $matches)) {
$decimal = $this->_quoteDecimal($matches[1 ], $quote, $escape_wildcards);
$exponent = str_pad($matches[3 ], 2 , '0', STR_PAD_LEFT );
$value = $decimal. 'E'. $sign. $exponent;
$value = $this->_quoteDecimal($value, $quote, $escape_wildcards);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @param bool $quote determines if the value should be quoted and escaped
* @param bool $escape_wildcards if to escape escape wildcards
* @return string text string that represents the given argument value in
* a DBMS specific format.
// convert the last "," to a "."
// convert the last "," to a "."
* retrieve LOB from the database
* @param resource $lob stream handle
* @param string $file name of the file into which the LOb should be fetched
* @return mixed MDB2_OK on success, a MDB2 error on failure
if (preg_match('/^(\w+:\/\/)(.*)$/', $file, $match)) {
if ($match[1 ] == 'file://') {
$fp = @fopen($file, 'wb');
$result = @fread($lob, $db->options ['lob_buffer_length']);
if (@fwrite($fp, $result, $read) != $read) {
'could not write to the output file', __FUNCTION__ );
* retrieve LOB from the database
* @param array $lob array
* @return mixed MDB2_OK on success, a MDB2 error on failure
if (null === $lob['value']) {
$lob['value'] = $lob['resource'];
* Read data from large object input stream.
* @param resource $lob stream handle
* @param string $data reference to a variable that will hold data
* to be read from the large object input stream
* @param integer $length value that indicates the largest ammount ofdata
* to be read from the large object input stream.
* @return mixed the effective number of bytes read from the large object
* input stream on sucess or an MDB2 error object.
return substr($lob['value'], $lob['position'], $length);
* Determine whether it was reached the end of the large object and
* therefore there is no more data to be read for the its input stream.
* @param array $lob array
* @return mixed true or false on success, a MDB2 error on failure
* Free any resources allocated during the lifetime of the large object
* @param resource $lob stream handle
$lob_index = $lob_data['wrapper_data']->lob_index;
if (isset ($this->lobs[$lob_index])) {
$this->_destroyLOB ($this->lobs[$lob_index]);
unset ($this->lobs[$lob_index]);
* Free any resources allocated during the lifetime of the large object
* @param array $lob array
function _destroyLOB (&$lob)
* apply a type to all values of an array and return as a comma seperated string
* useful for generating IN statements
* @param array $array data array
* @param string $type determines type of the field
* @return string comma seperated values
if (!is_array($array) || empty ($array)) {
foreach ($array as $value) {
$return[] = $this->quote($value, $type);
* build a pattern matching string
* @param array $pattern even keys are strings, odd are patterns (% and _)
* @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
* @param string $field optional field name that is being matched against
* (might be required when emulating ILIKE)
* @return string SQL pattern
function matchPattern($pattern, $operator = null , $field = null )
if (null !== $operator) {
'case insensitive LIKE matching requires passing the field name', __FUNCTION__ );
$db->loadModule ('Function', null , true );
$match = $db->function ->lower ($field). ' LIKE ';
'case insensitive NOT ILIKE matching requires passing the field name', __FUNCTION__ );
$db->loadModule ('Function', null , true );
$match = $db->function ->lower ($field). ' NOT LIKE ';
$match = (null === $field) ? 'LIKE ' : ($field. ' LIKE ');
$match = (null === $field) ? 'NOT LIKE ' : ($field. ' NOT LIKE ');
'not a supported operator type:'. $operator, __FUNCTION__ );
foreach ($pattern as $key => $value) {
$escaped = $db->escape ($value);
$match.= $db->escapePattern ($escaped);
// {{{ patternEscapeString()
* build string to define pattern escape character
* @return string define pattern escape character
// {{{ mapNativeDatatype()
* Maps a native array description of a field to a MDB2 datatype and length
* @param array $field native field description
* @return array containing the various possible types, length, sign, fixed
// If the user has specified an option to map the native field
// type to a custom MDB2 datatype...
$db_type = strtok($field['type'], '(), ');
if (!empty ($db->options ['nativetype_map_callback'][$db_type])) {
// Otherwise perform the built-in (i.e. normal) MDB2 native type to
// MDB2 datatype conversion
// {{{ _mapNativeDatatype()
* Maps a native array description of a field to a MDB2 datatype and length
* @param array $field native field description
* @return array containing the various possible types, length, sign, fixed
'method not implemented', __FUNCTION__ );
// {{{ mapPrepareDatatype()
* Maps an mdb2 datatype to mysqli prepare type
if (!empty ($db->options ['datatype_map'][$type])) {
$type = $db->options ['datatype_map'][$type];
if (!empty ($db->options ['datatype_map_callback'][$type])) {
$parameter = array ('type' => $type);
return call_user_func_array($db->options ['datatype_map_callback'][$type], array (&$db, __FUNCTION__ , $parameter));
Documentation generated on Mon, 11 Mar 2019 15:51:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|