Added new information to this screen.
[openemr.git] / library / adodb / adodb-pear.inc.php
blob3894730469cab7d1c0a3755b0d7bc7614e824ac6
1 <?php
2 /**
3 * @version V4.20 22 Feb 2004 (c) 2000-2004 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.
9 *
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.
17 We support:
19 DB_Common
20 ---------
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
26 errorNative
27 quote
28 nextID
29 disconnect
31 DB_Result
32 ---------
33 numRows - returns -1 if not supported
34 numCols
35 fetchInto - does not support passing of fetchmode
36 fetchRows - does not support passing of fetchmode
37 free
40 define('ADODB_PEAR',dirname(__FILE__));
41 include_once "PEAR.php";
42 include_once ADODB_PEAR."/adodb-errorpear.inc.php";
43 include_once ADODB_PEAR."/adodb.inc.php";
45 if (!defined('DB_OK')) {
46 define("DB_OK", 1);
47 define("DB_ERROR",-1);
48 /**
49 * This is a special constant that tells DB the user hasn't specified
50 * any particular get mode, so the default should be used.
53 define('DB_FETCHMODE_DEFAULT', 0);
55 /**
56 * Column data indexed by numbers, ordered from 0 and up
59 define('DB_FETCHMODE_ORDERED', 1);
61 /**
62 * Column data indexed by column names
65 define('DB_FETCHMODE_ASSOC', 2);
67 /* for compatibility */
69 define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
70 define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC);
72 /**
73 * these are constants for the tableInfo-function
74 * they are bitwised or'ed. so if there are more constants to be defined
75 * in the future, adjust DB_TABLEINFO_FULL accordingly
78 define('DB_TABLEINFO_ORDER', 1);
79 define('DB_TABLEINFO_ORDERTABLE', 2);
80 define('DB_TABLEINFO_FULL', 3);
83 /**
84 * The main "DB" class is simply a container class with some static
85 * methods for creating DB objects as well as some utility functions
86 * common to all parts of DB.
90 class DB
92 /**
93 * Create a new DB object for the specified database type
95 * @param $type string database type, for example "mysql"
97 * @return object a newly created DB object, or a DB error code on
98 * error
101 function &factory($type)
103 include_once(ADODB_DIR."/drivers/adodb-$type.inc.php");
104 $obj = &NewADOConnection($type);
105 if (!is_object($obj)) $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1);
106 return $obj;
110 * Create a new DB object and connect to the specified database
112 * @param $dsn mixed "data source name", see the DB::parseDSN
113 * method for a description of the dsn format. Can also be
114 * specified as an array of the format returned by DB::parseDSN.
116 * @param $options mixed if boolean (or scalar), tells whether
117 * this connection should be persistent (for backends that support
118 * this). This parameter can also be an array of options, see
119 * DB_common::setOption for more information on connection
120 * options.
122 * @return object a newly created DB connection object, or a DB
123 * error object on error
125 * @see DB::parseDSN
126 * @see DB::isError
128 function &connect($dsn, $options = false)
130 if (is_array($dsn)) {
131 $dsninfo = $dsn;
132 } else {
133 $dsninfo = DB::parseDSN($dsn);
135 switch ($dsninfo["phptype"]) {
136 case 'pgsql': $type = 'postgres7'; break;
137 case 'ifx': $type = 'informix9'; break;
138 default: $type = $dsninfo["phptype"]; break;
141 if (is_array($options) && isset($options["debug"]) &&
142 $options["debug"] >= 2) {
143 // expose php errors with sufficient debug level
144 @include_once("adodb-$type.inc.php");
145 } else {
146 @include_once("adodb-$type.inc.php");
149 @$obj =& NewADOConnection($type);
150 if (!is_object($obj)) {
151 $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1);
152 return $obj;
154 if (is_array($options)) {
155 foreach($options as $k => $v) {
156 switch(strtolower($k)) {
157 case 'persistent': $persist = $v; break;
158 #ibase
159 case 'dialect': $obj->dialect = $v; break;
160 case 'charset': $obj->charset = $v; break;
161 case 'buffers': $obj->buffers = $v; break;
162 #ado
163 case 'charpage': $obj->charPage = $v; break;
164 #mysql
165 case 'clientflags': $obj->clientFlags = $v; break;
168 } else {
169 $persist = false;
172 if (isset($dsninfo['socket'])) $dsninfo['hostspec'] .= ':'.$dsninfo['socket'];
173 else if (isset($dsninfo['port'])) $dsninfo['hostspec'] .= ':'.$dsninfo['port'];
175 if($persist) $ok = $obj->PConnect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']);
176 else $ok = $obj->Connect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']);
178 if (!$ok) $obj = ADODB_PEAR_Error();
179 return $obj;
183 * Return the DB API version
185 * @return int the DB API version number
187 function apiVersion()
189 return 2;
193 * Tell whether a result code from a DB method is an error
195 * @param $value int result code
197 * @return bool whether $value is an error
199 function isError($value)
201 return (is_object($value) &&
202 (get_class($value) == 'db_error' ||
203 is_subclass_of($value, 'db_error')));
208 * Tell whether a result code from a DB method is a warning.
209 * Warnings differ from errors in that they are generated by DB,
210 * and are not fatal.
212 * @param $value mixed result value
214 * @return bool whether $value is a warning
216 function isWarning($value)
218 return is_object($value) &&
219 (get_class( $value ) == "db_warning" ||
220 is_subclass_of($value, "db_warning"));
224 * Parse a data source name
226 * @param $dsn string Data Source Name to be parsed
228 * @return array an associative array with the following keys:
230 * phptype: Database backend used in PHP (mysql, odbc etc.)
231 * dbsyntax: Database used with regards to SQL syntax etc.
232 * protocol: Communication protocol to use (tcp, unix etc.)
233 * hostspec: Host specification (hostname[:port])
234 * database: Database to use on the DBMS server
235 * username: User name for login
236 * password: Password for login
238 * The format of the supplied DSN is in its fullest form:
240 * phptype(dbsyntax)://username:password@protocol+hostspec/database
242 * Most variations are allowed:
244 * phptype://username:password@protocol+hostspec:110//usr/db_file.db
245 * phptype://username:password@hostspec/database_name
246 * phptype://username:password@hostspec
247 * phptype://username@hostspec
248 * phptype://hostspec/database
249 * phptype://hostspec
250 * phptype(dbsyntax)
251 * phptype
253 * @author Tomas V.V.Cox <cox@idecnet.com>
255 function parseDSN($dsn)
257 if (is_array($dsn)) {
258 return $dsn;
261 $parsed = array(
262 'phptype' => false,
263 'dbsyntax' => false,
264 'protocol' => false,
265 'hostspec' => false,
266 'database' => false,
267 'username' => false,
268 'password' => false
271 // Find phptype and dbsyntax
272 if (($pos = strpos($dsn, '://')) !== false) {
273 $str = substr($dsn, 0, $pos);
274 $dsn = substr($dsn, $pos + 3);
275 } else {
276 $str = $dsn;
277 $dsn = NULL;
280 // Get phptype and dbsyntax
281 // $str => phptype(dbsyntax)
282 if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
283 $parsed['phptype'] = $arr[1];
284 $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
285 } else {
286 $parsed['phptype'] = $str;
287 $parsed['dbsyntax'] = $str;
290 if (empty($dsn)) {
291 return $parsed;
294 // Get (if found): username and password
295 // $dsn => username:password@protocol+hostspec/database
296 if (($at = strpos($dsn,'@')) !== false) {
297 $str = substr($dsn, 0, $at);
298 $dsn = substr($dsn, $at + 1);
299 if (($pos = strpos($str, ':')) !== false) {
300 $parsed['username'] = urldecode(substr($str, 0, $pos));
301 $parsed['password'] = urldecode(substr($str, $pos + 1));
302 } else {
303 $parsed['username'] = urldecode($str);
307 // Find protocol and hostspec
308 // $dsn => protocol+hostspec/database
309 if (($pos=strpos($dsn, '/')) !== false) {
310 $str = substr($dsn, 0, $pos);
311 $dsn = substr($dsn, $pos + 1);
312 } else {
313 $str = $dsn;
314 $dsn = NULL;
317 // Get protocol + hostspec
318 // $str => protocol+hostspec
319 if (($pos=strpos($str, '+')) !== false) {
320 $parsed['protocol'] = substr($str, 0, $pos);
321 $parsed['hostspec'] = urldecode(substr($str, $pos + 1));
322 } else {
323 $parsed['hostspec'] = urldecode($str);
326 // Get dabase if any
327 // $dsn => database
328 if (!empty($dsn)) {
329 $parsed['database'] = $dsn;
332 return $parsed;
336 * Load a PHP database extension if it is not loaded already.
338 * @access public
340 * @param $name the base name of the extension (without the .so or
341 * .dll suffix)
343 * @return bool true if the extension was already or successfully
344 * loaded, false if it could not be loaded
346 function assertExtension($name)
348 if (!extension_loaded($name)) {
349 $dlext = (strncmp(PHP_OS,'WIN',3) === 0) ? '.dll' : '.so';
350 @dl($name . $dlext);
352 if (!extension_loaded($name)) {
353 return false;
355 return true;