2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2004 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Stig Bakken <ssb@php.net> |
17 // | Tomas V.V.Cox <cox@idecnet.com> |
18 // | Maintainer: Daniel Convissor <danielc@php.net> |
19 // +----------------------------------------------------------------------+
21 require_once 'PEAR.php';
24 * DB_common is a base class for DB implementations, and must be
25 * inherited by all such
30 * @author Stig Bakken <ssb@php.net>
31 * @author Tomas V.V.Cox <cox@idecnet.com>
33 class DB_common extends PEAR
38 * assoc of capabilities for this DB implementation
39 * $features['limit'] => 'emulate' => emulate with fetch row by number
40 * 'alter' => alter the query
44 var $features = array();
47 * assoc mapping native error codes to DB ones
50 var $errorcode_map = array();
53 * DB type (mysql, oci8, odbc etc.)
71 var $prepared_queries;
76 var $prepare_maxstmt = 0;
86 var $fetchmode = DB_FETCHMODE_ORDERED;
91 var $fetchmode_object_class = 'stdClass';
94 * Run-time configuration options.
96 * The 'optimize' option has been deprecated. Use the 'portability'
99 * @see DB_common::setOption()
102 var $options = array(
103 'persistent' => false,
106 'seqname_format' => '%s_seq',
108 'portability' => DB_PORTABILITY_NONE,
109 'optimize' => 'performance', // Deprecated. Use 'portability'.
122 * String conversation
129 $info = strtolower(get_class($this));
130 $info .= ': (phptype=' . $this->phptype .
131 ', dbsyntax=' . $this->dbsyntax .
134 if ($this->connection) {
135 $info .= ' [connected]';
149 $this->PEAR('DB_Error');
156 * DEPRECATED: Quotes a string so it can be safely used within string
157 * delimiters in a query
159 * @return string quoted string
161 * @see DB_common::quoteSmart(), DB_common::escapeSimple()
162 * @deprecated Deprecated in release 1.2 or lower
165 function quoteString($string)
167 $string = $this->quote($string);
168 if ($string{0} == "'") {
169 return substr($string, 1, -1);
178 * DEPRECATED: Quotes a string so it can be safely used in a query
180 * @param string $string the input string to quote
182 * @return string The NULL string or the string quotes
183 * in magic_quote_sybase style
185 * @see DB_common::quoteSmart(), DB_common::escapeSimple()
186 * @deprecated Deprecated in release 1.6.0
189 function quote($string = null)
191 return ($string === null) ? 'NULL' : "'".str_replace("'", "''", $string)."'";
195 // {{{ quoteIdentifier()
198 * Quote a string so it can be safely used as a table or column name
200 * Delimiting style depends on which database driver is being used.
202 * NOTE: just because you CAN use delimited identifiers doesn't mean
203 * you SHOULD use them. In general, they end up causing way more
204 * problems than they solve.
206 * Portability is broken by using the following characters inside
207 * delimited identifiers:
208 * + backtick (<kbd>`</kbd>) -- due to MySQL
209 * + double quote (<kbd>"</kbd>) -- due to Oracle
210 * + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
212 * Delimited identifiers are known to generally work correctly under
213 * the following drivers:
224 * InterBase doesn't seem to be able to use delimited identifiers
225 * via PHP 4. They work fine under PHP 5.
227 * @param string $str identifier name to be quoted
229 * @return string quoted identifier string
234 function quoteIdentifier($str)
236 return '"' . str_replace('"', '""', $str) . '"';
243 * Format input so it can be safely used in a query
245 * The output depends on the PHP data type of input and the database
248 * @param mixed $in data to be quoted
250 * @return mixed the format of the results depends on the input's
255 * <kbd>input</kbd> -> <samp>returns</samp>
258 * <kbd>null</kbd> -> the string <samp>NULL</samp>
261 * <kbd>integer</kbd> or <kbd>double</kbd> -> the unquoted number
264 * &type.bool; -> output depends on the driver in use
265 * Most drivers return integers: <samp>1</samp> if
266 * <kbd>true</kbd> or <samp>0</samp> if
268 * Some return strings: <samp>TRUE</samp> if
269 * <kbd>true</kbd> or <samp>FALSE</samp> if
271 * Finally one returns strings: <samp>T</samp> if
272 * <kbd>true</kbd> or <samp>F</samp> if
273 * <kbd>false</kbd>. Here is a list of each DBMS,
274 * the values returned and the suggested column type:
277 * <kbd>dbase</kbd> -> <samp>T/F</samp>
278 * (<kbd>Logical</kbd>)
281 * <kbd>fbase</kbd> -> <samp>TRUE/FALSE</samp>
282 * (<kbd>BOOLEAN</kbd>)
285 * <kbd>ibase</kbd> -> <samp>1/0</samp>
286 * (<kbd>SMALLINT</kbd>) [1]
289 * <kbd>ifx</kbd> -> <samp>1/0</samp>
290 * (<kbd>SMALLINT</kbd>) [1]
293 * <kbd>msql</kbd> -> <samp>1/0</samp>
294 * (<kbd>INTEGER</kbd>)
297 * <kbd>mssql</kbd> -> <samp>1/0</samp>
301 * <kbd>mysql</kbd> -> <samp>1/0</samp>
302 * (<kbd>TINYINT(1)</kbd>)
305 * <kbd>mysqli</kbd> -> <samp>1/0</samp>
306 * (<kbd>TINYINT(1)</kbd>)
309 * <kbd>oci8</kbd> -> <samp>1/0</samp>
310 * (<kbd>NUMBER(1)</kbd>)
313 * <kbd>odbc</kbd> -> <samp>1/0</samp>
314 * (<kbd>SMALLINT</kbd>) [1]
317 * <kbd>pgsql</kbd> -> <samp>TRUE/FALSE</samp>
318 * (<kbd>BOOLEAN</kbd>)
321 * <kbd>sqlite</kbd> -> <samp>1/0</samp>
322 * (<kbd>INTEGER</kbd>)
325 * <kbd>sybase</kbd> -> <samp>1/0</samp>
326 * (<kbd>TINYINT(1)</kbd>)
329 * [1] Accommodate the lowest common denominator because not all
330 * versions of have <kbd>BOOLEAN</kbd>.
333 * other (including strings and numeric strings) ->
334 * the data with single quotes escaped by preceeding
335 * single quotes, backslashes are escaped by preceeding
336 * backslashes, then the whole string is encapsulated
337 * between single quotes
342 * @see DB_common::escapeSimple()
345 function quoteSmart($in)
347 if (is_int($in) || is_double($in)) {
349 } elseif (is_bool($in)) {
351 } elseif (is_null($in)) {
354 return "'" . $this->escapeSimple($in) . "'";
359 // {{{ escapeSimple()
362 * Escape a string according to the current DBMS's standards
364 * In SQLite, this makes things safe for inserts/updates, but may
365 * cause problems when performing text comparisons against columns
366 * containing binary data. See the
367 * {@link http://php.net/sqlite_escape_string PHP manual} for more info.
369 * @param string $str the string to be escaped
371 * @return string the escaped string
374 * @see DB_common::quoteSmart()
377 function escapeSimple($str) {
378 return str_replace("'", "''", $str);
385 * Tell whether a DB implementation or its backend extension
386 * supports a given feature
388 * @param array $feature name of the feature (see the DB class doc)
389 * @return bool whether this DB implementation supports $feature
392 function provides($feature)
394 return $this->features[$feature];
401 * Map native error codes to DB's portable ones
403 * Requires that the DB implementation's constructor fills
404 * in the <var>$errorcode_map</var> property.
406 * @param mixed $nativecode the native error code, as returned by the
407 * backend database extension (string or integer)
409 * @return int a portable DB error code, or DB_ERROR if this DB
410 * implementation has no mapping for the given error code.
414 function errorCode($nativecode)
416 if (isset($this->errorcode_map[$nativecode])) {
417 return $this->errorcode_map[$nativecode];
419 // Fall back to DB_ERROR if there was no mapping.
424 // {{{ errorMessage()
427 * Map a DB error code to a textual message. This is actually
428 * just a wrapper for DB::errorMessage()
430 * @param integer $dbcode the DB error code
432 * @return string the corresponding error message, of false
433 * if the error code was unknown
437 function errorMessage($dbcode)
439 return DB::errorMessage($this->errorcode_map[$dbcode]);
446 * Communicate an error and invoke error callbacks, etc
448 * Basically a wrapper for PEAR::raiseError without the message string.
450 * @param mixed integer error code, or a PEAR error object (all
451 * other parameters are ignored if this parameter is
454 * @param int error mode, see PEAR_Error docs
456 * @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the
457 * error level (E_USER_NOTICE etc). If error mode is
458 * PEAR_ERROR_CALLBACK, this is the callback function,
459 * either as a function name, or as an array of an
460 * object and method name. For other error modes this
461 * parameter is ignored.
463 * @param string Extra debug information. Defaults to the last
464 * query and native error code.
466 * @param mixed Native error code, integer or string depending the
469 * @return object a PEAR error object
474 function &raiseError($code = DB_ERROR, $mode = null, $options = null,
475 $userinfo = null, $nativecode = null)
477 // The error is yet a DB error object
478 if (is_object($code)) {
479 // because we the static PEAR::raiseError, our global
480 // handler should be used if it is set
481 if ($mode === null && !empty($this->_default_error_mode)) {
482 $mode = $this->_default_error_mode;
483 $options = $this->_default_error_options;
485 $tmp = PEAR::raiseError($code, null, $mode, $options, null, null, true);
489 if ($userinfo === null) {
490 $userinfo = $this->last_query;
494 $userinfo .= ' [nativecode=' . trim($nativecode) . ']';
497 $tmp = PEAR::raiseError(null, $code, $mode, $options, $userinfo,
503 // {{{ setFetchMode()
506 * Sets which fetch mode should be used by default on queries
509 * @param integer $fetchmode DB_FETCHMODE_ORDERED or
510 * DB_FETCHMODE_ASSOC, possibly bit-wise OR'ed with
511 * DB_FETCHMODE_FLIPPED.
513 * @param string $object_class The class of the object
514 * to be returned by the fetch methods when
515 * the DB_FETCHMODE_OBJECT mode is selected.
516 * If no class is specified by default a cast
517 * to object from the assoc array row will be done.
518 * There is also the posibility to use and extend the
521 * @see DB_FETCHMODE_ORDERED
522 * @see DB_FETCHMODE_ASSOC
523 * @see DB_FETCHMODE_FLIPPED
524 * @see DB_FETCHMODE_OBJECT
525 * @see DB_row::DB_row()
528 function setFetchMode($fetchmode, $object_class = 'stdClass')
530 switch ($fetchmode) {
531 case DB_FETCHMODE_OBJECT:
532 $this->fetchmode_object_class = $object_class;
533 case DB_FETCHMODE_ORDERED:
534 case DB_FETCHMODE_ASSOC:
535 $this->fetchmode = $fetchmode;
538 return $this->raiseError('invalid fetchmode mode');
546 * Set run-time configuration options for PEAR DB
548 * Options, their data types, default values and description:
551 * <var>autofree</var> <kbd>boolean</kbd> = <samp>false</samp>
552 * <br />should results be freed automatically when there are no
555 * <var>debug</var> <kbd>integer</kbd> = <samp>0</samp>
558 * <var>persistent</var> <kbd>boolean</kbd> = <samp>false</samp>
559 * <br />should the connection be persistent?
561 * <var>portability</var> <kbd>integer</kbd> = <samp>DB_PORTABILITY_NONE</samp>
562 * <br />portability mode constant (see below)
564 * <var>seqname_format</var> <kbd>string</kbd> = <samp>%s_seq</samp>
565 * <br />the sprintf() format string used on sequence names. This
566 * format is applied to sequence names passed to
567 * createSequence(), nextID() and dropSequence().
569 * <var>ssl</var> <kbd>boolean</kbd> = <samp>false</samp>
570 * <br />use ssl to connect?
574 * -----------------------------------------
578 * These modes are bitwised, so they can be combined using <kbd>|</kbd>
579 * and removed using <kbd>^</kbd>. See the examples section below on how
582 * <samp>DB_PORTABILITY_NONE</samp>
583 * turn off all portability features
585 * This mode gets automatically turned on if the deprecated
586 * <var>optimize</var> option gets set to <samp>performance</samp>.
589 * <samp>DB_PORTABILITY_LOWERCASE</samp>
590 * convert names of tables and fields to lower case when using
591 * <kbd>get*()</kbd>, <kbd>fetch*()</kbd> and <kbd>tableInfo()</kbd>
593 * This mode gets automatically turned on in the following databases
594 * if the deprecated option <var>optimize</var> gets set to
595 * <samp>portability</samp>:
599 * <samp>DB_PORTABILITY_RTRIM</samp>
600 * right trim the data output by <kbd>get*()</kbd> <kbd>fetch*()</kbd>
603 * <samp>DB_PORTABILITY_DELETE_COUNT</samp>
604 * force reporting the number of rows deleted
606 * Some DBMS's don't count the number of rows deleted when performing
607 * simple <kbd>DELETE FROM tablename</kbd> queries. This portability
608 * mode tricks such DBMS's into telling the count by adding
609 * <samp>WHERE 1=1</samp> to the end of <kbd>DELETE</kbd> queries.
611 * This mode gets automatically turned on in the following databases
612 * if the deprecated option <var>optimize</var> gets set to
613 * <samp>portability</samp>:
620 * <samp>DB_PORTABILITY_NUMROWS</samp>
621 * enable hack that makes <kbd>numRows()</kbd> work in Oracle
623 * This mode gets automatically turned on in the following databases
624 * if the deprecated option <var>optimize</var> gets set to
625 * <samp>portability</samp>:
629 * <samp>DB_PORTABILITY_ERRORS</samp>
630 * makes certain error messages in certain drivers compatible
631 * with those from other DBMS's
633 * + mysql, mysqli: change unique/primary key constraints
634 * DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
636 * + odbc(access): MS's ODBC driver reports 'no such field' as code
637 * 07001, which means 'too few parameters.' When this option is on
638 * that code gets mapped to DB_ERROR_NOSUCHFIELD.
639 * DB_ERROR_MISMATCH -> DB_ERROR_NOSUCHFIELD
642 * <samp>DB_PORTABILITY_NULL_TO_EMPTY</samp>
643 * convert null values to empty strings in data output by get*() and
644 * fetch*(). Needed because Oracle considers empty strings to be null,
645 * while most other DBMS's know the difference between empty and null.
648 * <samp>DB_PORTABILITY_ALL</samp>
649 * turn on all portability features
651 * -----------------------------------------
653 * Example 1. Simple setOption() example
655 * $dbh->setOption('autofree', true);
658 * Example 2. Portability for lowercasing and trimming
660 * $dbh->setOption('portability',
661 * DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM);
664 * Example 3. All portability options except trimming
666 * $dbh->setOption('portability',
667 * DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM);
670 * @param string $option option name
671 * @param mixed $value value for the option
673 * @return int DB_OK on success. DB_Error object on failure.
675 * @see DB_common::$options
677 function setOption($option, $value)
679 if (isset($this->options[$option])) {
680 $this->options[$option] = $value;
683 * Backwards compatibility check for the deprecated 'optimize'
684 * option. Done here in case settings change after connecting.
686 if ($option == 'optimize') {
687 if ($value == 'portability') {
688 switch ($this->phptype) {
690 $this->options['portability'] =
691 DB_PORTABILITY_LOWERCASE |
692 DB_PORTABILITY_NUMROWS;
698 $this->options['portability'] =
699 DB_PORTABILITY_DELETE_COUNT;
703 $this->options['portability'] = DB_PORTABILITY_NONE;
709 return $this->raiseError("unknown option $option");
716 * Returns the value of an option
718 * @param string $option option name
720 * @return mixed the option value
722 function getOption($option)
724 if (isset($this->options[$option])) {
725 return $this->options[$option];
727 return $this->raiseError("unknown option $option");
734 * Prepares a query for multiple execution with execute()
736 * Creates a query that can be run multiple times. Each time it is run,
737 * the placeholders, if any, will be replaced by the contents of
738 * execute()'s $data argument.
740 * Three types of placeholders can be used:
741 * + <kbd>?</kbd> scalar value (i.e. strings, integers). The system
742 * will automatically quote and escape the data.
743 * + <kbd>!</kbd> value is inserted 'as is'
744 * + <kbd>&</kbd> requires a file name. The file's contents get
745 * inserted into the query (i.e. saving binary
750 * $sth = $dbh->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
756 * $res = $dbh->execute($sth, $data);
759 * Use backslashes to escape placeholder characters if you don't want
760 * them to be interpreted as placeholders:
762 * "UPDATE foo SET col=? WHERE col='over \& under'"
765 * With some database backends, this is emulated.
767 * {@internal ibase and oci8 have their own prepare() methods.}}
769 * @param string $query query to be prepared
771 * @return mixed DB statement resource on success. DB_Error on failure.
773 * @see DB_common::execute()
776 function prepare($query)
778 $tokens = preg_split('/((?<!\\\)[&?!])/', $query, -1,
779 PREG_SPLIT_DELIM_CAPTURE);
782 $newtokens = array();
784 foreach ($tokens as $val) {
787 $types[$token++] = DB_PARAM_SCALAR;
790 $types[$token++] = DB_PARAM_OPAQUE;
793 $types[$token++] = DB_PARAM_MISC;
796 $newtokens[] = preg_replace('/\\\([&?!])/', "\\1", $val);
800 $this->prepare_tokens[] = &$newtokens;
801 end($this->prepare_tokens);
803 $k = key($this->prepare_tokens);
804 $this->prepare_types[$k] = $types;
805 $this->prepared_queries[$k] = implode(' ', $newtokens);
814 * Automaticaly generate an insert or update query and pass it to prepare()
816 * @param string $table name of the table
817 * @param array $table_fields ordered array containing the fields names
818 * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)
819 * @param string $where in case of update queries, this string will be put after the sql WHERE statement
820 * @return resource handle for the query
821 * @see DB_common::prepare(), DB_common::buildManipSQL()
824 function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT, $where = false)
826 $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
827 return $this->prepare($query);
834 * Automaticaly generate an insert or update query and call prepare()
835 * and execute() with it
837 * @param string $table name of the table
838 * @param array $fields_values assoc ($key=>$value) where $key is a field name and $value its value
839 * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)
840 * @param string $where in case of update queries, this string will be put after the sql WHERE statement
841 * @return mixed a new DB_Result or a DB_Error when fail
842 * @see DB_common::autoPrepare(), DB_common::buildManipSQL()
845 function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT, $where = false)
847 $sth = $this->autoPrepare($table, array_keys($fields_values), $mode, $where);
848 $ret =& $this->execute($sth, array_values($fields_values));
849 $this->freePrepared($sth);
855 // {{{ buildManipSQL()
858 * Make automaticaly an sql query for prepare()
860 * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), DB_AUTOQUERY_INSERT)
861 * will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
862 * NB : - This belongs more to a SQL Builder class, but this is a simple facility
863 * - Be carefull ! If you don't give a $where param with an UPDATE query, all
864 * the records of the table will be updated !
866 * @param string $table name of the table
867 * @param array $table_fields ordered array containing the fields names
868 * @param int $mode type of query to make (DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE)
869 * @param string $where in case of update queries, this string will be put after the sql WHERE statement
870 * @return string sql query for prepare()
873 function buildManipSQL($table, $table_fields, $mode, $where = false)
875 if (count($table_fields) == 0) {
876 $this->raiseError(DB_ERROR_NEED_MORE_DATA);
880 case DB_AUTOQUERY_INSERT:
883 foreach ($table_fields as $value) {
893 return "INSERT INTO $table ($names) VALUES ($values)";
894 case DB_AUTOQUERY_UPDATE:
896 foreach ($table_fields as $value) {
902 $set .= "$value = ?";
904 $sql = "UPDATE $table SET $set";
906 $sql .= " WHERE $where";
910 $this->raiseError(DB_ERROR_SYNTAX);
918 * Executes a DB statement prepared with prepare()
922 * $sth = $dbh->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
928 * $res =& $dbh->execute($sth, $data);
931 * @param resource $stmt a DB statement resource returned from prepare()
932 * @param mixed $data array, string or numeric data to be used in
933 * execution of the statement. Quantity of items
934 * passed must match quantity of placeholders in
935 * query: meaning 1 placeholder for non-array
936 * parameters or 1 placeholder per array element.
938 * @return object a new DB_Result or a DB_Error when fail
940 * {@internal ibase and oci8 have their own execute() methods.}}
942 * @see DB_common::prepare()
945 function execute($stmt, $data = array())
947 $realquery = $this->executeEmulateQuery($stmt, $data);
948 if (DB::isError($realquery)) {
951 $result = $this->simpleQuery($realquery);
953 if (DB::isError($result) || $result === DB_OK) {
956 $tmp = new DB_result($this, $result);
962 // {{{ executeEmulateQuery()
965 * Emulates the execute statement, when not supported
967 * @param resource $stmt a DB statement resource returned from execute()
968 * @param mixed $data array, string or numeric data to be used in
969 * execution of the statement. Quantity of items
970 * passed must match quantity of placeholders in
971 * query: meaning 1 placeholder for non-array
972 * parameters or 1 placeholder per array element.
974 * @return mixed a string containing the real query run when emulating
975 * prepare/execute. A DB error code is returned on failure.
977 * @see DB_common::execute()
980 function executeEmulateQuery($stmt, $data = array())
982 if (!is_array($data)) {
983 $data = array($data);
986 if (count($this->prepare_types[$stmt]) != count($data)) {
987 $this->last_query = $this->prepared_queries[$stmt];
988 return $this->raiseError(DB_ERROR_MISMATCH);
991 $realquery = $this->prepare_tokens[$stmt][0];
994 foreach ($data as $value) {
995 if ($this->prepare_types[$stmt][$i] == DB_PARAM_SCALAR) {
996 $realquery .= $this->quoteSmart($value);
997 } elseif ($this->prepare_types[$stmt][$i] == DB_PARAM_OPAQUE) {
998 $fp = @fopen($value, 'rb');
1000 return $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
1002 $realquery .= $this->quoteSmart(fread($fp, filesize($value)));
1005 $realquery .= $value;
1008 $realquery .= $this->prepare_tokens[$stmt][++$i];
1015 // {{{ executeMultiple()
1018 * This function does several execute() calls on the same
1021 * $data must be an array indexed numerically
1022 * from 0, one execute call is done for every "row" in the array.
1024 * If an error occurs during execute(), executeMultiple() does not
1025 * execute the unfinished rows, but rather returns that error.
1027 * @param resource $stmt query handle from prepare()
1028 * @param array $data numeric array containing the
1029 * data to insert into the query
1031 * @return mixed DB_OK or DB_Error
1033 * @see DB_common::prepare(), DB_common::execute()
1036 function executeMultiple($stmt, $data)
1038 foreach ($data as $value) {
1039 $res =& $this->execute($stmt, $value);
1040 if (DB::isError($res)) {
1048 // {{{ freePrepared()
1051 * Free the resource used in a prepared query
1053 * @param $stmt The resurce returned by the prepare() function
1054 * @see DB_common::prepare()
1056 function freePrepared($stmt)
1058 // Free the internal prepared vars
1059 if (isset($this->prepare_tokens[$stmt])) {
1060 unset($this->prepare_tokens[$stmt]);
1061 unset($this->prepare_types[$stmt]);
1062 unset($this->prepared_queries[$stmt]);
1069 // {{{ modifyQuery()
1072 * This method is used by backends to alter queries for various
1075 * It is defined here to assure that all implementations
1076 * have this method defined.
1078 * @param string $query query to modify
1080 * @return the new (modified) query
1084 function modifyQuery($query) {
1089 // {{{ modifyLimitQuery()
1092 * This method is used by backends to alter limited queries
1094 * @param string $query query to modify
1095 * @param integer $from the row to start to fetching
1096 * @param integer $count the numbers of rows to fetch
1098 * @return the new (modified) query
1102 function modifyLimitQuery($query, $from, $count)
1111 * Send a query to the database and return any results with a
1114 * The query string can be either a normal statement to be sent directly
1115 * to the server OR if <var>$params</var> are passed the query can have
1116 * placeholders and it will be passed through prepare() and execute().
1118 * @param string $query the SQL query or the statement to prepare
1119 * @param mixed $params array, string or numeric data to be used in
1120 * execution of the statement. Quantity of items
1121 * passed must match quantity of placeholders in
1122 * query: meaning 1 placeholder for non-array
1123 * parameters or 1 placeholder per array element.
1125 * @return mixed a DB_result object or DB_OK on success, a DB
1128 * @see DB_result, DB_common::prepare(), DB_common::execute()
1131 function &query($query, $params = array())
1133 if (sizeof($params) > 0) {
1134 $sth = $this->prepare($query);
1135 if (DB::isError($sth)) {
1138 $ret = $this->execute($sth, $params);
1139 $this->freePrepared($sth);
1142 $result = $this->simpleQuery($query);
1143 if (DB::isError($result) || $result === DB_OK) {
1146 $tmp =& new DB_result($this, $result);
1156 * Generates a limited query
1158 * @param string $query query
1159 * @param integer $from the row to start to fetching
1160 * @param integer $count the numbers of rows to fetch
1161 * @param array $params required for a statement
1163 * @return mixed a DB_Result object, DB_OK or a DB_Error
1167 function &limitQuery($query, $from, $count, $params = array())
1169 $query = $this->modifyLimitQuery($query, $from, $count);
1170 if (DB::isError($query)){
1173 $result =& $this->query($query, $params);
1174 if (is_a($result, 'DB_result')) {
1175 $result->setOption('limit_from', $from);
1176 $result->setOption('limit_count', $count);
1185 * Fetch the first column of the first row of data returned from
1188 * Takes care of doing the query and freeing the results when finished.
1190 * @param string $query the SQL query
1191 * @param mixed $params array, string or numeric data to be used in
1192 * execution of the statement. Quantity of items
1193 * passed must match quantity of placeholders in
1194 * query: meaning 1 placeholder for non-array
1195 * parameters or 1 placeholder per array element.
1197 * @return mixed the returned value of the query. DB_Error on failure.
1201 function &getOne($query, $params = array())
1203 settype($params, 'array');
1204 if (sizeof($params) > 0) {
1205 $sth = $this->prepare($query);
1206 if (DB::isError($sth)) {
1209 $res =& $this->execute($sth, $params);
1210 $this->freePrepared($sth);
1212 $res =& $this->query($query);
1215 if (DB::isError($res)) {
1219 $err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);
1222 if ($err !== DB_OK) {
1233 * Fetch the first row of data returned from a query
1235 * Takes care of doing the query and freeing the results when finished.
1237 * @param string $query the SQL query
1238 * @param array $params array to be used in execution of the statement.
1239 * Quantity of array elements must match quantity
1240 * of placeholders in query. This function does
1241 * NOT support scalars.
1242 * @param int $fetchmode the fetch mode to use
1244 * @return array the first row of results as an array indexed from
1245 * 0, or a DB error code.
1249 function &getRow($query,
1251 $fetchmode = DB_FETCHMODE_DEFAULT)
1253 // compat check, the params and fetchmode parameters used to
1254 // have the opposite order
1255 if (!is_array($params)) {
1256 if (is_array($fetchmode)) {
1257 if ($params === null) {
1258 $tmp = DB_FETCHMODE_DEFAULT;
1262 $params = $fetchmode;
1264 } elseif ($params !== null) {
1265 $fetchmode = $params;
1270 if (sizeof($params) > 0) {
1271 $sth = $this->prepare($query);
1272 if (DB::isError($sth)) {
1275 $res =& $this->execute($sth, $params);
1276 $this->freePrepared($sth);
1278 $res =& $this->query($query);
1281 if (DB::isError($res)) {
1285 $err = $res->fetchInto($row, $fetchmode);
1289 if ($err !== DB_OK) {
1300 * Fetch a single column from a result set and return it as an
1303 * @param string $query the SQL query
1304 * @param mixed $col which column to return (integer [column number,
1305 * starting at 0] or string [column name])
1306 * @param mixed $params array, string or numeric data to be used in
1307 * execution of the statement. Quantity of items
1308 * passed must match quantity of placeholders in
1309 * query: meaning 1 placeholder for non-array
1310 * parameters or 1 placeholder per array element.
1312 * @return array an indexed array with the data from the first
1313 * row at index 0, or a DB error code
1315 * @see DB_common::query()
1318 function &getCol($query, $col = 0, $params = array())
1320 settype($params, 'array');
1321 if (sizeof($params) > 0) {
1322 $sth = $this->prepare($query);
1324 if (DB::isError($sth)) {
1328 $res =& $this->execute($sth, $params);
1329 $this->freePrepared($sth);
1331 $res =& $this->query($query);
1334 if (DB::isError($res)) {
1338 $fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC;
1341 while (is_array($row = $res->fetchRow($fetchmode))) {
1342 $ret[] = $row[$col];
1347 if (DB::isError($row)) {
1358 * Fetch the entire result set of a query and return it as an
1359 * associative array using the first column as the key
1361 * If the result set contains more than two columns, the value
1362 * will be an array of the values from column 2-n. If the result
1363 * set contains only two columns, the returned value will be a
1364 * scalar with the value of the second column (unless forced to an
1365 * array with the $force_array parameter). A DB error code is
1366 * returned on errors. If the result set contains fewer than two
1367 * columns, a DB_ERROR_TRUNCATED error is returned.
1369 * For example, if the table "mytable" contains:
1373 * --------------------------------
1376 * 3 'three' 944679408
1379 * Then the call getAssoc('SELECT id,text FROM mytable') returns:
1388 * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
1391 * '1' => array('one', '944679408'),
1392 * '2' => array('two', '944679408'),
1393 * '3' => array('three', '944679408')
1397 * If the more than one row occurs with the same value in the
1398 * first column, the last row overwrites all previous ones by
1399 * default. Use the $group parameter if you don't want to
1400 * overwrite like this. Example:
1403 * getAssoc('SELECT category,id,name FROM mytable', false, null,
1404 * DB_FETCHMODE_ASSOC, true) returns:
1407 * '1' => array(array('id' => '4', 'name' => 'number four'),
1408 * array('id' => '6', 'name' => 'number six')
1410 * '9' => array(array('id' => '4', 'name' => 'number four'),
1411 * array('id' => '6', 'name' => 'number six')
1416 * Keep in mind that database functions in PHP usually return string
1417 * values for results regardless of the database's internal type.
1419 * @param string $query the SQL query
1420 * @param boolean $force_array used only when the query returns
1421 * exactly two columns. If true, the values
1422 * of the returned array will be one-element
1423 * arrays instead of scalars.
1424 * @param mixed $params array, string or numeric data to be used in
1425 * execution of the statement. Quantity of items
1426 * passed must match quantity of placeholders in
1427 * query: meaning 1 placeholder for non-array
1428 * parameters or 1 placeholder per array element.
1429 * @param boolean $group if true, the values of the returned array
1430 * is wrapped in another array. If the same
1431 * key value (in the first column) repeats
1432 * itself, the values will be appended to
1433 * this array instead of overwriting the
1436 * @return array associative array with results from the query.
1437 * DB Error on failure.
1441 function &getAssoc($query, $force_array = false, $params = array(),
1442 $fetchmode = DB_FETCHMODE_DEFAULT, $group = false)
1444 settype($params, 'array');
1445 if (sizeof($params) > 0) {
1446 $sth = $this->prepare($query);
1448 if (DB::isError($sth)) {
1452 $res =& $this->execute($sth, $params);
1453 $this->freePrepared($sth);
1455 $res =& $this->query($query);
1458 if (DB::isError($res)) {
1461 if ($fetchmode == DB_FETCHMODE_DEFAULT) {
1462 $fetchmode = $this->fetchmode;
1464 $cols = $res->numCols();
1467 $tmp =& $this->raiseError(DB_ERROR_TRUNCATED);
1473 if ($cols > 2 || $force_array) {
1474 // return array values
1475 // XXX this part can be optimized
1476 if ($fetchmode == DB_FETCHMODE_ASSOC) {
1477 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ASSOC))) {
1479 $key = current($row);
1480 unset($row[key($row)]);
1482 $results[$key][] = $row;
1484 $results[$key] = $row;
1487 } elseif ($fetchmode == DB_FETCHMODE_OBJECT) {
1488 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
1489 $arr = get_object_vars($row);
1490 $key = current($arr);
1492 $results[$key][] = $row;
1494 $results[$key] = $row;
1498 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
1499 // we shift away the first element to get
1500 // indices running from 0 again
1501 $key = array_shift($row);
1503 $results[$key][] = $row;
1505 $results[$key] = $row;
1509 if (DB::isError($row)) {
1513 // return scalar values
1514 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
1516 $results[$row[0]][] = $row[1];
1518 $results[$row[0]] = $row[1];
1521 if (DB::isError($row)) {
1535 * Fetch all the rows returned from a query
1537 * @param string $query the SQL query
1538 * @param array $params array to be used in execution of the statement.
1539 * Quantity of array elements must match quantity
1540 * of placeholders in query. This function does
1541 * NOT support scalars.
1542 * @param int $fetchmode the fetch mode to use
1544 * @return array an nested array. DB error on failure.
1548 function &getAll($query,
1550 $fetchmode = DB_FETCHMODE_DEFAULT)
1552 // compat check, the params and fetchmode parameters used to
1553 // have the opposite order
1554 if (!is_array($params)) {
1555 if (is_array($fetchmode)) {
1556 if ($params === null) {
1557 $tmp = DB_FETCHMODE_DEFAULT;
1561 $params = $fetchmode;
1563 } elseif ($params !== null) {
1564 $fetchmode = $params;
1569 if (sizeof($params) > 0) {
1570 $sth = $this->prepare($query);
1572 if (DB::isError($sth)) {
1576 $res =& $this->execute($sth, $params);
1577 $this->freePrepared($sth);
1579 $res =& $this->query($query);
1582 if (DB::isError($res) || $res == DB_OK) {
1587 while (DB_OK === $res->fetchInto($row, $fetchmode)) {
1588 if ($fetchmode & DB_FETCHMODE_FLIPPED) {
1589 foreach ($row as $key => $val) {
1590 $results[$key][] = $val;
1599 if (DB::isError($row)) {
1600 $tmp =& $this->raiseError($row);
1610 * enable automatic Commit
1612 * @param boolean $onoff
1613 * @return mixed DB_Error
1617 function autoCommit($onoff=false)
1619 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1628 * @return mixed DB_Error
1634 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1643 * @return mixed DB_Error
1649 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1656 * Returns the number of rows in a result object
1658 * @param object DB_Result the result object to check
1660 * @return mixed DB_Error or the number of rows
1664 function numRows($result)
1666 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1670 // {{{ affectedRows()
1673 * Returns the affected rows of a query
1675 * @return mixed DB_Error or number of rows
1679 function affectedRows()
1681 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1685 // {{{ errorNative()
1688 * Returns an errormessage, provides by the database
1690 * @return mixed DB_Error or message
1694 function errorNative()
1696 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1700 // {{{ getSequenceName()
1703 * Generate the name used inside the database for a sequence
1705 * The createSequence() docblock contains notes about storing sequence
1708 * @param string $sqn the sequence's public name
1710 * @return string the sequence's name in the backend
1712 * @see DB_common::createSequence(), DB_common::dropSequence(),
1713 * DB_common::nextID(), DB_common::setOption()
1716 function getSequenceName($sqn)
1718 return sprintf($this->getOption('seqname_format'),
1719 preg_replace('/[^a-z0-9_.]/i', '_', $sqn));
1726 * Returns the next free id in a sequence
1728 * @param string $seq_name name of the sequence
1729 * @param boolean $ondemand when true, the seqence is automatically
1730 * created if it does not exist
1732 * @return int the next id number in the sequence. DB_Error if problem.
1734 * @see DB_common::createSequence(), DB_common::dropSequence(),
1735 * DB_common::getSequenceName()
1738 function nextId($seq_name, $ondemand = true)
1740 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1744 // {{{ createSequence()
1747 * Creates a new sequence
1749 * The name of a given sequence is determined by passing the string
1750 * provided in the <var>$seq_name</var> argument through PHP's sprintf()
1751 * function using the value from the <var>seqname_format</var> option as
1752 * the sprintf()'s format argument.
1754 * <var>seqname_format</var> is set via setOption().
1756 * @param string $seq_name name of the new sequence
1758 * @return int DB_OK on success. A DB_Error object is returned if
1761 * @see DB_common::dropSequence(), DB_common::getSequenceName(),
1762 * DB_common::nextID()
1765 function createSequence($seq_name)
1767 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1771 // {{{ dropSequence()
1774 * Deletes a sequence
1776 * @param string $seq_name name of the sequence to be deleted
1778 * @return int DB_OK on success. DB_Error if problems.
1780 * @see DB_common::createSequence(), DB_common::getSequenceName(),
1781 * DB_common::nextID()
1784 function dropSequence($seq_name)
1786 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1793 * Returns information about a table or a result set
1795 * The format of the resulting array depends on which <var>$mode</var>
1796 * you select. The sample output below is based on this query:
1798 * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
1800 * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
1806 * <kbd>null</kbd> (default)
1813 * [flags] => primary_key not_null
1817 * [name] => fldPhone
1827 * [flags] => primary_key not_null
1833 * <kbd>DB_TABLEINFO_ORDER</kbd>
1835 * <p>In addition to the information found in the default output,
1836 * a notation of the number of columns is provided by the
1837 * <samp>num_fields</samp> element while the <samp>order</samp>
1838 * element provides an array with the column names as the keys and
1839 * their location index number (corresponding to the keys in the
1840 * the default output) as the values.</p>
1842 * <p>If a result set has identical field names, the last one is
1847 * [order] => Array (
1855 * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>
1857 * <p>Similar to <kbd>DB_TABLEINFO_ORDER</kbd> but adds more
1858 * dimensions to the array in which the table names are keys and
1859 * the field names are sub-keys. This is helpful for queries that
1860 * join tables which have identical field names.</p>
1864 * [ordertable] => Array (
1865 * [tblFoo] => Array (
1869 * [tblBar] => Array (
1878 * The <samp>flags</samp> element contains a space separated list
1879 * of extra information about the field. This data is inconsistent
1880 * between DBMS's due to the way each DBMS works.
1881 * + <samp>primary_key</samp>
1882 * + <samp>unique_key</samp>
1883 * + <samp>multiple_key</samp>
1884 * + <samp>not_null</samp>
1886 * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
1887 * elements if <var>$result</var> is a table name. The following DBMS's
1888 * provide full information from queries:
1892 * If the 'portability' option has <samp>DB_PORTABILITY_LOWERCASE</samp>
1893 * turned on, the names of tables and fields will be lowercased.
1895 * @param object|string $result DB_result object from a query or a
1896 * string containing the name of a table.
1897 * While this also accepts a query result
1898 * resource identifier, this behavior is
1900 * @param int $mode either unused or one of the tableInfo modes:
1901 * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>,
1902 * <kbd>DB_TABLEINFO_ORDER</kbd> or
1903 * <kbd>DB_TABLEINFO_FULL</kbd> (which does both).
1904 * These are bitwise, so the first two can be
1905 * combined using <kbd>|</kbd>.
1906 * @return array an associative array with the information requested.
1907 * If something goes wrong an error object is returned.
1909 * @see DB_common::setOption()
1912 function tableInfo($result, $mode = null)
1915 * If the DB_<driver> class has a tableInfo() method, that one
1916 * overrides this one. But, if the driver doesn't have one,
1917 * this method runs and tells users about that fact.
1919 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1926 * @deprecated Deprecated in release 1.2 or lower
1928 function getTables()
1930 return $this->getListOf('tables');
1937 * list internal DB info
1938 * valid values for $type are db dependent,
1939 * often: databases, users, view, functions
1941 * @param string $type type of requested info
1943 * @return mixed DB_Error or the requested data
1947 function getListOf($type)
1949 $sql = $this->getSpecialQuery($type);
1950 if ($sql === null) { // No support
1951 return $this->raiseError(DB_ERROR_UNSUPPORTED);
1952 } elseif (is_int($sql) || DB::isError($sql)) { // Previous error
1953 return $this->raiseError($sql);
1954 } elseif (is_array($sql)) { // Already the result
1957 return $this->getCol($sql); // Launch this query
1961 // {{{ getSpecialQuery()
1964 * Returns the query needed to get some backend info
1966 * @param string $type What kind of info you want to retrieve
1968 * @return string The SQL query string
1972 function getSpecialQuery($type)
1974 return $this->raiseError(DB_ERROR_UNSUPPORTED);
1978 // {{{ _rtrimArrayValues()
1981 * Right trim all strings in an array
1983 * @param array $array the array to be trimmed (passed by reference)
1987 function _rtrimArrayValues(&$array)
1989 foreach ($array as $key => $value) {
1990 if (is_string($value)) {
1991 $array[$key] = rtrim($value);
1997 // {{{ _convertNullArrayValuesToEmpty()
2000 * Convert all null values in an array to empty strings
2002 * @param array $array the array to be de-nullified (passed by reference)
2006 function _convertNullArrayValuesToEmpty(&$array)
2008 foreach ($array as $key => $value) {
2009 if (is_null($value)) {