1 | <?php |
---|
2 | |
---|
3 | /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ |
---|
4 | |
---|
5 | /** |
---|
6 | * Database independent query interface |
---|
7 | * |
---|
8 | * PHP versions 4 and 5 |
---|
9 | * |
---|
10 | * LICENSE: This source file is subject to version 3.0 of the PHP license |
---|
11 | * that is available through the world-wide-web at the following URI: |
---|
12 | * http://www.php.net/license/3_0.txt. If you did not receive a copy of |
---|
13 | * the PHP License and are unable to obtain it through the web, please |
---|
14 | * send a note to license@php.net so we can mail you a copy immediately. |
---|
15 | * |
---|
16 | * @category Database |
---|
17 | * @package DB |
---|
18 | * @author Stig Bakken <ssb@php.net> |
---|
19 | * @author Tomas V.V.Cox <cox@idecnet.com> |
---|
20 | * @author Daniel Convissor <danielc@php.net> |
---|
21 | * @copyright 1997-2007 The PHP Group |
---|
22 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
23 | * @version CVS: $Id$ |
---|
24 | * @link http://pear.php.net/package/DB |
---|
25 | */ |
---|
26 | |
---|
27 | /** |
---|
28 | * Obtain the PEAR class so it can be extended from |
---|
29 | */ |
---|
30 | if(!defined('DB_PHP_DIR')) { |
---|
31 | $DB_PHP_DIR = realpath(dirname( __FILE__)); |
---|
32 | define("DB_PHP_DIR", $DB_PHP_DIR); |
---|
33 | } |
---|
34 | require_once DB_PHP_DIR . '/PEAR.php'; |
---|
35 | |
---|
36 | |
---|
37 | // {{{ constants |
---|
38 | // {{{ error codes |
---|
39 | |
---|
40 | /**#@+ |
---|
41 | * One of PEAR DB's portable error codes. |
---|
42 | * @see DB_common::errorCode(), DB::errorMessage() |
---|
43 | * |
---|
44 | * {@internal If you add an error code here, make sure you also add a textual |
---|
45 | * version of it in DB::errorMessage().}} |
---|
46 | */ |
---|
47 | |
---|
48 | /** |
---|
49 | * The code returned by many methods upon success |
---|
50 | */ |
---|
51 | define('DB_OK', 1); |
---|
52 | |
---|
53 | /** |
---|
54 | * Unkown error |
---|
55 | */ |
---|
56 | define('DB_ERROR', -1); |
---|
57 | |
---|
58 | /** |
---|
59 | * Syntax error |
---|
60 | */ |
---|
61 | define('DB_ERROR_SYNTAX', -2); |
---|
62 | |
---|
63 | /** |
---|
64 | * Tried to insert a duplicate value into a primary or unique index |
---|
65 | */ |
---|
66 | define('DB_ERROR_CONSTRAINT', -3); |
---|
67 | |
---|
68 | /** |
---|
69 | * An identifier in the query refers to a non-existant object |
---|
70 | */ |
---|
71 | define('DB_ERROR_NOT_FOUND', -4); |
---|
72 | |
---|
73 | /** |
---|
74 | * Tried to create a duplicate object |
---|
75 | */ |
---|
76 | define('DB_ERROR_ALREADY_EXISTS', -5); |
---|
77 | |
---|
78 | /** |
---|
79 | * The current driver does not support the action you attempted |
---|
80 | */ |
---|
81 | define('DB_ERROR_UNSUPPORTED', -6); |
---|
82 | |
---|
83 | /** |
---|
84 | * The number of parameters does not match the number of placeholders |
---|
85 | */ |
---|
86 | define('DB_ERROR_MISMATCH', -7); |
---|
87 | |
---|
88 | /** |
---|
89 | * A literal submitted did not match the data type expected |
---|
90 | */ |
---|
91 | define('DB_ERROR_INVALID', -8); |
---|
92 | |
---|
93 | /** |
---|
94 | * The current DBMS does not support the action you attempted |
---|
95 | */ |
---|
96 | define('DB_ERROR_NOT_CAPABLE', -9); |
---|
97 | |
---|
98 | /** |
---|
99 | * A literal submitted was too long so the end of it was removed |
---|
100 | */ |
---|
101 | define('DB_ERROR_TRUNCATED', -10); |
---|
102 | |
---|
103 | /** |
---|
104 | * A literal number submitted did not match the data type expected |
---|
105 | */ |
---|
106 | define('DB_ERROR_INVALID_NUMBER', -11); |
---|
107 | |
---|
108 | /** |
---|
109 | * A literal date submitted did not match the data type expected |
---|
110 | */ |
---|
111 | define('DB_ERROR_INVALID_DATE', -12); |
---|
112 | |
---|
113 | /** |
---|
114 | * Attempt to divide something by zero |
---|
115 | */ |
---|
116 | define('DB_ERROR_DIVZERO', -13); |
---|
117 | |
---|
118 | /** |
---|
119 | * A database needs to be selected |
---|
120 | */ |
---|
121 | define('DB_ERROR_NODBSELECTED', -14); |
---|
122 | |
---|
123 | /** |
---|
124 | * Could not create the object requested |
---|
125 | */ |
---|
126 | define('DB_ERROR_CANNOT_CREATE', -15); |
---|
127 | |
---|
128 | /** |
---|
129 | * Could not drop the database requested because it does not exist |
---|
130 | */ |
---|
131 | define('DB_ERROR_CANNOT_DROP', -17); |
---|
132 | |
---|
133 | /** |
---|
134 | * An identifier in the query refers to a non-existant table |
---|
135 | */ |
---|
136 | define('DB_ERROR_NOSUCHTABLE', -18); |
---|
137 | |
---|
138 | /** |
---|
139 | * An identifier in the query refers to a non-existant column |
---|
140 | */ |
---|
141 | define('DB_ERROR_NOSUCHFIELD', -19); |
---|
142 | |
---|
143 | /** |
---|
144 | * The data submitted to the method was inappropriate |
---|
145 | */ |
---|
146 | define('DB_ERROR_NEED_MORE_DATA', -20); |
---|
147 | |
---|
148 | /** |
---|
149 | * The attempt to lock the table failed |
---|
150 | */ |
---|
151 | define('DB_ERROR_NOT_LOCKED', -21); |
---|
152 | |
---|
153 | /** |
---|
154 | * The number of columns doesn't match the number of values |
---|
155 | */ |
---|
156 | define('DB_ERROR_VALUE_COUNT_ON_ROW', -22); |
---|
157 | |
---|
158 | /** |
---|
159 | * The DSN submitted has problems |
---|
160 | */ |
---|
161 | define('DB_ERROR_INVALID_DSN', -23); |
---|
162 | |
---|
163 | /** |
---|
164 | * Could not connect to the database |
---|
165 | */ |
---|
166 | define('DB_ERROR_CONNECT_FAILED', -24); |
---|
167 | |
---|
168 | /** |
---|
169 | * The PHP extension needed for this DBMS could not be found |
---|
170 | */ |
---|
171 | define('DB_ERROR_EXTENSION_NOT_FOUND',-25); |
---|
172 | |
---|
173 | /** |
---|
174 | * The present user has inadequate permissions to perform the task requestd |
---|
175 | */ |
---|
176 | define('DB_ERROR_ACCESS_VIOLATION', -26); |
---|
177 | |
---|
178 | /** |
---|
179 | * The database requested does not exist |
---|
180 | */ |
---|
181 | define('DB_ERROR_NOSUCHDB', -27); |
---|
182 | |
---|
183 | /** |
---|
184 | * Tried to insert a null value into a column that doesn't allow nulls |
---|
185 | */ |
---|
186 | define('DB_ERROR_CONSTRAINT_NOT_NULL',-29); |
---|
187 | /**#@-*/ |
---|
188 | |
---|
189 | |
---|
190 | // }}} |
---|
191 | // {{{ prepared statement-related |
---|
192 | |
---|
193 | |
---|
194 | /**#@+ |
---|
195 | * Identifiers for the placeholders used in prepared statements. |
---|
196 | * @see DB_common::prepare() |
---|
197 | */ |
---|
198 | |
---|
199 | /** |
---|
200 | * Indicates a scalar (<kbd>?</kbd>) placeholder was used |
---|
201 | * |
---|
202 | * Quote and escape the value as necessary. |
---|
203 | */ |
---|
204 | define('DB_PARAM_SCALAR', 1); |
---|
205 | |
---|
206 | /** |
---|
207 | * Indicates an opaque (<kbd>&</kbd>) placeholder was used |
---|
208 | * |
---|
209 | * The value presented is a file name. Extract the contents of that file |
---|
210 | * and place them in this column. |
---|
211 | */ |
---|
212 | define('DB_PARAM_OPAQUE', 2); |
---|
213 | |
---|
214 | /** |
---|
215 | * Indicates a misc (<kbd>!</kbd>) placeholder was used |
---|
216 | * |
---|
217 | * The value should not be quoted or escaped. |
---|
218 | */ |
---|
219 | define('DB_PARAM_MISC', 3); |
---|
220 | /**#@-*/ |
---|
221 | |
---|
222 | |
---|
223 | // }}} |
---|
224 | // {{{ binary data-related |
---|
225 | |
---|
226 | |
---|
227 | /**#@+ |
---|
228 | * The different ways of returning binary data from queries. |
---|
229 | */ |
---|
230 | |
---|
231 | /** |
---|
232 | * Sends the fetched data straight through to output |
---|
233 | */ |
---|
234 | define('DB_BINMODE_PASSTHRU', 1); |
---|
235 | |
---|
236 | /** |
---|
237 | * Lets you return data as usual |
---|
238 | */ |
---|
239 | define('DB_BINMODE_RETURN', 2); |
---|
240 | |
---|
241 | /** |
---|
242 | * Converts the data to hex format before returning it |
---|
243 | * |
---|
244 | * For example the string "123" would become "313233". |
---|
245 | */ |
---|
246 | define('DB_BINMODE_CONVERT', 3); |
---|
247 | /**#@-*/ |
---|
248 | |
---|
249 | |
---|
250 | // }}} |
---|
251 | // {{{ fetch modes |
---|
252 | |
---|
253 | |
---|
254 | /**#@+ |
---|
255 | * Fetch Modes. |
---|
256 | * @see DB_common::setFetchMode() |
---|
257 | */ |
---|
258 | |
---|
259 | /** |
---|
260 | * Indicates the current default fetch mode should be used |
---|
261 | * @see DB_common::$fetchmode |
---|
262 | */ |
---|
263 | define('DB_FETCHMODE_DEFAULT', 0); |
---|
264 | |
---|
265 | /** |
---|
266 | * Column data indexed by numbers, ordered from 0 and up |
---|
267 | */ |
---|
268 | define('DB_FETCHMODE_ORDERED', 1); |
---|
269 | |
---|
270 | /** |
---|
271 | * Column data indexed by column names |
---|
272 | */ |
---|
273 | define('DB_FETCHMODE_ASSOC', 2); |
---|
274 | |
---|
275 | /** |
---|
276 | * Column data as object properties |
---|
277 | */ |
---|
278 | define('DB_FETCHMODE_OBJECT', 3); |
---|
279 | |
---|
280 | /** |
---|
281 | * For multi-dimensional results, make the column name the first level |
---|
282 | * of the array and put the row number in the second level of the array |
---|
283 | * |
---|
284 | * This is flipped from the normal behavior, which puts the row numbers |
---|
285 | * in the first level of the array and the column names in the second level. |
---|
286 | */ |
---|
287 | define('DB_FETCHMODE_FLIPPED', 4); |
---|
288 | /**#@-*/ |
---|
289 | |
---|
290 | /**#@+ |
---|
291 | * Old fetch modes. Left here for compatibility. |
---|
292 | */ |
---|
293 | define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED); |
---|
294 | define('DB_GETMODE_ASSOC', DB_FETCHMODE_ASSOC); |
---|
295 | define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED); |
---|
296 | /**#@-*/ |
---|
297 | |
---|
298 | |
---|
299 | // }}} |
---|
300 | // {{{ tableInfo() && autoPrepare()-related |
---|
301 | |
---|
302 | |
---|
303 | /**#@+ |
---|
304 | * The type of information to return from the tableInfo() method. |
---|
305 | * |
---|
306 | * Bitwised constants, so they can be combined using <kbd>|</kbd> |
---|
307 | * and removed using <kbd>^</kbd>. |
---|
308 | * |
---|
309 | * @see DB_common::tableInfo() |
---|
310 | * |
---|
311 | * {@internal Since the TABLEINFO constants are bitwised, if more of them are |
---|
312 | * added in the future, make sure to adjust DB_TABLEINFO_FULL accordingly.}} |
---|
313 | */ |
---|
314 | define('DB_TABLEINFO_ORDER', 1); |
---|
315 | define('DB_TABLEINFO_ORDERTABLE', 2); |
---|
316 | define('DB_TABLEINFO_FULL', 3); |
---|
317 | /**#@-*/ |
---|
318 | |
---|
319 | |
---|
320 | /**#@+ |
---|
321 | * The type of query to create with the automatic query building methods. |
---|
322 | * @see DB_common::autoPrepare(), DB_common::autoExecute() |
---|
323 | */ |
---|
324 | define('DB_AUTOQUERY_INSERT', 1); |
---|
325 | define('DB_AUTOQUERY_UPDATE', 2); |
---|
326 | /**#@-*/ |
---|
327 | |
---|
328 | |
---|
329 | // }}} |
---|
330 | // {{{ portability modes |
---|
331 | |
---|
332 | |
---|
333 | /**#@+ |
---|
334 | * Portability Modes. |
---|
335 | * |
---|
336 | * Bitwised constants, so they can be combined using <kbd>|</kbd> |
---|
337 | * and removed using <kbd>^</kbd>. |
---|
338 | * |
---|
339 | * @see DB_common::setOption() |
---|
340 | * |
---|
341 | * {@internal Since the PORTABILITY constants are bitwised, if more of them are |
---|
342 | * added in the future, make sure to adjust DB_PORTABILITY_ALL accordingly.}} |
---|
343 | */ |
---|
344 | |
---|
345 | /** |
---|
346 | * Turn off all portability features |
---|
347 | */ |
---|
348 | define('DB_PORTABILITY_NONE', 0); |
---|
349 | |
---|
350 | /** |
---|
351 | * Convert names of tables and fields to lower case |
---|
352 | * when using the get*(), fetch*() and tableInfo() methods |
---|
353 | */ |
---|
354 | define('DB_PORTABILITY_LOWERCASE', 1); |
---|
355 | |
---|
356 | /** |
---|
357 | * Right trim the data output by get*() and fetch*() |
---|
358 | */ |
---|
359 | define('DB_PORTABILITY_RTRIM', 2); |
---|
360 | |
---|
361 | /** |
---|
362 | * Force reporting the number of rows deleted |
---|
363 | */ |
---|
364 | define('DB_PORTABILITY_DELETE_COUNT', 4); |
---|
365 | |
---|
366 | /** |
---|
367 | * Enable hack that makes numRows() work in Oracle |
---|
368 | */ |
---|
369 | define('DB_PORTABILITY_NUMROWS', 8); |
---|
370 | |
---|
371 | /** |
---|
372 | * Makes certain error messages in certain drivers compatible |
---|
373 | * with those from other DBMS's |
---|
374 | * |
---|
375 | * + mysql, mysqli: change unique/primary key constraints |
---|
376 | * DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT |
---|
377 | * |
---|
378 | * + odbc(access): MS's ODBC driver reports 'no such field' as code |
---|
379 | * 07001, which means 'too few parameters.' When this option is on |
---|
380 | * that code gets mapped to DB_ERROR_NOSUCHFIELD. |
---|
381 | */ |
---|
382 | define('DB_PORTABILITY_ERRORS', 16); |
---|
383 | |
---|
384 | /** |
---|
385 | * Convert null values to empty strings in data output by |
---|
386 | * get*() and fetch*() |
---|
387 | */ |
---|
388 | define('DB_PORTABILITY_NULL_TO_EMPTY', 32); |
---|
389 | |
---|
390 | /** |
---|
391 | * Turn on all portability features |
---|
392 | */ |
---|
393 | define('DB_PORTABILITY_ALL', 63); |
---|
394 | /**#@-*/ |
---|
395 | |
---|
396 | // }}} |
---|
397 | |
---|
398 | |
---|
399 | // }}} |
---|
400 | // {{{ class DB |
---|
401 | |
---|
402 | /** |
---|
403 | * Database independent query interface |
---|
404 | * |
---|
405 | * The main "DB" class is simply a container class with some static |
---|
406 | * methods for creating DB objects as well as some utility functions |
---|
407 | * common to all parts of DB. |
---|
408 | * |
---|
409 | * The object model of DB is as follows (indentation means inheritance): |
---|
410 | * <pre> |
---|
411 | * DB The main DB class. This is simply a utility class |
---|
412 | * with some "static" methods for creating DB objects as |
---|
413 | * well as common utility functions for other DB classes. |
---|
414 | * |
---|
415 | * DB_common The base for each DB implementation. Provides default |
---|
416 | * | implementations (in OO lingo virtual methods) for |
---|
417 | * | the actual DB implementations as well as a bunch of |
---|
418 | * | query utility functions. |
---|
419 | * | |
---|
420 | * +-DB_mysql The DB implementation for MySQL. Inherits DB_common. |
---|
421 | * When calling DB::factory or DB::connect for MySQL |
---|
422 | * connections, the object returned is an instance of this |
---|
423 | * class. |
---|
424 | * </pre> |
---|
425 | * |
---|
426 | * @category Database |
---|
427 | * @package DB |
---|
428 | * @author Stig Bakken <ssb@php.net> |
---|
429 | * @author Tomas V.V.Cox <cox@idecnet.com> |
---|
430 | * @author Daniel Convissor <danielc@php.net> |
---|
431 | * @copyright 1997-2007 The PHP Group |
---|
432 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
433 | * @version Release: 1.7.14RC1 |
---|
434 | * @link http://pear.php.net/package/DB |
---|
435 | */ |
---|
436 | class DB |
---|
437 | { |
---|
438 | // {{{ &factory() |
---|
439 | |
---|
440 | /** |
---|
441 | * Create a new DB object for the specified database type but don't |
---|
442 | * connect to the database |
---|
443 | * |
---|
444 | * @param string $type the database type (eg "mysql") |
---|
445 | * @param array $options an associative array of option names and values |
---|
446 | * |
---|
447 | * @return object a new DB object. A DB_Error object on failure. |
---|
448 | * |
---|
449 | * @see DB_common::setOption() |
---|
450 | */ |
---|
451 | function &factory($type, $options = false) |
---|
452 | { |
---|
453 | if (!is_array($options)) { |
---|
454 | $options = array('persistent' => $options); |
---|
455 | } |
---|
456 | |
---|
457 | if (isset($options['debug']) && $options['debug'] >= 2) { |
---|
458 | // expose php errors with sufficient debug level |
---|
459 | include_once DB_PHP_DIR . "/DB/{$type}.php"; |
---|
460 | } else { |
---|
461 | @include_once DB_PHP_DIR . "/DB/{$type}.php"; |
---|
462 | } |
---|
463 | |
---|
464 | $classname = "DB_${type}"; |
---|
465 | |
---|
466 | if (!class_exists($classname)) { |
---|
467 | $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null, |
---|
468 | "Unable to include the DB/{$type}.php" |
---|
469 | . " file for '$dsn'", |
---|
470 | 'DB_Error', true); |
---|
471 | return $tmp; |
---|
472 | } |
---|
473 | |
---|
474 | @$obj = new $classname; |
---|
475 | |
---|
476 | foreach ($options as $option => $value) { |
---|
477 | $test = $obj->setOption($option, $value); |
---|
478 | if (DB::isError($test)) { |
---|
479 | return $test; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | return $obj; |
---|
484 | } |
---|
485 | |
---|
486 | // }}} |
---|
487 | // {{{ &connect() |
---|
488 | |
---|
489 | /** |
---|
490 | * Create a new DB object including a connection to the specified database |
---|
491 | * |
---|
492 | * Example 1. |
---|
493 | * <code> |
---|
494 | * require_once 'DB.php'; |
---|
495 | * |
---|
496 | * $dsn = 'pgsql://user:password@host/database'; |
---|
497 | * $options = array( |
---|
498 | * 'debug' => 2, |
---|
499 | * 'portability' => DB_PORTABILITY_ALL, |
---|
500 | * ); |
---|
501 | * |
---|
502 | * $db =& DB::connect($dsn, $options); |
---|
503 | * if (PEAR::isError($db)) { |
---|
504 | * die($db->getMessage()); |
---|
505 | * } |
---|
506 | * </code> |
---|
507 | * |
---|
508 | * @param mixed $dsn the string "data source name" or array in the |
---|
509 | * format returned by DB::parseDSN() |
---|
510 | * @param array $options an associative array of option names and values |
---|
511 | * |
---|
512 | * @return object a new DB object. A DB_Error object on failure. |
---|
513 | * |
---|
514 | * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(), |
---|
515 | * DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(), |
---|
516 | * DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(), |
---|
517 | * DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(), |
---|
518 | * DB_sybase::connect() |
---|
519 | * |
---|
520 | * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError() |
---|
521 | */ |
---|
522 | function &connect($dsn, $options = array()) |
---|
523 | { |
---|
524 | $dsninfo = DB::parseDSN($dsn); |
---|
525 | $type = $dsninfo['phptype']; |
---|
526 | |
---|
527 | if (!is_array($options)) { |
---|
528 | /* |
---|
529 | * For backwards compatibility. $options used to be boolean, |
---|
530 | * indicating whether the connection should be persistent. |
---|
531 | */ |
---|
532 | $options = array('persistent' => $options); |
---|
533 | } |
---|
534 | |
---|
535 | if (isset($options['debug']) && $options['debug'] >= 2) { |
---|
536 | // expose php errors with sufficient debug level |
---|
537 | include_once DB_PHP_DIR . "/DB/${type}.php"; |
---|
538 | } else { |
---|
539 | @include_once DB_PHP_DIR . "/DB/${type}.php"; |
---|
540 | } |
---|
541 | |
---|
542 | $classname = "DB_${type}"; |
---|
543 | if (!class_exists($classname)) { |
---|
544 | $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null, |
---|
545 | "Unable to include the DB/{$type}.php" |
---|
546 | . " file for '$dsn'", |
---|
547 | 'DB_Error', true); |
---|
548 | return $tmp; |
---|
549 | } |
---|
550 | |
---|
551 | @$obj = new $classname; |
---|
552 | |
---|
553 | foreach ($options as $option => $value) { |
---|
554 | $test = $obj->setOption($option, $value); |
---|
555 | if (DB::isError($test)) { |
---|
556 | return $test; |
---|
557 | } |
---|
558 | } |
---|
559 | |
---|
560 | $err = $obj->connect($dsninfo, $obj->getOption('persistent')); |
---|
561 | if (DB::isError($err)) { |
---|
562 | if (is_array($dsn)) { |
---|
563 | $err->addUserInfo(DB::getDSNString($dsn, true)); |
---|
564 | } else { |
---|
565 | $err->addUserInfo($dsn); |
---|
566 | } |
---|
567 | return $err; |
---|
568 | } |
---|
569 | |
---|
570 | return $obj; |
---|
571 | } |
---|
572 | |
---|
573 | // }}} |
---|
574 | // {{{ apiVersion() |
---|
575 | |
---|
576 | /** |
---|
577 | * Return the DB API version |
---|
578 | * |
---|
579 | * @return string the DB API version number |
---|
580 | */ |
---|
581 | function apiVersion() |
---|
582 | { |
---|
583 | return '1.7.14RC1'; |
---|
584 | } |
---|
585 | |
---|
586 | // }}} |
---|
587 | // {{{ isError() |
---|
588 | |
---|
589 | /** |
---|
590 | * Determines if a variable is a DB_Error object |
---|
591 | * |
---|
592 | * @param mixed $value the variable to check |
---|
593 | * |
---|
594 | * @return bool whether $value is DB_Error object |
---|
595 | */ |
---|
596 | function isError($value) |
---|
597 | { |
---|
598 | return is_a($value, 'DB_Error'); |
---|
599 | } |
---|
600 | |
---|
601 | // }}} |
---|
602 | // {{{ isConnection() |
---|
603 | |
---|
604 | /** |
---|
605 | * Determines if a value is a DB_<driver> object |
---|
606 | * |
---|
607 | * @param mixed $value the value to test |
---|
608 | * |
---|
609 | * @return bool whether $value is a DB_<driver> object |
---|
610 | */ |
---|
611 | function isConnection($value) |
---|
612 | { |
---|
613 | return (is_object($value) && |
---|
614 | is_subclass_of($value, 'db_common') && |
---|
615 | method_exists($value, 'simpleQuery')); |
---|
616 | } |
---|
617 | |
---|
618 | // }}} |
---|
619 | // {{{ isManip() |
---|
620 | |
---|
621 | /** |
---|
622 | * Tell whether a query is a data manipulation or data definition query |
---|
623 | * |
---|
624 | * Examples of data manipulation queries are INSERT, UPDATE and DELETE. |
---|
625 | * Examples of data definition queries are CREATE, DROP, ALTER, GRANT, |
---|
626 | * REVOKE. |
---|
627 | * |
---|
628 | * @param string $query the query |
---|
629 | * |
---|
630 | * @return boolean whether $query is a data manipulation query |
---|
631 | */ |
---|
632 | function isManip($query) |
---|
633 | { |
---|
634 | $manips = 'INSERT|UPDATE|DELETE|REPLACE|' |
---|
635 | . 'CREATE|DROP|' |
---|
636 | . 'LOAD DATA|SELECT .* INTO .* FROM|COPY|' |
---|
637 | . 'ALTER|GRANT|REVOKE|' |
---|
638 | . 'LOCK|UNLOCK'; |
---|
639 | if (preg_match('/^\s*"?(' . $manips . ')\s+/i', $query)) { |
---|
640 | return true; |
---|
641 | } |
---|
642 | return false; |
---|
643 | } |
---|
644 | |
---|
645 | // }}} |
---|
646 | // {{{ errorMessage() |
---|
647 | |
---|
648 | /** |
---|
649 | * Return a textual error message for a DB error code |
---|
650 | * |
---|
651 | * @param integer $value the DB error code |
---|
652 | * |
---|
653 | * @return string the error message or false if the error code was |
---|
654 | * not recognized |
---|
655 | */ |
---|
656 | function errorMessage($value) |
---|
657 | { |
---|
658 | static $errorMessages; |
---|
659 | if (!isset($errorMessages)) { |
---|
660 | $errorMessages = array( |
---|
661 | DB_ERROR => 'unknown error', |
---|
662 | DB_ERROR_ACCESS_VIOLATION => 'insufficient permissions', |
---|
663 | DB_ERROR_ALREADY_EXISTS => 'already exists', |
---|
664 | DB_ERROR_CANNOT_CREATE => 'can not create', |
---|
665 | DB_ERROR_CANNOT_DROP => 'can not drop', |
---|
666 | DB_ERROR_CONNECT_FAILED => 'connect failed', |
---|
667 | DB_ERROR_CONSTRAINT => 'constraint violation', |
---|
668 | DB_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint', |
---|
669 | DB_ERROR_DIVZERO => 'division by zero', |
---|
670 | DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found', |
---|
671 | DB_ERROR_INVALID => 'invalid', |
---|
672 | DB_ERROR_INVALID_DATE => 'invalid date or time', |
---|
673 | DB_ERROR_INVALID_DSN => 'invalid DSN', |
---|
674 | DB_ERROR_INVALID_NUMBER => 'invalid number', |
---|
675 | DB_ERROR_MISMATCH => 'mismatch', |
---|
676 | DB_ERROR_NEED_MORE_DATA => 'insufficient data supplied', |
---|
677 | DB_ERROR_NODBSELECTED => 'no database selected', |
---|
678 | DB_ERROR_NOSUCHDB => 'no such database', |
---|
679 | DB_ERROR_NOSUCHFIELD => 'no such field', |
---|
680 | DB_ERROR_NOSUCHTABLE => 'no such table', |
---|
681 | DB_ERROR_NOT_CAPABLE => 'DB backend not capable', |
---|
682 | DB_ERROR_NOT_FOUND => 'not found', |
---|
683 | DB_ERROR_NOT_LOCKED => 'not locked', |
---|
684 | DB_ERROR_SYNTAX => 'syntax error', |
---|
685 | DB_ERROR_UNSUPPORTED => 'not supported', |
---|
686 | DB_ERROR_TRUNCATED => 'truncated', |
---|
687 | DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row', |
---|
688 | DB_OK => 'no error', |
---|
689 | ); |
---|
690 | } |
---|
691 | |
---|
692 | if (DB::isError($value)) { |
---|
693 | $value = $value->getCode(); |
---|
694 | } |
---|
695 | |
---|
696 | return isset($errorMessages[$value]) ? $errorMessages[$value] |
---|
697 | : $errorMessages[DB_ERROR]; |
---|
698 | } |
---|
699 | |
---|
700 | // }}} |
---|
701 | // {{{ parseDSN() |
---|
702 | |
---|
703 | /** |
---|
704 | * Parse a data source name |
---|
705 | * |
---|
706 | * Additional keys can be added by appending a URI query string to the |
---|
707 | * end of the DSN. |
---|
708 | * |
---|
709 | * The format of the supplied DSN is in its fullest form: |
---|
710 | * <code> |
---|
711 | * phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true |
---|
712 | * </code> |
---|
713 | * |
---|
714 | * Most variations are allowed: |
---|
715 | * <code> |
---|
716 | * phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644 |
---|
717 | * phptype://username:password@hostspec/database_name |
---|
718 | * phptype://username:password@hostspec |
---|
719 | * phptype://username@hostspec |
---|
720 | * phptype://hostspec/database |
---|
721 | * phptype://hostspec |
---|
722 | * phptype(dbsyntax) |
---|
723 | * phptype |
---|
724 | * </code> |
---|
725 | * |
---|
726 | * @param string $dsn Data Source Name to be parsed |
---|
727 | * |
---|
728 | * @return array an associative array with the following keys: |
---|
729 | * + phptype: Database backend used in PHP (mysql, odbc etc.) |
---|
730 | * + dbsyntax: Database used with regards to SQL syntax etc. |
---|
731 | * + protocol: Communication protocol to use (tcp, unix etc.) |
---|
732 | * + hostspec: Host specification (hostname[:port]) |
---|
733 | * + database: Database to use on the DBMS server |
---|
734 | * + username: User name for login |
---|
735 | * + password: Password for login |
---|
736 | */ |
---|
737 | function parseDSN($dsn) |
---|
738 | { |
---|
739 | $parsed = array( |
---|
740 | 'phptype' => false, |
---|
741 | 'dbsyntax' => false, |
---|
742 | 'username' => false, |
---|
743 | 'password' => false, |
---|
744 | 'protocol' => false, |
---|
745 | 'hostspec' => false, |
---|
746 | 'port' => false, |
---|
747 | 'socket' => false, |
---|
748 | 'database' => false, |
---|
749 | ); |
---|
750 | |
---|
751 | if (is_array($dsn)) { |
---|
752 | $dsn = array_merge($parsed, $dsn); |
---|
753 | if (!$dsn['dbsyntax']) { |
---|
754 | $dsn['dbsyntax'] = $dsn['phptype']; |
---|
755 | } |
---|
756 | return $dsn; |
---|
757 | } |
---|
758 | |
---|
759 | // Find phptype and dbsyntax |
---|
760 | if (($pos = strpos($dsn, '://')) !== false) { |
---|
761 | $str = substr($dsn, 0, $pos); |
---|
762 | $dsn = substr($dsn, $pos + 3); |
---|
763 | } else { |
---|
764 | $str = $dsn; |
---|
765 | $dsn = null; |
---|
766 | } |
---|
767 | |
---|
768 | // Get phptype and dbsyntax |
---|
769 | // $str => phptype(dbsyntax) |
---|
770 | if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) { |
---|
771 | $parsed['phptype'] = $arr[1]; |
---|
772 | $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2]; |
---|
773 | } else { |
---|
774 | $parsed['phptype'] = $str; |
---|
775 | $parsed['dbsyntax'] = $str; |
---|
776 | } |
---|
777 | |
---|
778 | if (!count($dsn)) { |
---|
779 | return $parsed; |
---|
780 | } |
---|
781 | |
---|
782 | // Get (if found): username and password |
---|
783 | // $dsn => username:password@protocol+hostspec/database |
---|
784 | if (($at = strrpos($dsn,'@')) !== false) { |
---|
785 | $str = substr($dsn, 0, $at); |
---|
786 | $dsn = substr($dsn, $at + 1); |
---|
787 | if (($pos = strpos($str, ':')) !== false) { |
---|
788 | $parsed['username'] = rawurldecode(substr($str, 0, $pos)); |
---|
789 | $parsed['password'] = rawurldecode(substr($str, $pos + 1)); |
---|
790 | } else { |
---|
791 | $parsed['username'] = rawurldecode($str); |
---|
792 | } |
---|
793 | } |
---|
794 | |
---|
795 | // Find protocol and hostspec |
---|
796 | |
---|
797 | if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) { |
---|
798 | // $dsn => proto(proto_opts)/database |
---|
799 | $proto = $match[1]; |
---|
800 | $proto_opts = $match[2] ? $match[2] : false; |
---|
801 | $dsn = $match[3]; |
---|
802 | |
---|
803 | } else { |
---|
804 | // $dsn => protocol+hostspec/database (old format) |
---|
805 | if (strpos($dsn, '+') !== false) { |
---|
806 | list($proto, $dsn) = explode('+', $dsn, 2); |
---|
807 | } |
---|
808 | if (strpos($dsn, '/') !== false) { |
---|
809 | list($proto_opts, $dsn) = explode('/', $dsn, 2); |
---|
810 | } else { |
---|
811 | $proto_opts = $dsn; |
---|
812 | $dsn = null; |
---|
813 | } |
---|
814 | } |
---|
815 | |
---|
816 | // process the different protocol options |
---|
817 | $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp'; |
---|
818 | $proto_opts = rawurldecode($proto_opts); |
---|
819 | if (strpos($proto_opts, ':') !== false) { |
---|
820 | list($proto_opts, $parsed['port']) = explode(':', $proto_opts); |
---|
821 | } |
---|
822 | if ($parsed['protocol'] == 'tcp') { |
---|
823 | $parsed['hostspec'] = $proto_opts; |
---|
824 | } elseif ($parsed['protocol'] == 'unix') { |
---|
825 | $parsed['socket'] = $proto_opts; |
---|
826 | } |
---|
827 | |
---|
828 | // Get dabase if any |
---|
829 | // $dsn => database |
---|
830 | if ($dsn) { |
---|
831 | if (($pos = strpos($dsn, '?')) === false) { |
---|
832 | // /database |
---|
833 | $parsed['database'] = rawurldecode($dsn); |
---|
834 | } else { |
---|
835 | // /database?param1=value1¶m2=value2 |
---|
836 | $parsed['database'] = rawurldecode(substr($dsn, 0, $pos)); |
---|
837 | $dsn = substr($dsn, $pos + 1); |
---|
838 | if (strpos($dsn, '&') !== false) { |
---|
839 | $opts = explode('&', $dsn); |
---|
840 | } else { // database?param1=value1 |
---|
841 | $opts = array($dsn); |
---|
842 | } |
---|
843 | foreach ($opts as $opt) { |
---|
844 | list($key, $value) = explode('=', $opt); |
---|
845 | if (!isset($parsed[$key])) { |
---|
846 | // don't allow params overwrite |
---|
847 | $parsed[$key] = rawurldecode($value); |
---|
848 | } |
---|
849 | } |
---|
850 | } |
---|
851 | } |
---|
852 | |
---|
853 | return $parsed; |
---|
854 | } |
---|
855 | |
---|
856 | // }}} |
---|
857 | // {{{ getDSNString() |
---|
858 | |
---|
859 | /** |
---|
860 | * Returns the given DSN in a string format suitable for output. |
---|
861 | * |
---|
862 | * @param array|string the DSN to parse and format |
---|
863 | * @param boolean true to hide the password, false to include it |
---|
864 | * @return string |
---|
865 | */ |
---|
866 | function getDSNString($dsn, $hidePassword) { |
---|
867 | /* Calling parseDSN will ensure that we have all the array elements |
---|
868 | * defined, and means that we deal with strings and array in the same |
---|
869 | * manner. */ |
---|
870 | $dsnArray = DB::parseDSN($dsn); |
---|
871 | |
---|
872 | if ($hidePassword) { |
---|
873 | $dsnArray['password'] = 'PASSWORD'; |
---|
874 | } |
---|
875 | |
---|
876 | /* Protocol is special-cased, as using the default "tcp" along with an |
---|
877 | * Oracle TNS connection string fails. */ |
---|
878 | if (is_string($dsn) && strpos($dsn, 'tcp') === false && $dsnArray['protocol'] == 'tcp') { |
---|
879 | $dsnArray['protocol'] = false; |
---|
880 | } |
---|
881 | |
---|
882 | // Now we just have to construct the actual string. This is ugly. |
---|
883 | $dsnString = $dsnArray['phptype']; |
---|
884 | if ($dsnArray['dbsyntax']) { |
---|
885 | $dsnString .= '('.$dsnArray['dbsyntax'].')'; |
---|
886 | } |
---|
887 | $dsnString .= '://' |
---|
888 | .$dsnArray['username'] |
---|
889 | .':' |
---|
890 | .$dsnArray['password'] |
---|
891 | .'@' |
---|
892 | .$dsnArray['protocol']; |
---|
893 | if ($dsnArray['socket']) { |
---|
894 | $dsnString .= '('.$dsnArray['socket'].')'; |
---|
895 | } |
---|
896 | if ($dsnArray['protocol'] && $dsnArray['hostspec']) { |
---|
897 | $dsnString .= '+'; |
---|
898 | } |
---|
899 | $dsnString .= $dsnArray['hostspec']; |
---|
900 | if ($dsnArray['port']) { |
---|
901 | $dsnString .= ':'.$dsnArray['port']; |
---|
902 | } |
---|
903 | $dsnString .= '/'.$dsnArray['database']; |
---|
904 | |
---|
905 | /* Option handling. Unfortunately, parseDSN simply places options into |
---|
906 | * the top-level array, so we'll first get rid of the fields defined by |
---|
907 | * DB and see what's left. */ |
---|
908 | unset($dsnArray['phptype'], |
---|
909 | $dsnArray['dbsyntax'], |
---|
910 | $dsnArray['username'], |
---|
911 | $dsnArray['password'], |
---|
912 | $dsnArray['protocol'], |
---|
913 | $dsnArray['socket'], |
---|
914 | $dsnArray['hostspec'], |
---|
915 | $dsnArray['port'], |
---|
916 | $dsnArray['database'] |
---|
917 | ); |
---|
918 | if (count($dsnArray) > 0) { |
---|
919 | $dsnString .= '?'; |
---|
920 | $i = 0; |
---|
921 | foreach ($dsnArray as $key => $value) { |
---|
922 | if (++$i > 1) { |
---|
923 | $dsnString .= '&'; |
---|
924 | } |
---|
925 | $dsnString .= $key.'='.$value; |
---|
926 | } |
---|
927 | } |
---|
928 | |
---|
929 | return $dsnString; |
---|
930 | } |
---|
931 | |
---|
932 | // }}} |
---|
933 | } |
---|
934 | |
---|
935 | // }}} |
---|
936 | // {{{ class DB_Error |
---|
937 | |
---|
938 | /** |
---|
939 | * DB_Error implements a class for reporting portable database error |
---|
940 | * messages |
---|
941 | * |
---|
942 | * @category Database |
---|
943 | * @package DB |
---|
944 | * @author Stig Bakken <ssb@php.net> |
---|
945 | * @copyright 1997-2007 The PHP Group |
---|
946 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
947 | * @version Release: 1.7.14RC1 |
---|
948 | * @link http://pear.php.net/package/DB |
---|
949 | */ |
---|
950 | class DB_Error extends PEAR_Error |
---|
951 | { |
---|
952 | // {{{ constructor |
---|
953 | |
---|
954 | /** |
---|
955 | * DB_Error constructor |
---|
956 | * |
---|
957 | * @param mixed $code DB error code, or string with error message |
---|
958 | * @param int $mode what "error mode" to operate in |
---|
959 | * @param int $level what error level to use for $mode & |
---|
960 | * PEAR_ERROR_TRIGGER |
---|
961 | * @param mixed $debuginfo additional debug info, such as the last query |
---|
962 | * |
---|
963 | * @see PEAR_Error |
---|
964 | */ |
---|
965 | function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN, |
---|
966 | $level = E_USER_NOTICE, $debuginfo = null) |
---|
967 | { |
---|
968 | if (is_int($code)) { |
---|
969 | $this->PEAR_Error('DB Error: ' . DB::errorMessage($code), $code, |
---|
970 | $mode, $level, $debuginfo); |
---|
971 | } else { |
---|
972 | $this->PEAR_Error("DB Error: $code", DB_ERROR, |
---|
973 | $mode, $level, $debuginfo); |
---|
974 | } |
---|
975 | } |
---|
976 | |
---|
977 | // }}} |
---|
978 | } |
---|
979 | |
---|
980 | // }}} |
---|
981 | // {{{ class DB_result |
---|
982 | |
---|
983 | /** |
---|
984 | * This class implements a wrapper for a DB result set |
---|
985 | * |
---|
986 | * A new instance of this class will be returned by the DB implementation |
---|
987 | * after processing a query that returns data. |
---|
988 | * |
---|
989 | * @category Database |
---|
990 | * @package DB |
---|
991 | * @author Stig Bakken <ssb@php.net> |
---|
992 | * @copyright 1997-2007 The PHP Group |
---|
993 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
994 | * @version Release: 1.7.14RC1 |
---|
995 | * @link http://pear.php.net/package/DB |
---|
996 | */ |
---|
997 | class DB_result |
---|
998 | { |
---|
999 | // {{{ properties |
---|
1000 | |
---|
1001 | /** |
---|
1002 | * Should results be freed automatically when there are no more rows? |
---|
1003 | * @var boolean |
---|
1004 | * @see DB_common::$options |
---|
1005 | */ |
---|
1006 | var $autofree; |
---|
1007 | |
---|
1008 | /** |
---|
1009 | * A reference to the DB_<driver> object |
---|
1010 | * @var object |
---|
1011 | */ |
---|
1012 | var $dbh; |
---|
1013 | |
---|
1014 | /** |
---|
1015 | * The current default fetch mode |
---|
1016 | * @var integer |
---|
1017 | * @see DB_common::$fetchmode |
---|
1018 | */ |
---|
1019 | var $fetchmode; |
---|
1020 | |
---|
1021 | /** |
---|
1022 | * The name of the class into which results should be fetched when |
---|
1023 | * DB_FETCHMODE_OBJECT is in effect |
---|
1024 | * |
---|
1025 | * @var string |
---|
1026 | * @see DB_common::$fetchmode_object_class |
---|
1027 | */ |
---|
1028 | var $fetchmode_object_class; |
---|
1029 | |
---|
1030 | /** |
---|
1031 | * The number of rows to fetch from a limit query |
---|
1032 | * @var integer |
---|
1033 | */ |
---|
1034 | var $limit_count = null; |
---|
1035 | |
---|
1036 | /** |
---|
1037 | * The row to start fetching from in limit queries |
---|
1038 | * @var integer |
---|
1039 | */ |
---|
1040 | var $limit_from = null; |
---|
1041 | |
---|
1042 | /** |
---|
1043 | * The execute parameters that created this result |
---|
1044 | * @var array |
---|
1045 | * @since Property available since Release 1.7.0 |
---|
1046 | */ |
---|
1047 | var $parameters; |
---|
1048 | |
---|
1049 | /** |
---|
1050 | * The query string that created this result |
---|
1051 | * |
---|
1052 | * Copied here incase it changes in $dbh, which is referenced |
---|
1053 | * |
---|
1054 | * @var string |
---|
1055 | * @since Property available since Release 1.7.0 |
---|
1056 | */ |
---|
1057 | var $query; |
---|
1058 | |
---|
1059 | /** |
---|
1060 | * The query result resource id created by PHP |
---|
1061 | * @var resource |
---|
1062 | */ |
---|
1063 | var $result; |
---|
1064 | |
---|
1065 | /** |
---|
1066 | * The present row being dealt with |
---|
1067 | * @var integer |
---|
1068 | */ |
---|
1069 | var $row_counter = null; |
---|
1070 | |
---|
1071 | /** |
---|
1072 | * The prepared statement resource id created by PHP in $dbh |
---|
1073 | * |
---|
1074 | * This resource is only available when the result set was created using |
---|
1075 | * a driver's native execute() method, not PEAR DB's emulated one. |
---|
1076 | * |
---|
1077 | * Copied here incase it changes in $dbh, which is referenced |
---|
1078 | * |
---|
1079 | * {@internal Mainly here because the InterBase/Firebird API is only |
---|
1080 | * able to retrieve data from result sets if the statemnt handle is |
---|
1081 | * still in scope.}} |
---|
1082 | * |
---|
1083 | * @var resource |
---|
1084 | * @since Property available since Release 1.7.0 |
---|
1085 | */ |
---|
1086 | var $statement; |
---|
1087 | |
---|
1088 | |
---|
1089 | // }}} |
---|
1090 | // {{{ constructor |
---|
1091 | |
---|
1092 | /** |
---|
1093 | * This constructor sets the object's properties |
---|
1094 | * |
---|
1095 | * @param object &$dbh the DB object reference |
---|
1096 | * @param resource $result the result resource id |
---|
1097 | * @param array $options an associative array with result options |
---|
1098 | * |
---|
1099 | * @return void |
---|
1100 | */ |
---|
1101 | function DB_result(&$dbh, $result, $options = array()) |
---|
1102 | { |
---|
1103 | $this->autofree = $dbh->options['autofree']; |
---|
1104 | $this->dbh = &$dbh; |
---|
1105 | $this->fetchmode = $dbh->fetchmode; |
---|
1106 | $this->fetchmode_object_class = $dbh->fetchmode_object_class; |
---|
1107 | $this->parameters = $dbh->last_parameters; |
---|
1108 | $this->query = $dbh->last_query; |
---|
1109 | $this->result = $result; |
---|
1110 | $this->statement = empty($dbh->last_stmt) ? null : $dbh->last_stmt; |
---|
1111 | foreach ($options as $key => $value) { |
---|
1112 | $this->setOption($key, $value); |
---|
1113 | } |
---|
1114 | } |
---|
1115 | |
---|
1116 | /** |
---|
1117 | * Set options for the DB_result object |
---|
1118 | * |
---|
1119 | * @param string $key the option to set |
---|
1120 | * @param mixed $value the value to set the option to |
---|
1121 | * |
---|
1122 | * @return void |
---|
1123 | */ |
---|
1124 | function setOption($key, $value = null) |
---|
1125 | { |
---|
1126 | switch ($key) { |
---|
1127 | case 'limit_from': |
---|
1128 | $this->limit_from = $value; |
---|
1129 | break; |
---|
1130 | case 'limit_count': |
---|
1131 | $this->limit_count = $value; |
---|
1132 | } |
---|
1133 | } |
---|
1134 | |
---|
1135 | // }}} |
---|
1136 | // {{{ fetchRow() |
---|
1137 | |
---|
1138 | /** |
---|
1139 | * Fetch a row of data and return it by reference into an array |
---|
1140 | * |
---|
1141 | * The type of array returned can be controlled either by setting this |
---|
1142 | * method's <var>$fetchmode</var> parameter or by changing the default |
---|
1143 | * fetch mode setFetchMode() before calling this method. |
---|
1144 | * |
---|
1145 | * There are two options for standardizing the information returned |
---|
1146 | * from databases, ensuring their values are consistent when changing |
---|
1147 | * DBMS's. These portability options can be turned on when creating a |
---|
1148 | * new DB object or by using setOption(). |
---|
1149 | * |
---|
1150 | * + <var>DB_PORTABILITY_LOWERCASE</var> |
---|
1151 | * convert names of fields to lower case |
---|
1152 | * |
---|
1153 | * + <var>DB_PORTABILITY_RTRIM</var> |
---|
1154 | * right trim the data |
---|
1155 | * |
---|
1156 | * @param int $fetchmode the constant indicating how to format the data |
---|
1157 | * @param int $rownum the row number to fetch (index starts at 0) |
---|
1158 | * |
---|
1159 | * @return mixed an array or object containing the row's data, |
---|
1160 | * NULL when the end of the result set is reached |
---|
1161 | * or a DB_Error object on failure. |
---|
1162 | * |
---|
1163 | * @see DB_common::setOption(), DB_common::setFetchMode() |
---|
1164 | */ |
---|
1165 | function &fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) |
---|
1166 | { |
---|
1167 | if ($fetchmode === DB_FETCHMODE_DEFAULT) { |
---|
1168 | $fetchmode = $this->fetchmode; |
---|
1169 | } |
---|
1170 | if ($fetchmode === DB_FETCHMODE_OBJECT) { |
---|
1171 | $fetchmode = DB_FETCHMODE_ASSOC; |
---|
1172 | $object_class = $this->fetchmode_object_class; |
---|
1173 | } |
---|
1174 | if (is_null($rownum) && $this->limit_from !== null) { |
---|
1175 | if ($this->row_counter === null) { |
---|
1176 | $this->row_counter = $this->limit_from; |
---|
1177 | // Skip rows |
---|
1178 | if ($this->dbh->features['limit'] === false) { |
---|
1179 | $i = 0; |
---|
1180 | while ($i++ < $this->limit_from) { |
---|
1181 | $this->dbh->fetchInto($this->result, $arr, $fetchmode); |
---|
1182 | } |
---|
1183 | } |
---|
1184 | } |
---|
1185 | if ($this->row_counter >= ($this->limit_from + $this->limit_count)) |
---|
1186 | { |
---|
1187 | if ($this->autofree) { |
---|
1188 | $this->free(); |
---|
1189 | } |
---|
1190 | $tmp = null; |
---|
1191 | return $tmp; |
---|
1192 | } |
---|
1193 | if ($this->dbh->features['limit'] === 'emulate') { |
---|
1194 | $rownum = $this->row_counter; |
---|
1195 | } |
---|
1196 | $this->row_counter++; |
---|
1197 | } |
---|
1198 | $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); |
---|
1199 | if ($res === DB_OK) { |
---|
1200 | if (isset($object_class)) { |
---|
1201 | // The default mode is specified in the |
---|
1202 | // DB_common::fetchmode_object_class property |
---|
1203 | if ($object_class == 'stdClass') { |
---|
1204 | $arr = (object) $arr; |
---|
1205 | } else { |
---|
1206 | $arr = new $object_class($arr); |
---|
1207 | } |
---|
1208 | } |
---|
1209 | return $arr; |
---|
1210 | } |
---|
1211 | if ($res == null && $this->autofree) { |
---|
1212 | $this->free(); |
---|
1213 | } |
---|
1214 | return $res; |
---|
1215 | } |
---|
1216 | |
---|
1217 | // }}} |
---|
1218 | // {{{ fetchInto() |
---|
1219 | |
---|
1220 | /** |
---|
1221 | * Fetch a row of data into an array which is passed by reference |
---|
1222 | * |
---|
1223 | * The type of array returned can be controlled either by setting this |
---|
1224 | * method's <var>$fetchmode</var> parameter or by changing the default |
---|
1225 | * fetch mode setFetchMode() before calling this method. |
---|
1226 | * |
---|
1227 | * There are two options for standardizing the information returned |
---|
1228 | * from databases, ensuring their values are consistent when changing |
---|
1229 | * DBMS's. These portability options can be turned on when creating a |
---|
1230 | * new DB object or by using setOption(). |
---|
1231 | * |
---|
1232 | * + <var>DB_PORTABILITY_LOWERCASE</var> |
---|
1233 | * convert names of fields to lower case |
---|
1234 | * |
---|
1235 | * + <var>DB_PORTABILITY_RTRIM</var> |
---|
1236 | * right trim the data |
---|
1237 | * |
---|
1238 | * @param array &$arr the variable where the data should be placed |
---|
1239 | * @param int $fetchmode the constant indicating how to format the data |
---|
1240 | * @param int $rownum the row number to fetch (index starts at 0) |
---|
1241 | * |
---|
1242 | * @return mixed DB_OK if a row is processed, NULL when the end of the |
---|
1243 | * result set is reached or a DB_Error object on failure |
---|
1244 | * |
---|
1245 | * @see DB_common::setOption(), DB_common::setFetchMode() |
---|
1246 | */ |
---|
1247 | function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null) |
---|
1248 | { |
---|
1249 | if ($fetchmode === DB_FETCHMODE_DEFAULT) { |
---|
1250 | $fetchmode = $this->fetchmode; |
---|
1251 | } |
---|
1252 | if ($fetchmode === DB_FETCHMODE_OBJECT) { |
---|
1253 | $fetchmode = DB_FETCHMODE_ASSOC; |
---|
1254 | $object_class = $this->fetchmode_object_class; |
---|
1255 | } |
---|
1256 | if (is_null($rownum) && $this->limit_from !== null) { |
---|
1257 | if ($this->row_counter === null) { |
---|
1258 | $this->row_counter = $this->limit_from; |
---|
1259 | // Skip rows |
---|
1260 | if ($this->dbh->features['limit'] === false) { |
---|
1261 | $i = 0; |
---|
1262 | while ($i++ < $this->limit_from) { |
---|
1263 | $this->dbh->fetchInto($this->result, $arr, $fetchmode); |
---|
1264 | } |
---|
1265 | } |
---|
1266 | } |
---|
1267 | if ($this->row_counter >= ( |
---|
1268 | $this->limit_from + $this->limit_count)) |
---|
1269 | { |
---|
1270 | if ($this->autofree) { |
---|
1271 | $this->free(); |
---|
1272 | } |
---|
1273 | return null; |
---|
1274 | } |
---|
1275 | if ($this->dbh->features['limit'] === 'emulate') { |
---|
1276 | $rownum = $this->row_counter; |
---|
1277 | } |
---|
1278 | |
---|
1279 | $this->row_counter++; |
---|
1280 | } |
---|
1281 | $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); |
---|
1282 | if ($res === DB_OK) { |
---|
1283 | if (isset($object_class)) { |
---|
1284 | // default mode specified in the |
---|
1285 | // DB_common::fetchmode_object_class property |
---|
1286 | if ($object_class == 'stdClass') { |
---|
1287 | $arr = (object) $arr; |
---|
1288 | } else { |
---|
1289 | $arr = new $object_class($arr); |
---|
1290 | } |
---|
1291 | } |
---|
1292 | return DB_OK; |
---|
1293 | } |
---|
1294 | if ($res == null && $this->autofree) { |
---|
1295 | $this->free(); |
---|
1296 | } |
---|
1297 | return $res; |
---|
1298 | } |
---|
1299 | |
---|
1300 | // }}} |
---|
1301 | // {{{ numCols() |
---|
1302 | |
---|
1303 | /** |
---|
1304 | * Get the the number of columns in a result set |
---|
1305 | * |
---|
1306 | * @return int the number of columns. A DB_Error object on failure. |
---|
1307 | */ |
---|
1308 | function numCols() |
---|
1309 | { |
---|
1310 | return $this->dbh->numCols($this->result); |
---|
1311 | } |
---|
1312 | |
---|
1313 | // }}} |
---|
1314 | // {{{ numRows() |
---|
1315 | |
---|
1316 | /** |
---|
1317 | * Get the number of rows in a result set |
---|
1318 | * |
---|
1319 | * @return int the number of rows. A DB_Error object on failure. |
---|
1320 | */ |
---|
1321 | function numRows() |
---|
1322 | { |
---|
1323 | if ($this->dbh->features['numrows'] === 'emulate' |
---|
1324 | && $this->dbh->options['portability'] & DB_PORTABILITY_NUMROWS) |
---|
1325 | { |
---|
1326 | if ($this->dbh->features['prepare']) { |
---|
1327 | $res = $this->dbh->query($this->query, $this->parameters); |
---|
1328 | } else { |
---|
1329 | $res = $this->dbh->query($this->query); |
---|
1330 | } |
---|
1331 | if (DB::isError($res)) { |
---|
1332 | return $res; |
---|
1333 | } |
---|
1334 | $i = 0; |
---|
1335 | while ($res->fetchInto($tmp, DB_FETCHMODE_ORDERED)) { |
---|
1336 | $i++; |
---|
1337 | } |
---|
1338 | $count = $i; |
---|
1339 | } else { |
---|
1340 | $count = $this->dbh->numRows($this->result); |
---|
1341 | } |
---|
1342 | |
---|
1343 | /* fbsql is checked for here because limit queries are implemented |
---|
1344 | * using a TOP() function, which results in fbsql_num_rows still |
---|
1345 | * returning the total number of rows that would have been returned, |
---|
1346 | * rather than the real number. As a result, we'll just do the limit |
---|
1347 | * calculations for fbsql in the same way as a database with emulated |
---|
1348 | * limits. Unfortunately, we can't just do this in DB_fbsql::numRows() |
---|
1349 | * because that only gets the result resource, rather than the full |
---|
1350 | * DB_Result object. */ |
---|
1351 | if (($this->dbh->features['limit'] === 'emulate' |
---|
1352 | && $this->limit_from !== null) |
---|
1353 | || $this->dbh->phptype == 'fbsql') { |
---|
1354 | $limit_count = is_null($this->limit_count) ? $count : $this->limit_count; |
---|
1355 | if ($count < $this->limit_from) { |
---|
1356 | $count = 0; |
---|
1357 | } elseif ($count < ($this->limit_from + $limit_count)) { |
---|
1358 | $count -= $this->limit_from; |
---|
1359 | } else { |
---|
1360 | $count = $limit_count; |
---|
1361 | } |
---|
1362 | } |
---|
1363 | |
---|
1364 | return $count; |
---|
1365 | } |
---|
1366 | |
---|
1367 | // }}} |
---|
1368 | // {{{ nextResult() |
---|
1369 | |
---|
1370 | /** |
---|
1371 | * Get the next result if a batch of queries was executed |
---|
1372 | * |
---|
1373 | * @return bool true if a new result is available or false if not |
---|
1374 | */ |
---|
1375 | function nextResult() |
---|
1376 | { |
---|
1377 | return $this->dbh->nextResult($this->result); |
---|
1378 | } |
---|
1379 | |
---|
1380 | // }}} |
---|
1381 | // {{{ free() |
---|
1382 | |
---|
1383 | /** |
---|
1384 | * Frees the resources allocated for this result set |
---|
1385 | * |
---|
1386 | * @return bool true on success. A DB_Error object on failure. |
---|
1387 | */ |
---|
1388 | function free() |
---|
1389 | { |
---|
1390 | $err = $this->dbh->freeResult($this->result); |
---|
1391 | if (DB::isError($err)) { |
---|
1392 | return $err; |
---|
1393 | } |
---|
1394 | $this->result = false; |
---|
1395 | $this->statement = false; |
---|
1396 | return true; |
---|
1397 | } |
---|
1398 | |
---|
1399 | // }}} |
---|
1400 | // {{{ tableInfo() |
---|
1401 | |
---|
1402 | /** |
---|
1403 | * @see DB_common::tableInfo() |
---|
1404 | * @deprecated Method deprecated some time before Release 1.2 |
---|
1405 | */ |
---|
1406 | function tableInfo($mode = null) |
---|
1407 | { |
---|
1408 | if (is_string($mode)) { |
---|
1409 | return $this->dbh->raiseError(DB_ERROR_NEED_MORE_DATA); |
---|
1410 | } |
---|
1411 | return $this->dbh->tableInfo($this, $mode); |
---|
1412 | } |
---|
1413 | |
---|
1414 | // }}} |
---|
1415 | // {{{ getQuery() |
---|
1416 | |
---|
1417 | /** |
---|
1418 | * Determine the query string that created this result |
---|
1419 | * |
---|
1420 | * @return string the query string |
---|
1421 | * |
---|
1422 | * @since Method available since Release 1.7.0 |
---|
1423 | */ |
---|
1424 | function getQuery() |
---|
1425 | { |
---|
1426 | return $this->query; |
---|
1427 | } |
---|
1428 | |
---|
1429 | // }}} |
---|
1430 | // {{{ getRowCounter() |
---|
1431 | |
---|
1432 | /** |
---|
1433 | * Tells which row number is currently being processed |
---|
1434 | * |
---|
1435 | * @return integer the current row being looked at. Starts at 1. |
---|
1436 | */ |
---|
1437 | function getRowCounter() |
---|
1438 | { |
---|
1439 | return $this->row_counter; |
---|
1440 | } |
---|
1441 | |
---|
1442 | // }}} |
---|
1443 | } |
---|
1444 | |
---|
1445 | // }}} |
---|
1446 | // {{{ class DB_row |
---|
1447 | |
---|
1448 | /** |
---|
1449 | * PEAR DB Row Object |
---|
1450 | * |
---|
1451 | * The object contains a row of data from a result set. Each column's data |
---|
1452 | * is placed in a property named for the column. |
---|
1453 | * |
---|
1454 | * @category Database |
---|
1455 | * @package DB |
---|
1456 | * @author Stig Bakken <ssb@php.net> |
---|
1457 | * @copyright 1997-2007 The PHP Group |
---|
1458 | * @license http://www.php.net/license/3_0.txt PHP License 3.0 |
---|
1459 | * @version Release: 1.7.14RC1 |
---|
1460 | * @link http://pear.php.net/package/DB |
---|
1461 | * @see DB_common::setFetchMode() |
---|
1462 | */ |
---|
1463 | class DB_row |
---|
1464 | { |
---|
1465 | // {{{ constructor |
---|
1466 | |
---|
1467 | /** |
---|
1468 | * The constructor places a row's data into properties of this object |
---|
1469 | * |
---|
1470 | * @param array the array containing the row's data |
---|
1471 | * |
---|
1472 | * @return void |
---|
1473 | */ |
---|
1474 | function DB_row(&$arr) |
---|
1475 | { |
---|
1476 | foreach ($arr as $key => $value) { |
---|
1477 | $this->$key = &$arr[$key]; |
---|
1478 | } |
---|
1479 | } |
---|
1480 | |
---|
1481 | // }}} |
---|
1482 | } |
---|
1483 | |
---|
1484 | // }}} |
---|
1485 | |
---|
1486 | /* |
---|
1487 | * Local variables: |
---|
1488 | * tab-width: 4 |
---|
1489 | * c-basic-offset: 4 |
---|
1490 | * End: |
---|
1491 | */ |
---|
1492 | |
---|
1493 | ?> |
---|