3 * @version V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
4 * Released under both BSD license and Lesser GPL library license.
5 * Whenever there is any discrepancy between the two licenses,
6 * the BSD license will take precedence.
8 * Set tabs to 4 for best viewing.
10 * PEAR DB Emulation Layer for ADODB.
12 * The following code is modelled on PEAR DB code by Stig Bakken <ssb@fast.no> |
13 * and Tomas V.V.Cox <cox@idecnet.com>. Portions (c)1997-2002 The PHP Group.
21 query - returns PEAR_Error on error
22 limitQuery - return PEAR_Error on error
23 prepare - does not return PEAR_Error on error
24 execute - does not return PEAR_Error on error
25 setFetchMode - supports ASSOC and ORDERED
39 numRows - returns -1 if not supported
41 fetchInto - does not support passing of fetchmode
42 fetchRows - does not support passing of fetchmode
46 define('ADODB_PEAR',dirname(__FILE__
));
47 include_once "PEAR.php";
48 include_once ADODB_PEAR
."/adodb-errorpear.inc.php";
49 include_once ADODB_PEAR
."/adodb.inc.php";
51 if (!defined('DB_OK')) {
53 define("DB_ERROR",-1);
55 // autoExecute constants
56 define('DB_AUTOQUERY_INSERT', 1);
57 define('DB_AUTOQUERY_UPDATE', 2);
60 * This is a special constant that tells DB the user hasn't specified
61 * any particular get mode, so the default should be used.
64 define('DB_FETCHMODE_DEFAULT', 0);
67 * Column data indexed by numbers, ordered from 0 and up
70 define('DB_FETCHMODE_ORDERED', 1);
73 * Column data indexed by column names
76 define('DB_FETCHMODE_ASSOC', 2);
78 /* for compatibility */
80 define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED
);
81 define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC
);
84 * these are constants for the tableInfo-function
85 * they are bitwised or'ed. so if there are more constants to be defined
86 * in the future, adjust DB_TABLEINFO_FULL accordingly
89 define('DB_TABLEINFO_ORDER', 1);
90 define('DB_TABLEINFO_ORDERTABLE', 2);
91 define('DB_TABLEINFO_FULL', 3);
95 * The main "DB" class is simply a container class with some static
96 * methods for creating DB objects as well as some utility functions
97 * common to all parts of DB.
104 * Create a new DB object for the specified database type
106 * @param $type string database type, for example "mysql"
108 * @return object a newly created DB object, or a DB error code on
112 function &factory($type)
114 include_once(ADODB_DIR
."/drivers/adodb-$type.inc.php");
115 $obj = &NewADOConnection($type);
116 if (!is_object($obj)) $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1);
121 * Create a new DB object and connect to the specified database
123 * @param $dsn mixed "data source name", see the DB::parseDSN
124 * method for a description of the dsn format. Can also be
125 * specified as an array of the format returned by DB::parseDSN.
127 * @param $options mixed if boolean (or scalar), tells whether
128 * this connection should be persistent (for backends that support
129 * this). This parameter can also be an array of options, see
130 * DB_common::setOption for more information on connection
133 * @return object a newly created DB connection object, or a DB
134 * error object on error
139 function &connect($dsn, $options = false)
141 if (is_array($dsn)) {
144 $dsninfo = DB
::parseDSN($dsn);
146 switch ($dsninfo["phptype"]) {
147 case 'pgsql': $type = 'postgres7'; break;
148 case 'ifx': $type = 'informix9'; break;
149 default: $type = $dsninfo["phptype"]; break;
152 if (is_array($options) && isset($options["debug"]) &&
153 $options["debug"] >= 2) {
154 // expose php errors with sufficient debug level
155 @include_once
("adodb-$type.inc.php");
157 @include_once
("adodb-$type.inc.php");
160 @$obj =& NewADOConnection($type);
161 if (!is_object($obj)) {
162 $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1);
165 if (is_array($options)) {
166 foreach($options as $k => $v) {
167 switch(strtolower($k)) {
169 case 'persistent': $persist = $v; break;
171 case 'dialect': $obj->dialect
= $v; break;
172 case 'charset': $obj->charset
= $v; break;
173 case 'buffers': $obj->buffers
= $v; break;
175 case 'charpage': $obj->charPage
= $v; break;
177 case 'clientflags': $obj->clientFlags
= $v; break;
184 if (isset($dsninfo['socket'])) $dsninfo['hostspec'] .= ':'.$dsninfo['socket'];
185 else if (isset($dsninfo['port'])) $dsninfo['hostspec'] .= ':'.$dsninfo['port'];
187 if($persist) $ok = $obj->PConnect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']);
188 else $ok = $obj->Connect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']);
190 if (!$ok) $obj = ADODB_PEAR_Error();
195 * Return the DB API version
197 * @return int the DB API version number
199 function apiVersion()
205 * Tell whether a result code from a DB method is an error
207 * @param $value int result code
209 * @return bool whether $value is an error
211 function isError($value)
213 if (!is_object($value)) return false;
214 $class = get_class($value);
215 return $class == 'pear_error' ||
is_subclass_of($value, 'pear_error') ||
216 $class == 'db_error' ||
is_subclass_of($value, 'db_error');
221 * Tell whether a result code from a DB method is a warning.
222 * Warnings differ from errors in that they are generated by DB,
225 * @param $value mixed result value
227 * @return bool whether $value is a warning
229 function isWarning($value)
233 return is_object($value) &&
234 (get_class( $value ) == "db_warning" ||
235 is_subclass_of($value, "db_warning"));*/
239 * Parse a data source name
241 * @param $dsn string Data Source Name to be parsed
243 * @return array an associative array with the following keys:
245 * phptype: Database backend used in PHP (mysql, odbc etc.)
246 * dbsyntax: Database used with regards to SQL syntax etc.
247 * protocol: Communication protocol to use (tcp, unix etc.)
248 * hostspec: Host specification (hostname[:port])
249 * database: Database to use on the DBMS server
250 * username: User name for login
251 * password: Password for login
253 * The format of the supplied DSN is in its fullest form:
255 * phptype(dbsyntax)://username:password@protocol+hostspec/database
257 * Most variations are allowed:
259 * phptype://username:password@protocol+hostspec:110//usr/db_file.db
260 * phptype://username:password@hostspec/database_name
261 * phptype://username:password@hostspec
262 * phptype://username@hostspec
263 * phptype://hostspec/database
268 * @author Tomas V.V.Cox <cox@idecnet.com>
270 function parseDSN($dsn)
272 if (is_array($dsn)) {
286 // Find phptype and dbsyntax
287 if (($pos = strpos($dsn, '://')) !== false) {
288 $str = substr($dsn, 0, $pos);
289 $dsn = substr($dsn, $pos +
3);
295 // Get phptype and dbsyntax
296 // $str => phptype(dbsyntax)
297 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
298 $parsed['phptype'] = $arr[1];
299 $parsed['dbsyntax'] = (empty($arr[2])) ?
$arr[1] : $arr[2];
301 $parsed['phptype'] = $str;
302 $parsed['dbsyntax'] = $str;
309 // Get (if found): username and password
310 // $dsn => username:password@protocol+hostspec/database
311 if (($at = strpos($dsn,'@')) !== false) {
312 $str = substr($dsn, 0, $at);
313 $dsn = substr($dsn, $at +
1);
314 if (($pos = strpos($str, ':')) !== false) {
315 $parsed['username'] = urldecode(substr($str, 0, $pos));
316 $parsed['password'] = urldecode(substr($str, $pos +
1));
318 $parsed['username'] = urldecode($str);
322 // Find protocol and hostspec
323 // $dsn => protocol+hostspec/database
324 if (($pos=strpos($dsn, '/')) !== false) {
325 $str = substr($dsn, 0, $pos);
326 $dsn = substr($dsn, $pos +
1);
332 // Get protocol + hostspec
333 // $str => protocol+hostspec
334 if (($pos=strpos($str, '+')) !== false) {
335 $parsed['protocol'] = substr($str, 0, $pos);
336 $parsed['hostspec'] = urldecode(substr($str, $pos +
1));
338 $parsed['hostspec'] = urldecode($str);
344 $parsed['database'] = $dsn;
351 * Load a PHP database extension if it is not loaded already.
355 * @param $name the base name of the extension (without the .so or
358 * @return bool true if the extension was already or successfully
359 * loaded, false if it could not be loaded
361 function assertExtension($name)
363 if (!extension_loaded($name)) {
364 $dlext = (strncmp(PHP_OS
,'WIN',3) === 0) ?
'.dll' : '.so';
367 if (!extension_loaded($name)) {