3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Contains the DB_common base class
10 * LICENSE: This source file is subject to version 3.0 of the PHP license
11 * that is available through the world-wide-web at the following URI:
12 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
13 * the PHP License and are unable to obtain it through the web, please
14 * send a note to license@php.net so we can mail you a copy immediately.
18 * @author Stig Bakken <ssb@php.net>
19 * @author Tomas V.V. Cox <cox@idecnet.com>
20 * @author Daniel Convissor <danielc@php.net>
21 * @copyright 1997-2007 The PHP Group
22 * @license http://www.php.net/license/3_0.txt PHP License 3.0
23 * @version CVS: $Id: common.php,v 1.144 2007/11/26 22:54:03 aharvey Exp $
24 * @link http://pear.php.net/package/DB
28 * Obtain the PEAR class so it can be extended from
30 require_once 'PEAR.php';
33 * DB_common is the base class from which each database driver class extends
35 * All common methods are declared here. If a given DBMS driver contains
36 * a particular method, that method will overload the one here.
40 * @author Stig Bakken <ssb@php.net>
41 * @author Tomas V.V. Cox <cox@idecnet.com>
42 * @author Daniel Convissor <danielc@php.net>
43 * @copyright 1997-2007 The PHP Group
44 * @license http://www.php.net/license/3_0.txt PHP License 3.0
45 * @version Release: 1.7.14RC1
46 * @link http://pear.php.net/package/DB
48 class DB_common extends PEAR
53 * The current default fetch mode
56 var $fetchmode = DB_FETCHMODE_ORDERED;
59 * The name of the class into which results should be fetched when
60 * DB_FETCHMODE_OBJECT is in effect
64 var $fetchmode_object_class = 'stdClass';
67 * Was a connection present when the object was serialized()?
69 * @see DB_common::__sleep(), DB_common::__wake()
71 var $was_connected = null;
74 * The most recently executed query
80 * Run-time configuration options
82 * The 'optimize' option has been deprecated. Use the 'portability'
86 * @see DB_common::setOption()
89 'result_buffering' => 500,
90 'persistent' => false,
93 'seqname_format' => '%s_seq',
95 'portability' => DB_PORTABILITY_NONE,
96 'optimize' => 'performance', // Deprecated. Use 'portability'.
100 * The parameters from the most recently executed query
102 * @since Property available since Release 1.7.0
104 var $last_parameters = array();
107 * The elements from each prepared statement
110 var $prepare_tokens = array();
113 * The data types of the various elements in each prepared statement
116 var $prepare_types = array();
119 * The prepared queries
122 var $prepared_queries = array();
125 * Flag indicating that the last query was a manipulation query.
129 var $_last_query_manip = false;
132 * Flag indicating that the next query <em>must</em> be a manipulation
137 var $_next_query_manip = false;
144 * This constructor calls <kbd>$this->PEAR('DB_Error')</kbd>
150 $this->PEAR('DB_Error');
157 * Automatically indicates which properties should be saved
158 * when PHP's serialize() function is called
160 * @return array the array of properties names that should be saved
164 if ($this->connection) {
165 // Don't disconnect(), people use serialize() for many reasons
166 $this->was_connected = true;
168 $this->was_connected = false;
170 if (isset($this->autocommit)) {
171 return array('autocommit',
176 'fetchmode_object_class',
181 return array('dbsyntax',
185 'fetchmode_object_class',
196 * Automatically reconnects to the database when PHP's unserialize()
199 * The reconnection attempt is only performed if the object was connected
200 * at the time PHP's serialize() function was run.
206 if ($this->was_connected) {
207 $this->connect($this->dsn, $this->options);
215 * Automatic string conversion for PHP 5
217 * @return string a string describing the current PEAR DB object
219 * @since Method available since Release 1.7.0
221 function __toString()
223 $info = strtolower(get_class($this));
224 $info .= ': (phptype=' . $this->phptype .
225 ', dbsyntax=' . $this->dbsyntax .
227 if ($this->connection) {
228 $info .= ' [connected]';
237 * DEPRECATED: String conversion method
239 * @return string a string describing the current PEAR DB object
241 * @deprecated Method deprecated in Release 1.7.0
245 return $this->__toString();
252 * DEPRECATED: Quotes a string so it can be safely used within string
253 * delimiters in a query
255 * @param string $string the string to be quoted
257 * @return string the quoted string
259 * @see DB_common::quoteSmart(), DB_common::escapeSimple()
260 * @deprecated Method deprecated some time before Release 1.2
262 function quoteString($string)
264 $string = $this->quote($string);
265 if ($string{0} == "'") {
266 return substr($string, 1, -1);
275 * DEPRECATED: Quotes a string so it can be safely used in a query
277 * @param string $string the string to quote
279 * @return string the quoted string or the string <samp>NULL</samp>
280 * if the value submitted is <kbd>null</kbd>.
282 * @see DB_common::quoteSmart(), DB_common::escapeSimple()
283 * @deprecated Deprecated in release 1.6.0
285 function quote($string = null)
287 return ($string === null) ? 'NULL'
288 : "'" . str_replace("'", "''", $string) . "'";
292 // {{{ quoteIdentifier()
295 * Quotes a string so it can be safely used as a table or column name
297 * Delimiting style depends on which database driver is being used.
299 * NOTE: just because you CAN use delimited identifiers doesn't mean
300 * you SHOULD use them. In general, they end up causing way more
301 * problems than they solve.
303 * Portability is broken by using the following characters inside
304 * delimited identifiers:
305 * + backtick (<kbd>`</kbd>) -- due to MySQL
306 * + double quote (<kbd>"</kbd>) -- due to Oracle
307 * + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
309 * Delimited identifiers are known to generally work correctly under
310 * the following drivers:
319 * + sybase (must execute <kbd>set quoted_identifier on</kbd> sometime
322 * InterBase doesn't seem to be able to use delimited identifiers
323 * via PHP 4. They work fine under PHP 5.
325 * @param string $str the identifier name to be quoted
327 * @return string the quoted identifier
329 * @since Method available since Release 1.6.0
331 function quoteIdentifier($str)
333 return '"' . str_replace('"', '""', $str) . '"';
340 * Formats input so it can be safely used in a query
342 * The output depends on the PHP data type of input and the database
345 * @param mixed $in the data to be formatted
347 * @return mixed the formatted data. The format depends on the input's
351 * <kbd>input</kbd> -> <samp>returns</samp>
354 * <kbd>null</kbd> -> the string <samp>NULL</samp>
357 * <kbd>integer</kbd> or <kbd>double</kbd> -> the unquoted number
360 * <kbd>bool</kbd> -> output depends on the driver in use
361 * Most drivers return integers: <samp>1</samp> if
362 * <kbd>true</kbd> or <samp>0</samp> if
364 * Some return strings: <samp>TRUE</samp> if
365 * <kbd>true</kbd> or <samp>FALSE</samp> if
367 * Finally one returns strings: <samp>T</samp> if
368 * <kbd>true</kbd> or <samp>F</samp> if
369 * <kbd>false</kbd>. Here is a list of each DBMS,
370 * the values returned and the suggested column type:
373 * <kbd>dbase</kbd> -> <samp>T/F</samp>
374 * (<kbd>Logical</kbd>)
377 * <kbd>fbase</kbd> -> <samp>TRUE/FALSE</samp>
378 * (<kbd>BOOLEAN</kbd>)
381 * <kbd>ibase</kbd> -> <samp>1/0</samp>
382 * (<kbd>SMALLINT</kbd>) [1]
385 * <kbd>ifx</kbd> -> <samp>1/0</samp>
386 * (<kbd>SMALLINT</kbd>) [1]
389 * <kbd>msql</kbd> -> <samp>1/0</samp>
390 * (<kbd>INTEGER</kbd>)
393 * <kbd>mssql</kbd> -> <samp>1/0</samp>
397 * <kbd>mysql</kbd> -> <samp>1/0</samp>
398 * (<kbd>TINYINT(1)</kbd>)
401 * <kbd>mysqli</kbd> -> <samp>1/0</samp>
402 * (<kbd>TINYINT(1)</kbd>)
405 * <kbd>oci8</kbd> -> <samp>1/0</samp>
406 * (<kbd>NUMBER(1)</kbd>)
409 * <kbd>odbc</kbd> -> <samp>1/0</samp>
410 * (<kbd>SMALLINT</kbd>) [1]
413 * <kbd>pgsql</kbd> -> <samp>TRUE/FALSE</samp>
414 * (<kbd>BOOLEAN</kbd>)
417 * <kbd>sqlite</kbd> -> <samp>1/0</samp>
418 * (<kbd>INTEGER</kbd>)
421 * <kbd>sybase</kbd> -> <samp>1/0</samp>
422 * (<kbd>TINYINT(1)</kbd>)
425 * [1] Accommodate the lowest common denominator because not all
426 * versions of have <kbd>BOOLEAN</kbd>.
429 * other (including strings and numeric strings) ->
430 * the data with single quotes escaped by preceeding
431 * single quotes, backslashes are escaped by preceeding
432 * backslashes, then the whole string is encapsulated
433 * between single quotes
437 * @see DB_common::escapeSimple()
438 * @since Method available since Release 1.6.0
440 function quoteSmart($in)
444 } elseif (is_float($in)) {
445 return $this->quoteFloat($in);
446 } elseif (is_bool($in)) {
447 return $this->quoteBoolean($in);
448 } elseif (is_null($in)) {
451 if ($this->dbsyntax == 'access'
452 && preg_match('/^#.+#$/', $in))
454 return $this->escapeSimple($in);
456 return "'" . $this->escapeSimple($in) . "'";
461 // {{{ quoteBoolean()
464 * Formats a boolean value for use within a query in a locale-independent
467 * @param boolean the boolean value to be quoted.
468 * @return string the quoted string.
469 * @see DB_common::quoteSmart()
470 * @since Method available since release 1.7.8.
472 function quoteBoolean($boolean) {
473 return $boolean ? '1' : '0';
480 * Formats a float value for use within a query in a locale-independent
483 * @param float the float value to be quoted.
484 * @return string the quoted string.
485 * @see DB_common::quoteSmart()
486 * @since Method available since release 1.7.8.
488 function quoteFloat($float) {
489 return "'".$this->escapeSimple(str_replace(',', '.', strval(floatval($float))))."'";
493 // {{{ escapeSimple()
496 * Escapes a string according to the current DBMS's standards
498 * In SQLite, this makes things safe for inserts/updates, but may
499 * cause problems when performing text comparisons against columns
500 * containing binary data. See the
501 * {@link http://php.net/sqlite_escape_string PHP manual} for more info.
503 * @param string $str the string to be escaped
505 * @return string the escaped string
507 * @see DB_common::quoteSmart()
508 * @since Method available since Release 1.6.0
510 function escapeSimple($str)
512 return str_replace("'", "''", $str);
519 * Tells whether the present driver supports a given feature
521 * @param string $feature the feature you're curious about
523 * @return bool whether this driver supports $feature
525 function provides($feature)
527 return $this->features[$feature];
531 // {{{ setFetchMode()
534 * Sets the fetch mode that should be used by default for query results
536 * @param integer $fetchmode DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC
537 * or DB_FETCHMODE_OBJECT
538 * @param string $object_class the class name of the object to be returned
539 * by the fetch methods when the
540 * DB_FETCHMODE_OBJECT mode is selected.
541 * If no class is specified by default a cast
542 * to object from the assoc array row will be
543 * done. There is also the posibility to use
544 * and extend the 'DB_row' class.
546 * @see DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC, DB_FETCHMODE_OBJECT
548 function setFetchMode($fetchmode, $object_class = 'stdClass')
550 switch ($fetchmode) {
551 case DB_FETCHMODE_OBJECT:
552 $this->fetchmode_object_class = $object_class;
553 case DB_FETCHMODE_ORDERED:
554 case DB_FETCHMODE_ASSOC:
555 $this->fetchmode = $fetchmode;
558 return $this->raiseError('invalid fetchmode mode');
566 * Sets run-time configuration options for PEAR DB
568 * Options, their data types, default values and description:
571 * <var>autofree</var> <kbd>boolean</kbd> = <samp>false</samp>
572 * <br />should results be freed automatically when there are no
575 * <var>result_buffering</var> <kbd>integer</kbd> = <samp>500</samp>
576 * <br />how many rows of the result set should be buffered?
577 * <br />In mysql: mysql_unbuffered_query() is used instead of
578 * mysql_query() if this value is 0. (Release 1.7.0)
579 * <br />In oci8: this value is passed to ocisetprefetch().
582 * <var>debug</var> <kbd>integer</kbd> = <samp>0</samp>
585 * <var>persistent</var> <kbd>boolean</kbd> = <samp>false</samp>
586 * <br />should the connection be persistent?
588 * <var>portability</var> <kbd>integer</kbd> = <samp>DB_PORTABILITY_NONE</samp>
589 * <br />portability mode constant (see below)
591 * <var>seqname_format</var> <kbd>string</kbd> = <samp>%s_seq</samp>
592 * <br />the sprintf() format string used on sequence names. This
593 * format is applied to sequence names passed to
594 * createSequence(), nextID() and dropSequence().
596 * <var>ssl</var> <kbd>boolean</kbd> = <samp>false</samp>
597 * <br />use ssl to connect?
601 * -----------------------------------------
605 * These modes are bitwised, so they can be combined using <kbd>|</kbd>
606 * and removed using <kbd>^</kbd>. See the examples section below on how
609 * <samp>DB_PORTABILITY_NONE</samp>
610 * turn off all portability features
612 * This mode gets automatically turned on if the deprecated
613 * <var>optimize</var> option gets set to <samp>performance</samp>.
616 * <samp>DB_PORTABILITY_LOWERCASE</samp>
617 * convert names of tables and fields to lower case when using
618 * <kbd>get*()</kbd>, <kbd>fetch*()</kbd> and <kbd>tableInfo()</kbd>
620 * This mode gets automatically turned on in the following databases
621 * if the deprecated option <var>optimize</var> gets set to
622 * <samp>portability</samp>:
626 * <samp>DB_PORTABILITY_RTRIM</samp>
627 * right trim the data output by <kbd>get*()</kbd> <kbd>fetch*()</kbd>
630 * <samp>DB_PORTABILITY_DELETE_COUNT</samp>
631 * force reporting the number of rows deleted
633 * Some DBMS's don't count the number of rows deleted when performing
634 * simple <kbd>DELETE FROM tablename</kbd> queries. This portability
635 * mode tricks such DBMS's into telling the count by adding
636 * <samp>WHERE 1=1</samp> to the end of <kbd>DELETE</kbd> queries.
638 * This mode gets automatically turned on in the following databases
639 * if the deprecated option <var>optimize</var> gets set to
640 * <samp>portability</samp>:
647 * <samp>DB_PORTABILITY_NUMROWS</samp>
648 * enable hack that makes <kbd>numRows()</kbd> work in Oracle
650 * This mode gets automatically turned on in the following databases
651 * if the deprecated option <var>optimize</var> gets set to
652 * <samp>portability</samp>:
656 * <samp>DB_PORTABILITY_ERRORS</samp>
657 * makes certain error messages in certain drivers compatible
658 * with those from other DBMS's
660 * + mysql, mysqli: change unique/primary key constraints
661 * DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
663 * + odbc(access): MS's ODBC driver reports 'no such field' as code
664 * 07001, which means 'too few parameters.' When this option is on
665 * that code gets mapped to DB_ERROR_NOSUCHFIELD.
666 * DB_ERROR_MISMATCH -> DB_ERROR_NOSUCHFIELD
668 * <samp>DB_PORTABILITY_NULL_TO_EMPTY</samp>
669 * convert null values to empty strings in data output by get*() and
670 * fetch*(). Needed because Oracle considers empty strings to be null,
671 * while most other DBMS's know the difference between empty and null.
674 * <samp>DB_PORTABILITY_ALL</samp>
675 * turn on all portability features
677 * -----------------------------------------
679 * Example 1. Simple setOption() example
681 * $db->setOption('autofree', true);
684 * Example 2. Portability for lowercasing and trimming
686 * $db->setOption('portability',
687 * DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM);
690 * Example 3. All portability options except trimming
692 * $db->setOption('portability',
693 * DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM);
696 * @param string $option option name
697 * @param mixed $value value for the option
699 * @return int DB_OK on success. A DB_Error object on failure.
701 * @see DB_common::$options
703 function setOption($option, $value)
705 if (isset($this->options[$option])) {
706 $this->options[$option] = $value;
709 * Backwards compatibility check for the deprecated 'optimize'
710 * option. Done here in case settings change after connecting.
712 if ($option == 'optimize') {
713 if ($value == 'portability') {
714 switch ($this->phptype) {
716 $this->options['portability'] =
717 DB_PORTABILITY_LOWERCASE |
718 DB_PORTABILITY_NUMROWS;
724 $this->options['portability'] =
725 DB_PORTABILITY_DELETE_COUNT;
729 $this->options['portability'] = DB_PORTABILITY_NONE;
735 return $this->raiseError("unknown option $option");
742 * Returns the value of an option
744 * @param string $option the option name you're curious about
746 * @return mixed the option's value
748 function getOption($option)
750 if (isset($this->options[$option])) {
751 return $this->options[$option];
753 return $this->raiseError("unknown option $option");
760 * Prepares a query for multiple execution with execute()
762 * Creates a query that can be run multiple times. Each time it is run,
763 * the placeholders, if any, will be replaced by the contents of
764 * execute()'s $data argument.
766 * Three types of placeholders can be used:
767 * + <kbd>?</kbd> scalar value (i.e. strings, integers). The system
768 * will automatically quote and escape the data.
769 * + <kbd>!</kbd> value is inserted 'as is'
770 * + <kbd>&</kbd> requires a file name. The file's contents get
771 * inserted into the query (i.e. saving binary
776 * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
782 * $res = $db->execute($sth, $data);
785 * Use backslashes to escape placeholder characters if you don't want
786 * them to be interpreted as placeholders:
788 * "UPDATE foo SET col=? WHERE col='over \& under'"
791 * With some database backends, this is emulated.
793 * {@internal ibase and oci8 have their own prepare() methods.}}
795 * @param string $query the query to be prepared
797 * @return mixed DB statement resource on success. A DB_Error object
800 * @see DB_common::execute()
802 function prepare($query)
804 $tokens = preg_split('/((?<!\\\)[&?!])/', $query, -1,
805 PREG_SPLIT_DELIM_CAPTURE);
808 $newtokens = array();
810 foreach ($tokens as $val) {
813 $types[$token++] = DB_PARAM_SCALAR;
816 $types[$token++] = DB_PARAM_OPAQUE;
819 $types[$token++] = DB_PARAM_MISC;
822 $newtokens[] = preg_replace('/\\\([&?!])/', "\\1", $val);
826 $this->prepare_tokens[] = &$newtokens;
827 end($this->prepare_tokens);
829 $k = key($this->prepare_tokens);
830 $this->prepare_types[$k] = $types;
831 $this->prepared_queries[$k] = implode(' ', $newtokens);
840 * Automaticaly generates an insert or update query and pass it to prepare()
842 * @param string $table the table name
843 * @param array $table_fields the array of field names
844 * @param int $mode a type of query to make:
845 * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
846 * @param string $where for update queries: the WHERE clause to
847 * append to the SQL statement. Don't
848 * include the "WHERE" keyword.
850 * @return resource the query handle
852 * @uses DB_common::prepare(), DB_common::buildManipSQL()
854 function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT,
857 $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
858 if (DB::isError($query)) {
861 return $this->prepare($query);
868 * Automaticaly generates an insert or update query and call prepare()
869 * and execute() with it
871 * @param string $table the table name
872 * @param array $fields_values the associative array where $key is a
873 * field name and $value its value
874 * @param int $mode a type of query to make:
875 * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
876 * @param string $where for update queries: the WHERE clause to
877 * append to the SQL statement. Don't
878 * include the "WHERE" keyword.
880 * @return mixed a new DB_result object for successful SELECT queries
881 * or DB_OK for successul data manipulation queries.
882 * A DB_Error object on failure.
884 * @uses DB_common::autoPrepare(), DB_common::execute()
886 function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT,
889 $sth = $this->autoPrepare($table, array_keys($fields_values), $mode,
891 if (DB::isError($sth)) {
894 $ret = $this->execute($sth, array_values($fields_values));
895 $this->freePrepared($sth);
901 // {{{ buildManipSQL()
904 * Produces an SQL query string for autoPrepare()
908 * buildManipSQL('table_sql', array('field1', 'field2', 'field3'),
909 * DB_AUTOQUERY_INSERT);
914 * INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
918 * - This belongs more to a SQL Builder class, but this is a simple
920 * - Be carefull! If you don't give a $where param with an UPDATE
921 * query, all the records of the table will be updated!
923 * @param string $table the table name
924 * @param array $table_fields the array of field names
925 * @param int $mode a type of query to make:
926 * DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
927 * @param string $where for update queries: the WHERE clause to
928 * append to the SQL statement. Don't
929 * include the "WHERE" keyword.
931 * @return string the sql query for autoPrepare()
933 function buildManipSQL($table, $table_fields, $mode, $where = false)
935 if (count($table_fields) == 0) {
936 return $this->raiseError(DB_ERROR_NEED_MORE_DATA);
940 case DB_AUTOQUERY_INSERT:
943 foreach ($table_fields as $value) {
953 return "INSERT INTO $table ($names) VALUES ($values)";
954 case DB_AUTOQUERY_UPDATE:
956 foreach ($table_fields as $value) {
962 $set .= "$value = ?";
964 $sql = "UPDATE $table SET $set";
966 $sql .= " WHERE $where";
970 return $this->raiseError(DB_ERROR_SYNTAX);
978 * Executes a DB statement prepared with prepare()
982 * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
988 * $res = $db->execute($sth, $data);
991 * @param resource $stmt a DB statement resource returned from prepare()
992 * @param mixed $data array, string or numeric data to be used in
993 * execution of the statement. Quantity of items
994 * passed must match quantity of placeholders in
995 * query: meaning 1 placeholder for non-array
996 * parameters or 1 placeholder per array element.
998 * @return mixed a new DB_result object for successful SELECT queries
999 * or DB_OK for successul data manipulation queries.
1000 * A DB_Error object on failure.
1002 * {@internal ibase and oci8 have their own execute() methods.}}
1004 * @see DB_common::prepare()
1006 function &execute($stmt, $data = array())
1008 $realquery = $this->executeEmulateQuery($stmt, $data);
1009 if (DB::isError($realquery)) {
1012 $result = $this->simpleQuery($realquery);
1014 if ($result === DB_OK || DB::isError($result)) {
1017 $tmp = new DB_result($this, $result);
1023 // {{{ executeEmulateQuery()
1026 * Emulates executing prepared statements if the DBMS not support them
1028 * @param resource $stmt a DB statement resource returned from execute()
1029 * @param mixed $data array, string or numeric data to be used in
1030 * execution of the statement. Quantity of items
1031 * passed must match quantity of placeholders in
1032 * query: meaning 1 placeholder for non-array
1033 * parameters or 1 placeholder per array element.
1035 * @return mixed a string containing the real query run when emulating
1036 * prepare/execute. A DB_Error object on failure.
1039 * @see DB_common::execute()
1041 function executeEmulateQuery($stmt, $data = array())
1044 $data = (array)$data;
1045 $this->last_parameters = $data;
1047 if (count($this->prepare_types[$stmt]) != count($data)) {
1048 $this->last_query = $this->prepared_queries[$stmt];
1049 return $this->raiseError(DB_ERROR_MISMATCH);
1052 $realquery = $this->prepare_tokens[$stmt][0];
1055 foreach ($data as $value) {
1056 if ($this->prepare_types[$stmt][$i] == DB_PARAM_SCALAR) {
1057 $realquery .= $this->quoteSmart($value);
1058 } elseif ($this->prepare_types[$stmt][$i] == DB_PARAM_OPAQUE) {
1059 $fp = @fopen($value, 'rb');
1061 return $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
1063 $realquery .= $this->quoteSmart(fread($fp, filesize($value)));
1066 $realquery .= $value;
1069 $realquery .= $this->prepare_tokens[$stmt][++$i];
1076 // {{{ executeMultiple()
1079 * Performs several execute() calls on the same statement handle
1081 * $data must be an array indexed numerically
1082 * from 0, one execute call is done for every "row" in the array.
1084 * If an error occurs during execute(), executeMultiple() does not
1085 * execute the unfinished rows, but rather returns that error.
1087 * @param resource $stmt query handle from prepare()
1088 * @param array $data numeric array containing the
1089 * data to insert into the query
1091 * @return int DB_OK on success. A DB_Error object on failure.
1093 * @see DB_common::prepare(), DB_common::execute()
1095 function executeMultiple($stmt, $data)
1097 foreach ($data as $value) {
1098 $res = $this->execute($stmt, $value);
1099 if (DB::isError($res)) {
1107 // {{{ freePrepared()
1110 * Frees the internal resources associated with a prepared query
1112 * @param resource $stmt the prepared statement's PHP resource
1113 * @param bool $free_resource should the PHP resource be freed too?
1114 * Use false if you need to get data
1115 * from the result set later.
1117 * @return bool TRUE on success, FALSE if $result is invalid
1119 * @see DB_common::prepare()
1121 function freePrepared($stmt, $free_resource = true)
1124 if (isset($this->prepare_tokens[$stmt])) {
1125 unset($this->prepare_tokens[$stmt]);
1126 unset($this->prepare_types[$stmt]);
1127 unset($this->prepared_queries[$stmt]);
1134 // {{{ modifyQuery()
1137 * Changes a query string for various DBMS specific reasons
1139 * It is defined here to ensure all drivers have this method available.
1141 * @param string $query the query string to modify
1143 * @return string the modified query string
1146 * @see DB_mysql::modifyQuery(), DB_oci8::modifyQuery(),
1147 * DB_sqlite::modifyQuery()
1149 function modifyQuery($query)
1155 // {{{ modifyLimitQuery()
1158 * Adds LIMIT clauses to a query string according to current DBMS standards
1160 * It is defined here to assure that all implementations
1161 * have this method defined.
1163 * @param string $query the query to modify
1164 * @param int $from the row to start to fetching (0 = the first row)
1165 * @param int $count the numbers of rows to fetch
1166 * @param mixed $params array, string or numeric data to be used in
1167 * execution of the statement. Quantity of items
1168 * passed must match quantity of placeholders in
1169 * query: meaning 1 placeholder for non-array
1170 * parameters or 1 placeholder per array element.
1172 * @return string the query string with LIMIT clauses added
1176 function modifyLimitQuery($query, $from, $count, $params = array())
1185 * Sends a query to the database server
1187 * The query string can be either a normal statement to be sent directly
1188 * to the server OR if <var>$params</var> are passed the query can have
1189 * placeholders and it will be passed through prepare() and execute().
1191 * @param string $query the SQL query or the statement to prepare
1192 * @param mixed $params array, string or numeric data to be used in
1193 * execution of the statement. Quantity of items
1194 * passed must match quantity of placeholders in
1195 * query: meaning 1 placeholder for non-array
1196 * parameters or 1 placeholder per array element.
1198 * @return mixed a new DB_result object for successful SELECT queries
1199 * or DB_OK for successul data manipulation queries.
1200 * A DB_Error object on failure.
1202 * @see DB_result, DB_common::prepare(), DB_common::execute()
1204 function &query($query, $params = array())
1206 if (sizeof($params) > 0) {
1207 $sth = $this->prepare($query);
1208 if (DB::isError($sth)) {
1211 $ret = $this->execute($sth, $params);
1212 $this->freePrepared($sth, false);
1215 $this->last_parameters = array();
1216 $result = $this->simpleQuery($query);
1217 if ($result === DB_OK || DB::isError($result)) {
1220 $tmp = new DB_result($this, $result);
1230 * Generates and executes a LIMIT query
1232 * @param string $query the query
1233 * @param intr $from the row to start to fetching (0 = the first row)
1234 * @param int $count the numbers of rows to fetch
1235 * @param mixed $params array, string or numeric data to be used in
1236 * execution of the statement. Quantity of items
1237 * passed must match quantity of placeholders in
1238 * query: meaning 1 placeholder for non-array
1239 * parameters or 1 placeholder per array element.
1241 * @return mixed a new DB_result object for successful SELECT queries
1242 * or DB_OK for successul data manipulation queries.
1243 * A DB_Error object on failure.
1245 function &limitQuery($query, $from, $count, $params = array())
1247 $query = $this->modifyLimitQuery($query, $from, $count, $params);
1248 if (DB::isError($query)){
1251 $result = $this->query($query, $params);
1252 if (is_a($result, 'DB_result')) {
1253 $result->setOption('limit_from', $from);
1254 $result->setOption('limit_count', $count);
1263 * Fetches the first column of the first row from a query result
1265 * Takes care of doing the query and freeing the results when finished.
1267 * @param string $query the SQL query
1268 * @param mixed $params array, string or numeric data to be used in
1269 * execution of the statement. Quantity of items
1270 * passed must match quantity of placeholders in
1271 * query: meaning 1 placeholder for non-array
1272 * parameters or 1 placeholder per array element.
1274 * @return mixed the returned value of the query.
1275 * A DB_Error object on failure.
1277 function &getOne($query, $params = array())
1279 $params = (array)$params;
1280 // modifyLimitQuery() would be nice here, but it causes BC issues
1281 if (sizeof($params) > 0) {
1282 $sth = $this->prepare($query);
1283 if (DB::isError($sth)) {
1286 $res = $this->execute($sth, $params);
1287 $this->freePrepared($sth);
1289 $res = $this->query($query);
1292 if (DB::isError($res)) {
1296 $err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);
1299 if ($err !== DB_OK) {
1310 * Fetches the first row of data returned from a query result
1312 * Takes care of doing the query and freeing the results when finished.
1314 * @param string $query the SQL query
1315 * @param mixed $params array, string or numeric data to be used in
1316 * execution of the statement. Quantity of items
1317 * passed must match quantity of placeholders in
1318 * query: meaning 1 placeholder for non-array
1319 * parameters or 1 placeholder per array element.
1320 * @param int $fetchmode the fetch mode to use
1322 * @return array the first row of results as an array.
1323 * A DB_Error object on failure.
1325 function &getRow($query, $params = array(),
1326 $fetchmode = DB_FETCHMODE_DEFAULT)
1328 // compat check, the params and fetchmode parameters used to
1329 // have the opposite order
1330 if (!is_array($params)) {
1331 if (is_array($fetchmode)) {
1332 if ($params === null) {
1333 $tmp = DB_FETCHMODE_DEFAULT;
1337 $params = $fetchmode;
1339 } elseif ($params !== null) {
1340 $fetchmode = $params;
1344 // modifyLimitQuery() would be nice here, but it causes BC issues
1345 if (sizeof($params) > 0) {
1346 $sth = $this->prepare($query);
1347 if (DB::isError($sth)) {
1350 $res = $this->execute($sth, $params);
1351 $this->freePrepared($sth);
1353 $res = $this->query($query);
1356 if (DB::isError($res)) {
1360 $err = $res->fetchInto($row, $fetchmode);
1364 if ($err !== DB_OK) {
1375 * Fetches a single column from a query result and returns it as an
1378 * @param string $query the SQL query
1379 * @param mixed $col which column to return (integer [column number,
1380 * starting at 0] or string [column name])
1381 * @param mixed $params array, string or numeric data to be used in
1382 * execution of the statement. Quantity of items
1383 * passed must match quantity of placeholders in
1384 * query: meaning 1 placeholder for non-array
1385 * parameters or 1 placeholder per array element.
1387 * @return array the results as an array. A DB_Error object on failure.
1389 * @see DB_common::query()
1391 function &getCol($query, $col = 0, $params = array())
1393 $params = (array)$params;
1394 if (sizeof($params) > 0) {
1395 $sth = $this->prepare($query);
1397 if (DB::isError($sth)) {
1401 $res = $this->execute($sth, $params);
1402 $this->freePrepared($sth);
1404 $res = $this->query($query);
1407 if (DB::isError($res)) {
1411 $fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC;
1413 if (!is_array($row = $res->fetchRow($fetchmode))) {
1416 if (!array_key_exists($col, $row)) {
1417 $ret = $this->raiseError(DB_ERROR_NOSUCHFIELD);
1419 $ret = array($row[$col]);
1420 while (is_array($row = $res->fetchRow($fetchmode))) {
1421 $ret[] = $row[$col];
1428 if (DB::isError($row)) {
1439 * Fetches an entire query result and returns it as an
1440 * associative array using the first column as the key
1442 * If the result set contains more than two columns, the value
1443 * will be an array of the values from column 2-n. If the result
1444 * set contains only two columns, the returned value will be a
1445 * scalar with the value of the second column (unless forced to an
1446 * array with the $force_array parameter). A DB error code is
1447 * returned on errors. If the result set contains fewer than two
1448 * columns, a DB_ERROR_TRUNCATED error is returned.
1450 * For example, if the table "mytable" contains:
1454 * --------------------------------
1457 * 3 'three' 944679408
1460 * Then the call getAssoc('SELECT id,text FROM mytable') returns:
1469 * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
1472 * '1' => array('one', '944679408'),
1473 * '2' => array('two', '944679408'),
1474 * '3' => array('three', '944679408')
1478 * If the more than one row occurs with the same value in the
1479 * first column, the last row overwrites all previous ones by
1480 * default. Use the $group parameter if you don't want to
1481 * overwrite like this. Example:
1484 * getAssoc('SELECT category,id,name FROM mytable', false, null,
1485 * DB_FETCHMODE_ASSOC, true) returns:
1488 * '1' => array(array('id' => '4', 'name' => 'number four'),
1489 * array('id' => '6', 'name' => 'number six')
1491 * '9' => array(array('id' => '4', 'name' => 'number four'),
1492 * array('id' => '6', 'name' => 'number six')
1497 * Keep in mind that database functions in PHP usually return string
1498 * values for results regardless of the database's internal type.
1500 * @param string $query the SQL query
1501 * @param bool $force_array used only when the query returns
1502 * exactly two columns. If true, the values
1503 * of the returned array will be one-element
1504 * arrays instead of scalars.
1505 * @param mixed $params array, string or numeric data to be used in
1506 * execution of the statement. Quantity of
1507 * items passed must match quantity of
1508 * placeholders in query: meaning 1
1509 * placeholder for non-array parameters or
1510 * 1 placeholder per array element.
1511 * @param int $fetchmode the fetch mode to use
1512 * @param bool $group if true, the values of the returned array
1513 * is wrapped in another array. If the same
1514 * key value (in the first column) repeats
1515 * itself, the values will be appended to
1516 * this array instead of overwriting the
1519 * @return array the associative array containing the query results.
1520 * A DB_Error object on failure.
1522 function &getAssoc($query, $force_array = false, $params = array(),
1523 $fetchmode = DB_FETCHMODE_DEFAULT, $group = false)
1525 $params = (array)$params;
1526 if (sizeof($params) > 0) {
1527 $sth = $this->prepare($query);
1529 if (DB::isError($sth)) {
1533 $res = $this->execute($sth, $params);
1534 $this->freePrepared($sth);
1536 $res = $this->query($query);
1539 if (DB::isError($res)) {
1542 if ($fetchmode == DB_FETCHMODE_DEFAULT) {
1543 $fetchmode = $this->fetchmode;
1545 $cols = $res->numCols();
1548 $tmp = $this->raiseError(DB_ERROR_TRUNCATED);
1554 if ($cols > 2 || $force_array) {
1555 // return array values
1556 // XXX this part can be optimized
1557 if ($fetchmode == DB_FETCHMODE_ASSOC) {
1558 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ASSOC))) {
1560 $key = current($row);
1561 unset($row[key($row)]);
1563 $results[$key][] = $row;
1565 $results[$key] = $row;
1568 } elseif ($fetchmode == DB_FETCHMODE_OBJECT) {
1569 while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
1570 $arr = get_object_vars($row);
1571 $key = current($arr);
1573 $results[$key][] = $row;
1575 $results[$key] = $row;
1579 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
1580 // we shift away the first element to get
1581 // indices running from 0 again
1582 $key = array_shift($row);
1584 $results[$key][] = $row;
1586 $results[$key] = $row;
1590 if (DB::isError($row)) {
1594 // return scalar values
1595 while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
1597 $results[$row[0]][] = $row[1];
1599 $results[$row[0]] = $row[1];
1602 if (DB::isError($row)) {
1616 * Fetches all of the rows from a query result
1618 * @param string $query the SQL query
1619 * @param mixed $params array, string or numeric data to be used in
1620 * execution of the statement. Quantity of
1621 * items passed must match quantity of
1622 * placeholders in query: meaning 1
1623 * placeholder for non-array parameters or
1624 * 1 placeholder per array element.
1625 * @param int $fetchmode the fetch mode to use:
1626 * + DB_FETCHMODE_ORDERED
1627 * + DB_FETCHMODE_ASSOC
1628 * + DB_FETCHMODE_ORDERED | DB_FETCHMODE_FLIPPED
1629 * + DB_FETCHMODE_ASSOC | DB_FETCHMODE_FLIPPED
1631 * @return array the nested array. A DB_Error object on failure.
1633 function &getAll($query, $params = array(),
1634 $fetchmode = DB_FETCHMODE_DEFAULT)
1636 // compat check, the params and fetchmode parameters used to
1637 // have the opposite order
1638 if (!is_array($params)) {
1639 if (is_array($fetchmode)) {
1640 if ($params === null) {
1641 $tmp = DB_FETCHMODE_DEFAULT;
1645 $params = $fetchmode;
1647 } elseif ($params !== null) {
1648 $fetchmode = $params;
1653 if (sizeof($params) > 0) {
1654 $sth = $this->prepare($query);
1656 if (DB::isError($sth)) {
1660 $res = $this->execute($sth, $params);
1661 $this->freePrepared($sth);
1663 $res = $this->query($query);
1666 if ($res === DB_OK || DB::isError($res)) {
1671 while (DB_OK === $res->fetchInto($row, $fetchmode)) {
1672 if ($fetchmode & DB_FETCHMODE_FLIPPED) {
1673 foreach ($row as $key => $val) {
1674 $results[$key][] = $val;
1683 if (DB::isError($row)) {
1684 $tmp = $this->raiseError($row);
1694 * Enables or disables automatic commits
1696 * @param bool $onoff true turns it on, false turns it off
1698 * @return int DB_OK on success. A DB_Error object if the driver
1699 * doesn't support auto-committing transactions.
1701 function autoCommit($onoff = false)
1703 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1710 * Commits the current transaction
1712 * @return int DB_OK on success. A DB_Error object on failure.
1716 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1723 * Reverts the current transaction
1725 * @return int DB_OK on success. A DB_Error object on failure.
1729 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1736 * Determines the number of rows in a query result
1738 * @param resource $result the query result idenifier produced by PHP
1740 * @return int the number of rows. A DB_Error object on failure.
1742 function numRows($result)
1744 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1748 // {{{ affectedRows()
1751 * Determines the number of rows affected by a data maniuplation query
1753 * 0 is returned for queries that don't manipulate data.
1755 * @return int the number of rows. A DB_Error object on failure.
1757 function affectedRows()
1759 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1763 // {{{ getSequenceName()
1766 * Generates the name used inside the database for a sequence
1768 * The createSequence() docblock contains notes about storing sequence
1771 * @param string $sqn the sequence's public name
1773 * @return string the sequence's name in the backend
1776 * @see DB_common::createSequence(), DB_common::dropSequence(),
1777 * DB_common::nextID(), DB_common::setOption()
1779 function getSequenceName($sqn)
1781 return sprintf($this->getOption('seqname_format'),
1782 preg_replace('/[^a-z0-9_.]/i', '_', $sqn));
1789 * Returns the next free id in a sequence
1791 * @param string $seq_name name of the sequence
1792 * @param boolean $ondemand when true, the seqence is automatically
1793 * created if it does not exist
1795 * @return int the next id number in the sequence.
1796 * A DB_Error object on failure.
1798 * @see DB_common::createSequence(), DB_common::dropSequence(),
1799 * DB_common::getSequenceName()
1801 function nextId($seq_name, $ondemand = true)
1803 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1807 // {{{ createSequence()
1810 * Creates a new sequence
1812 * The name of a given sequence is determined by passing the string
1813 * provided in the <var>$seq_name</var> argument through PHP's sprintf()
1814 * function using the value from the <var>seqname_format</var> option as
1815 * the sprintf()'s format argument.
1817 * <var>seqname_format</var> is set via setOption().
1819 * @param string $seq_name name of the new sequence
1821 * @return int DB_OK on success. A DB_Error object on failure.
1823 * @see DB_common::dropSequence(), DB_common::getSequenceName(),
1824 * DB_common::nextID()
1826 function createSequence($seq_name)
1828 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1832 // {{{ dropSequence()
1835 * Deletes a sequence
1837 * @param string $seq_name name of the sequence to be deleted
1839 * @return int DB_OK on success. A DB_Error object on failure.
1841 * @see DB_common::createSequence(), DB_common::getSequenceName(),
1842 * DB_common::nextID()
1844 function dropSequence($seq_name)
1846 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1853 * Communicates an error and invoke error callbacks, etc
1855 * Basically a wrapper for PEAR::raiseError without the message string.
1857 * @param mixed integer error code, or a PEAR error object (all
1858 * other parameters are ignored if this parameter is
1860 * @param int error mode, see PEAR_Error docs
1861 * @param mixed if error mode is PEAR_ERROR_TRIGGER, this is the
1862 * error level (E_USER_NOTICE etc). If error mode is
1863 * PEAR_ERROR_CALLBACK, this is the callback function,
1864 * either as a function name, or as an array of an
1865 * object and method name. For other error modes this
1866 * parameter is ignored.
1867 * @param string extra debug information. Defaults to the last
1868 * query and native error code.
1869 * @param mixed native error code, integer or string depending the
1871 * @param mixed dummy parameter for E_STRICT compatibility with
1873 * @param mixed dummy parameter for E_STRICT compatibility with
1876 * @return object the PEAR_Error object
1880 function &raiseError($code = DB_ERROR, $mode = null, $options = null,
1881 $userinfo = null, $nativecode = null, $dummy1 = null,
1884 // The error is yet a DB error object
1885 if (is_object($code)) {
1886 // because we the static PEAR::raiseError, our global
1887 // handler should be used if it is set
1888 if ($mode === null && !empty($this->_default_error_mode)) {
1889 $mode = $this->_default_error_mode;
1890 $options = $this->_default_error_options;
1892 $tmp = PEAR::raiseError($code, null, $mode, $options,
1897 if ($userinfo === null) {
1898 $userinfo = $this->last_query;
1902 $userinfo .= ' [nativecode=' . trim($nativecode) . ']';
1904 $userinfo .= ' [DB Error: ' . DB::errorMessage($code) . ']';
1907 $tmp = PEAR::raiseError(null, $code, $mode, $options, $userinfo,
1913 // {{{ errorNative()
1916 * Gets the DBMS' native error code produced by the last query
1918 * @return mixed the DBMS' error code. A DB_Error object on failure.
1920 function errorNative()
1922 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1929 * Maps native error codes to DB's portable ones
1931 * Uses the <var>$errorcode_map</var> property defined in each driver.
1933 * @param string|int $nativecode the error code returned by the DBMS
1935 * @return int the portable DB error code. Return DB_ERROR if the
1936 * current driver doesn't have a mapping for the
1937 * $nativecode submitted.
1939 function errorCode($nativecode)
1941 if (isset($this->errorcode_map[$nativecode])) {
1942 return $this->errorcode_map[$nativecode];
1944 // Fall back to DB_ERROR if there was no mapping.
1949 // {{{ errorMessage()
1952 * Maps a DB error code to a textual message
1954 * @param integer $dbcode the DB error code
1956 * @return string the error message corresponding to the error code
1957 * submitted. FALSE if the error code is unknown.
1959 * @see DB::errorMessage()
1961 function errorMessage($dbcode)
1963 return DB::errorMessage($this->errorcode_map[$dbcode]);
1970 * Returns information about a table or a result set
1972 * The format of the resulting array depends on which <var>$mode</var>
1973 * you select. The sample output below is based on this query:
1975 * SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
1977 * JOIN tblBar ON tblFoo.fldId = tblBar.fldId
1983 * <kbd>null</kbd> (default)
1990 * [flags] => primary_key not_null
1994 * [name] => fldPhone
2004 * [flags] => primary_key not_null
2010 * <kbd>DB_TABLEINFO_ORDER</kbd>
2012 * <p>In addition to the information found in the default output,
2013 * a notation of the number of columns is provided by the
2014 * <samp>num_fields</samp> element while the <samp>order</samp>
2015 * element provides an array with the column names as the keys and
2016 * their location index number (corresponding to the keys in the
2017 * the default output) as the values.</p>
2019 * <p>If a result set has identical field names, the last one is
2024 * [order] => Array (
2032 * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>
2034 * <p>Similar to <kbd>DB_TABLEINFO_ORDER</kbd> but adds more
2035 * dimensions to the array in which the table names are keys and
2036 * the field names are sub-keys. This is helpful for queries that
2037 * join tables which have identical field names.</p>
2041 * [ordertable] => Array (
2042 * [tblFoo] => Array (
2046 * [tblBar] => Array (
2055 * The <samp>flags</samp> element contains a space separated list
2056 * of extra information about the field. This data is inconsistent
2057 * between DBMS's due to the way each DBMS works.
2058 * + <samp>primary_key</samp>
2059 * + <samp>unique_key</samp>
2060 * + <samp>multiple_key</samp>
2061 * + <samp>not_null</samp>
2063 * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
2064 * elements if <var>$result</var> is a table name. The following DBMS's
2065 * provide full information from queries:
2069 * If the 'portability' option has <samp>DB_PORTABILITY_LOWERCASE</samp>
2070 * turned on, the names of tables and fields will be lowercased.
2072 * @param object|string $result DB_result object from a query or a
2073 * string containing the name of a table.
2074 * While this also accepts a query result
2075 * resource identifier, this behavior is
2077 * @param int $mode either unused or one of the tableInfo modes:
2078 * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>,
2079 * <kbd>DB_TABLEINFO_ORDER</kbd> or
2080 * <kbd>DB_TABLEINFO_FULL</kbd> (which does both).
2081 * These are bitwise, so the first two can be
2082 * combined using <kbd>|</kbd>.
2084 * @return array an associative array with the information requested.
2085 * A DB_Error object on failure.
2087 * @see DB_common::setOption()
2089 function tableInfo($result, $mode = null)
2092 * If the DB_<driver> class has a tableInfo() method, that one
2093 * overrides this one. But, if the driver doesn't have one,
2094 * this method runs and tells users about that fact.
2096 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
2103 * Lists the tables in the current database
2105 * @return array the list of tables. A DB_Error object on failure.
2107 * @deprecated Method deprecated some time before Release 1.2
2109 function getTables()
2111 return $this->getListOf('tables');
2118 * Lists internal database information
2120 * @param string $type type of information being sought.
2121 * Common items being sought are:
2122 * tables, databases, users, views, functions
2123 * Each DBMS's has its own capabilities.
2125 * @return array an array listing the items sought.
2126 * A DB DB_Error object on failure.
2128 function getListOf($type)
2130 $sql = $this->getSpecialQuery($type);
2131 if ($sql === null) {
2132 $this->last_query = '';
2133 return $this->raiseError(DB_ERROR_UNSUPPORTED);
2134 } elseif (is_int($sql) || DB::isError($sql)) {
2136 return $this->raiseError($sql);
2137 } elseif (is_array($sql)) {
2138 // Already the result
2141 // Launch this query
2142 return $this->getCol($sql);
2146 // {{{ getSpecialQuery()
2149 * Obtains the query string needed for listing a given type of objects
2151 * @param string $type the kind of objects you want to retrieve
2153 * @return string the SQL query string or null if the driver doesn't
2154 * support the object type requested
2157 * @see DB_common::getListOf()
2159 function getSpecialQuery($type)
2161 return $this->raiseError(DB_ERROR_UNSUPPORTED);
2165 // {{{ nextQueryIsManip()
2168 * Sets (or unsets) a flag indicating that the next query will be a
2169 * manipulation query, regardless of the usual DB::isManip() heuristics.
2171 * @param boolean true to set the flag overriding the isManip() behaviour,
2172 * false to clear it and fall back onto isManip()
2178 function nextQueryIsManip($manip)
2180 $this->_next_query_manip = $manip;
2184 // {{{ _checkManip()
2187 * Checks if the given query is a manipulation query. This also takes into
2188 * account the _next_query_manip flag and sets the _last_query_manip flag
2189 * (and resets _next_query_manip) according to the result.
2191 * @param string The query to check.
2193 * @return boolean true if the query is a manipulation query, false
2198 function _checkManip($query)
2200 if ($this->_next_query_manip || DB::isManip($query)) {
2201 $this->_last_query_manip = true;
2203 $this->_last_query_manip = false;
2205 $this->_next_query_manip = false;
2206 return $this->_last_query_manip;
2207 $manip = $this->_next_query_manip;
2211 // {{{ _rtrimArrayValues()
2214 * Right-trims all strings in an array
2216 * @param array $array the array to be trimmed (passed by reference)
2222 function _rtrimArrayValues(&$array)
2224 foreach ($array as $key => $value) {
2225 if (is_string($value)) {
2226 $array[$key] = rtrim($value);
2232 // {{{ _convertNullArrayValuesToEmpty()
2235 * Converts all null values in an array to empty strings
2237 * @param array $array the array to be de-nullified (passed by reference)
2243 function _convertNullArrayValuesToEmpty(&$array)
2245 foreach ($array as $key => $value) {
2246 if (is_null($value)) {