2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 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 // | Authors: Stig Bakken <ssb@php.net> |
17 // | Tomas V.V.Cox <cox@idecnet.com> |
18 // +----------------------------------------------------------------------+
21 // Based on DB 1.3 from the pear.php.net repository.
22 // The only modifications made have been modification of the include paths,
23 // plus the inclusion depricated DB_Warning class.
25 rcs_id('$Id: DB.php,v 1.3 2004-02-04 17:04:18 rurban Exp $');
26 rcs_id('From Pear CVS: Id: DB.php,v 1.20 2003/05/07 16:54:45 mj Exp');
28 // Database independent query interface.
31 require_once "lib/pear/PEAR.php";
36 * The method mapErrorCode in each DB_dbtype implementation maps
37 * native error codes to one of these.
39 * If you add an error code here, make sure you also add a textual
40 * version of it in DB::errorMessage().
44 define("DB_ERROR", -1);
45 define("DB_ERROR_SYNTAX", -2);
46 define("DB_ERROR_CONSTRAINT", -3);
47 define("DB_ERROR_NOT_FOUND", -4);
48 define("DB_ERROR_ALREADY_EXISTS", -5);
49 define("DB_ERROR_UNSUPPORTED", -6);
50 define("DB_ERROR_MISMATCH", -7);
51 define("DB_ERROR_INVALID", -8);
52 define("DB_ERROR_NOT_CAPABLE", -9);
53 define("DB_ERROR_TRUNCATED", -10);
54 define("DB_ERROR_INVALID_NUMBER", -11);
55 define("DB_ERROR_INVALID_DATE", -12);
56 define("DB_ERROR_DIVZERO", -13);
57 define("DB_ERROR_NODBSELECTED", -14);
58 define("DB_ERROR_CANNOT_CREATE", -15);
59 define("DB_ERROR_CANNOT_DELETE", -16);
60 define("DB_ERROR_CANNOT_DROP", -17);
61 define("DB_ERROR_NOSUCHTABLE", -18);
62 define("DB_ERROR_NOSUCHFIELD", -19);
63 define("DB_ERROR_NEED_MORE_DATA", -20);
64 define("DB_ERROR_NOT_LOCKED", -21);
65 define("DB_ERROR_VALUE_COUNT_ON_ROW", -22);
66 define("DB_ERROR_INVALID_DSN", -23);
67 define("DB_ERROR_CONNECT_FAILED", -24);
68 define("DB_ERROR_EXTENSION_NOT_FOUND",-25);
69 define("DB_ERROR_ACCESS_VIOLATION", -26);
70 define("DB_ERROR_NOSUCHDB", -27);
75 * Warnings are not detected as errors by DB::isError(), and are not
76 * fatal. You can detect whether an error is in fact a warning with
82 define('DB_WARNING', -1000);
83 define('DB_WARNING_READ_ONLY', -1001);
86 // {{{ prepared statement-related
88 * These constants are used when storing information about prepared
89 * statements (using the "prepare" method in DB_dbtype).
91 * The prepare/execute model in DB is mostly borrowed from the ODBC
92 * extension, in a query the "?" character means a scalar parameter.
93 * There are two extensions though, a "&" character means an opaque
94 * parameter. An opaque parameter is simply a file name, the real
95 * data are in that file (useful for putting uploaded files into your
96 * database and such). The "!" char means a parameter that must be
98 * They modify the quote behavoir:
99 * DB_PARAM_SCALAR (?) => 'original string quoted'
100 * DB_PARAM_OPAQUE (&) => 'string from file quoted'
101 * DB_PARAM_MISC (!) => original string
104 define('DB_PARAM_SCALAR', 1);
105 define('DB_PARAM_OPAQUE', 2);
106 define('DB_PARAM_MISC', 3);
109 // {{{ binary data-related
111 * These constants define different ways of returning binary data
112 * from queries. Again, this model has been borrowed from the ODBC
115 * DB_BINMODE_PASSTHRU sends the data directly through to the browser
116 * when data is fetched from the database.
117 * DB_BINMODE_RETURN lets you return data as usual.
118 * DB_BINMODE_CONVERT returns data as well, only it is converted to
119 * hex format, for example the string "123" would become "313233".
122 define('DB_BINMODE_PASSTHRU', 1);
123 define('DB_BINMODE_RETURN', 2);
124 define('DB_BINMODE_CONVERT', 3);
129 * This is a special constant that tells DB the user hasn't specified
130 * any particular get mode, so the default should be used.
133 define('DB_FETCHMODE_DEFAULT', 0);
136 * Column data indexed by numbers, ordered from 0 and up
139 define('DB_FETCHMODE_ORDERED', 1);
142 * Column data indexed by column names
145 define('DB_FETCHMODE_ASSOC', 2);
148 * Column data as object properties
151 define('DB_FETCHMODE_OBJECT', 3);
154 * For multi-dimensional results: normally the first level of arrays
155 * is the row number, and the second level indexed by column number or name.
156 * DB_FETCHMODE_FLIPPED switches this order, so the first level of arrays
157 * is the column name, and the second level the row number.
160 define('DB_FETCHMODE_FLIPPED', 4);
162 /* for compatibility */
164 define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
165 define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC);
166 define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
169 // {{{ tableInfo() && autoPrepare()-related
171 * these are constants for the tableInfo-function
172 * they are bitwised or'ed. so if there are more constants to be defined
173 * in the future, adjust DB_TABLEINFO_FULL accordingly
176 define('DB_TABLEINFO_ORDER', 1);
177 define('DB_TABLEINFO_ORDERTABLE', 2);
178 define('DB_TABLEINFO_FULL', 3);
181 * Used by autoPrepare()
183 define('DB_AUTOQUERY_INSERT', 1);
184 define('DB_AUTOQUERY_UPDATE', 2);
191 * The main "DB" class is simply a container class with some static
192 * methods for creating DB objects as well as some utility functions
193 * common to all parts of DB.
195 * The object model of DB is as follows (indentation means inheritance):
197 * DB The main DB class. This is simply a utility class
198 * with some "static" methods for creating DB objects as
199 * well as common utility functions for other DB classes.
201 * DB_common The base for each DB implementation. Provides default
202 * | implementations (in OO lingo virtual methods) for
203 * | the actual DB implementations as well as a bunch of
204 * | query utility functions.
206 * +-DB_mysql The DB implementation for MySQL. Inherits DB_common.
207 * When calling DB::factory or DB::connect for MySQL
208 * connections, the object returned is an instance of this
212 * @author Stig Bakken <ssb@php.net>
220 * Create a new DB connection object for the specified database
223 * @param string $type database type, for example "mysql"
225 * @return mixed a newly created DB object, or a DB error code on
231 function &factory($type)
233 @include_once("lib/pear/DB/${type}.php");
235 $classname = "DB_${type}";
237 if (!class_exists($classname)) {
238 return PEAR::raiseError(null, DB_ERROR_NOT_FOUND,
239 null, null, null, 'DB_Error', true);
242 @$obj =& new $classname;
250 * Create a new DB connection object and connect to the specified
253 * @param mixed $dsn "data source name", see the DB::parseDSN
254 * method for a description of the dsn format. Can also be
255 * specified as an array of the format returned by DB::parseDSN.
257 * @param mixed $options An associative array of option names and
258 * their values. For backwards compatibility, this parameter may
259 * also be a boolean that tells whether the connection should be
260 * persistent. See DB_common::setOption for more information on
261 * connection options.
263 * @return mixed a newly created DB connection object, or a DB
264 * error object on error
268 * @see DB_common::setOption
270 function &connect($dsn, $options = false)
272 if (is_array($dsn)) {
275 $dsninfo = DB::parseDSN($dsn);
277 $type = $dsninfo["phptype"];
279 if (is_array($options) && isset($options["debug"]) &&
280 $options["debug"] >= 2) {
281 // expose php errors with sufficient debug level
282 include_once "lib/pear/DB/${type}.php";
284 @include_once "lib/pear/DB/${type}.php";
287 $classname = "DB_${type}";
288 if (!class_exists($classname)) {
289 return PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
290 "Unable to include the DB/{$type}.php file for `$dsn'",
294 @$obj =& new $classname;
296 if (is_array($options)) {
297 foreach ($options as $option => $value) {
298 $test = $obj->setOption($option, $value);
299 if (DB::isError($test)) {
304 $obj->setOption('persistent', $options);
306 $err = $obj->connect($dsninfo, $obj->getOption('persistent'));
307 if (DB::isError($err)) {
308 $err->addUserInfo($dsn);
318 * Return the DB API version
320 * @return int the DB API version number
324 function apiVersion()
332 * Tell whether a result code from a DB method is an error
334 * @param int $value result code
336 * @return bool whether $value is an error
340 function isError($value)
342 return (is_object($value) &&
343 (get_class($value) == 'db_error' ||
344 is_subclass_of($value, 'db_error')));
348 // {{{ isConnection()
350 * Tell whether a value is a DB connection
352 * @param mixed $value value to test
354 * @return bool whether $value is a DB connection
358 function isConnection($value)
360 return (is_object($value) &&
361 is_subclass_of($value, 'db_common') &&
362 method_exists($value, 'simpleQuery'));
368 * Tell whether a query is a data manipulation query (insert,
369 * update or delete) or a data definition query (create, drop,
370 * alter, grant, revoke).
374 * @param string $query the query
376 * @return boolean whether $query is a data manipulation query
378 function isManip($query)
380 $manips = 'INSERT|UPDATE|DELETE|'.'REPLACE|CREATE|DROP|'.
381 'ALTER|GRANT|REVOKE|'.'LOCK|UNLOCK';
382 if (preg_match('/^\s*"?('.$manips.')\s+/i', $query)) {
389 // {{{ errorMessage()
391 * Return a textual error message for a DB error code
393 * @param integer $value error code
395 * @return string error message, or false if the error code was
398 function errorMessage($value)
400 static $errorMessages;
401 if (!isset($errorMessages)) {
402 $errorMessages = array(
403 DB_ERROR => 'unknown error',
404 DB_ERROR_ALREADY_EXISTS => 'already exists',
405 DB_ERROR_CANNOT_CREATE => 'can not create',
406 DB_ERROR_CANNOT_DELETE => 'can not delete',
407 DB_ERROR_CANNOT_DROP => 'can not drop',
408 DB_ERROR_CONSTRAINT => 'constraint violation',
409 DB_ERROR_DIVZERO => 'division by zero',
410 DB_ERROR_INVALID => 'invalid',
411 DB_ERROR_INVALID_DATE => 'invalid date or time',
412 DB_ERROR_INVALID_NUMBER => 'invalid number',
413 DB_ERROR_MISMATCH => 'mismatch',
414 DB_ERROR_NODBSELECTED => 'no database selected',
415 DB_ERROR_NOSUCHFIELD => 'no such field',
416 DB_ERROR_NOSUCHTABLE => 'no such table',
417 DB_ERROR_NOT_CAPABLE => 'DB backend not capable',
418 DB_ERROR_NOT_FOUND => 'not found',
419 DB_ERROR_NOT_LOCKED => 'not locked',
420 DB_ERROR_SYNTAX => 'syntax error',
421 DB_ERROR_UNSUPPORTED => 'not supported',
422 DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
423 DB_ERROR_INVALID_DSN => 'invalid DSN',
424 DB_ERROR_CONNECT_FAILED => 'connect failed',
426 DB_WARNING => 'unknown warning',
427 DB_WARNING_READ_ONLY => 'read only',
428 DB_ERROR_NEED_MORE_DATA => 'insufficient data supplied',
429 DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
430 DB_ERROR_NOSUCHDB => 'no such database',
431 DB_ERROR_ACCESS_VIOLATION => 'insufficient permissions',
432 DB_ERROR_TRUNCATED => 'truncated'
436 if (DB::isError($value)) {
437 $value = $value->getCode();
440 return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[DB_ERROR];
446 * Parse a data source name
448 * A array with the following keys will be returned:
449 * phptype: Database backend used in PHP (mysql, odbc etc.)
450 * dbsyntax: Database used with regards to SQL syntax etc.
451 * protocol: Communication protocol to use (tcp, unix etc.)
452 * hostspec: Host specification (hostname[:port])
453 * database: Database to use on the DBMS server
454 * username: User name for login
455 * password: Password for login
457 * The format of the supplied DSN is in its fullest form:
459 * phptype(dbsyntax)://username:password@protocol+hostspec/database
461 * Most variations are allowed:
463 * phptype://username:password@protocol+hostspec:110//usr/db_file.db
464 * phptype://username:password@hostspec/database_name
465 * phptype://username:password@hostspec
466 * phptype://username@hostspec
467 * phptype://hostspec/database
472 * @param string $dsn Data Source Name to be parsed
474 * @return array an associative array
476 * @author Tomas V.V.Cox <cox@idecnet.com>
478 function parseDSN($dsn)
480 if (is_array($dsn)) {
496 // Find phptype and dbsyntax
497 if (($pos = strpos($dsn, '://')) !== false) {
498 $str = substr($dsn, 0, $pos);
499 $dsn = substr($dsn, $pos + 3);
505 // Get phptype and dbsyntax
506 // $str => phptype(dbsyntax)
507 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
508 $parsed['phptype'] = $arr[1];
509 $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
511 $parsed['phptype'] = $str;
512 $parsed['dbsyntax'] = $str;
519 // Get (if found): username and password
520 // $dsn => username:password@protocol+hostspec/database
521 if (($at = strrpos($dsn,'@')) !== false) {
522 $str = substr($dsn, 0, $at);
523 $dsn = substr($dsn, $at + 1);
524 if (($pos = strpos($str, ':')) !== false) {
525 $parsed['username'] = rawurldecode(substr($str, 0, $pos));
526 $parsed['password'] = rawurldecode(substr($str, $pos + 1));
528 $parsed['username'] = rawurldecode($str);
532 // Find protocol and hostspec
534 // $dsn => proto(proto_opts)/database
535 if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
537 $proto_opts = (!empty($match[2])) ? $match[2] : false;
540 // $dsn => protocol+hostspec/database (old format)
542 if (strpos($dsn, '+') !== false) {
543 list($proto, $dsn) = explode('+', $dsn, 2);
545 if (strpos($dsn, '/') !== false) {
546 list($proto_opts, $dsn) = explode('/', $dsn, 2);
553 // process the different protocol options
554 $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
555 $proto_opts = rawurldecode($proto_opts);
556 if ($parsed['protocol'] == 'tcp') {
557 if (strpos($proto_opts, ':') !== false) {
558 list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
560 $parsed['hostspec'] = $proto_opts;
562 } elseif ($parsed['protocol'] == 'unix') {
563 $parsed['socket'] = $proto_opts;
570 if (($pos = strpos($dsn, '?')) === false) {
571 $parsed['database'] = $dsn;
572 // /database?param1=value1¶m2=value2
574 $parsed['database'] = substr($dsn, 0, $pos);
575 $dsn = substr($dsn, $pos + 1);
576 if (strpos($dsn, '&') !== false) {
577 $opts = explode('&', $dsn);
578 } else { // database?param1=value1
581 foreach ($opts as $opt) {
582 list($key, $value) = explode('=', $opt);
583 if (!isset($parsed[$key])) { // don't allow params overwrite
584 $parsed[$key] = rawurldecode($value);
594 // {{{ assertExtension()
596 * Load a PHP database extension if it is not loaded already.
600 * @param string $name the base name of the extension (without the .so or
603 * @return boolean true if the extension was already or successfully
604 * loaded, false if it could not be loaded
606 function assertExtension($name)
608 if (!extension_loaded($name)) {
609 $dlext = OS_WINDOWS ? '.dll' : '.so';
610 $dlprefix = OS_WINDOWS ? 'php_' : '';
611 @dl($dlprefix . $name . $dlext);
612 return extension_loaded($name);
620 // {{{ class DB_Error
622 * DB_Error implements a class for reporting portable database error
626 * @author Stig Bakken <ssb@php.net>
628 class DB_Error extends PEAR_Error
632 * DB_Error constructor.
634 * @param mixed $code DB error code, or string with error message.
635 * @param integer $mode what "error mode" to operate in
636 * @param integer $level what error level to use for $mode & PEAR_ERROR_TRIGGER
637 * @param mixed $debuginfo additional debug info, such as the last query
644 function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
645 $level = E_USER_NOTICE, $debuginfo = null)
648 $this->PEAR_Error('DB Error: ' . DB::errorMessage($code), $code, $mode, $level, $debuginfo);
650 $this->PEAR_Error("DB Error: $code", DB_ERROR, $mode, $level, $debuginfo);
658 * DB_Warning implements a class for reporting portable database
662 * @author Stig Bakken <ssb@fast.no>
665 class DB_Warning extends PEAR_Error
668 * DB_Warning constructor.
670 * @param mixed $code DB error code, or string with error message.
671 * @param integer $mode what "error mode" to operate in
672 * @param integer $level what error level to use for $mode == PEAR_ERROR_TRIGGER
673 * @param mmixed $debuginfo additional debug info, such as the last query
680 function DB_Warning($code = DB_WARNING, $mode = PEAR_ERROR_RETURN,
681 $level = E_USER_NOTICE, $debuginfo = null)
684 $this->PEAR_Error('DB Warning: ' . DB::errorMessage($code), $code, $mode, $level, $debuginfo);
686 $this->PEAR_Error("DB Warning: $code", 0, $mode, $level, $debuginfo);
692 * This class implements a wrapper for a DB result set.
693 * A new instance of this class will be returned by the DB implementation
694 * after processing a query that returns data.
697 * @author Stig Bakken <ssb@php.net>
706 var $row_counter = null;
708 * for limit queries, the row to start fetching
711 var $limit_from = null;
714 * for limit queries, the number of rows to fetch
717 var $limit_count = null;
722 * DB_result constructor.
723 * @param resource &$dbh DB object reference
724 * @param resource $result result resource id
727 function DB_result(&$dbh, $result, $options = array())
730 $this->result = $result;
731 $this->limit_from = isset($options['limit_from']) ? $options['limit_from'] : null;
732 $this->limit_count = isset($options['limit_count']) ? $options['limit_count'] : null;
733 $this->limit_type = $dbh->features['limit'];
734 $this->autofree = $dbh->options['autofree'];
735 $this->fetchmode = $dbh->fetchmode;
736 $this->fetchmode_object_class = $dbh->fetchmode_object_class;
742 * Fetch and return a row of data (it uses driver->fetchInto for that)
743 * @param int $fetchmode format of fetched row
744 * @param int $rownum the row number to fetch
746 * @return array a row of data, NULL on no more rows or PEAR_Error on error
750 function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
752 if ($fetchmode === DB_FETCHMODE_DEFAULT) {
753 $fetchmode = $this->fetchmode;
755 if ($fetchmode === DB_FETCHMODE_OBJECT) {
756 $fetchmode = DB_FETCHMODE_ASSOC;
757 $object_class = $this->fetchmode_object_class;
759 if ($this->limit_from !== null) {
760 if ($this->row_counter === null) {
761 $this->row_counter = $this->limit_from;
763 if ($this->limit_type == false) {
765 while ($i++ < $this->limit_from) {
766 $this->dbh->fetchInto($this->result, $arr, $fetchmode);
770 if ($this->row_counter >= (
771 $this->limit_from + $this->limit_count))
775 if ($this->limit_type == 'emulate') {
776 $rownum = $this->row_counter;
779 $this->row_counter++;
781 $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
782 if ($res !== DB_OK) {
783 if ($res == null && $this->autofree) {
788 if (isset($object_class)) {
789 // default mode specified in DB_common::fetchmode_object_class property
790 if ($object_class == 'stdClass') {
791 $ret = (object) $arr;
793 $ret =& new $object_class($arr);
803 * Fetch a row of data into an existing variable.
805 * @param mixed &$arr reference to data containing the row
806 * @param integer $fetchmod format of fetched row
807 * @param integer $rownum the row number to fetch
809 * @return mixed DB_OK on success, NULL on no more rows or
810 * a DB_Error object on error
814 function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null)
816 if ($fetchmode === DB_FETCHMODE_DEFAULT) {
817 $fetchmode = $this->fetchmode;
819 if ($fetchmode === DB_FETCHMODE_OBJECT) {
820 $fetchmode = DB_FETCHMODE_ASSOC;
821 $object_class = $this->fetchmode_object_class;
823 if ($this->limit_from !== null) {
824 if ($this->row_counter === null) {
825 $this->row_counter = $this->limit_from;
827 if ($this->limit_type == false) {
829 while ($i++ < $this->limit_from) {
830 $this->dbh->fetchInto($this->result, $arr, $fetchmode);
834 if ($this->row_counter >= (
835 $this->limit_from + $this->limit_count))
839 if ($this->limit_type == 'emulate') {
840 $rownum = $this->row_counter;
843 $this->row_counter++;
845 $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
846 if (($res === DB_OK) && isset($object_class)) {
847 // default mode specified in DB_common::fetchmode_object_class property
848 if ($object_class == 'stdClass') {
849 $arr = (object) $arr;
851 $arr = new $object_class($arr);
853 } elseif ($res == null && $this->autofree) {
862 * Get the the number of columns in a result set.
864 * @return int the number of columns, or a DB error
870 return $this->dbh->numCols($this->result);
876 * Get the number of rows in a result set.
878 * @return int the number of rows, or a DB error
884 return $this->dbh->numRows($this->result);
890 * Get the next result if a batch of queries was executed.
892 * @return bool true if a new result is available or false if not.
896 function nextResult()
898 return $this->dbh->nextResult($this->result);
904 * Frees the resources allocated for this result set.
905 * @return int error code
911 $err = $this->dbh->freeResult($this->result);
912 if(DB::isError($err)) {
915 $this->result = false;
924 function tableInfo($mode = null)
926 return $this->dbh->tableInfo($this->result, $mode);
930 // {{{ getRowCounter()
932 * returns the actual row number
935 function getRowCounter()
937 return $this->row_counter;
946 * @see DB_common::setFetchMode()
954 * @param resource row data as array
956 function DB_row(&$arr)
958 for (reset($arr); $key = key($arr); next($arr)) {
959 $this->$key = &$arr[$key];