From: <var...@us...> - 2012-11-20 11:33:20
|
Revision: 8495 http://phpwiki.svn.sourceforge.net/phpwiki/?rev=8495&view=rev Author: vargenau Date: 2012-11-20 11:33:07 +0000 (Tue, 20 Nov 2012) Log Message: ----------- Update to DB-1.7.14 Modified Paths: -------------- trunk/lib/pear/DB/common.php trunk/lib/pear/DB/dbase.php trunk/lib/pear/DB/fbsql.php trunk/lib/pear/DB/ibase.php trunk/lib/pear/DB/ifx.php trunk/lib/pear/DB/msql.php trunk/lib/pear/DB/mssql.php trunk/lib/pear/DB/mysql.php trunk/lib/pear/DB/mysqli.php trunk/lib/pear/DB/oci8.php trunk/lib/pear/DB/odbc.php trunk/lib/pear/DB/pgsql.php trunk/lib/pear/DB/sqlite.php trunk/lib/pear/DB/storage.php trunk/lib/pear/DB/sybase.php trunk/lib/pear/DB.php Modified: trunk/lib/pear/DB/common.php =================================================================== --- trunk/lib/pear/DB/common.php 2012-11-20 09:06:44 UTC (rev 8494) +++ trunk/lib/pear/DB/common.php 2012-11-20 11:33:07 UTC (rev 8495) @@ -1,152 +1,248 @@ <?php -/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */ -// +----------------------------------------------------------------------+ -// | PHP Version 4 | -// +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2004 The PHP Group | -// +----------------------------------------------------------------------+ -// | This source file is subject to version 2.02 of the PHP license, | -// | that is bundled with this package in the file LICENSE, and is | -// | available at through the world-wide-web at | -// | http://www.php.net/license/2_02.txt. | -// | If you did not receive a copy of the PHP license and are unable to | -// | obtain it through the world-wide-web, please send a note to | -// | li...@ph... so we can mail you a copy immediately. | -// +----------------------------------------------------------------------+ -// | Author: Stig Bakken <ss...@ph...> | -// | Tomas V.V.Cox <co...@id...> | -// | Maintainer: Daniel Convissor <da...@ph...> | -// +----------------------------------------------------------------------+ +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ + +/** + * Contains the DB_common base class + * + * PHP versions 4 and 5 + * + * 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 li...@ph... so we can mail you a copy immediately. + * + * @category Database + * @package DB + * @author Stig Bakken <ss...@ph...> + * @author Tomas V.V. Cox <co...@id...> + * @author Daniel Convissor <da...@ph...> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version CVS: $Id: common.php 315557 2011-08-26 14:32:35Z danielc $ + * @link http://pear.php.net/package/DB + */ + +/** + * Obtain the PEAR class so it can be extended from + */ require_once 'PEAR.php'; /** - * DB_common is a base class for DB implementations, and must be - * inherited by all such + * DB_common is the base class from which each database driver class extends * - * @package DB - * @version - * @category Database - * @author Stig Bakken <ss...@ph...> - * @author Tomas V.V.Cox <co...@id...> + * All common methods are declared here. If a given DBMS driver contains + * a particular method, that method will overload the one here. + * + * @category Database + * @package DB + * @author Stig Bakken <ss...@ph...> + * @author Tomas V.V. Cox <co...@id...> + * @author Daniel Convissor <da...@ph...> + * @copyright 1997-2007 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @version Release: 1.7.14 + * @link http://pear.php.net/package/DB */ class DB_common extends PEAR { // {{{ properties /** - * assoc of capabilities for this DB implementation - * $features['limit'] => 'emulate' => emulate with fetch row by number - * 'alter' => alter the query - * false => skip rows - * @var array + * The current default fetch mode + * @var integer */ - var $features = array(); + var $fetchmode = DB_FETCHMODE_ORDERED; /** - * assoc mapping native error codes to DB ones - * @var array + * The name of the class into which results should be fetched when + * DB_FETCHMODE_OBJECT is in effect + * + * @var string */ - var $errorcode_map = array(); + var $fetchmode_object_class = 'stdClass'; /** - * DB type (mysql, oci8, odbc etc.) - * @var string + * Was a connection present when the object was serialized()? + * @var bool + * @see DB_common::__sleep(), DB_common::__wake() */ - var $phptype; + var $was_connected = null; /** + * The most recently executed query * @var string */ - var $prepare_tokens; + var $last_query = ''; /** - * @var string + * Run-time configuration options + * + * The 'optimize' option has been deprecated. Use the 'portability' + * option instead. + * + * @var array + * @see DB_common::setOption() */ - var $prepare_types; + var $options = array( + 'result_buffering' => 500, + 'persistent' => false, + 'ssl' => false, + 'debug' => 0, + 'seqname_format' => '%s_seq', + 'autofree' => false, + 'portability' => DB_PORTABILITY_NONE, + 'optimize' => 'performance', // Deprecated. Use 'portability'. + ); /** - * @var string + * The parameters from the most recently executed query + * @var array + * @since Property available since Release 1.7.0 */ - var $prepared_queries; + var $last_parameters = array(); /** - * @var integer + * The elements from each prepared statement + * @var array */ - var $prepare_maxstmt = 0; + var $prepare_tokens = array(); /** - * @var string + * The data types of the various elements in each prepared statement + * @var array */ - var $last_query = ''; + var $prepare_types = array(); /** - * @var integer + * The prepared queries + * @var array */ - var $fetchmode = DB_FETCHMODE_ORDERED; + var $prepared_queries = array(); /** - * @var string + * Flag indicating that the last query was a manipulation query. + * @access protected + * @var boolean */ - var $fetchmode_object_class = 'stdClass'; + var $_last_query_manip = false; /** - * Run-time configuration options. + * Flag indicating that the next query <em>must</em> be a manipulation + * query. + * @access protected + * @var boolean + */ + var $_next_query_manip = false; + + + // }}} + // {{{ DB_common + + /** + * This constructor calls <kbd>$this->PEAR('DB_Error')</kbd> * - * The 'optimize' option has been deprecated. Use the 'portability' - * option instead. + * @return void + */ + function DB_common() + { + $this->PEAR('DB_Error'); + } + + // }}} + // {{{ __sleep() + + /** + * Automatically indicates which properties should be saved + * when PHP's serialize() function is called * - * @see DB_common::setOption() - * @var array + * @return array the array of properties names that should be saved */ - var $options = array( - 'persistent' => false, - 'ssl' => false, - 'debug' => 0, - 'seqname_format' => '%s_seq', - 'autofree' => false, - 'portability' => DB_PORTABILITY_NONE, - 'optimize' => 'performance', // Deprecated. Use 'portability'. - ); + function __sleep() + { + if ($this->connection) { + // Don't disconnect(), people use serialize() for many reasons + $this->was_connected = true; + } else { + $this->was_connected = false; + } + if (isset($this->autocommit)) { + return array('autocommit', + 'dbsyntax', + 'dsn', + 'features', + 'fetchmode', + 'fetchmode_object_class', + 'options', + 'was_connected', + ); + } else { + return array('dbsyntax', + 'dsn', + 'features', + 'fetchmode', + 'fetchmode_object_class', + 'options', + 'was_connected', + ); + } + } + // }}} + // {{{ __wakeup() + /** - * DB handle - * @var resource + * Automatically reconnects to the database when PHP's unserialize() + * function is called + * + * The reconnection attempt is only performed if the object was connected + * at the time PHP's serialize() function was run. + * + * @return void */ - var $dbh; + function __wakeup() + { + if ($this->was_connected) { + $this->connect($this->dsn, $this->options['persistent']); + } + } // }}} - // {{{ toString() + // {{{ __toString() /** - * String conversation + * Automatic string conversion for PHP 5 * - * @return string - * @access private + * @return string a string describing the current PEAR DB object + * + * @since Method available since Release 1.7.0 */ - function toString() + function __toString() { $info = strtolower(get_class($this)); $info .= ': (phptype=' . $this->phptype . ', dbsyntax=' . $this->dbsyntax . ')'; - if ($this->connection) { $info .= ' [connected]'; } - return $info; } // }}} - // {{{ constructor + // {{{ toString() /** - * Constructor + * DEPRECATED: String conversion method + * + * @return string a string describing the current PEAR DB object + * + * @deprecated Method deprecated in Release 1.7.0 */ - function DB_common() + function toString() { - $this->PEAR('DB_Error'); + return $this->__toString(); } // }}} @@ -156,11 +252,12 @@ * DEPRECATED: Quotes a string so it can be safely used within string * delimiters in a query * - * @return string quoted string + * @param string $string the string to be quoted * + * @return string the quoted string + * * @see DB_common::quoteSmart(), DB_common::escapeSimple() - * @deprecated Deprecated in release 1.2 or lower - * @internal + * @deprecated Method deprecated some time before Release 1.2 */ function quoteString($string) { @@ -177,25 +274,25 @@ /** * DEPRECATED: Quotes a string so it can be safely used in a query * - * @param string $string the input string to quote + * @param string $string the string to quote * - * @return string The NULL string or the string quotes - * in magic_quote_sybase style + * @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 - * @internal + * @deprecated Deprecated in release 1.6.0 */ function quote($string = null) { - return ($string === null) ? 'NULL' : "'".str_replace("'", "''", $string)."'"; + return ($string === null) ? 'NULL' + : "'" . str_replace("'", "''", $string) . "'"; } // }}} // {{{ quoteIdentifier() /** - * Quote a string so it can be safely used as a table or column name + * 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. * @@ -219,17 +316,17 @@ * + odbc(db2) * + pgsql * + sqlite - * + sybase + * + sybase (must execute <kbd>set quoted_identifier on</kbd> sometime + * prior to use) * * InterBase doesn't seem to be able to use delimited identifiers * via PHP 4. They work fine under PHP 5. * - * @param string $str identifier name to be quoted + * @param string $str the identifier name to be quoted * - * @return string quoted identifier string + * @return string the quoted identifier * - * @since 1.6.0 - * @access public + * @since Method available since Release 1.6.0 */ function quoteIdentifier($str) { @@ -240,16 +337,15 @@ // {{{ quoteSmart() /** - * Format input so it can be safely used in a query + * Formats input so it can be safely used in a query * * The output depends on the PHP data type of input and the database * type being used. * - * @param mixed $in data to be quoted + * @param mixed $in the data to be formatted * - * @return mixed the format of the results depends on the input's - * PHP type: - * + * @return mixed the formatted data. The format depends on the input's + * PHP type: * <ul> * <li> * <kbd>input</kbd> -> <samp>returns</samp> @@ -261,7 +357,7 @@ * <kbd>integer</kbd> or <kbd>double</kbd> -> the unquoted number * </li> * <li> - * &type.bool; -> output depends on the driver in use + * <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 * <kbd>false</kbd>. @@ -338,192 +434,116 @@ * </li> * </ul> * - * @since 1.6.0 * @see DB_common::escapeSimple() - * @access public + * @since Method available since Release 1.6.0 */ function quoteSmart($in) { - if (is_int($in) || is_double($in)) { + if (is_int($in)) { return $in; + } elseif (is_float($in)) { + return $this->quoteFloat($in); } elseif (is_bool($in)) { - return $in ? 1 : 0; + return $this->quoteBoolean($in); } elseif (is_null($in)) { return 'NULL'; } else { + if ($this->dbsyntax == 'access' + && preg_match('/^#.+#$/', $in)) + { + return $this->escapeSimple($in); + } return "'" . $this->escapeSimple($in) . "'"; } } // }}} - // {{{ escapeSimple() + // {{{ quoteBoolean() /** - * Escape a string according to the current DBMS's standards + * Formats a boolean value for use within a query in a locale-independent + * manner. * - * 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 - * - * @since 1.6.0 + * @param boolean the boolean value to be quoted. + * @return string the quoted string. * @see DB_common::quoteSmart() - * @access public + * @since Method available since release 1.7.8. */ - function escapeSimple($str) { - return str_replace("'", "''", $str); + function quoteBoolean($boolean) { + return $boolean ? '1' : '0'; } - + // }}} - // {{{ provides() + // {{{ quoteFloat() /** - * Tell whether a DB implementation or its backend extension - * supports a given feature + * Formats a float value for use within a query in a locale-independent + * manner. * - * @param array $feature name of the feature (see the DB class doc) - * @return bool whether this DB implementation supports $feature - * @access public + * @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. */ - function provides($feature) - { - return $this->features[$feature]; + function quoteFloat($float) { + return "'".$this->escapeSimple(str_replace(',', '.', strval(floatval($float))))."'"; } - + // }}} - // {{{ errorCode() + // {{{ escapeSimple() /** - * Map native error codes to DB's portable ones + * Escapes a string according to the current DBMS's standards * - * Requires that the DB implementation's constructor fills - * in the <var>$errorcode_map</var> property. + * 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 mixed $nativecode the native error code, as returned by the - * backend database extension (string or integer) + * @param string $str the string to be escaped * - * @return int a portable DB error code, or DB_ERROR if this DB - * implementation has no mapping for the given error code. + * @return string the escaped string * - * @access public + * @see DB_common::quoteSmart() + * @since Method available since Release 1.6.0 */ - function errorCode($nativecode) + function escapeSimple($str) { - if (isset($this->errorcode_map[$nativecode])) { - return $this->errorcode_map[$nativecode]; - } - // Fall back to DB_ERROR if there was no mapping. - return DB_ERROR; + return str_replace("'", "''", $str); } // }}} - // {{{ errorMessage() + // {{{ provides() /** - * Map a DB error code to a textual message. This is actually - * just a wrapper for DB::errorMessage() + * Tells whether the present driver supports a given feature * - * @param integer $dbcode the DB error code + * @param string $feature the feature you're curious about * - * @return string the corresponding error message, of false - * if the error code was unknown - * - * @access public + * @return bool whether this driver supports $feature */ - function errorMessage($dbcode) + function provides($feature) { - return DB::errorMessage($this->errorcode_map[$dbcode]); + return $this->features[$feature]; } // }}} - // {{{ raiseError() - - /** - * Communicate an error and invoke error callbacks, etc - * - * Basically a wrapper for PEAR::raiseError without the message string. - * - * @param mixed integer error code, or a PEAR error object (all - * other parameters are ignored if this parameter is - * an object - * - * @param int error mode, see PEAR_Error docs - * - * @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the - * error level (E_USER_NOTICE etc). If error mode is - * PEAR_ERROR_CALLBACK, this is the callback function, - * either as a function name, or as an array of an - * object and method name. For other error modes this - * parameter is ignored. - * - * @param string Extra debug information. Defaults to the last - * query and native error code. - * - * @param mixed Native error code, integer or string depending the - * backend. - * - * @return object a PEAR error object - * - * @access public - * @see PEAR_Error - */ - function &raiseError($code = DB_ERROR, $mode = null, $options = null, - $userinfo = null, $nativecode = null) - { - // The error is yet a DB error object - if (is_object($code)) { - // because we the static PEAR::raiseError, our global - // handler should be used if it is set - if ($mode === null && !empty($this->_default_error_mode)) { - $mode = $this->_default_error_mode; - $options = $this->_default_error_options; - } - $tmp = PEAR::raiseError($code, null, $mode, $options, null, null, true); - return $tmp; - } - - if ($userinfo === null) { - $userinfo = $this->last_query; - } - - if ($nativecode) { - $userinfo .= ' [nativecode=' . trim($nativecode) . ']'; - } - - $tmp = PEAR::raiseError(null, $code, $mode, $options, $userinfo, - 'DB_Error', true); - return $tmp; - } - - // }}} // {{{ setFetchMode() /** - * Sets which fetch mode should be used by default on queries - * on this connection + * Sets the fetch mode that should be used by default for query results * - * @param integer $fetchmode DB_FETCHMODE_ORDERED or - * DB_FETCHMODE_ASSOC, possibly bit-wise OR'ed with - * DB_FETCHMODE_FLIPPED. + * @param integer $fetchmode DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC + * or DB_FETCHMODE_OBJECT + * @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. * - * @param string $object_class The class 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 - * @see DB_FETCHMODE_ASSOC - * @see DB_FETCHMODE_FLIPPED - * @see DB_FETCHMODE_OBJECT - * @see DB_row::DB_row() - * @access public + * @see DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT */ function setFetchMode($fetchmode, $object_class = 'stdClass') { @@ -543,7 +563,7 @@ // {{{ setOption() /** - * Set run-time configuration options for PEAR DB + * Sets run-time configuration options for PEAR DB * * Options, their data types, default values and description: * <ul> @@ -552,6 +572,13 @@ * <br />should results be freed automatically when there are no * more rows? * </li><li> + * <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(). + * (Release 1.7.0) + * </li><li> * <var>debug</var> <kbd>integer</kbd> = <samp>0</samp> * <br />debug level * </li><li> @@ -638,7 +665,6 @@ * 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, @@ -651,26 +677,26 @@ * ----------------------------------------- * * Example 1. Simple setOption() example - * <code> <?php - * $dbh->setOption('autofree', true); - * ?></code> + * <code> + * $db->setOption('autofree', true); + * </code> * * Example 2. Portability for lowercasing and trimming - * <code> <?php - * $dbh->setOption('portability', - * DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM); - * ?></code> + * <code> + * $db->setOption('portability', + * DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM); + * </code> * * Example 3. All portability options except trimming - * <code> <?php - * $dbh->setOption('portability', - * DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM); - * ?></code> + * <code> + * $db->setOption('portability', + * DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM); + * </code> * * @param string $option option name - * @param mixed $value value for the option + * @param mixed $value value for the option * - * @return int DB_OK on success. DB_Error object on failure. + * @return int DB_OK on success. A DB_Error object on failure. * * @see DB_common::$options */ @@ -715,9 +741,9 @@ /** * Returns the value of an option * - * @param string $option option name + * @param string $option the option name you're curious about * - * @return mixed the option value + * @return mixed the option's value */ function getOption($option) { @@ -746,15 +772,15 @@ * data in a db) * * Example 1. - * <code> <?php - * $sth = $dbh->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)'); + * <code> + * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)'); * $data = array( * "John's text", * "'it''s good'", * 'filename.txt' * ); - * $res = $dbh->execute($sth, $data); - * ?></code> + * $res = $db->execute($sth, $data); + * </code> * * Use backslashes to escape placeholder characters if you don't want * them to be interpreted as placeholders: @@ -766,12 +792,12 @@ * * {@internal ibase and oci8 have their own prepare() methods.}} * - * @param string $query query to be prepared + * @param string $query the query to be prepared * - * @return mixed DB statement resource on success. DB_Error on failure. + * @return mixed DB statement resource on success. A DB_Error object + * on failure. * * @see DB_common::execute() - * @access public */ function prepare($query) { @@ -811,19 +837,27 @@ // {{{ autoPrepare() /** - * Automaticaly generate an insert or update query and pass it to prepare() + * Automaticaly generates an insert or update query and pass it to prepare() * - * @param string $table name of the table - * @param array $table_fields ordered array containing the fields names - * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE) - * @param string $where in case of update queries, this string will be put after the sql WHERE statement - * @return resource handle for the query - * @see DB_common::prepare(), DB_common::buildManipSQL() - * @access public + * @param string $table the table name + * @param array $table_fields the array of field names + * @param int $mode a type of query to make: + * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE + * @param string $where for update queries: the WHERE clause to + * append to the SQL statement. Don't + * include the "WHERE" keyword. + * + * @return resource the query handle + * + * @uses DB_common::prepare(), DB_common::buildManipSQL() */ - function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = false) + function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, + $where = false) { $query = $this->buildManipSQL($table, $table_fields, $mode, $where); + if (DB::isError($query)) { + return $query; + } return $this->prepare($query); } @@ -831,21 +865,33 @@ // {{{ autoExecute() /** - * Automaticaly generate an insert or update query and call prepare() + * Automaticaly generates an insert or update query and call prepare() * and execute() with it * - * @param string $table name of the table - * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value - * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE) - * @param string $where in case of update queries, this string will be put after the sql WHERE statement - * @return mixed a new DB_Result or a DB_Error when fail - * @see DB_common::autoPrepare(), DB_common::buildManipSQL() - * @access public + * @param string $table the table name + * @param array $fields_values the associative array where $key is a + * field name and $value its value + * @param int $mode a type of query to make: + * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE + * @param string $where for update queries: the WHERE clause to + * append to the SQL statement. Don't + * include the "WHERE" keyword. + * + * @return mixed a new DB_result object for successful SELECT queries + * or DB_OK for successul data manipulation queries. + * A DB_Error object on failure. + * + * @uses DB_common::autoPrepare(), DB_common::execute() */ - function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = false) + function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, + $where = false) { - $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where); - $ret =& $this->execute($sth, array_values($fields_values)); + $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, + $where); + if (DB::isError($sth)) { + return $sth; + } + $ret = $this->execute($sth, array_values($fields_values)); $this->freePrepared($sth); return $ret; @@ -855,25 +901,39 @@ // {{{ buildManipSQL() /** - * Make automaticaly an sql query for prepare() + * Produces an SQL query string for autoPrepare() * - * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), DB_AUTOQUERY_INSERT) - * will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?) - * NB : - This belongs more to a SQL Builder class, but this is a simple facility - * - Be carefull ! If you don't give a $where param with an UPDATE query, all - * the records of the table will be updated ! + * Example: + * <pre> + * buildManipSQL('table_sql', array('field1', 'field2', 'field3'), + * DB_AUTOQUERY_INSERT); + * </pre> * - * @param string $table name of the table - * @param array $table_fields ordered array containing the fields names - * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE) - * @param string $where in case of update queries, this string will be put after the sql WHERE statement - * @return string sql query for prepare() - * @access public + * That returns + * <samp> + * INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?) + * </samp> + * + * NOTES: + * - This belongs more to a SQL Builder class, but this is a simple + * facility. + * - Be carefull! If you don't give a $where param with an UPDATE + * query, all the records of the table will be updated! + * + * @param string $table the table name + * @param array $table_fields the array of field names + * @param int $mode a type of query to make: + * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE + * @param string $where for update queries: the WHERE clause to + * append to the SQL statement. Don't + * include the "WHERE" keyword. + * + * @return string the sql query for autoPrepare() */ function buildManipSQL($table, $table_fields, $mode, $where = false) { if (count($table_fields) == 0) { - $this->raiseError(DB_ERROR_NEED_MORE_DATA); + return $this->raiseError(DB_ERROR_NEED_MORE_DATA); } $first = true; switch ($mode) { @@ -907,7 +967,7 @@ } return $sql; default: - $this->raiseError(DB_ERROR_SYNTAX); + return $this->raiseError(DB_ERROR_SYNTAX); } } @@ -918,31 +978,32 @@ * Executes a DB statement prepared with prepare() * * Example 1. - * <code> <?php - * $sth = $dbh->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)'); + * <code> + * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)'); * $data = array( * "John's text", * "'it''s good'", * 'filename.txt' * ); - * $res =& $dbh->execute($sth, $data); - * ?></code> + * $res = $db->execute($sth, $data); + * </code> * - * @param resource $stmt a DB statement resource returned from prepare() - * @param mixed $data array, string or numeric data to be used in - * execution of the statement. Quantity of items - * passed must match quantity of placeholders in - * query: meaning 1 placeholder for non-array - * parameters or 1 placeholder per array element. + * @param resource $stmt a DB statement resource returned from prepare() + * @param mixed $data array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @return object a new DB_Result or a DB_Error when fail + * @return mixed a new DB_result object for successful SELECT queries + * or DB_OK for successul data manipulation queries. + * A DB_Error object on failure. * * {@internal ibase and oci8 have their own execute() methods.}} * * @see DB_common::prepare() - * @access public */ - function execute($stmt, $data = array()) + function &execute($stmt, $data = array()) { $realquery = $this->executeEmulateQuery($stmt, $data); if (DB::isError($realquery)) { @@ -950,7 +1011,7 @@ } $result = $this->simpleQuery($realquery); - if (DB::isError($result) || $result === DB_OK) { + if ($result === DB_OK || DB::isError($result)) { return $result; } else { $tmp = new DB_result($this, $result); @@ -962,26 +1023,26 @@ // {{{ executeEmulateQuery() /** - * Emulates the execute statement, when not supported + * Emulates executing prepared statements if the DBMS not support them * - * @param resource $stmt a DB statement resource returned from execute() - * @param mixed $data array, string or numeric data to be used in - * execution of the statement. Quantity of items - * passed must match quantity of placeholders in - * query: meaning 1 placeholder for non-array - * parameters or 1 placeholder per array element. + * @param resource $stmt a DB statement resource returned from execute() + * @param mixed $data array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @return mixed a string containing the real query run when emulating - * prepare/execute. A DB error code is returned on failure. + * @return mixed a string containing the real query run when emulating + * prepare/execute. A DB_Error object on failure. * + * @access protected * @see DB_common::execute() - * @access private */ function executeEmulateQuery($stmt, $data = array()) { - if (!is_array($data)) { - $data = array($data); - } + $stmt = (int)$stmt; + $data = (array)$data; + $this->last_parameters = $data; if (count($this->prepare_types[$stmt]) != count($data)) { $this->last_query = $this->prepared_queries[$stmt]; @@ -1015,8 +1076,7 @@ // {{{ executeMultiple() /** - * This function does several execute() calls on the same - * statement handle + * Performs several execute() calls on the same statement handle * * $data must be an array indexed numerically * from 0, one execute call is done for every "row" in the array. @@ -1024,19 +1084,18 @@ * If an error occurs during execute(), executeMultiple() does not * execute the unfinished rows, but rather returns that error. * - * @param resource $stmt query handle from prepare() - * @param array $data numeric array containing the - * data to insert into the query + * @param resource $stmt query handle from prepare() + * @param array $data numeric array containing the + * data to insert into the query * - * @return mixed DB_OK or DB_Error + * @return int DB_OK on success. A DB_Error object on failure. * * @see DB_common::prepare(), DB_common::execute() - * @access public */ function executeMultiple($stmt, $data) { foreach ($data as $value) { - $res =& $this->execute($stmt, $value); + $res = $this->execute($stmt, $value); if (DB::isError($res)) { return $res; } @@ -1048,14 +1107,20 @@ // {{{ freePrepared() /** - * Free the resource used in a prepared query + * Frees the internal resources associated with a prepared query * - * @param $stmt The resurce returned by the prepare() function + * @param resource $stmt the prepared statement's PHP resource + * @param bool $free_resource should the PHP resource be freed too? + * Use false if you need to get data + * from the result set later. + * + * @return bool TRUE on success, FALSE if $result is invalid + * * @see DB_common::prepare() */ - function freePrepared($stmt) + function freePrepared($stmt, $free_resource = true) { - // Free the internal prepared vars + $stmt = (int)$stmt; if (isset($this->prepare_tokens[$stmt])) { unset($this->prepare_tokens[$stmt]); unset($this->prepare_types[$stmt]); @@ -1069,19 +1134,20 @@ // {{{ modifyQuery() /** - * This method is used by backends to alter queries for various - * reasons + * Changes a query string for various DBMS specific reasons * - * It is defined here to assure that all implementations - * have this method defined. + * It is defined here to ensure all drivers have this method available. * - * @param string $query query to modify + * @param string $query the query string to modify * - * @return the new (modified) query + * @return string the modified query string * - * @access private + * @access protected + * @see DB_mysql::modifyQuery(), DB_oci8::modifyQuery(), + * DB_sqlite::modifyQuery() */ - function modifyQuery($query) { + function modifyQuery($query) + { return $query; } @@ -1089,17 +1155,25 @@ // {{{ modifyLimitQuery() /** - * This method is used by backends to alter limited queries + * Adds LIMIT clauses to a query string according to current DBMS standards * - * @param string $query query to modify - * @param integer $from the row to start to fetching - * @param integer $count the numbers of rows to fetch + * It is defined here to assure that all implementations + * have this method defined. * - * @return the new (modified) query + * @param string $query the query to modify + * @param int $from the row to start to fetching (0 = the first row) + * @param int $count the numbers of rows to fetch + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @access private + * @return string the query string with LIMIT clauses added + * + * @access protected */ - function modifyLimitQuery($query, $from, $count) + function modifyLimitQuery($query, $from, $count, $params = array()) { return $query; } @@ -1108,25 +1182,24 @@ // {{{ query() /** - * Send a query to the database and return any results with a - * DB_result object + * Sends a query to the database server * * The query string can be either a normal statement to be sent directly * to the server OR if <var>$params</var> are passed the query can have * placeholders and it will be passed through prepare() and execute(). * - * @param string $query the SQL query or the statement to prepare - * @param mixed $params array, string or numeric data to be used in - * execution of the statement. Quantity of items - * passed must match quantity of placeholders in - * query: meaning 1 placeholder for non-array - * parameters or 1 placeholder per array element. + * @param string $query the SQL query or the statement to prepare + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @return mixed a DB_result object or DB_OK on success, a DB - * error on failure + * @return mixed a new DB_result object for successful SELECT queries + * or DB_OK for successul data manipulation queries. + * A DB_Error object on failure. * * @see DB_result, DB_common::prepare(), DB_common::execute() - * @access public */ function &query($query, $params = array()) { @@ -1136,14 +1209,15 @@ return $sth; } $ret = $this->execute($sth, $params); - $this->freePrepared($sth); + $this->freePrepared($sth, false); return $ret; } else { + $this->last_parameters = array(); $result = $this->simpleQuery($query); - if (DB::isError($result) || $result === DB_OK) { + if ($result === DB_OK || DB::isError($result)) { return $result; } else { - $tmp =& new DB_result($this, $result); + $tmp = new DB_result($this, $result); return $tmp; } } @@ -1153,25 +1227,29 @@ // {{{ limitQuery() /** - * Generates a limited query + * Generates and executes a LIMIT query * - * @param string $query query - * @param integer $from the row to start to fetching - * @param integer $count the numbers of rows to fetch - * @param array $params required for a statement + * @param string $query the query + * @param intr $from the row to start to fetching (0 = the first row) + * @param int $count the numbers of rows to fetch + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @return mixed a DB_Result object, DB_OK or a DB_Error - * - * @access public + * @return mixed a new DB_result object for successful SELECT queries + * or DB_OK for successul data manipulation queries. + * A DB_Error object on failure. */ function &limitQuery($query, $from, $count, $params = array()) { - $query = $this->modifyLimitQuery($query, $from, $count); + $query = $this->modifyLimitQuery($query, $from, $count, $params); if (DB::isError($query)){ return $query; } - $result =& $this->query($query, $params); - if (is_a($result, 'DB_result')) { + $result = $this->query($query, $params); + if (is_object($result) && is_a($result, 'DB_result')) { $result->setOption('limit_from', $from); $result->setOption('limit_count', $count); } @@ -1182,34 +1260,33 @@ // {{{ getOne() /** - * Fetch the first column of the first row of data returned from - * a query + * Fetches the first column of the first row from a query result * * Takes care of doing the query and freeing the results when finished. * - * @param string $query the SQL query - * @param mixed $params array, string or numeric data to be used in - * execution of the statement. Quantity of items - * passed must match quantity of placeholders in - * query: meaning 1 placeholder for non-array - * parameters or 1 placeholder per array element. + * @param string $query the SQL query + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @return mixed the returned value of the query. DB_Error on failure. - * - * @access public + * @return mixed the returned value of the query. + * A DB_Error object on failure. */ function &getOne($query, $params = array()) { - settype($params, 'array'); + $params = (array)$params; + // modifyLimitQuery() would be nice here, but it causes BC issues if (sizeof($params) > 0) { $sth = $this->prepare($query); if (DB::isError($sth)) { return $sth; } - $res =& $this->execute($sth, $params); + $res = $this->execute($sth, $params); $this->freePrepared($sth); } else { - $res =& $this->query($query); + $res = $this->query($query); } if (DB::isError($res)) { @@ -1230,24 +1307,22 @@ // {{{ getRow() /** - * Fetch the first row of data returned from a query + * Fetches the first row of data returned from a query result * * Takes care of doing the query and freeing the results when finished. * - * @param string $query the SQL query - * @param array $params array to be used in execution of the statement. - * Quantity of array elements must match quantity - * of placeholders in query. This function does - * NOT support scalars. - * @param int $fetchmode the fetch mode to use + * @param string $query the SQL query + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. + * @param int $fetchmode the fetch mode to use * - * @return array the first row of results as an array indexed from - * 0, or a DB error code. - * - * @access public + * @return array the first row of results as an array. + * A DB_Error object on failure. */ - function &getRow($query, - $params = array(), + function &getRow($query, $params = array(), $fetchmode = DB_FETCHMODE_DEFAULT) { // compat check, the params and fetchmode parameters used to @@ -1266,16 +1341,16 @@ $params = array(); } } - + // modifyLimitQuery() would be nice here, but it causes BC issues if (sizeof($params) > 0) { $sth = $this->prepare($query); if (DB::isError($sth)) { return $sth; } - $res =& $this->execute($sth, $params); + $res = $this->execute($sth, $params); $this->freePrepared($sth); } else { - $res =& $this->query($query); + $res = $this->query($query); } if (DB::isError($res)) { @@ -1297,27 +1372,25 @@ // {{{ getCol() /** - * Fetch a single column from a result set and return it as an + * Fetches a single column from a query result and returns it as an * indexed array * - * @param string $query the SQL query - * @param mixed $col which column to return (integer [column number, - * starting at 0] or string [column name]) - * @param mixed $params array, string or numeric data to be used in - * execution of the statement. Quantity of items - * passed must match quantity of placeholders in - * query: meaning 1 placeholder for non-array - * parameters or 1 placeholder per array element. + * @param string $query the SQL query + * @param mixed $col which column to return (integer [column number, + * starting at 0] or string [column name]) + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of items + * passed must match quantity of placeholders in + * query: meaning 1 placeholder for non-array + * parameters or 1 placeholder per array element. * - * @return array an indexed array with the data from the first - * row at index 0, or a DB error code + * @return array the results as an array. A DB_Error object on failure. * * @see DB_common::query() - * @access public */ function &getCol($query, $col = 0, $params = array()) { - settype($params, 'array'); + $params = (array)$params; if (sizeof($params) > 0) { $sth = $this->prepare($query); @@ -1325,10 +1398,10 @@ return $sth; } - $res =& $this->execute($sth, $params); + $res = $this->execute($sth, $params); $this->freePrepared($sth); } else { - $res =& $this->query($query); + $res = $this->query($query); } if (DB::isError($res)) { @@ -1336,10 +1409,18 @@ } $fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC; - $ret = array(); - while (is_array($row = $res->fetchRow($fetchmode))) { - $ret[] = $row[$col]; + if (!is_array($row = $res->fetchRow($fetchmode))) { + $ret = array(); + } else { + if (!array_key_exists($col, $row)) { + $ret = $this->raiseError(DB_ERROR_NOSUCHFIELD); + } else { + $ret = array($row[$col]); + while (is_array($row = $res->fetchRow($fetchmode))) { + $ret[] = $row[$col]; + } + } } $res->free(); @@ -1355,7 +1436,7 @@ // {{{ getAssoc() /** - * Fetch the entire result set of a query and return it as an + * Fetches an entire query result and returns it as an * associative array using the first column as the key * * If the result set contains more than two columns, the value @@ -1416,32 +1497,32 @@ * Keep in mind that database functions in PHP usually return string * values for results regardless of the database's internal type. * - * @param string $query the SQL query - * @param boolean $force_array used only when the query returns + * @param string $query the SQL query + * @param bool $force_array used only when the query returns * exactly two columns. If true, the values * of the returned array will be one-element * arrays instead of scalars. - * @param mixed $params array, string or numeric data to be used in - * execution of the statement. Quantity of items - * passed must match quantity of placeholders in - * query: meaning 1 placeholder for non-array - * parameters or 1 placeholder per array element. - * @param boolean $group if true, the values of the returned array - * is wrapped in another array. If the same - * key value (in the first column) repeats - * itself, the values will be appended to - * this array instead of overwriting the - * existing values. + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of + * items passed must match quantity of + * placeholders in query: meaning 1 + * placeholder for non-array parameters or + * 1 placeholder per array element. + * @param int $fetchmode the fetch mode to use + * @param bool $group if true, the values of the returned array + * is wrapped in another array. If the same + * key value (in the first column) repeats + * itself, the values will be appended to + * this array instead of overwriting the + * existing values. * - * @return array associative array with results from the query. - * DB Error on failure. - * - * @access public + * @return array the associative array containing the query results. + * A DB_Error object on failure. */ function &getAssoc($query, $force_array = false, $params = array(), $fetchmode = DB_FETCHMODE_DEFAULT, $group = false) { - settype($params, 'array'); + $params = (array)$params; if (sizeof($params) > 0) { $sth = $this->prepare($query); @@ -1449,10 +1530,10 @@ return $sth; } - $res =& $this->execute($sth, $params); + $res = $this->execute($sth, $params); $this->freePrepared($sth); } else { - $res =& $this->query($query); + $res = $this->query($query); } if (DB::isError($res)) { @@ -1464,7 +1545,7 @@ $cols = $res->numCols(); if ($cols < 2) { - $tmp =& $this->raiseError(DB_ERROR_TRUNCATED); + $tmp = $this->raiseError(DB_ERROR_TRUNCATED); return $tmp; } @@ -1532,21 +1613,24 @@ // {{{ getAll() /** - * Fetch all the rows returned from a query + * Fetches all of the rows from a query result * - * @param string $query the SQL query - * @param array $params array to be used in execution of the statement. - * Quantity of array elements must match quantity - * of placeholders in query. This function does - * NOT support scalars. - * @param int $fetchmode the fetch mode to use + * @param string $query the SQL query + * @param mixed $params array, string or numeric data to be used in + * execution of the statement. Quantity of + * items passed must match quantity of + * placeholders in query: meaning 1 + * placeholder for non-array parameters or + * 1 placeholder per array element. + * @param int $fetchmode the fetch mode to use: + * + DB_FETCHMODE_ORDERED + * + DB_FETCHMODE_ASSOC + * + DB_FETCHMODE_ORDERED | DB_FETCHMODE_FLIPPED + * + DB_FETCHMODE_ASSOC | DB_FETCHMODE_FLIPPED * - * @return array an nested array. DB error on failure. - * - * @access public + * @return array the nested array. A DB_Error object on failure. */ - function &getAll($query, - $params = array(), + function &getAll($query, $params = array(), $fetchmode =... [truncated message content] |