Source for file Common.php
Documentation is available at Common.php
// +----------------------------------------------------------------------+
// | PHP versions 4 and 5 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2004 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@backendmedia.com> |
// +----------------------------------------------------------------------+
// $Id: Common.php,v 1.31 2005/06/08 12:49:57 lsmith Exp $
require_once 'MDB2/LOB.php';
* @author Lukas Smith <smith@backendmedia.com>
* MDB2_Driver_Common: Base class that is extended by each MDB2 driver
* @author Lukas Smith <smith@backendmedia.com>
* contains all LOB objects created with this MDB2 instance
* 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 resource $result result identifier
* @param string $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
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
foreach ($types as $key => $type) {
'setResultTypes: ' . $type . ' is not a supported column type');
// {{{ _baseConvertResult()
* general type conversion method
* @param mixed $value refernce to a value to be converted
* @param int $type constant that specifies which type to convert to
* @return object a MDB2 error on failure
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$this->lobs[$lob_index]['lob_index'] = $lob_index;
'attempt to convert result value to an unknown type ' . $type);
* convert a value to a RDBMS indepdenant MDB2 type
* @param mixed $value value to be converted
* @param int $type constant that specifies which type to convert to
* @return mixed converted value or a MDB2 error on failure
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
// {{{ convertResultRow()
* @param resource $result result identifier
* @param array $row array with data
* @return mixed MDB2_OK on success, a MDB2 error on failure
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
foreach ($row as $key => $column) {
if (!isset ($column) || !isset ($types[$current_column])) {
$value = $this->convertResult($row[$key], $types[$current_column]);
if (PEAR ::isError ($value)) {
* 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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
return $db->raiseError ('type not defined: '. $type);
return $this->{" _get{$type}Declaration" }($name, $field);
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
if (isset ($field['unsigned']) && $field['unsigned']) {
$db->warnings [] = " unsigned integer field \"$name\" is being declared as signed integer";
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'integer') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' INT'. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'text') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
$type = isset ($field['length']) ? 'CHAR ('. $field['length']. ')' : 'TEXT';
return $name. ' '. $type. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
$type = isset ($field['length']) ? 'CHAR ('. $field['length']. ')' : 'TEXT';
return $name. ' '. $type. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
$type = isset ($field['length']) ? 'CHAR ('. $field['length']. ')' : 'TEXT';
return $name. ' '. $type. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'boolean') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' CHAR (1)'. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'date') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' CHAR ('. strlen('YYYY-MM-DD'). ')'. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'timestamp') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' CHAR ('. strlen('YYYY-MM-DD HH:MM:SS'). ')'. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'time') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' CHAR ('. strlen('HH:MM:SS'). ')'. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'float') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' TEXT'. $default. $notnull;
// {{{ _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.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$default = isset ($field['default']) ? ' DEFAULT '.
$this->quote($field['default'], 'decimal') : '';
$notnull = (isset ($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
return $name. ' TEXT'. $default. $notnull;
// {{{ compareDefinition()
* Obtain an array of changes that may need to applied
* @param array $current new definition
* @param array $previous old definition
* @return array containg all changes that will need to be applied
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$type = isset ($current['type']) ? $current['type'] : null;
'type "'. $current['type']. '" is not yet supported');
if (!isset ($previous['type']) || $previous['type'] != $type) {
$change = $this->{" _compare{$type}Definition" }($current, $previous);
$previous_notnull = isset ($previous['notnull']);
$notnull = isset ($current['notnull']);
if ($previous_notnull != $notnull) {
$change['changed_not_null'] = true;
$change['notnull'] = isset ($current['notnull']);
$previous_default = isset ($previous['default']);
$default = isset ($current['default']);
if ($previous_default != $default) {
$change['changed_default'] = true;
$change['default'] = $current['default'];
} elseif ($default && $previous['default']!= $current['default']) {
$change['changed_default'] = true;
$change['default'] = $current['default'];
// {{{ _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 containg all changes that will need to be applied
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$previous_unsigned = isset ($previous['unsigned']);
$unsigned = isset ($current['unsigned']);
if ($previous_unsigned != $unsigned) {
$change['unsigned'] = $unsigned;
// {{{ _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 containg all changes that will need to be applied
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$previous_length = (isset ($previous['length']) ? $previous['length'] : 0 );
$length = (isset ($current['length']) ? $current['length'] : 0 );
if ($previous_length != $length) {
$change['length'] = $length;
// {{{ _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 containg 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 containg 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 containg 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 containg 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 containg 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 containg 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 containg 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 containg 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
* @return string text string that represents the given argument value in
* a DBMS specific format.
function quote($value, $type = null , $quote = true )
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
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)) {
return $db->raiseError ('type not defined: '. $type);
$value = $this->{" _quote{$type}" }($value);
// ugly hack to remove single quotes
if (!$quote && isset ($value[0 ]) && $value[0 ] === "'") {
$value = substr($value, 1 , -1 );
* 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.
* 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 already contains any DBMS specific
* escaped character sequences.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
return "'". $db->escape ($value). "'";
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @return string text string that represents the given argument value in
* a DBMS specific format.
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
if (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
if ($match[1 ] == 'file://') {
$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.
* @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.
* @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.
* @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.
* @return string text string that represents the given argument value in
* a DBMS specific format.
return ($value ? "'Y'" : "'N'");
* 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.
* 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.
* 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.
* 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.
* 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.
* 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
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
$fp = fopen($file, 'wb');
$result = fread($lob, $db->options ['lob_buffer_length']);
if (fwrite($fp, $result, $read) != $read) {
'writeLOBToFile: could not write to the output file');
* retrieve LOB from the database
* @param resource $lob stream handle
* @return mixed MDB2_OK on success, a MDB2 error on failure
$lob['value'] = $lob['ressource'];
* 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 resource $lob stream handle
* @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 ($lob_index);
unset ($this->lobs[$lob_index]);
* Free any resources allocated during the lifetime of the large object
* @param int $lob_index from the lob array
function _destroyLOB ($lob_index)
* 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
$db = & $GLOBALS['_MDB2_databases'][$this->db_index];
if (!is_array($array) || empty ($array)) {
foreach ($array as $value) {
$return[] = $db->quote($value, $type);
Documentation generated on Mon, 11 Mar 2019 14:19:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|