Support for larger size codes (such as SNOMED US Extension codes)
[openemr.git] / gacl / adodb / adodb-pear.inc.php
blob5d167086134be3b43266e76c02ed8791d1df6df8
1 <?php
2 /**
3 * @version V4.92a 29 Aug 2006 (c) 2000-2006 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 getOne
32 getAssoc
33 getRow
34 getCol
35 getAll
37 DB_Result
38 ---------
39 numRows - returns -1 if not supported
40 numCols
41 fetchInto - does not support passing of fetchmode
42 fetchRows - does not support passing of fetchmode
43 free
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')) {
52 define("DB_OK", 1);
53 define("DB_ERROR",-1);
55 // autoExecute constants
56 define('DB_AUTOQUERY_INSERT', 1);
57 define('DB_AUTOQUERY_UPDATE', 2);
59 /**
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);
66 /**
67 * Column data indexed by numbers, ordered from 0 and up
70 define('DB_FETCHMODE_ORDERED', 1);
72 /**
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);
83 /**
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);
94 /**
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.
101 class 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
109 * error
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);
117 return $obj;
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
131 * options.
133 * @return object a newly created DB connection object, or a DB
134 * error object on error
136 * @see DB::parseDSN
137 * @see DB::isError
139 function &connect($dsn, $options = false)
141 if (is_array($dsn)) {
142 $dsninfo = $dsn;
143 } else {
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");
156 } else {
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);
163 return $obj;
165 if (is_array($options)) {
166 foreach($options as $k => $v) {
167 switch(strtolower($k)) {
168 case 'persist':
169 case 'persistent': $persist = $v; break;
170 #ibase
171 case 'dialect': $obj->dialect = $v; break;
172 case 'charset': $obj->charset = $v; break;
173 case 'buffers': $obj->buffers = $v; break;
174 #ado
175 case 'charpage': $obj->charPage = $v; break;
176 #mysql
177 case 'clientflags': $obj->clientFlags = $v; break;
180 } else {
181 $persist = false;
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();
191 return $obj;
195 * Return the DB API version
197 * @return int the DB API version number
199 function apiVersion()
201 return 2;
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,
223 * and are not fatal.
225 * @param $value mixed result value
227 * @return bool whether $value is a warning
229 function isWarning($value)
231 return false;
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
264 * phptype://hostspec
265 * phptype(dbsyntax)
266 * phptype
268 * @author Tomas V.V.Cox <cox@idecnet.com>
270 function parseDSN($dsn)
272 if (is_array($dsn)) {
273 return $dsn;
276 $parsed = array(
277 'phptype' => false,
278 'dbsyntax' => false,
279 'protocol' => false,
280 'hostspec' => false,
281 'database' => false,
282 'username' => false,
283 'password' => false
286 // Find phptype and dbsyntax
287 if (($pos = strpos($dsn, '://')) !== false) {
288 $str = substr($dsn, 0, $pos);
289 $dsn = substr($dsn, $pos + 3);
290 } else {
291 $str = $dsn;
292 $dsn = NULL;
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];
300 } else {
301 $parsed['phptype'] = $str;
302 $parsed['dbsyntax'] = $str;
305 if (empty($dsn)) {
306 return $parsed;
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));
317 } else {
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);
327 } else {
328 $str = $dsn;
329 $dsn = NULL;
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));
337 } else {
338 $parsed['hostspec'] = urldecode($str);
341 // Get dabase if any
342 // $dsn => database
343 if (!empty($dsn)) {
344 $parsed['database'] = $dsn;
347 return $parsed;
351 * Load a PHP database extension if it is not loaded already.
353 * @access public
355 * @param $name the base name of the extension (without the .so or
356 * .dll suffix)
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';
365 @dl($name . $dlext);
367 if (!extension_loaded($name)) {
368 return false;
370 return true;