1 | <?php |
---|
2 | /** |
---|
3 | * @version V4.50 6 July 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. |
---|
7 | * |
---|
8 | * Set tabs to 4 for best viewing. |
---|
9 | * |
---|
10 | * PEAR DB Emulation Layer for ADODB. |
---|
11 | * |
---|
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. |
---|
14 | */ |
---|
15 | |
---|
16 | /* |
---|
17 | We support: |
---|
18 | |
---|
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 |
---|
30 | |
---|
31 | getOne |
---|
32 | getAssoc |
---|
33 | getRow |
---|
34 | getCol |
---|
35 | getAll |
---|
36 | |
---|
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 |
---|
44 | */ |
---|
45 | |
---|
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"; |
---|
50 | |
---|
51 | if (!defined('DB_OK')) { |
---|
52 | define("DB_OK", 1); |
---|
53 | define("DB_ERROR",-1); |
---|
54 | /** |
---|
55 | * This is a special constant that tells DB the user hasn't specified |
---|
56 | * any particular get mode, so the default should be used. |
---|
57 | */ |
---|
58 | |
---|
59 | define('DB_FETCHMODE_DEFAULT', 0); |
---|
60 | |
---|
61 | /** |
---|
62 | * Column data indexed by numbers, ordered from 0 and up |
---|
63 | */ |
---|
64 | |
---|
65 | define('DB_FETCHMODE_ORDERED', 1); |
---|
66 | |
---|
67 | /** |
---|
68 | * Column data indexed by column names |
---|
69 | */ |
---|
70 | |
---|
71 | define('DB_FETCHMODE_ASSOC', 2); |
---|
72 | |
---|
73 | /* for compatibility */ |
---|
74 | |
---|
75 | define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED); |
---|
76 | define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC); |
---|
77 | |
---|
78 | /** |
---|
79 | * these are constants for the tableInfo-function |
---|
80 | * they are bitwised or'ed. so if there are more constants to be defined |
---|
81 | * in the future, adjust DB_TABLEINFO_FULL accordingly |
---|
82 | */ |
---|
83 | |
---|
84 | define('DB_TABLEINFO_ORDER', 1); |
---|
85 | define('DB_TABLEINFO_ORDERTABLE', 2); |
---|
86 | define('DB_TABLEINFO_FULL', 3); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * The main "DB" class is simply a container class with some static |
---|
91 | * methods for creating DB objects as well as some utility functions |
---|
92 | * common to all parts of DB. |
---|
93 | * |
---|
94 | */ |
---|
95 | |
---|
96 | class DB |
---|
97 | { |
---|
98 | /** |
---|
99 | * Create a new DB object for the specified database type |
---|
100 | * |
---|
101 | * @param $type string database type, for example "mysql" |
---|
102 | * |
---|
103 | * @return object a newly created DB object, or a DB error code on |
---|
104 | * error |
---|
105 | */ |
---|
106 | |
---|
107 | function &factory($type) |
---|
108 | { |
---|
109 | include_once(ADODB_DIR."/drivers/adodb-$type.inc.php"); |
---|
110 | $obj = &NewADOConnection($type); |
---|
111 | if (!is_object($obj)) $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1); |
---|
112 | return $obj; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Create a new DB object and connect to the specified database |
---|
117 | * |
---|
118 | * @param $dsn mixed "data source name", see the DB::parseDSN |
---|
119 | * method for a description of the dsn format. Can also be |
---|
120 | * specified as an array of the format returned by DB::parseDSN. |
---|
121 | * |
---|
122 | * @param $options mixed if boolean (or scalar), tells whether |
---|
123 | * this connection should be persistent (for backends that support |
---|
124 | * this). This parameter can also be an array of options, see |
---|
125 | * DB_common::setOption for more information on connection |
---|
126 | * options. |
---|
127 | * |
---|
128 | * @return object a newly created DB connection object, or a DB |
---|
129 | * error object on error |
---|
130 | * |
---|
131 | * @see DB::parseDSN |
---|
132 | * @see DB::isError |
---|
133 | */ |
---|
134 | function &connect($dsn, $options = false) |
---|
135 | { |
---|
136 | if (is_array($dsn)) { |
---|
137 | $dsninfo = $dsn; |
---|
138 | } else { |
---|
139 | $dsninfo = DB::parseDSN($dsn); |
---|
140 | } |
---|
141 | switch ($dsninfo["phptype"]) { |
---|
142 | case 'pgsql': $type = 'postgres7'; break; |
---|
143 | case 'ifx': $type = 'informix9'; break; |
---|
144 | default: $type = $dsninfo["phptype"]; break; |
---|
145 | } |
---|
146 | |
---|
147 | if (is_array($options) && isset($options["debug"]) && |
---|
148 | $options["debug"] >= 2) { |
---|
149 | // expose php errors with sufficient debug level |
---|
150 | @include_once("adodb-$type.inc.php"); |
---|
151 | } else { |
---|
152 | @include_once("adodb-$type.inc.php"); |
---|
153 | } |
---|
154 | |
---|
155 | @$obj =& NewADOConnection($type); |
---|
156 | if (!is_object($obj)) { |
---|
157 | $obj =& new PEAR_Error('Unknown Database Driver: '.$dsninfo['phptype'],-1); |
---|
158 | return $obj; |
---|
159 | } |
---|
160 | if (is_array($options)) { |
---|
161 | foreach($options as $k => $v) { |
---|
162 | switch(strtolower($k)) { |
---|
163 | case 'persistent': $persist = $v; break; |
---|
164 | #ibase |
---|
165 | case 'dialect': $obj->dialect = $v; break; |
---|
166 | case 'charset': $obj->charset = $v; break; |
---|
167 | case 'buffers': $obj->buffers = $v; break; |
---|
168 | #ado |
---|
169 | case 'charpage': $obj->charPage = $v; break; |
---|
170 | #mysql |
---|
171 | case 'clientflags': $obj->clientFlags = $v; break; |
---|
172 | } |
---|
173 | } |
---|
174 | } else { |
---|
175 | $persist = false; |
---|
176 | } |
---|
177 | |
---|
178 | if (isset($dsninfo['socket'])) $dsninfo['hostspec'] .= ':'.$dsninfo['socket']; |
---|
179 | else if (isset($dsninfo['port'])) $dsninfo['hostspec'] .= ':'.$dsninfo['port']; |
---|
180 | |
---|
181 | if($persist) $ok = $obj->PConnect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']); |
---|
182 | else $ok = $obj->Connect($dsninfo['hostspec'], $dsninfo['username'],$dsninfo['password'],$dsninfo['database']); |
---|
183 | |
---|
184 | if (!$ok) $obj = ADODB_PEAR_Error(); |
---|
185 | return $obj; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Return the DB API version |
---|
190 | * |
---|
191 | * @return int the DB API version number |
---|
192 | */ |
---|
193 | function apiVersion() |
---|
194 | { |
---|
195 | return 2; |
---|
196 | } |
---|
197 | |
---|
198 | /** |
---|
199 | * Tell whether a result code from a DB method is an error |
---|
200 | * |
---|
201 | * @param $value int result code |
---|
202 | * |
---|
203 | * @return bool whether $value is an error |
---|
204 | */ |
---|
205 | function isError($value) |
---|
206 | { |
---|
207 | return (is_object($value) && |
---|
208 | (get_class($value) == 'db_error' || |
---|
209 | is_subclass_of($value, 'db_error'))); |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | /** |
---|
214 | * Tell whether a result code from a DB method is a warning. |
---|
215 | * Warnings differ from errors in that they are generated by DB, |
---|
216 | * and are not fatal. |
---|
217 | * |
---|
218 | * @param $value mixed result value |
---|
219 | * |
---|
220 | * @return bool whether $value is a warning |
---|
221 | */ |
---|
222 | function isWarning($value) |
---|
223 | { |
---|
224 | return is_object($value) && |
---|
225 | (get_class( $value ) == "db_warning" || |
---|
226 | is_subclass_of($value, "db_warning")); |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Parse a data source name |
---|
231 | * |
---|
232 | * @param $dsn string Data Source Name to be parsed |
---|
233 | * |
---|
234 | * @return array an associative array with the following keys: |
---|
235 | * |
---|
236 | * phptype: Database backend used in PHP (mysql, odbc etc.) |
---|
237 | * dbsyntax: Database used with regards to SQL syntax etc. |
---|
238 | * protocol: Communication protocol to use (tcp, unix etc.) |
---|
239 | * hostspec: Host specification (hostname[:port]) |
---|
240 | * database: Database to use on the DBMS server |
---|
241 | * username: User name for login |
---|
242 | * password: Password for login |
---|
243 | * |
---|
244 | * The format of the supplied DSN is in its fullest form: |
---|
245 | * |
---|
246 | * phptype(dbsyntax)://username:password@protocol+hostspec/database |
---|
247 | * |
---|
248 | * Most variations are allowed: |
---|
249 | * |
---|
250 | * phptype://username:password@protocol+hostspec:110//usr/db_file.db |
---|
251 | * phptype://username:password@hostspec/database_name |
---|
252 | * phptype://username:password@hostspec |
---|
253 | * phptype://username@hostspec |
---|
254 | * phptype://hostspec/database |
---|
255 | * phptype://hostspec |
---|
256 | * phptype(dbsyntax) |
---|
257 | * phptype |
---|
258 | * |
---|
259 | * @author Tomas V.V.Cox <cox@idecnet.com> |
---|
260 | */ |
---|
261 | function parseDSN($dsn) |
---|
262 | { |
---|
263 | if (is_array($dsn)) { |
---|
264 | return $dsn; |
---|
265 | } |
---|
266 | |
---|
267 | $parsed = array( |
---|
268 | 'phptype' => false, |
---|
269 | 'dbsyntax' => false, |
---|
270 | 'protocol' => false, |
---|
271 | 'hostspec' => false, |
---|
272 | 'database' => false, |
---|
273 | 'username' => false, |
---|
274 | 'password' => false |
---|
275 | ); |
---|
276 | |
---|
277 | // Find phptype and dbsyntax |
---|
278 | if (($pos = strpos($dsn, '://')) !== false) { |
---|
279 | $str = substr($dsn, 0, $pos); |
---|
280 | $dsn = substr($dsn, $pos + 3); |
---|
281 | } else { |
---|
282 | $str = $dsn; |
---|
283 | $dsn = NULL; |
---|
284 | } |
---|
285 | |
---|
286 | // Get phptype and dbsyntax |
---|
287 | // $str => phptype(dbsyntax) |
---|
288 | if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) { |
---|
289 | $parsed['phptype'] = $arr[1]; |
---|
290 | $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2]; |
---|
291 | } else { |
---|
292 | $parsed['phptype'] = $str; |
---|
293 | $parsed['dbsyntax'] = $str; |
---|
294 | } |
---|
295 | |
---|
296 | if (empty($dsn)) { |
---|
297 | return $parsed; |
---|
298 | } |
---|
299 | |
---|
300 | // Get (if found): username and password |
---|
301 | // $dsn => username:password@protocol+hostspec/database |
---|
302 | if (($at = strpos($dsn,'@')) !== false) { |
---|
303 | $str = substr($dsn, 0, $at); |
---|
304 | $dsn = substr($dsn, $at + 1); |
---|
305 | if (($pos = strpos($str, ':')) !== false) { |
---|
306 | $parsed['username'] = urldecode(substr($str, 0, $pos)); |
---|
307 | $parsed['password'] = urldecode(substr($str, $pos + 1)); |
---|
308 | } else { |
---|
309 | $parsed['username'] = urldecode($str); |
---|
310 | } |
---|
311 | } |
---|
312 | |
---|
313 | // Find protocol and hostspec |
---|
314 | // $dsn => protocol+hostspec/database |
---|
315 | if (($pos=strpos($dsn, '/')) !== false) { |
---|
316 | $str = substr($dsn, 0, $pos); |
---|
317 | $dsn = substr($dsn, $pos + 1); |
---|
318 | } else { |
---|
319 | $str = $dsn; |
---|
320 | $dsn = NULL; |
---|
321 | } |
---|
322 | |
---|
323 | // Get protocol + hostspec |
---|
324 | // $str => protocol+hostspec |
---|
325 | if (($pos=strpos($str, '+')) !== false) { |
---|
326 | $parsed['protocol'] = substr($str, 0, $pos); |
---|
327 | $parsed['hostspec'] = urldecode(substr($str, $pos + 1)); |
---|
328 | } else { |
---|
329 | $parsed['hostspec'] = urldecode($str); |
---|
330 | } |
---|
331 | |
---|
332 | // Get dabase if any |
---|
333 | // $dsn => database |
---|
334 | if (!empty($dsn)) { |
---|
335 | $parsed['database'] = $dsn; |
---|
336 | } |
---|
337 | |
---|
338 | return $parsed; |
---|
339 | } |
---|
340 | |
---|
341 | /** |
---|
342 | * Load a PHP database extension if it is not loaded already. |
---|
343 | * |
---|
344 | * @access public |
---|
345 | * |
---|
346 | * @param $name the base name of the extension (without the .so or |
---|
347 | * .dll suffix) |
---|
348 | * |
---|
349 | * @return bool true if the extension was already or successfully |
---|
350 | * loaded, false if it could not be loaded |
---|
351 | */ |
---|
352 | function assertExtension($name) |
---|
353 | { |
---|
354 | if (!extension_loaded($name)) { |
---|
355 | $dlext = (strncmp(PHP_OS,'WIN',3) === 0) ? '.dll' : '.so'; |
---|
356 | @dl($name . $dlext); |
---|
357 | } |
---|
358 | if (!extension_loaded($name)) { |
---|
359 | return false; |
---|
360 | } |
---|
361 | return true; |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | ?> |
---|