Source for file common.php
Documentation is available at common.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* Contains the DB_common base class
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @author Stig Bakken <ssb@php.net>
* @author Tomas V.V. Cox <cox@idecnet.com>
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: common.php,v 1.144 2007/11/26 22:54:03 aharvey Exp $
* @link http://pear.php.net/package/DB
* Obtain the PEAR class so it can be extended from
* DB_common is the base class from which each database driver class extends
* All common methods are declared here. If a given DBMS driver contains
* a particular method, that method will overload the one here.
* @author Stig Bakken <ssb@php.net>
* @author Tomas V.V. Cox <cox@idecnet.com>
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 1.7.14RC1
* @link http://pear.php.net/package/DB
* The current default fetch mode
* The name of the class into which results should be fetched when
* DB_FETCHMODE_OBJECT is in effect
* Was a connection present when the object was serialized()?
* @see DB_common::__sleep(), DB_common::__wake()
* The most recently executed query
* Run-time configuration options
* The 'optimize' option has been deprecated. Use the 'portability'
* @see DB_common::setOption()
'result_buffering' => 500 ,
'seqname_format' => '%s_seq',
'portability' => DB_PORTABILITY_NONE ,
'optimize' => 'performance', // Deprecated. Use 'portability'.
* The parameters from the most recently executed query
* @since Property available since Release 1.7.0
* The elements from each prepared statement
* The data types of the various elements in each prepared statement
* Flag indicating that the last query was a manipulation query.
* Flag indicating that the next query <em>must</em> be a manipulation
* This constructor calls <kbd>$this->PEAR('DB_Error')</kbd>
* Automatically indicates which properties should be saved
* when PHP's serialize() function is called
* @return array the array of properties names that should be saved
// Don't disconnect(), people use serialize() for many reasons
if (isset ($this->autocommit)) {
return array ('autocommit',
'fetchmode_object_class',
'fetchmode_object_class',
* Automatically reconnects to the database when PHP's unserialize()
* The reconnection attempt is only performed if the object was connected
* at the time PHP's serialize() function was run.
$this->connect ($this->dsn, $this->options);
* Automatic string conversion for PHP 5
* @return string a string describing the current PEAR DB object
* @since Method available since Release 1.7.0
$info .= ': (phptype=' . $this->phptype .
', dbsyntax=' . $this->dbsyntax .
* DEPRECATED: String conversion method
* @return string a string describing the current PEAR DB object
* @deprecated Method deprecated in Release 1.7.0
* DEPRECATED: Quotes a string so it can be safely used within string
* @param string $string the string to be quoted
* @return string the quoted string
* @see DB_common::quoteSmart(), DB_common::escapeSimple()
* @deprecated Method deprecated some time before Release 1.2
$string = $this->quote($string);
return substr($string, 1 , -1 );
* DEPRECATED: Quotes a string so it can be safely used in a query
* @param string $string the string to quote
* @return string the quoted string or the string <samp>NULL</samp>
* if the value submitted is <kbd>null</kbd>.
* @see DB_common::quoteSmart(), DB_common::escapeSimple()
* @deprecated Deprecated in release 1.6.0
function quote($string = null )
return ($string === null ) ? 'NULL'
* Quotes a string so it can be safely used as a table or column name
* Delimiting style depends on which database driver is being used.
* NOTE: just because you CAN use delimited identifiers doesn't mean
* you SHOULD use them. In general, they end up causing way more
* problems than they solve.
* Portability is broken by using the following characters inside
* + backtick (<kbd>`</kbd>) -- due to MySQL
* + double quote (<kbd>"</kbd>) -- due to Oracle
* + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
* Delimited identifiers are known to generally work correctly under
* + sybase (must execute <kbd>set quoted_identifier on</kbd> sometime
* InterBase doesn't seem to be able to use delimited identifiers
* via PHP 4. They work fine under PHP 5.
* @param string $str the identifier name to be quoted
* @return string the quoted identifier
* @since Method available since Release 1.6.0
* Formats input so it can be safely used in a query
* The output depends on the PHP data type of input and the database
* @param mixed $in the data to be formatted
* @return mixed the formatted data. The format depends on the input's
* <kbd>input</kbd> -> <samp>returns</samp>
* <kbd>null</kbd> -> the string <samp>NULL</samp>
* <kbd>integer</kbd> or <kbd>double</kbd> -> the unquoted number
* <kbd>bool</kbd> -> output depends on the driver in use
* Most drivers return integers: <samp>1</samp> if
* <kbd>true</kbd> or <samp>0</samp> if
* Some return strings: <samp>TRUE</samp> if
* <kbd>true</kbd> or <samp>FALSE</samp> if
* Finally one returns strings: <samp>T</samp> if
* <kbd>true</kbd> or <samp>F</samp> if
* <kbd>false</kbd>. Here is a list of each DBMS,
* the values returned and the suggested column type:
* <kbd>dbase</kbd> -> <samp>T/F</samp>
* <kbd>fbase</kbd> -> <samp>TRUE/FALSE</samp>
* <kbd>ibase</kbd> -> <samp>1/0</samp>
* (<kbd>SMALLINT</kbd>) [1]
* <kbd>ifx</kbd> -> <samp>1/0</samp>
* (<kbd>SMALLINT</kbd>) [1]
* <kbd>msql</kbd> -> <samp>1/0</samp>
* <kbd>mssql</kbd> -> <samp>1/0</samp>
* <kbd>mysql</kbd> -> <samp>1/0</samp>
* (<kbd>TINYINT(1)</kbd>)
* <kbd>mysqli</kbd> -> <samp>1/0</samp>
* (<kbd>TINYINT(1)</kbd>)
* <kbd>oci8</kbd> -> <samp>1/0</samp>
* <kbd>odbc</kbd> -> <samp>1/0</samp>
* (<kbd>SMALLINT</kbd>) [1]
* <kbd>pgsql</kbd> -> <samp>TRUE/FALSE</samp>
* <kbd>sqlite</kbd> -> <samp>1/0</samp>
* <kbd>sybase</kbd> -> <samp>1/0</samp>
* (<kbd>TINYINT(1)</kbd>)
* [1] Accommodate the lowest common denominator because not all
* versions of have <kbd>BOOLEAN</kbd>.
* other (including strings and numeric strings) ->
* the data with single quotes escaped by preceeding
* single quotes, backslashes are escaped by preceeding
* backslashes, then the whole string is encapsulated
* @see DB_common::escapeSimple()
* @since Method available since Release 1.6.0
if ($this->dbsyntax == 'access'
* Formats a boolean value for use within a query in a locale-independent
* @param boolean the boolean value to be quoted.
* @return string the quoted string.
* @see DB_common::quoteSmart()
* @since Method available since release 1.7.8.
return $boolean ? '1' : '0';
* Formats a float value for use within a query in a locale-independent
* @param float the float value to be quoted.
* @return string the quoted string.
* @see DB_common::quoteSmart()
* @since Method available since release 1.7.8.
* Escapes a string according to the current DBMS's standards
* In SQLite, this makes things safe for inserts/updates, but may
* cause problems when performing text comparisons against columns
* containing binary data. See the
* {@link http://php.net/sqlite_escape_string PHP manual} for more info.
* @param string $str the string to be escaped
* @return string the escaped string
* @see DB_common::quoteSmart()
* @since Method available since Release 1.6.0
* Tells whether the present driver supports a given feature
* @param string $feature the feature you're curious about
* @return bool whether this driver supports $feature
return $this->features[$feature];
* Sets the fetch mode that should be used by default for query results
* @param integer $fetchmode DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC
* @param string $object_class the class name of the object to be returned
* by the fetch methods when the
* DB_FETCHMODE_OBJECT mode is selected.
* If no class is specified by default a cast
* to object from the assoc array row will be
* done. There is also the posibility to use
* and extend the 'DB_row' class.
* @see DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT
function setFetchMode($fetchmode, $object_class = 'stdClass')
return $this->raiseError('invalid fetchmode mode');
* Sets run-time configuration options for PEAR DB
* Options, their data types, default values and description:
* <var>autofree</var> <kbd>boolean</kbd> = <samp>false</samp>
* <br />should results be freed automatically when there are no
* <var>result_buffering</var> <kbd>integer</kbd> = <samp>500</samp>
* <br />how many rows of the result set should be buffered?
* <br />In mysql: mysql_unbuffered_query() is used instead of
* mysql_query() if this value is 0. (Release 1.7.0)
* <br />In oci8: this value is passed to ocisetprefetch().
* <var>debug</var> <kbd>integer</kbd> = <samp>0</samp>
* <var>persistent</var> <kbd>boolean</kbd> = <samp>false</samp>
* <br />should the connection be persistent?
* <var>portability</var> <kbd>integer</kbd> = <samp>DB_PORTABILITY_NONE</samp>
* <br />portability mode constant (see below)
* <var>seqname_format</var> <kbd>string</kbd> = <samp>%s_seq</samp>
* <br />the sprintf() format string used on sequence names. This
* format is applied to sequence names passed to
* createSequence(), nextID() and dropSequence().
* <var>ssl</var> <kbd>boolean</kbd> = <samp>false</samp>
* <br />use ssl to connect?
* -----------------------------------------
* These modes are bitwised, so they can be combined using <kbd>|</kbd>
* and removed using <kbd>^</kbd>. See the examples section below on how
* <samp>DB_PORTABILITY_NONE</samp>
* turn off all portability features
* This mode gets automatically turned on if the deprecated
* <var>optimize</var> option gets set to <samp>performance</samp>.
* <samp>DB_PORTABILITY_LOWERCASE</samp>
* convert names of tables and fields to lower case when using
* <kbd>get*()</kbd>, <kbd>fetch*()</kbd> and <kbd>tableInfo()</kbd>
* This mode gets automatically turned on in the following databases
* if the deprecated option <var>optimize</var> gets set to
* <samp>portability</samp>:
* <samp>DB_PORTABILITY_RTRIM</samp>
* right trim the data output by <kbd>get*()</kbd> <kbd>fetch*()</kbd>
* <samp>DB_PORTABILITY_DELETE_COUNT</samp>
* force reporting the number of rows deleted
* Some DBMS's don't count the number of rows deleted when performing
* simple <kbd>DELETE FROM tablename</kbd> queries. This portability
* mode tricks such DBMS's into telling the count by adding
* <samp>WHERE 1=1</samp> to the end of <kbd>DELETE</kbd> queries.
* This mode gets automatically turned on in the following databases
* if the deprecated option <var>optimize</var> gets set to
* <samp>portability</samp>:
* <samp>DB_PORTABILITY_NUMROWS</samp>
* enable hack that makes <kbd>numRows()</kbd> work in Oracle
* This mode gets automatically turned on in the following databases
* if the deprecated option <var>optimize</var> gets set to
* <samp>portability</samp>:
* <samp>DB_PORTABILITY_ERRORS</samp>
* makes certain error messages in certain drivers compatible
* with those from other DBMS's
* + mysql, mysqli: change unique/primary key constraints
* DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
* + odbc(access): MS's ODBC driver reports 'no such field' as code
* 07001, which means 'too few parameters.' When this option is on
* that code gets mapped to DB_ERROR_NOSUCHFIELD.
* DB_ERROR_MISMATCH -> DB_ERROR_NOSUCHFIELD
* <samp>DB_PORTABILITY_NULL_TO_EMPTY</samp>
* convert null values to empty strings in data output by get*() and
* fetch*(). Needed because Oracle considers empty strings to be null,
* while most other DBMS's know the difference between empty and null.
* <samp>DB_PORTABILITY_ALL</samp>
* turn on all portability features
* -----------------------------------------
* Example 1. Simple setOption() example
* $db->setOption('autofree', true);
* Example 2. Portability for lowercasing and trimming
* $db->setOption('portability',
* DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM);
* Example 3. All portability options except trimming
* $db->setOption('portability',
* DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM);
* @param string $option option name
* @param mixed $value value for the option
* @return int DB_OK on success. A DB_Error object on failure.
* @see DB_common::$options
if (isset ($this->options[$option]))
|