commit | author | age
|
f45ec7
|
1 |
<?php |
S |
2 |
// vim: set et ts=4 sw=4 fdm=marker: |
|
3 |
// +----------------------------------------------------------------------+ |
|
4 |
// | PHP versions 4 and 5 | |
|
5 |
// +----------------------------------------------------------------------+ |
|
6 |
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, | |
|
7 |
// | Stig. S. Bakken, Lukas Smith | |
|
8 |
// | All rights reserved. | |
|
9 |
// +----------------------------------------------------------------------+ |
|
10 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB | |
|
11 |
// | API as well as database abstraction for PHP applications. | |
|
12 |
// | This LICENSE is in the BSD license style. | |
|
13 |
// | | |
|
14 |
// | Redistribution and use in source and binary forms, with or without | |
|
15 |
// | modification, are permitted provided that the following conditions | |
|
16 |
// | are met: | |
|
17 |
// | | |
|
18 |
// | Redistributions of source code must retain the above copyright | |
|
19 |
// | notice, this list of conditions and the following disclaimer. | |
|
20 |
// | | |
|
21 |
// | Redistributions in binary form must reproduce the above copyright | |
|
22 |
// | notice, this list of conditions and the following disclaimer in the | |
|
23 |
// | documentation and/or other materials provided with the distribution. | |
|
24 |
// | | |
|
25 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, | |
|
26 |
// | Lukas Smith nor the names of his contributors may be used to endorse | |
|
27 |
// | or promote products derived from this software without specific prior| |
|
28 |
// | written permission. | |
|
29 |
// | | |
|
30 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
31 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
32 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
|
33 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
|
34 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
|
35 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
|
36 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS| |
|
37 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
|
38 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
|
39 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY| |
|
40 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
|
41 |
// | POSSIBILITY OF SUCH DAMAGE. | |
|
42 |
// +----------------------------------------------------------------------+ |
|
43 |
// | Author: Lukas Smith <smith@pooteeweet.org> | |
|
44 |
// +----------------------------------------------------------------------+ |
|
45 |
// |
|
46 |
// $Id$ |
|
47 |
// |
|
48 |
|
|
49 |
/** |
|
50 |
* @package MDB2 |
|
51 |
* @category Database |
|
52 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
53 |
*/ |
|
54 |
|
|
55 |
require_once 'PEAR.php'; |
|
56 |
|
|
57 |
/** |
|
58 |
* The method mapErrorCode in each MDB2_dbtype implementation maps |
|
59 |
* native error codes to one of these. |
|
60 |
* |
|
61 |
* If you add an error code here, make sure you also add a textual |
|
62 |
* version of it in MDB2::errorMessage(). |
|
63 |
*/ |
|
64 |
|
|
65 |
define('MDB2_OK', 1); |
|
66 |
define('MDB2_ERROR', -1); |
|
67 |
define('MDB2_ERROR_SYNTAX', -2); |
|
68 |
define('MDB2_ERROR_CONSTRAINT', -3); |
|
69 |
define('MDB2_ERROR_NOT_FOUND', -4); |
|
70 |
define('MDB2_ERROR_ALREADY_EXISTS', -5); |
|
71 |
define('MDB2_ERROR_UNSUPPORTED', -6); |
|
72 |
define('MDB2_ERROR_MISMATCH', -7); |
|
73 |
define('MDB2_ERROR_INVALID', -8); |
|
74 |
define('MDB2_ERROR_NOT_CAPABLE', -9); |
|
75 |
define('MDB2_ERROR_TRUNCATED', -10); |
|
76 |
define('MDB2_ERROR_INVALID_NUMBER', -11); |
|
77 |
define('MDB2_ERROR_INVALID_DATE', -12); |
|
78 |
define('MDB2_ERROR_DIVZERO', -13); |
|
79 |
define('MDB2_ERROR_NODBSELECTED', -14); |
|
80 |
define('MDB2_ERROR_CANNOT_CREATE', -15); |
|
81 |
define('MDB2_ERROR_CANNOT_DELETE', -16); |
|
82 |
define('MDB2_ERROR_CANNOT_DROP', -17); |
|
83 |
define('MDB2_ERROR_NOSUCHTABLE', -18); |
|
84 |
define('MDB2_ERROR_NOSUCHFIELD', -19); |
|
85 |
define('MDB2_ERROR_NEED_MORE_DATA', -20); |
|
86 |
define('MDB2_ERROR_NOT_LOCKED', -21); |
|
87 |
define('MDB2_ERROR_VALUE_COUNT_ON_ROW', -22); |
|
88 |
define('MDB2_ERROR_INVALID_DSN', -23); |
|
89 |
define('MDB2_ERROR_CONNECT_FAILED', -24); |
|
90 |
define('MDB2_ERROR_EXTENSION_NOT_FOUND',-25); |
|
91 |
define('MDB2_ERROR_NOSUCHDB', -26); |
|
92 |
define('MDB2_ERROR_ACCESS_VIOLATION', -27); |
|
93 |
define('MDB2_ERROR_CANNOT_REPLACE', -28); |
|
94 |
define('MDB2_ERROR_CONSTRAINT_NOT_NULL',-29); |
|
95 |
define('MDB2_ERROR_DEADLOCK', -30); |
|
96 |
define('MDB2_ERROR_CANNOT_ALTER', -31); |
|
97 |
define('MDB2_ERROR_MANAGER', -32); |
|
98 |
define('MDB2_ERROR_MANAGER_PARSE', -33); |
|
99 |
define('MDB2_ERROR_LOADMODULE', -34); |
|
100 |
define('MDB2_ERROR_INSUFFICIENT_DATA', -35); |
|
101 |
|
|
102 |
/** |
|
103 |
* This is a special constant that tells MDB2 the user hasn't specified |
|
104 |
* any particular get mode, so the default should be used. |
|
105 |
*/ |
|
106 |
|
|
107 |
define('MDB2_FETCHMODE_DEFAULT', 0); |
|
108 |
|
|
109 |
/** |
|
110 |
* Column data indexed by numbers, ordered from 0 and up |
|
111 |
*/ |
|
112 |
|
|
113 |
define('MDB2_FETCHMODE_ORDERED', 1); |
|
114 |
|
|
115 |
/** |
|
116 |
* Column data indexed by column names |
|
117 |
*/ |
|
118 |
|
|
119 |
define('MDB2_FETCHMODE_ASSOC', 2); |
|
120 |
|
|
121 |
/** |
|
122 |
* Column data as object properties |
|
123 |
*/ |
|
124 |
define('MDB2_FETCHMODE_OBJECT', 3); |
|
125 |
|
|
126 |
/** |
|
127 |
* For multi-dimensional results: normally the first level of arrays |
|
128 |
* is the row number, and the second level indexed by column number or name. |
|
129 |
* MDB2_FETCHMODE_FLIPPED switches this order, so the first level of arrays |
|
130 |
* is the column name, and the second level the row number. |
|
131 |
*/ |
|
132 |
|
|
133 |
define('MDB2_FETCHMODE_FLIPPED', 4); |
|
134 |
|
|
135 |
// }}} |
|
136 |
// {{{ portability modes |
|
137 |
|
|
138 |
|
|
139 |
/** |
|
140 |
* Portability: turn off all portability features. |
|
141 |
* @see MDB2_Driver_Common::setOption() |
|
142 |
*/ |
|
143 |
define('MDB2_PORTABILITY_NONE', 0); |
|
144 |
|
|
145 |
/** |
|
146 |
* Portability: convert names of tables and fields to case defined in the |
|
147 |
* "field_case" option when using the query*(), fetch*() and tableInfo() methods. |
|
148 |
* @see MDB2_Driver_Common::setOption() |
|
149 |
*/ |
|
150 |
define('MDB2_PORTABILITY_FIX_CASE', 1); |
|
151 |
|
|
152 |
/** |
|
153 |
* Portability: right trim the data output by query*() and fetch*(). |
|
154 |
* @see MDB2_Driver_Common::setOption() |
|
155 |
*/ |
|
156 |
define('MDB2_PORTABILITY_RTRIM', 2); |
|
157 |
|
|
158 |
/** |
|
159 |
* Portability: force reporting the number of rows deleted. |
|
160 |
* @see MDB2_Driver_Common::setOption() |
|
161 |
*/ |
|
162 |
define('MDB2_PORTABILITY_DELETE_COUNT', 4); |
|
163 |
|
|
164 |
/** |
|
165 |
* Portability: not needed in MDB2 (just left here for compatibility to DB) |
|
166 |
* @see MDB2_Driver_Common::setOption() |
|
167 |
*/ |
|
168 |
define('MDB2_PORTABILITY_NUMROWS', 8); |
|
169 |
|
|
170 |
/** |
|
171 |
* Portability: makes certain error messages in certain drivers compatible |
|
172 |
* with those from other DBMS's. |
|
173 |
* |
|
174 |
* + mysql, mysqli: change unique/primary key constraints |
|
175 |
* MDB2_ERROR_ALREADY_EXISTS -> MDB2_ERROR_CONSTRAINT |
|
176 |
* |
|
177 |
* + odbc(access): MS's ODBC driver reports 'no such field' as code |
|
178 |
* 07001, which means 'too few parameters.' When this option is on |
|
179 |
* that code gets mapped to MDB2_ERROR_NOSUCHFIELD. |
|
180 |
* |
|
181 |
* @see MDB2_Driver_Common::setOption() |
|
182 |
*/ |
|
183 |
define('MDB2_PORTABILITY_ERRORS', 16); |
|
184 |
|
|
185 |
/** |
|
186 |
* Portability: convert empty values to null strings in data output by |
|
187 |
* query*() and fetch*(). |
|
188 |
* @see MDB2_Driver_Common::setOption() |
|
189 |
*/ |
|
190 |
define('MDB2_PORTABILITY_EMPTY_TO_NULL', 32); |
|
191 |
|
|
192 |
/** |
|
193 |
* Portability: removes database/table qualifiers from associative indexes |
|
194 |
* @see MDB2_Driver_Common::setOption() |
|
195 |
*/ |
|
196 |
define('MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES', 64); |
|
197 |
|
|
198 |
/** |
|
199 |
* Portability: turn on all portability features. |
|
200 |
* @see MDB2_Driver_Common::setOption() |
|
201 |
*/ |
|
202 |
define('MDB2_PORTABILITY_ALL', 127); |
|
203 |
|
|
204 |
/** |
|
205 |
* These are global variables that are used to track the various class instances |
|
206 |
*/ |
|
207 |
|
|
208 |
$GLOBALS['_MDB2_databases'] = array(); |
|
209 |
$GLOBALS['_MDB2_dsninfo_default'] = array( |
|
210 |
'phptype' => false, |
|
211 |
'dbsyntax' => false, |
|
212 |
'username' => false, |
|
213 |
'password' => false, |
|
214 |
'protocol' => false, |
|
215 |
'hostspec' => false, |
|
216 |
'port' => false, |
|
217 |
'socket' => false, |
|
218 |
'database' => false, |
|
219 |
'mode' => false, |
|
220 |
); |
|
221 |
|
|
222 |
/** |
|
223 |
* The main 'MDB2' class is simply a container class with some static |
|
224 |
* methods for creating DB objects as well as some utility functions |
|
225 |
* common to all parts of DB. |
|
226 |
* |
|
227 |
* The object model of MDB2 is as follows (indentation means inheritance): |
|
228 |
* |
|
229 |
* MDB2 The main MDB2 class. This is simply a utility class |
|
230 |
* with some 'static' methods for creating MDB2 objects as |
|
231 |
* well as common utility functions for other MDB2 classes. |
|
232 |
* |
|
233 |
* MDB2_Driver_Common The base for each MDB2 implementation. Provides default |
|
234 |
* | implementations (in OO lingo virtual methods) for |
|
235 |
* | the actual DB implementations as well as a bunch of |
|
236 |
* | query utility functions. |
|
237 |
* | |
|
238 |
* +-MDB2_mysql The MDB2 implementation for MySQL. Inherits MDB2_Driver_Common. |
|
239 |
* When calling MDB2::factory or MDB2::connect for MySQL |
|
240 |
* connections, the object returned is an instance of this |
|
241 |
* class. |
|
242 |
* +-MDB2_pgsql The MDB2 implementation for PostGreSQL. Inherits MDB2_Driver_Common. |
|
243 |
* When calling MDB2::factory or MDB2::connect for PostGreSQL |
|
244 |
* connections, the object returned is an instance of this |
|
245 |
* class. |
|
246 |
* |
|
247 |
* MDB2_Date This class provides several method to convert from and to |
|
248 |
* MDB2 timestamps. |
|
249 |
* |
|
250 |
* @package MDB2 |
|
251 |
* @category Database |
|
252 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
253 |
*/ |
|
254 |
class MDB2 |
|
255 |
{ |
|
256 |
// }}} |
|
257 |
// {{{ setOptions() |
|
258 |
|
|
259 |
/** |
|
260 |
* set option array in an exiting database object |
|
261 |
* |
|
262 |
* @param object $db MDB2 object |
|
263 |
* @param array $options An associative array of option names and their values. |
|
264 |
* @access public |
|
265 |
*/ |
|
266 |
function setOptions(&$db, $options) |
|
267 |
{ |
|
268 |
if (is_array($options)) { |
|
269 |
foreach ($options as $option => $value) { |
|
270 |
$test = $db->setOption($option, $value); |
|
271 |
if (PEAR::isError($test)) { |
|
272 |
return $test; |
|
273 |
} |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
return MDB2_OK; |
|
278 |
} |
|
279 |
|
|
280 |
// }}} |
|
281 |
// {{{ factory() |
|
282 |
|
|
283 |
/** |
|
284 |
* Create a new MDB2 object for the specified database type |
|
285 |
* |
|
286 |
* IMPORTANT: In order for MDB2 to work properly it is necessary that |
|
287 |
* you make sure that you work with a reference of the original |
|
288 |
* object instead of a copy (this is a PHP4 quirk). |
|
289 |
* |
|
290 |
* For example: |
|
291 |
* $db =& MDB2::factory($dsn); |
|
292 |
* ^^ |
|
293 |
* And not: |
|
294 |
* $db = MDB2::factory($dsn); |
|
295 |
* |
|
296 |
* @param mixed $dsn 'data source name', see the MDB2::parseDSN |
|
297 |
* method for a description of the dsn format. |
|
298 |
* Can also be specified as an array of the |
|
299 |
* format returned by MDB2::parseDSN. |
|
300 |
* @param array $options An associative array of option names and |
|
301 |
* their values. |
|
302 |
* @return mixed a newly created MDB2 object, or false on error |
|
303 |
* @access public |
|
304 |
*/ |
|
305 |
function &factory($dsn, $options = false) |
|
306 |
{ |
|
307 |
$dsninfo = MDB2::parseDSN($dsn); |
|
308 |
if (!array_key_exists('phptype', $dsninfo)) { |
|
309 |
$err =& MDB2::raiseError(MDB2_ERROR_NOT_FOUND, |
|
310 |
null, null, 'no RDBMS driver specified'); |
|
311 |
return $err; |
|
312 |
} |
|
313 |
$class_name = 'MDB2_Driver_'.$dsninfo['phptype']; |
|
314 |
|
|
315 |
if (!class_exists($class_name)) { |
|
316 |
$file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php'; |
|
317 |
if (!MDB2::fileExists($file_name)) { |
|
318 |
$err =& MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
319 |
'unable to find: '.$file_name); |
|
320 |
return $err; |
|
321 |
} |
|
322 |
if (!include_once($file_name)) { |
|
323 |
$err =& MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
324 |
'unable to load driver class: '.$file_name); |
|
325 |
return $err; |
|
326 |
} |
|
327 |
} |
|
328 |
|
|
329 |
$db =& new $class_name(); |
|
330 |
$db->setDSN($dsninfo); |
|
331 |
$err = MDB2::setOptions($db, $options); |
|
332 |
if (PEAR::isError($err)) { |
|
333 |
return $err; |
|
334 |
} |
|
335 |
|
|
336 |
return $db; |
|
337 |
} |
|
338 |
|
|
339 |
// }}} |
|
340 |
// {{{ connect() |
|
341 |
|
|
342 |
/** |
|
343 |
* Create a new MDB2 connection object and connect to the specified |
|
344 |
* database |
|
345 |
* |
|
346 |
* IMPORTANT: In order for MDB2 to work properly it is necessary that |
|
347 |
* you make sure that you work with a reference of the original |
|
348 |
* object instead of a copy (this is a PHP4 quirk). |
|
349 |
* |
|
350 |
* For example: |
|
351 |
* $db =& MDB2::connect($dsn); |
|
352 |
* ^^ |
|
353 |
* And not: |
|
354 |
* $db = MDB2::connect($dsn); |
|
355 |
* ^^ |
|
356 |
* |
|
357 |
* @param mixed $dsn 'data source name', see the MDB2::parseDSN |
|
358 |
* method for a description of the dsn format. |
|
359 |
* Can also be specified as an array of the |
|
360 |
* format returned by MDB2::parseDSN. |
|
361 |
* @param array $options An associative array of option names and |
|
362 |
* their values. |
|
363 |
* @return mixed a newly created MDB2 connection object, or a MDB2 |
|
364 |
* error object on error |
|
365 |
* @access public |
|
366 |
* @see MDB2::parseDSN |
|
367 |
*/ |
|
368 |
function &connect($dsn, $options = false) |
|
369 |
{ |
|
370 |
$db =& MDB2::factory($dsn, $options); |
|
371 |
if (PEAR::isError($db)) { |
|
372 |
return $db; |
|
373 |
} |
|
374 |
|
|
375 |
$err = $db->connect(); |
|
376 |
if (PEAR::isError($err)) { |
|
377 |
$dsn = $db->getDSN('string', 'xxx'); |
|
378 |
$db->disconnect(); |
|
379 |
$err->addUserInfo($dsn); |
|
380 |
return $err; |
|
381 |
} |
|
382 |
|
|
383 |
return $db; |
|
384 |
} |
|
385 |
|
|
386 |
// }}} |
|
387 |
// {{{ singleton() |
|
388 |
|
|
389 |
/** |
|
390 |
* Returns a MDB2 connection with the requested DSN. |
|
391 |
* A newnew MDB2 connection object is only created if no object with the |
|
392 |
* reuested DSN exists yet. |
|
393 |
* |
|
394 |
* IMPORTANT: In order for MDB2 to work properly it is necessary that |
|
395 |
* you make sure that you work with a reference of the original |
|
396 |
* object instead of a copy (this is a PHP4 quirk). |
|
397 |
* |
|
398 |
* For example: |
|
399 |
* $db =& MDB2::singleton($dsn); |
|
400 |
* ^^ |
|
401 |
* And not: |
|
402 |
* $db = MDB2::singleton($dsn); |
|
403 |
* ^^ |
|
404 |
* |
|
405 |
* @param mixed $dsn 'data source name', see the MDB2::parseDSN |
|
406 |
* method for a description of the dsn format. |
|
407 |
* Can also be specified as an array of the |
|
408 |
* format returned by MDB2::parseDSN. |
|
409 |
* @param array $options An associative array of option names and |
|
410 |
* their values. |
|
411 |
* @return mixed a newly created MDB2 connection object, or a MDB2 |
|
412 |
* error object on error |
|
413 |
* @access public |
|
414 |
* @see MDB2::parseDSN |
|
415 |
*/ |
|
416 |
function &singleton($dsn = null, $options = false) |
|
417 |
{ |
|
418 |
if ($dsn) { |
|
419 |
$dsninfo = MDB2::parseDSN($dsn); |
|
420 |
$dsninfo = array_merge($GLOBALS['_MDB2_dsninfo_default'], $dsninfo); |
|
421 |
$keys = array_keys($GLOBALS['_MDB2_databases']); |
|
422 |
for ($i=0, $j=count($keys); $i<$j; ++$i) { |
|
423 |
$tmp_dsn = $GLOBALS['_MDB2_databases'][$keys[$i]]->getDSN('array'); |
|
424 |
if (count(array_diff($tmp_dsn, $dsninfo)) == 0) { |
|
425 |
MDB2::setOptions($GLOBALS['_MDB2_databases'][$keys[$i]], $options); |
|
426 |
return $GLOBALS['_MDB2_databases'][$keys[$i]]; |
|
427 |
} |
|
428 |
} |
|
429 |
} elseif (is_array($GLOBALS['_MDB2_databases']) && reset($GLOBALS['_MDB2_databases'])) { |
|
430 |
$db =& $GLOBALS['_MDB2_databases'][key($GLOBALS['_MDB2_databases'])]; |
|
431 |
return $db; |
|
432 |
} |
|
433 |
$db =& MDB2::factory($dsn, $options); |
|
434 |
return $db; |
|
435 |
} |
|
436 |
|
|
437 |
// }}} |
|
438 |
// {{{ loadFile() |
|
439 |
|
|
440 |
/** |
|
441 |
* load a file (like 'Date') |
|
442 |
* |
|
443 |
* @param string $file name of the file in the MDB2 directory (without '.php') |
|
444 |
* @return string name of the file that was included |
|
445 |
* @access public |
|
446 |
*/ |
|
447 |
function loadFile($file) |
|
448 |
{ |
|
449 |
$file_name = 'MDB2'.DIRECTORY_SEPARATOR.$file.'.php'; |
|
450 |
if (!MDB2::fileExists($file_name)) { |
|
451 |
return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
452 |
'unable to find: '.$file_name); |
|
453 |
} |
|
454 |
if (!include_once($file_name)) { |
|
455 |
return MDB2::raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
456 |
'unable to load driver class: '.$file_name); |
|
457 |
} |
|
458 |
return $file_name; |
|
459 |
} |
|
460 |
|
|
461 |
// }}} |
|
462 |
// {{{ apiVersion() |
|
463 |
|
|
464 |
/** |
|
465 |
* Return the MDB2 API version |
|
466 |
* |
|
467 |
* @return string the MDB2 API version number |
|
468 |
* @access public |
|
469 |
*/ |
|
470 |
function apiVersion() |
|
471 |
{ |
|
472 |
return '@package_version@'; |
|
473 |
} |
|
474 |
|
|
475 |
// }}} |
|
476 |
// {{{ raiseError() |
|
477 |
|
|
478 |
/** |
|
479 |
* This method is used to communicate an error and invoke error |
|
480 |
* callbacks etc. Basically a wrapper for PEAR::raiseError |
|
481 |
* without the message string. |
|
482 |
* |
|
483 |
* @param mixed integer error code |
|
484 |
* |
|
485 |
* @param int error mode, see PEAR_Error docs |
|
486 |
* |
|
487 |
* @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the |
|
488 |
* error level (E_USER_NOTICE etc). If error mode is |
|
489 |
* PEAR_ERROR_CALLBACK, this is the callback function, |
|
490 |
* either as a function name, or as an array of an |
|
491 |
* object and method name. For other error modes this |
|
492 |
* parameter is ignored. |
|
493 |
* |
|
494 |
* @param string Extra debug information. Defaults to the last |
|
495 |
* query and native error code. |
|
496 |
* |
|
497 |
* @return object a PEAR error object |
|
498 |
* |
|
499 |
* @see PEAR_Error |
|
500 |
*/ |
|
501 |
function &raiseError($code = null, $mode = null, $options = null, $userinfo = null) |
|
502 |
{ |
|
503 |
$err =& PEAR::raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true); |
|
504 |
return $err; |
|
505 |
} |
|
506 |
|
|
507 |
// }}} |
|
508 |
// {{{ isError() |
|
509 |
|
|
510 |
/** |
|
511 |
* Tell whether a value is a MDB2 error. |
|
512 |
* |
|
513 |
* @param mixed $data the value to test |
|
514 |
* @param int $code if $data is an error object, return true |
|
515 |
* only if $code is a string and |
|
516 |
* $db->getMessage() == $code or |
|
517 |
* $code is an integer and $db->getCode() == $code |
|
518 |
* @access public |
|
519 |
* @return bool true if parameter is an error |
|
520 |
*/ |
|
521 |
function isError($data, $code = null) |
|
522 |
{ |
|
523 |
if (is_a($data, 'MDB2_Error')) { |
|
524 |
if (is_null($code)) { |
|
525 |
return true; |
|
526 |
} elseif (is_string($code)) { |
|
527 |
return $data->getMessage() === $code; |
|
528 |
} else { |
|
529 |
$code = (array)$code; |
|
530 |
return in_array($data->getCode(), $code); |
|
531 |
} |
|
532 |
} |
|
533 |
return false; |
|
534 |
} |
|
535 |
|
|
536 |
// }}} |
|
537 |
// {{{ isConnection() |
|
538 |
/** |
|
539 |
* Tell whether a value is a MDB2 connection |
|
540 |
* |
|
541 |
* @param mixed $value value to test |
|
542 |
* @return bool whether $value is a MDB2 connection |
|
543 |
* @access public |
|
544 |
*/ |
|
545 |
function isConnection($value) |
|
546 |
{ |
|
547 |
return is_a($value, 'MDB2_Driver_Common'); |
|
548 |
} |
|
549 |
|
|
550 |
// }}} |
|
551 |
// {{{ isResult() |
|
552 |
/** |
|
553 |
* Tell whether a value is a MDB2 result |
|
554 |
* |
|
555 |
* @param mixed $value value to test |
|
556 |
* @return bool whether $value is a MDB2 result |
|
557 |
* @access public |
|
558 |
*/ |
|
559 |
function isResult($value) |
|
560 |
{ |
|
561 |
return is_a($value, 'MDB2_Result'); |
|
562 |
} |
|
563 |
|
|
564 |
// }}} |
|
565 |
// {{{ isResultCommon() |
|
566 |
/** |
|
567 |
* Tell whether a value is a MDB2 result implementing the common interface |
|
568 |
* |
|
569 |
* @param mixed $value value to test |
|
570 |
* @return bool whether $value is a MDB2 result implementing the common interface |
|
571 |
* @access public |
|
572 |
*/ |
|
573 |
function isResultCommon($value) |
|
574 |
{ |
|
575 |
return is_a($value, 'MDB2_Result_Common'); |
|
576 |
} |
|
577 |
|
|
578 |
// }}} |
|
579 |
// {{{ isStatement() |
|
580 |
/** |
|
581 |
* Tell whether a value is a MDB2 statement interface |
|
582 |
* |
|
583 |
* @param mixed $value value to test |
|
584 |
* @return bool whether $value is a MDB2 statement interface |
|
585 |
* @access public |
|
586 |
*/ |
|
587 |
function isStatement($value) |
|
588 |
{ |
|
589 |
return is_a($value, 'MDB2_Statement'); |
|
590 |
} |
|
591 |
|
|
592 |
// }}} |
|
593 |
// {{{ isManip() |
|
594 |
|
|
595 |
/** |
|
596 |
* Tell whether a query is a data manipulation query (insert, |
|
597 |
* update or delete) or a data definition query (create, drop, |
|
598 |
* alter, grant, revoke). |
|
599 |
* |
|
600 |
* @param string $query the query |
|
601 |
* @return boolean whether $query is a data manipulation query |
|
602 |
* @access public |
|
603 |
*/ |
|
604 |
function isManip($query) |
|
605 |
{ |
|
606 |
$manips = 'INSERT|UPDATE|DELETE|REPLACE|' |
|
607 |
. 'CREATE|DROP|' |
|
608 |
. 'LOAD DATA|SELECT .* INTO|COPY|' |
|
609 |
. 'ALTER|GRANT|REVOKE|SET|' |
|
610 |
. 'LOCK|UNLOCK'; |
|
611 |
if (preg_match('/^\s*"?('.$manips.')\s+/i', $query)) { |
|
612 |
return true; |
|
613 |
} |
|
614 |
return false; |
|
615 |
} |
|
616 |
|
|
617 |
// }}} |
|
618 |
// {{{ errorMessage() |
|
619 |
|
|
620 |
/** |
|
621 |
* Return a textual error message for a MDB2 error code |
|
622 |
* |
|
623 |
* @param int $value error code |
|
624 |
* @return string error message, or false if the error code was |
|
625 |
* not recognized |
|
626 |
* @access public |
|
627 |
*/ |
|
628 |
function errorMessage($value) |
|
629 |
{ |
|
630 |
static $errorMessages; |
|
631 |
if (!isset($errorMessages)) { |
|
632 |
$errorMessages = array( |
|
633 |
MDB2_ERROR => 'unknown error', |
|
634 |
MDB2_ERROR_ALREADY_EXISTS => 'already exists', |
|
635 |
MDB2_ERROR_CANNOT_CREATE => 'can not create', |
|
636 |
MDB2_ERROR_CANNOT_ALTER => 'can not alter', |
|
637 |
MDB2_ERROR_CANNOT_REPLACE => 'can not replace', |
|
638 |
MDB2_ERROR_CANNOT_DELETE => 'can not delete', |
|
639 |
MDB2_ERROR_CANNOT_DROP => 'can not drop', |
|
640 |
MDB2_ERROR_CONSTRAINT => 'constraint violation', |
|
641 |
MDB2_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint', |
|
642 |
MDB2_ERROR_DIVZERO => 'division by zero', |
|
643 |
MDB2_ERROR_INVALID => 'invalid', |
|
644 |
MDB2_ERROR_INVALID_DATE => 'invalid date or time', |
|
645 |
MDB2_ERROR_INVALID_NUMBER => 'invalid number', |
|
646 |
MDB2_ERROR_MISMATCH => 'mismatch', |
|
647 |
MDB2_ERROR_NODBSELECTED => 'no database selected', |
|
648 |
MDB2_ERROR_NOSUCHFIELD => 'no such field', |
|
649 |
MDB2_ERROR_NOSUCHTABLE => 'no such table', |
|
650 |
MDB2_ERROR_NOT_CAPABLE => 'MDB2 backend not capable', |
|
651 |
MDB2_ERROR_NOT_FOUND => 'not found', |
|
652 |
MDB2_ERROR_NOT_LOCKED => 'not locked', |
|
653 |
MDB2_ERROR_SYNTAX => 'syntax error', |
|
654 |
MDB2_ERROR_UNSUPPORTED => 'not supported', |
|
655 |
MDB2_ERROR_VALUE_COUNT_ON_ROW => 'value count on row', |
|
656 |
MDB2_ERROR_INVALID_DSN => 'invalid DSN', |
|
657 |
MDB2_ERROR_CONNECT_FAILED => 'connect failed', |
|
658 |
MDB2_OK => 'no error', |
|
659 |
MDB2_ERROR_NEED_MORE_DATA => 'insufficient data supplied', |
|
660 |
MDB2_ERROR_EXTENSION_NOT_FOUND=> 'extension not found', |
|
661 |
MDB2_ERROR_NOSUCHDB => 'no such database', |
|
662 |
MDB2_ERROR_ACCESS_VIOLATION => 'insufficient permissions', |
|
663 |
MDB2_ERROR_LOADMODULE => 'error while including on demand module', |
|
664 |
MDB2_ERROR_TRUNCATED => 'truncated', |
|
665 |
MDB2_ERROR_DEADLOCK => 'deadlock detected', |
|
666 |
); |
|
667 |
} |
|
668 |
|
|
669 |
if (PEAR::isError($value)) { |
|
670 |
$value = $value->getCode(); |
|
671 |
} |
|
672 |
|
|
673 |
return isset($errorMessages[$value]) ? |
|
674 |
$errorMessages[$value] : $errorMessages[MDB2_ERROR]; |
|
675 |
} |
|
676 |
|
|
677 |
// }}} |
|
678 |
// {{{ parseDSN() |
|
679 |
|
|
680 |
/** |
|
681 |
* Parse a data source name. |
|
682 |
* |
|
683 |
* Additional keys can be added by appending a URI query string to the |
|
684 |
* end of the DSN. |
|
685 |
* |
|
686 |
* The format of the supplied DSN is in its fullest form: |
|
687 |
* <code> |
|
688 |
* phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true |
|
689 |
* </code> |
|
690 |
* |
|
691 |
* Most variations are allowed: |
|
692 |
* <code> |
|
693 |
* phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644 |
|
694 |
* phptype://username:password@hostspec/database_name |
|
695 |
* phptype://username:password@hostspec |
|
696 |
* phptype://username@hostspec |
|
697 |
* phptype://hostspec/database |
|
698 |
* phptype://hostspec |
|
699 |
* phptype(dbsyntax) |
|
700 |
* phptype |
|
701 |
* </code> |
|
702 |
* |
|
703 |
* @param string $dsn Data Source Name to be parsed |
|
704 |
* |
|
705 |
* @return array an associative array with the following keys: |
|
706 |
* + phptype: Database backend used in PHP (mysql, odbc etc.) |
|
707 |
* + dbsyntax: Database used with regards to SQL syntax etc. |
|
708 |
* + protocol: Communication protocol to use (tcp, unix etc.) |
|
709 |
* + hostspec: Host specification (hostname[:port]) |
|
710 |
* + database: Database to use on the DBMS server |
|
711 |
* + username: User name for login |
|
712 |
* + password: Password for login |
|
713 |
* |
|
714 |
* @author Tomas V.V.Cox <cox@idecnet.com> |
|
715 |
*/ |
|
716 |
function parseDSN($dsn) |
|
717 |
{ |
|
718 |
$parsed = $GLOBALS['_MDB2_dsninfo_default']; |
|
719 |
|
|
720 |
if (is_array($dsn)) { |
|
721 |
$dsn = array_merge($parsed, $dsn); |
|
722 |
if (!$dsn['dbsyntax']) { |
|
723 |
$dsn['dbsyntax'] = $dsn['phptype']; |
|
724 |
} |
|
725 |
return $dsn; |
|
726 |
} |
|
727 |
|
|
728 |
// Find phptype and dbsyntax |
|
729 |
if (($pos = strpos($dsn, '://')) !== false) { |
|
730 |
$str = substr($dsn, 0, $pos); |
|
731 |
$dsn = substr($dsn, $pos + 3); |
|
732 |
} else { |
|
733 |
$str = $dsn; |
|
734 |
$dsn = null; |
|
735 |
} |
|
736 |
|
|
737 |
// Get phptype and dbsyntax |
|
738 |
// $str => phptype(dbsyntax) |
|
739 |
if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) { |
|
740 |
$parsed['phptype'] = $arr[1]; |
|
741 |
$parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2]; |
|
742 |
} else { |
|
743 |
$parsed['phptype'] = $str; |
|
744 |
$parsed['dbsyntax'] = $str; |
|
745 |
} |
|
746 |
|
|
747 |
if (!count($dsn)) { |
|
748 |
return $parsed; |
|
749 |
} |
|
750 |
|
|
751 |
// Get (if found): username and password |
|
752 |
// $dsn => username:password@protocol+hostspec/database |
|
753 |
if (($at = strrpos($dsn,'@')) !== false) { |
|
754 |
$str = substr($dsn, 0, $at); |
|
755 |
$dsn = substr($dsn, $at + 1); |
|
756 |
if (($pos = strpos($str, ':')) !== false) { |
|
757 |
$parsed['username'] = rawurldecode(substr($str, 0, $pos)); |
|
758 |
$parsed['password'] = rawurldecode(substr($str, $pos + 1)); |
|
759 |
} else { |
|
760 |
$parsed['username'] = rawurldecode($str); |
|
761 |
} |
|
762 |
} |
|
763 |
|
|
764 |
// Find protocol and hostspec |
|
765 |
|
|
766 |
// $dsn => proto(proto_opts)/database |
|
767 |
if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) { |
|
768 |
$proto = $match[1]; |
|
769 |
$proto_opts = $match[2] ? $match[2] : false; |
|
770 |
$dsn = $match[3]; |
|
771 |
|
|
772 |
// $dsn => protocol+hostspec/database (old format) |
|
773 |
} else { |
|
774 |
if (strpos($dsn, '+') !== false) { |
|
775 |
list($proto, $dsn) = explode('+', $dsn, 2); |
|
776 |
} |
|
777 |
if (strpos($dsn, '/') !== false) { |
|
778 |
list($proto_opts, $dsn) = explode('/', $dsn, 2); |
|
779 |
} else { |
|
780 |
$proto_opts = $dsn; |
|
781 |
$dsn = null; |
|
782 |
} |
|
783 |
} |
|
784 |
|
|
785 |
// process the different protocol options |
|
786 |
$parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp'; |
|
787 |
$proto_opts = rawurldecode($proto_opts); |
|
788 |
if ($parsed['protocol'] == 'tcp') { |
|
789 |
if (strpos($proto_opts, ':') !== false) { |
|
790 |
list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts); |
|
791 |
} else { |
|
792 |
$parsed['hostspec'] = $proto_opts; |
|
793 |
} |
|
794 |
} elseif ($parsed['protocol'] == 'unix') { |
|
795 |
$parsed['socket'] = $proto_opts; |
|
796 |
} |
|
797 |
|
|
798 |
// Get dabase if any |
|
799 |
// $dsn => database |
|
800 |
if ($dsn) { |
|
801 |
// /database |
|
802 |
if (($pos = strpos($dsn, '?')) === false) { |
|
803 |
$parsed['database'] = $dsn; |
|
804 |
// /database?param1=value1¶m2=value2 |
|
805 |
} else { |
|
806 |
$parsed['database'] = substr($dsn, 0, $pos); |
|
807 |
$dsn = substr($dsn, $pos + 1); |
|
808 |
if (strpos($dsn, '&') !== false) { |
|
809 |
$opts = explode('&', $dsn); |
|
810 |
} else { // database?param1=value1 |
|
811 |
$opts = array($dsn); |
|
812 |
} |
|
813 |
foreach ($opts as $opt) { |
|
814 |
list($key, $value) = explode('=', $opt); |
|
815 |
if (!isset($parsed[$key])) { |
|
816 |
// don't allow params overwrite |
|
817 |
$parsed[$key] = rawurldecode($value); |
|
818 |
} |
|
819 |
} |
|
820 |
} |
|
821 |
} |
|
822 |
|
|
823 |
return $parsed; |
|
824 |
} |
|
825 |
|
|
826 |
// }}} |
|
827 |
// {{{ fileExists() |
|
828 |
|
|
829 |
/** |
|
830 |
* checks if a file exists in the include path |
|
831 |
* |
|
832 |
* @access public |
|
833 |
* @param string filename |
|
834 |
* |
|
835 |
* @return boolean true success and false on error |
|
836 |
*/ |
|
837 |
function fileExists($file) |
|
838 |
{ |
|
839 |
$dirs = explode(PATH_SEPARATOR, ini_get('include_path')); |
|
840 |
foreach ($dirs as $dir) { |
|
841 |
if (is_readable($dir . DIRECTORY_SEPARATOR . $file)) { |
|
842 |
return true; |
|
843 |
} |
|
844 |
} |
|
845 |
return false; |
|
846 |
} |
|
847 |
} |
|
848 |
|
|
849 |
/** |
|
850 |
* MDB2_Error implements a class for reporting portable database error |
|
851 |
* messages. |
|
852 |
* |
|
853 |
* @package MDB2 |
|
854 |
* @category Database |
|
855 |
* @author Stig Bakken <ssb@fast.no> |
|
856 |
*/ |
|
857 |
class MDB2_Error extends PEAR_Error |
|
858 |
{ |
|
859 |
// }}} |
|
860 |
// {{{ constructor |
|
861 |
|
|
862 |
/** |
|
863 |
* MDB2_Error constructor. |
|
864 |
* |
|
865 |
* @param mixed $code MDB error code, or string with error message. |
|
866 |
* @param integer $mode what 'error mode' to operate in |
|
867 |
* @param integer $level what error level to use for |
|
868 |
* $mode & PEAR_ERROR_TRIGGER |
|
869 |
* @param smixed $debuginfo additional debug info, such as the last query |
|
870 |
*/ |
|
871 |
function MDB2_Error($code = MDB2_ERROR, $mode = PEAR_ERROR_RETURN, |
|
872 |
$level = E_USER_NOTICE, $debuginfo = null) |
|
873 |
{ |
|
874 |
$this->PEAR_Error('MDB2 Error: '.MDB2::errorMessage($code), $code, |
|
875 |
$mode, $level, $debuginfo); |
|
876 |
} |
|
877 |
} |
|
878 |
|
|
879 |
/** |
|
880 |
* MDB2_Driver_Common: Base class that is extended by each MDB2 driver |
|
881 |
* |
|
882 |
* @package MDB2 |
|
883 |
* @category Database |
|
884 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
885 |
*/ |
|
886 |
class MDB2_Driver_Common extends PEAR |
|
887 |
{ |
|
888 |
/** |
|
889 |
* index of the MDB2 object within the $GLOBALS['_MDB2_databases'] array |
|
890 |
* @var integer |
|
891 |
* @access public |
|
892 |
*/ |
|
893 |
var $db_index = 0; |
|
894 |
|
|
895 |
/** |
|
896 |
* DSN used for the next query |
|
897 |
* @var array |
|
898 |
* @access protected |
|
899 |
*/ |
|
900 |
var $dsn = array(); |
|
901 |
|
|
902 |
/** |
|
903 |
* DSN that was used to create the current connection |
|
904 |
* @var array |
|
905 |
* @access protected |
|
906 |
*/ |
|
907 |
var $connected_dsn = array(); |
|
908 |
|
|
909 |
/** |
|
910 |
* connection resource |
|
911 |
* @var mixed |
|
912 |
* @access protected |
|
913 |
*/ |
|
914 |
var $connection = 0; |
|
915 |
|
|
916 |
/** |
|
917 |
* if the current opened connection is a persistent connection |
|
918 |
* @var boolean |
|
919 |
* @access protected |
|
920 |
*/ |
|
921 |
var $opened_persistent; |
|
922 |
|
|
923 |
/** |
|
924 |
* the name of the database for the next query |
|
925 |
* @var string |
|
926 |
* @access protected |
|
927 |
*/ |
|
928 |
var $database_name = ''; |
|
929 |
|
|
930 |
/** |
|
931 |
* the name of the database currrently selected |
|
932 |
* @var string |
|
933 |
* @access protected |
|
934 |
*/ |
|
935 |
var $connected_database_name = ''; |
|
936 |
|
|
937 |
/** |
|
938 |
* list of all supported features of the given driver |
|
939 |
* @var array |
|
940 |
* @access public |
|
941 |
*/ |
|
942 |
var $supported = array( |
|
943 |
'sequences' => false, |
|
944 |
'indexes' => false, |
|
945 |
'affected_rows' => false, |
|
946 |
'summary_functions' => false, |
|
947 |
'order_by_text' => false, |
|
948 |
'transactions' => false, |
|
949 |
'current_id' => false, |
|
950 |
'limit_queries' => false, |
|
951 |
'LOBs' => false, |
|
952 |
'replace' => false, |
|
953 |
'sub_selects' => false, |
|
954 |
'auto_increment' => false, |
|
955 |
'primary_key' => false, |
|
956 |
); |
|
957 |
|
|
958 |
/** |
|
959 |
* $options['ssl'] -> determines if ssl should be used for connections |
|
960 |
* $options['field_case'] -> determines what case to force on field/table names |
|
961 |
* $options['disable_query'] -> determines if querys should be executed |
|
962 |
* $options['result_class'] -> class used for result sets |
|
963 |
* $options['buffered_result_class'] -> class used for buffered result sets |
|
964 |
* $options['result_wrap_class'] -> class used to wrap result sets into |
|
965 |
* $options['result_buffering'] -> boolean|integer should results be buffered or not? |
|
966 |
* $options['fetch_class'] -> class to use when fetch mode object is used |
|
967 |
* $options['persistent'] -> boolean persistent connection? |
|
968 |
* $options['debug'] -> integer numeric debug level |
|
969 |
* $options['debug_handler'] -> string function/meothd that captures debug messages |
|
970 |
* $options['lob_buffer_length'] -> integer LOB buffer length |
|
971 |
* $options['log_line_break'] -> string line-break format |
|
972 |
* $options['seqname_format'] -> string pattern for sequence name |
|
973 |
* $options['seqcol_name'] -> string sequence column name |
|
974 |
* $options['use_transactions'] -> boolean |
|
975 |
* $options['decimal_places'] -> integer |
|
976 |
* $options['portability'] -> portability constant |
|
977 |
* $options['modules'] -> short to long module name mapping for __call() |
|
978 |
* @var array |
|
979 |
* @access public |
|
980 |
*/ |
|
981 |
var $options = array( |
|
982 |
'ssl' => false, |
|
983 |
'field_case' => CASE_LOWER, |
|
984 |
'disable_query' => false, |
|
985 |
'result_class' => 'MDB2_Result_%s', |
|
986 |
'buffered_result_class' => 'MDB2_BufferedResult_%s', |
|
987 |
'result_wrap_class' => false, |
|
988 |
'result_buffering' => true, |
|
989 |
'fetch_class' => 'stdClass', |
|
990 |
'persistent' => false, |
|
991 |
'debug' => 0, |
|
992 |
'debug_handler' => 'MDB2_defaultDebugOutput', |
|
993 |
'lob_buffer_length' => 8192, |
|
994 |
'log_line_break' => "\n", |
|
995 |
'seqname_format' => '%s_seq', |
|
996 |
'seqcol_name' => 'sequence', |
|
997 |
'use_transactions' => false, |
|
998 |
'decimal_places' => 2, |
|
999 |
'portability' => MDB2_PORTABILITY_ALL, |
|
1000 |
'modules' => array( |
|
1001 |
'ex' => 'Extended', |
|
1002 |
'dt' => 'Datatype', |
|
1003 |
'mg' => 'Manager', |
|
1004 |
'rv' => 'Reverse', |
|
1005 |
'na' => 'Native', |
|
1006 |
), |
|
1007 |
); |
|
1008 |
|
|
1009 |
/** |
|
1010 |
* escape character |
|
1011 |
* @var string |
|
1012 |
* @access protected |
|
1013 |
*/ |
|
1014 |
var $escape_quotes = ''; |
|
1015 |
|
|
1016 |
/** |
|
1017 |
* warnings |
|
1018 |
* @var array |
|
1019 |
* @access protected |
|
1020 |
*/ |
|
1021 |
var $warnings = array(); |
|
1022 |
|
|
1023 |
/** |
|
1024 |
* string with the debugging information |
|
1025 |
* @var string |
|
1026 |
* @access public |
|
1027 |
*/ |
|
1028 |
var $debug_output = ''; |
|
1029 |
|
|
1030 |
/** |
|
1031 |
* determine if there is an open transaction |
|
1032 |
* @var boolean |
|
1033 |
* @access protected |
|
1034 |
*/ |
|
1035 |
var $in_transaction = false; |
|
1036 |
|
|
1037 |
/** |
|
1038 |
* result offset used in the next query |
|
1039 |
* @var integer |
|
1040 |
* @access protected |
|
1041 |
*/ |
|
1042 |
var $row_offset = 0; |
|
1043 |
|
|
1044 |
/** |
|
1045 |
* result limit used in the next query |
|
1046 |
* @var integer |
|
1047 |
* @access protected |
|
1048 |
*/ |
|
1049 |
var $row_limit = 0; |
|
1050 |
|
|
1051 |
/** |
|
1052 |
* Database backend used in PHP (mysql, odbc etc.) |
|
1053 |
* @var string |
|
1054 |
* @access protected |
|
1055 |
*/ |
|
1056 |
var $phptype; |
|
1057 |
|
|
1058 |
/** |
|
1059 |
* Database used with regards to SQL syntax etc. |
|
1060 |
* @var string |
|
1061 |
* @access protected |
|
1062 |
*/ |
|
1063 |
var $dbsyntax; |
|
1064 |
|
|
1065 |
/** |
|
1066 |
* the last query sent to the driver |
|
1067 |
* @var string |
|
1068 |
* @access public |
|
1069 |
*/ |
|
1070 |
var $last_query; |
|
1071 |
|
|
1072 |
/** |
|
1073 |
* the default fetchmode used |
|
1074 |
* @var integer |
|
1075 |
* @access protected |
|
1076 |
*/ |
|
1077 |
var $fetchmode = MDB2_FETCHMODE_ORDERED; |
|
1078 |
|
|
1079 |
/** |
|
1080 |
* array of module instances |
|
1081 |
* @var array |
|
1082 |
* @access protected |
|
1083 |
*/ |
|
1084 |
var $modules = array(); |
|
1085 |
|
|
1086 |
/** |
|
1087 |
* determines of the PHP4 destructor emulation has been enabled yet |
|
1088 |
* @var array |
|
1089 |
* @access protected |
|
1090 |
*/ |
|
1091 |
var $destructor_registered = true; |
|
1092 |
|
|
1093 |
// }}} |
|
1094 |
// {{{ constructor |
|
1095 |
|
|
1096 |
/** |
|
1097 |
* Constructor |
|
1098 |
*/ |
|
1099 |
function __construct() |
|
1100 |
{ |
|
1101 |
end($GLOBALS['_MDB2_databases']); |
|
1102 |
$db_index = key($GLOBALS['_MDB2_databases']) + 1; |
|
1103 |
$GLOBALS['_MDB2_databases'][$db_index] = &$this; |
|
1104 |
$this->db_index = $db_index; |
|
1105 |
} |
|
1106 |
|
|
1107 |
// }}} |
|
1108 |
// {{{ MDB2_Driver_Common |
|
1109 |
|
|
1110 |
/** |
|
1111 |
* PHP 4 Constructor |
|
1112 |
*/ |
|
1113 |
function MDB2_Driver_Common() |
|
1114 |
{ |
|
1115 |
$this->destructor_registered = false; |
|
1116 |
$this->__construct(); |
|
1117 |
} |
|
1118 |
|
|
1119 |
// }}} |
|
1120 |
// {{{ Destructor |
|
1121 |
|
|
1122 |
/** |
|
1123 |
* Destructor |
|
1124 |
*/ |
|
1125 |
function __destruct() |
|
1126 |
{ |
|
1127 |
if ($this->connection) { |
|
1128 |
if ($this->opened_persistent) { |
|
1129 |
if ($this->in_transaction) { |
|
1130 |
$this->rollback(); |
|
1131 |
} |
|
1132 |
} else { |
|
1133 |
$this->disconnect(); |
|
1134 |
} |
|
1135 |
} |
|
1136 |
} |
|
1137 |
|
|
1138 |
// }}} |
|
1139 |
// {{{ free() |
|
1140 |
|
|
1141 |
/** |
|
1142 |
* Free the internal references so that the instance can be destroyed |
|
1143 |
* |
|
1144 |
* @return boolean true on success, false if result is invalid |
|
1145 |
* @access public |
|
1146 |
*/ |
|
1147 |
function free() |
|
1148 |
{ |
|
1149 |
unset($GLOBALS['_MDB2_databases'][$this->db_index]); |
|
1150 |
unset($this->db_index); |
|
1151 |
return MDB2_OK; |
|
1152 |
} |
|
1153 |
|
|
1154 |
// }}} |
|
1155 |
// {{{ __toString() |
|
1156 |
|
|
1157 |
/** |
|
1158 |
* String conversation |
|
1159 |
* |
|
1160 |
* @return string |
|
1161 |
* @access public |
|
1162 |
*/ |
|
1163 |
function __toString() |
|
1164 |
{ |
|
1165 |
$info = get_class($this); |
|
1166 |
$info.= ': (phptype = '.$this->phptype.', dbsyntax = '.$this->dbsyntax.')'; |
|
1167 |
if ($this->connection) { |
|
1168 |
$info.= ' [connected]'; |
|
1169 |
} |
|
1170 |
return $info; |
|
1171 |
} |
|
1172 |
|
|
1173 |
// }}} |
|
1174 |
// {{{ errorInfo() |
|
1175 |
|
|
1176 |
/** |
|
1177 |
* This method is used to collect information about an error |
|
1178 |
* |
|
1179 |
* @param integer $error |
|
1180 |
* @return array |
|
1181 |
* @access public |
|
1182 |
*/ |
|
1183 |
function errorInfo($error = null) |
|
1184 |
{ |
|
1185 |
return array($error, null, null); |
|
1186 |
} |
|
1187 |
|
|
1188 |
// }}} |
|
1189 |
// {{{ raiseError() |
|
1190 |
|
|
1191 |
/** |
|
1192 |
* This method is used to communicate an error and invoke error |
|
1193 |
* callbacks etc. Basically a wrapper for PEAR::raiseError |
|
1194 |
* without the message string. |
|
1195 |
* |
|
1196 |
* @param mixed integer error code, or a PEAR error object (all |
|
1197 |
* other parameters are ignored if this parameter is |
|
1198 |
* an object |
|
1199 |
* |
|
1200 |
* @param int error mode, see PEAR_Error docs |
|
1201 |
* |
|
1202 |
* @param mixed If error mode is PEAR_ERROR_TRIGGER, this is the |
|
1203 |
* error level (E_USER_NOTICE etc). If error mode is |
|
1204 |
* PEAR_ERROR_CALLBACK, this is the callback function, |
|
1205 |
* either as a function name, or as an array of an |
|
1206 |
* object and method name. For other error modes this |
|
1207 |
* parameter is ignored. |
|
1208 |
* |
|
1209 |
* @param string Extra debug information. Defaults to the last |
|
1210 |
* query and native error code. |
|
1211 |
* |
|
1212 |
* @return object a PEAR error object |
|
1213 |
* |
|
1214 |
* @see PEAR_Error |
|
1215 |
*/ |
|
1216 |
function &raiseError($code = null, $mode = null, $options = null, $userinfo = null) |
|
1217 |
{ |
|
1218 |
// The error is yet a MDB2 error object |
|
1219 |
if (PEAR::isError($code)) { |
|
1220 |
// because we use the static PEAR::raiseError, our global |
|
1221 |
// handler should be used if it is set |
|
1222 |
if (is_null($mode) && !empty($this->_default_error_mode)) { |
|
1223 |
$mode = $this->_default_error_mode; |
|
1224 |
$options = $this->_default_error_options; |
|
1225 |
} |
|
1226 |
} else { |
|
1227 |
if (is_null($userinfo) && isset($this->connection)) { |
|
1228 |
if (!empty($this->last_query)) { |
|
1229 |
$userinfo = "[Last query: {$this->last_query}]\n"; |
|
1230 |
} |
|
1231 |
$native_errno = $native_msg = null; |
|
1232 |
list($code, $native_errno, $native_msg) = $this->errorInfo($code); |
|
1233 |
if (!is_null($native_errno)) { |
|
1234 |
$userinfo.= "[Native code: $native_errno]\n"; |
|
1235 |
} |
|
1236 |
if (!is_null($native_msg)) { |
|
1237 |
$userinfo.= "[Native message: ". strip_tags($native_msg) ."]\n"; |
|
1238 |
} |
|
1239 |
} else { |
|
1240 |
$userinfo = "[Error message: $userinfo]\n"; |
|
1241 |
} |
|
1242 |
} |
|
1243 |
|
|
1244 |
$err =& PEAR::raiseError(null, $code, $mode, $options, $userinfo, 'MDB2_Error', true); |
|
1245 |
return $err; |
|
1246 |
} |
|
1247 |
|
|
1248 |
// }}} |
|
1249 |
// {{{ errorNative() |
|
1250 |
|
|
1251 |
/** |
|
1252 |
* returns an errormessage, provides by the database |
|
1253 |
* |
|
1254 |
* @return mixed MDB2 Error Object or message |
|
1255 |
* @access public |
|
1256 |
*/ |
|
1257 |
function errorNative() |
|
1258 |
{ |
|
1259 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED); |
|
1260 |
} |
|
1261 |
|
|
1262 |
// }}} |
|
1263 |
// {{{ resetWarnings() |
|
1264 |
|
|
1265 |
/** |
|
1266 |
* reset the warning array |
|
1267 |
* |
|
1268 |
* @access public |
|
1269 |
*/ |
|
1270 |
function resetWarnings() |
|
1271 |
{ |
|
1272 |
$this->warnings = array(); |
|
1273 |
} |
|
1274 |
|
|
1275 |
// }}} |
|
1276 |
// {{{ getWarnings() |
|
1277 |
|
|
1278 |
/** |
|
1279 |
* get all warnings in reverse order. |
|
1280 |
* This means that the last warning is the first element in the array |
|
1281 |
* |
|
1282 |
* @return array with warnings |
|
1283 |
* @access public |
|
1284 |
* @see resetWarnings() |
|
1285 |
*/ |
|
1286 |
function getWarnings() |
|
1287 |
{ |
|
1288 |
return array_reverse($this->warnings); |
|
1289 |
} |
|
1290 |
|
|
1291 |
// }}} |
|
1292 |
// {{{ setFetchMode() |
|
1293 |
|
|
1294 |
/** |
|
1295 |
* Sets which fetch mode should be used by default on queries |
|
1296 |
* on this connection |
|
1297 |
* |
|
1298 |
* @param integer $fetchmode MDB2_FETCHMODE_ORDERED, MDB2_FETCHMODE_ASSOC |
|
1299 |
* or MDB2_FETCHMODE_OBJECT |
|
1300 |
* @param string $object_class the class name of the object to be returned |
|
1301 |
* by the fetch methods when the |
|
1302 |
* MDB2_FETCHMODE_OBJECT mode is selected. |
|
1303 |
* If no class is specified by default a cast |
|
1304 |
* to object from the assoc array row will be |
|
1305 |
* done. There is also the posibility to use |
|
1306 |
* and extend the 'MDB2_row' class. |
|
1307 |
* |
|
1308 |
* @see MDB2_FETCHMODE_ORDERED, MDB2_FETCHMODE_ASSOC, MDB2_FETCHMODE_OBJECT |
|
1309 |
* @access public |
|
1310 |
*/ |
|
1311 |
function setFetchMode($fetchmode, $object_class = 'stdClass') |
|
1312 |
{ |
|
1313 |
switch ($fetchmode) { |
|
1314 |
case MDB2_FETCHMODE_OBJECT: |
|
1315 |
$this->options['fetch_class'] = $object_class; |
|
1316 |
case MDB2_FETCHMODE_ORDERED: |
|
1317 |
case MDB2_FETCHMODE_ASSOC: |
|
1318 |
$this->fetchmode = $fetchmode; |
|
1319 |
break; |
|
1320 |
default: |
|
1321 |
return $this->raiseError('invalid fetchmode mode'); |
|
1322 |
} |
|
1323 |
} |
|
1324 |
|
|
1325 |
// }}} |
|
1326 |
// {{{ setOption() |
|
1327 |
|
|
1328 |
/** |
|
1329 |
* set the option for the db class |
|
1330 |
* |
|
1331 |
* @param string $option option name |
|
1332 |
* @param mixed $value value for the option |
|
1333 |
* @return mixed MDB2_OK or MDB2 Error Object |
|
1334 |
* @access public |
|
1335 |
*/ |
|
1336 |
function setOption($option, $value) |
|
1337 |
{ |
|
1338 |
if (array_key_exists($option, $this->options)) { |
|
1339 |
$this->options[$option] = $value; |
|
1340 |
return MDB2_OK; |
|
1341 |
} |
|
1342 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1343 |
"unknown option $option"); |
|
1344 |
} |
|
1345 |
|
|
1346 |
// }}} |
|
1347 |
// {{{ getOption() |
|
1348 |
|
|
1349 |
/** |
|
1350 |
* returns the value of an option |
|
1351 |
* |
|
1352 |
* @param string $option option name |
|
1353 |
* @return mixed the option value or error object |
|
1354 |
* @access public |
|
1355 |
*/ |
|
1356 |
function getOption($option) |
|
1357 |
{ |
|
1358 |
if (array_key_exists($option, $this->options)) { |
|
1359 |
return $this->options[$option]; |
|
1360 |
} |
|
1361 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1362 |
"unknown option $option"); |
|
1363 |
} |
|
1364 |
|
|
1365 |
// }}} |
|
1366 |
// {{{ debug() |
|
1367 |
|
|
1368 |
/** |
|
1369 |
* set a debug message |
|
1370 |
* |
|
1371 |
* @param string $message Message with information for the user. |
|
1372 |
* @access public |
|
1373 |
*/ |
|
1374 |
function debug($message, $scope = '') |
|
1375 |
{ |
|
1376 |
if ($this->options['debug'] && $this->options['debug_handler']) { |
|
1377 |
call_user_func_array($this->options['debug_handler'], array(&$this, $scope, $message)); |
|
1378 |
} |
|
1379 |
} |
|
1380 |
|
|
1381 |
// }}} |
|
1382 |
// {{{ debugOutput() |
|
1383 |
|
|
1384 |
/** |
|
1385 |
* output debug info |
|
1386 |
* |
|
1387 |
* @return string content of the debug_output class variable |
|
1388 |
* @access public |
|
1389 |
*/ |
|
1390 |
function debugOutput() |
|
1391 |
{ |
|
1392 |
return $this->debug_output; |
|
1393 |
} |
|
1394 |
|
|
1395 |
// }}} |
|
1396 |
// {{{ escape() |
|
1397 |
|
|
1398 |
/** |
|
1399 |
* Quotes a string so it can be safely used in a query. It will quote |
|
1400 |
* the text so it can safely be used within a query. |
|
1401 |
* |
|
1402 |
* @param string $text the input string to quote |
|
1403 |
* @return string quoted string |
|
1404 |
* @access public |
|
1405 |
*/ |
|
1406 |
function escape($text) |
|
1407 |
{ |
|
1408 |
if ($this->escape_quotes !== "'") { |
|
1409 |
$text = str_replace($this->escape_quotes, $this->escape_quotes.$this->escape_quotes, $text); |
|
1410 |
} |
|
1411 |
return str_replace("'", $this->escape_quotes . "'", $text); |
|
1412 |
} |
|
1413 |
|
|
1414 |
// }}} |
|
1415 |
// {{{ quoteIdentifier() |
|
1416 |
|
|
1417 |
/** |
|
1418 |
* Quote a string so it can be safely used as a table or column name |
|
1419 |
* |
|
1420 |
* Delimiting style depends on which database driver is being used. |
|
1421 |
* |
|
1422 |
* NOTE: just because you CAN use delimited identifiers doesn't mean |
|
1423 |
* you SHOULD use them. In general, they end up causing way more |
|
1424 |
* problems than they solve. |
|
1425 |
* |
|
1426 |
* Portability is broken by using the following characters inside |
|
1427 |
* delimited identifiers: |
|
1428 |
* + backtick (<kbd>`</kbd>) -- due to MySQL |
|
1429 |
* + double quote (<kbd>"</kbd>) -- due to Oracle |
|
1430 |
* + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access |
|
1431 |
* |
|
1432 |
* Delimited identifiers are known to generally work correctly under |
|
1433 |
* the following drivers: |
|
1434 |
* + mssql |
|
1435 |
* + mysql |
|
1436 |
* + mysqli |
|
1437 |
* + oci8 |
|
1438 |
* + odbc(access) |
|
1439 |
* + odbc(db2) |
|
1440 |
* + pgsql |
|
1441 |
* + sqlite |
|
1442 |
* + sybase |
|
1443 |
* |
|
1444 |
* InterBase doesn't seem to be able to use delimited identifiers |
|
1445 |
* via PHP 4. They work fine under PHP 5. |
|
1446 |
* |
|
1447 |
* @param string $str identifier name to be quoted |
|
1448 |
* |
|
1449 |
* @return string quoted identifier string |
|
1450 |
* |
|
1451 |
* @access public |
|
1452 |
*/ |
|
1453 |
function quoteIdentifier($str) |
|
1454 |
{ |
|
1455 |
return '"' . str_replace('"', '""', $str) . '"'; |
|
1456 |
} |
|
1457 |
|
|
1458 |
// }}} |
|
1459 |
// {{{ _fixResultArrayValues() |
|
1460 |
|
|
1461 |
/** |
|
1462 |
* Do all necessary conversions on result arrays to fix DBMS quirks |
|
1463 |
* |
|
1464 |
* @param array $array the array to be fixed (passed by reference) |
|
1465 |
* @return void |
|
1466 |
* @access protected |
|
1467 |
*/ |
|
1468 |
function _fixResultArrayValues(&$array, $mode) |
|
1469 |
{ |
|
1470 |
switch ($mode) { |
|
1471 |
case MDB2_PORTABILITY_RTRIM: |
|
1472 |
foreach ($array as $key => $value) { |
|
1473 |
if (is_string($value)) { |
|
1474 |
$array[$key] = rtrim($value); |
|
1475 |
} |
|
1476 |
} |
|
1477 |
case MDB2_PORTABILITY_EMPTY_TO_NULL: |
|
1478 |
foreach ($array as $key => $value) { |
|
1479 |
if ($value === '') { |
|
1480 |
$array[$key] = null; |
|
1481 |
} |
|
1482 |
} |
|
1483 |
case MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES: |
|
1484 |
$tmp_array = array(); |
|
1485 |
foreach ($array as $key => $value) { |
|
1486 |
$tmp_array[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value; |
|
1487 |
} |
|
1488 |
$array = $tmp_array; |
|
1489 |
case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_EMPTY_TO_NULL): |
|
1490 |
foreach ($array as $key => $value) { |
|
1491 |
if ($value === '') { |
|
1492 |
$array[$key] = null; |
|
1493 |
} elseif (is_string($value)) { |
|
1494 |
$array[$key] = rtrim($value); |
|
1495 |
} |
|
1496 |
} |
|
1497 |
case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES): |
|
1498 |
$tmp_array = array(); |
|
1499 |
foreach ($array as $key => $value) { |
|
1500 |
if (is_string($value)) { |
|
1501 |
$value = rtrim($value); |
|
1502 |
} |
|
1503 |
$tmp_array[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value; |
|
1504 |
} |
|
1505 |
$array = $tmp_array; |
|
1506 |
case (MDB2_PORTABILITY_EMPTY_TO_NULL + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES): |
|
1507 |
$tmp_array = array(); |
|
1508 |
foreach ($array as $key => $value) { |
|
1509 |
if ($value === '') { |
|
1510 |
$value = null; |
|
1511 |
} |
|
1512 |
$tmp_array[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value; |
|
1513 |
} |
|
1514 |
$array = $tmp_array; |
|
1515 |
case (MDB2_PORTABILITY_RTRIM + MDB2_PORTABILITY_EMPTY_TO_NULL + MDB2_PORTABILITY_FIX_ASSOC_FIELD_NAMES): |
|
1516 |
$tmp_array = array(); |
|
1517 |
foreach ($array as $key => $value) { |
|
1518 |
if ($value === '') { |
|
1519 |
$value = null; |
|
1520 |
} elseif (is_string($value)) { |
|
1521 |
$value = rtrim($value); |
|
1522 |
} |
|
1523 |
$tmp_array[preg_replace('/^(?:.*\.)?([^.]+)$/', '\\1', $key)] = $value; |
|
1524 |
} |
|
1525 |
$array = $tmp_array; |
|
1526 |
} |
|
1527 |
} |
|
1528 |
|
|
1529 |
// }}} |
|
1530 |
// {{{ loadModule() |
|
1531 |
|
|
1532 |
/** |
|
1533 |
* loads a module |
|
1534 |
* |
|
1535 |
* @param string $module name of the module that should be loaded |
|
1536 |
* (only used for error messages) |
|
1537 |
* @param string $property name of the property into which the class will be loaded |
|
1538 |
* @return object on success a reference to the given module is returned |
|
1539 |
* and on failure a PEAR error |
|
1540 |
* @access public |
|
1541 |
*/ |
|
1542 |
function &loadModule($module, $property = null) |
|
1543 |
{ |
|
1544 |
if (!$property) { |
|
1545 |
$property = strtolower($module); |
|
1546 |
} |
|
1547 |
|
|
1548 |
if (!isset($this->{$property})) { |
|
1549 |
$version = false; |
|
1550 |
$class_name = 'MDB2_'.$module; |
|
1551 |
$file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php'; |
|
1552 |
if (!class_exists($class_name) && !MDB2::fileExists($file_name)) { |
|
1553 |
$class_name = 'MDB2_Driver_'.$module.'_'.$this->phptype; |
|
1554 |
$file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php'; |
|
1555 |
$version = true; |
|
1556 |
if (!class_exists($class_name) && !MDB2::fileExists($file_name)) { |
|
1557 |
$err =& $this->raiseError(MDB2_ERROR_LOADMODULE, null, null, |
|
1558 |
'unable to find module: '.$file_name); |
|
1559 |
return $err; |
|
1560 |
} |
|
1561 |
} |
|
1562 |
|
|
1563 |
if (!class_exists($class_name) && !include_once($file_name)) { |
|
1564 |
$err =& $this->raiseError(MDB2_ERROR_LOADMODULE, null, null, |
|
1565 |
'unable to load manager driver class: '.$file_name); |
|
1566 |
return $err; |
|
1567 |
} |
|
1568 |
|
|
1569 |
// load modul in a specific version |
|
1570 |
if ($version) { |
|
1571 |
if (method_exists($class_name, 'getClassName')) { |
|
1572 |
$class_name_new = call_user_func(array($class_name, 'getClassName'), $this->db_index); |
|
1573 |
if ($class_name != $class_name_new) { |
|
1574 |
$class_name = $class_name_new; |
|
1575 |
$file_name = str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php'; |
|
1576 |
if (!MDB2::fileExists($file_name)) { |
|
1577 |
$err =& $this->raiseError(MDB2_ERROR_LOADMODULE, null, null, |
|
1578 |
'unable to find module: '.$file_name); |
|
1579 |
return $err; |
|
1580 |
} |
|
1581 |
if (!include_once($file_name)) { |
|
1582 |
$err =& $this->raiseError(MDB2_ERROR_LOADMODULE, null, null, |
|
1583 |
'unable to load manager driver class: '.$file_name); |
|
1584 |
return $err; |
|
1585 |
} |
|
1586 |
} |
|
1587 |
} |
|
1588 |
} |
|
1589 |
|
|
1590 |
if (!class_exists($class_name)) { |
|
1591 |
$err =& $this->raiseError(MDB2_ERROR_LOADMODULE, null, null, |
|
1592 |
'unable to load module: '.$module.' into property: '.$property); |
|
1593 |
return $err; |
|
1594 |
} |
|
1595 |
$this->{$property} =& new $class_name($this->db_index); |
|
1596 |
$this->modules[$module] =& $this->{$property}; |
|
1597 |
if ($version) { |
|
1598 |
// this wil be used in the connect method to determine if the module |
|
1599 |
// needs to be loaded with a different version if the server |
|
1600 |
// version changed in between connects |
|
1601 |
$this->loaded_version_modules[] = $property; |
|
1602 |
} |
|
1603 |
} |
|
1604 |
|
|
1605 |
return $this->{$property}; |
|
1606 |
} |
|
1607 |
|
|
1608 |
/** |
|
1609 |
* Calls a module method using the __call magic method |
|
1610 |
* |
|
1611 |
* @param string Method name. |
|
1612 |
* @param array Arguments. |
|
1613 |
* @return mixed Returned value. |
|
1614 |
*/ |
|
1615 |
function __call($method, $params) |
|
1616 |
{ |
|
1617 |
$module = null; |
|
1618 |
if (preg_match('/^([a-z]+)([A-Z])(.*)$/', $method, $match) |
|
1619 |
&& isset($this->options['modules'][$match[1]]) |
|
1620 |
) { |
|
1621 |
$module = $this->options['modules'][$match[1]]; |
|
1622 |
$method = strtolower($match[2]).$match[3]; |
|
1623 |
if (!isset($this->modules[$module]) || !is_object($this->modules[$module])) { |
|
1624 |
$result =& $this->loadModule($module); |
|
1625 |
if (PEAR::isError($result)) { |
|
1626 |
return $result; |
|
1627 |
} |
|
1628 |
} |
|
1629 |
} else { |
|
1630 |
foreach ($this->modules as $key => $foo) { |
|
1631 |
if (is_object($this->modules[$key]) |
|
1632 |
&& method_exists($this->modules[$key], $method) |
|
1633 |
) { |
|
1634 |
$module = $key; |
|
1635 |
break; |
|
1636 |
} |
|
1637 |
} |
|
1638 |
} |
|
1639 |
if (!is_null($module)) { |
|
1640 |
return call_user_func_array(array(&$this->modules[$module], $method), $params); |
|
1641 |
} |
|
1642 |
trigger_error(sprintf('Call to undefined function: %s::%s().', get_class($this), $method), E_USER_ERROR); |
|
1643 |
} |
|
1644 |
|
|
1645 |
// }}} |
|
1646 |
// {{{ beginTransaction() |
|
1647 |
|
|
1648 |
/** |
|
1649 |
* Start a transaction. |
|
1650 |
* |
|
1651 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1652 |
* @access public |
|
1653 |
*/ |
|
1654 |
function beginTransaction() |
|
1655 |
{ |
|
1656 |
$this->debug('Starting transaction', 'beginTransaction'); |
|
1657 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1658 |
'beginTransaction: transactions are not supported'); |
|
1659 |
} |
|
1660 |
|
|
1661 |
// }}} |
|
1662 |
// {{{ commit() |
|
1663 |
|
|
1664 |
/** |
|
1665 |
* Commit the database changes done during a transaction that is in |
|
1666 |
* progress. This function may only be called when auto-committing is |
|
1667 |
* disabled, otherwise it will fail. Therefore, a new transaction is |
|
1668 |
* implicitly started after committing the pending changes. |
|
1669 |
* |
|
1670 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1671 |
* @access public |
|
1672 |
*/ |
|
1673 |
function commit() |
|
1674 |
{ |
|
1675 |
$this->debug('commiting transaction', 'commit'); |
|
1676 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1677 |
'commit: commiting transactions is not supported'); |
|
1678 |
} |
|
1679 |
|
|
1680 |
// }}} |
|
1681 |
// {{{ rollback() |
|
1682 |
|
|
1683 |
/** |
|
1684 |
* Cancel any database changes done during a transaction that is in |
|
1685 |
* progress. This function may only be called when auto-committing is |
|
1686 |
* disabled, otherwise it will fail. Therefore, a new transaction is |
|
1687 |
* implicitly started after canceling the pending changes. |
|
1688 |
* |
|
1689 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1690 |
* @access public |
|
1691 |
*/ |
|
1692 |
function rollback() |
|
1693 |
{ |
|
1694 |
$this->debug('rolling back transaction', 'rollback'); |
|
1695 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1696 |
'rollback: rolling back transactions is not supported'); |
|
1697 |
} |
|
1698 |
|
|
1699 |
// }}} |
|
1700 |
// {{{ disconnect() |
|
1701 |
|
|
1702 |
/** |
|
1703 |
* Log out and disconnect from the database. |
|
1704 |
* |
|
1705 |
* @return mixed true on success, false if not connected and error |
|
1706 |
* object on error |
|
1707 |
* @access public |
|
1708 |
*/ |
|
1709 |
function disconnect() |
|
1710 |
{ |
|
1711 |
return MDB2_OK; |
|
1712 |
} |
|
1713 |
|
|
1714 |
// }}} |
|
1715 |
// {{{ setDatabase() |
|
1716 |
|
|
1717 |
/** |
|
1718 |
* Select a different database |
|
1719 |
* |
|
1720 |
* @param string $name name of the database that should be selected |
|
1721 |
* @return string name of the database previously connected to |
|
1722 |
* @access public |
|
1723 |
*/ |
|
1724 |
function setDatabase($name) |
|
1725 |
{ |
|
1726 |
$previous_database_name = (isset($this->database_name)) ? $this->database_name : ''; |
|
1727 |
$this->database_name = $name; |
|
1728 |
return $previous_database_name; |
|
1729 |
} |
|
1730 |
|
|
1731 |
// }}} |
|
1732 |
// {{{ getDatabase() |
|
1733 |
|
|
1734 |
/** |
|
1735 |
* get the current database |
|
1736 |
* |
|
1737 |
* @return string name of the database |
|
1738 |
* @access public |
|
1739 |
*/ |
|
1740 |
function getDatabase() |
|
1741 |
{ |
|
1742 |
return $this->database_name; |
|
1743 |
} |
|
1744 |
|
|
1745 |
// }}} |
|
1746 |
// {{{ setDSN() |
|
1747 |
|
|
1748 |
/** |
|
1749 |
* set the DSN |
|
1750 |
* |
|
1751 |
* @param mixed $dsn DSN string or array |
|
1752 |
* @return MDB2_OK |
|
1753 |
* @access public |
|
1754 |
*/ |
|
1755 |
function setDSN($dsn) |
|
1756 |
{ |
|
1757 |
$dsn_default = $GLOBALS['_MDB2_dsninfo_default']; |
|
1758 |
$dsn = MDB2::parseDSN($dsn); |
|
1759 |
if (array_key_exists('database', $dsn)) { |
|
1760 |
$this->database_name = $dsn['database']; |
|
1761 |
unset($dsn['database']); |
|
1762 |
} |
|
1763 |
$this->dsn = array_merge($dsn_default, $dsn); |
|
1764 |
return MDB2_OK; |
|
1765 |
} |
|
1766 |
|
|
1767 |
// }}} |
|
1768 |
// {{{ getDSN() |
|
1769 |
|
|
1770 |
/** |
|
1771 |
* return the DSN as a string |
|
1772 |
* |
|
1773 |
* @param string $type format to return ("array", "string") |
|
1774 |
* @param string $hidepw string to hide the password with |
|
1775 |
* @return mixed DSN in the chosen type |
|
1776 |
* @access public |
|
1777 |
*/ |
|
1778 |
function getDSN($type = 'string', $hidepw = false) |
|
1779 |
{ |
|
1780 |
$dsn = array_merge($GLOBALS['_MDB2_dsninfo_default'], $this->dsn); |
|
1781 |
$dsn['phptype'] = $this->phptype; |
|
1782 |
$dsn['database'] = $this->database_name; |
|
1783 |
if ($hidepw) { |
|
1784 |
$dsn['password'] = $hidepw; |
|
1785 |
} |
|
1786 |
switch ($type) { |
|
1787 |
// expand to include all possible options |
|
1788 |
case 'string': |
|
1789 |
$dsn = $dsn['phptype'].'://'.$dsn['username'].':'. |
|
1790 |
$dsn['password'].'@'.$dsn['hostspec']. |
|
1791 |
($dsn['port'] ? (':'.$dsn['port']) : ''). |
|
1792 |
'/'.$dsn['database']; |
|
1793 |
break; |
|
1794 |
case 'array': |
|
1795 |
default: |
|
1796 |
break; |
|
1797 |
} |
|
1798 |
return $dsn; |
|
1799 |
} |
|
1800 |
|
|
1801 |
// }}} |
|
1802 |
// {{{ standaloneQuery() |
|
1803 |
|
|
1804 |
/** |
|
1805 |
* execute a query as database administrator |
|
1806 |
* |
|
1807 |
* @param string $query the SQL query |
|
1808 |
* @param mixed $types array that contains the types of the columns in |
|
1809 |
* the result set |
|
1810 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1811 |
* @access public |
|
1812 |
*/ |
|
1813 |
function &standaloneQuery($query, $types = null) |
|
1814 |
{ |
|
1815 |
$isManip = MDB2::isManip($query); |
|
1816 |
$offset = $this->row_offset; |
|
1817 |
$limit = $this->row_limit; |
|
1818 |
$this->row_offset = $this->row_limit = 0; |
|
1819 |
$query = $this->_modifyQuery($query, $isManip, $limit, $offset); |
|
1820 |
|
|
1821 |
$connected = $this->connect(); |
|
1822 |
if (PEAR::isError($connected)) { |
|
1823 |
return $connected; |
|
1824 |
} |
|
1825 |
|
|
1826 |
$result = $this->_doQuery($query, $isManip, $this->connection, false); |
|
1827 |
if (PEAR::isError($result)) { |
|
1828 |
return $result; |
|
1829 |
} |
|
1830 |
|
|
1831 |
if ($isManip) { |
|
1832 |
return $result; |
|
1833 |
} |
|
1834 |
|
|
1835 |
$result =& $this->_wrapResult($result, $types, true, false, $limit, $offset); |
|
1836 |
return $result; |
|
1837 |
} |
|
1838 |
|
|
1839 |
// }}} |
|
1840 |
// {{{ _modifyQuery() |
|
1841 |
|
|
1842 |
/** |
|
1843 |
* Changes a query string for various DBMS specific reasons |
|
1844 |
* |
|
1845 |
* @param string $query query to modify |
|
1846 |
* @return the new (modified) query |
|
1847 |
* @access protected |
|
1848 |
*/ |
|
1849 |
function _modifyQuery($query) |
|
1850 |
{ |
|
1851 |
return $query; |
|
1852 |
} |
|
1853 |
|
|
1854 |
// }}} |
|
1855 |
// {{{ _doQuery() |
|
1856 |
|
|
1857 |
/** |
|
1858 |
* Execute a query |
|
1859 |
* @param string $query query |
|
1860 |
* @param boolean $isManip if the query is a manipulation query |
|
1861 |
* @param resource $connection |
|
1862 |
* @param string $database_name |
|
1863 |
* @return result or error object |
|
1864 |
* @access protected |
|
1865 |
*/ |
|
1866 |
function _doQuery($query, $isManip = false, $connection = null, $database_name = null) |
|
1867 |
{ |
|
1868 |
$this->last_query = $query; |
|
1869 |
$this->debug($query, 'query'); |
|
1870 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1871 |
'query: method not implemented'); |
|
1872 |
} |
|
1873 |
|
|
1874 |
// }}} |
|
1875 |
// {{{ query() |
|
1876 |
|
|
1877 |
/** |
|
1878 |
* Send a query to the database and return any results |
|
1879 |
* |
|
1880 |
* @param string $query the SQL query |
|
1881 |
* @param mixed $types array that contains the types of the columns in |
|
1882 |
* the result set |
|
1883 |
* @param mixed $result_class string which specifies which result class to use |
|
1884 |
* @param mixed $result_wrap_class string which specifies which class to wrap results in |
|
1885 |
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure |
|
1886 |
* @access public |
|
1887 |
*/ |
|
1888 |
function &query($query, $types = null, $result_class = true, $result_wrap_class = false) |
|
1889 |
{ |
|
1890 |
$isManip = MDB2::isManip($query); |
|
1891 |
$offset = $this->row_offset; |
|
1892 |
$limit = $this->row_limit; |
|
1893 |
$this->row_offset = $this->row_limit = 0; |
|
1894 |
$query = $this->_modifyQuery($query, $isManip, $limit, $offset); |
|
1895 |
|
|
1896 |
$connected = $this->connect(); |
|
1897 |
if (PEAR::isError($connected)) { |
|
1898 |
return $connected; |
|
1899 |
} |
|
1900 |
|
|
1901 |
$result = $this->_doQuery($query, $isManip, $this->connection, $this->database_name); |
|
1902 |
if (PEAR::isError($result)) { |
|
1903 |
return $result; |
|
1904 |
} |
|
1905 |
|
|
1906 |
// check for integers instead of manip since drivers may do some |
|
1907 |
// custom checks not covered by MDB2::isManip() |
|
1908 |
if (is_int($result)) { |
|
1909 |
return $result; |
|
1910 |
} |
|
1911 |
|
|
1912 |
$result =& $this->_wrapResult($result, $types, $result_class, $result_wrap_class, $limit, $offset); |
|
1913 |
return $result; |
|
1914 |
} |
|
1915 |
|
|
1916 |
// }}} |
|
1917 |
// {{{ _wrapResult() |
|
1918 |
|
|
1919 |
/** |
|
1920 |
* wrap a result set into the correct class |
|
1921 |
* |
|
1922 |
* @param ressource $result |
|
1923 |
* @param mixed $types array that contains the types of the columns in |
|
1924 |
* the result set |
|
1925 |
* @param mixed $result_class string which specifies which result class to use |
|
1926 |
* @param mixed $result_wrap_class string which specifies which class to wrap results in |
|
1927 |
* @param string $limit number of rows to select |
|
1928 |
* @param string $offset first row to select |
|
1929 |
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure |
|
1930 |
* @access protected |
|
1931 |
*/ |
|
1932 |
function &_wrapResult($result, $types = array(), $result_class = true, |
|
1933 |
$result_wrap_class = false, $limit = null, $offset = null) |
|
1934 |
{ |
|
1935 |
if ($result_class === true) { |
|
1936 |
$result_class = $this->options['result_buffering'] |
|
1937 |
? $this->options['buffered_result_class'] : $this->options['result_class']; |
|
1938 |
} |
|
1939 |
|
|
1940 |
if ($result_class) { |
|
1941 |
$class_name = sprintf($result_class, $this->phptype); |
|
1942 |
if (!class_exists($class_name)) { |
|
1943 |
$err =& $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
1944 |
'_wrapResult: result class does not exist '.$class_name); |
|
1945 |
return $err; |
|
1946 |
} |
|
1947 |
$result =& new $class_name($this, $result, $limit, $offset); |
|
1948 |
if (!MDB2::isResultCommon($result)) { |
|
1949 |
$err =& $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
1950 |
'_wrapResult: result class is not extended from MDB2_Result_Common'); |
|
1951 |
return $err; |
|
1952 |
} |
|
1953 |
if (!empty($types)) { |
|
1954 |
$err = $result->setResultTypes($types); |
|
1955 |
if (PEAR::isError($err)) { |
|
1956 |
$result->free(); |
|
1957 |
return $err; |
|
1958 |
} |
|
1959 |
} |
|
1960 |
} |
|
1961 |
if ($result_wrap_class === true) { |
|
1962 |
$result_wrap_class = $this->options['result_wrap_class']; |
|
1963 |
} |
|
1964 |
if ($result_wrap_class) { |
|
1965 |
if (!class_exists($result_wrap_class)) { |
|
1966 |
$err =& $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, |
|
1967 |
'_wrapResult: result wrap class does not exist '.$result_wrap_class); |
|
1968 |
return $err; |
|
1969 |
} |
|
1970 |
$result =& new $result_wrap_class($result, $this->fetchmode); |
|
1971 |
} |
|
1972 |
return $result; |
|
1973 |
} |
|
1974 |
|
|
1975 |
// }}} |
|
1976 |
// {{{ setLimit() |
|
1977 |
|
|
1978 |
/** |
|
1979 |
* set the range of the next query |
|
1980 |
* |
|
1981 |
* @param string $limit number of rows to select |
|
1982 |
* @param string $offset first row to select |
|
1983 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
1984 |
* @access public |
|
1985 |
*/ |
|
1986 |
function setLimit($limit, $offset = null) |
|
1987 |
{ |
|
1988 |
if (!$this->supports('limit_queries')) { |
|
1989 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
1990 |
'setLimit: limit is not supported by this driver'); |
|
1991 |
} |
|
1992 |
$limit = (int)$limit; |
|
1993 |
if ($limit < 0) { |
|
1994 |
return $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
1995 |
'setLimit: it was not specified a valid selected range row limit'); |
|
1996 |
} |
|
1997 |
$this->row_limit = $limit; |
|
1998 |
if (!is_null($offset)) { |
|
1999 |
$offset = (int)$offset; |
|
2000 |
if ($offset < 0) { |
|
2001 |
return $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
2002 |
'setLimit: it was not specified a valid first selected range row'); |
|
2003 |
} |
|
2004 |
$this->row_offset = $offset; |
|
2005 |
} |
|
2006 |
return MDB2_OK; |
|
2007 |
} |
|
2008 |
|
|
2009 |
// }}} |
|
2010 |
// {{{ subSelect() |
|
2011 |
|
|
2012 |
/** |
|
2013 |
* simple subselect emulation: leaves the query untouched for all RDBMS |
|
2014 |
* that support subselects |
|
2015 |
* |
|
2016 |
* @access public |
|
2017 |
* |
|
2018 |
* @param string $query the SQL query for the subselect that may only |
|
2019 |
* return a column |
|
2020 |
* @param string $type determines type of the field |
|
2021 |
* |
|
2022 |
* @return string the query |
|
2023 |
*/ |
|
2024 |
function subSelect($query, $type = false) |
|
2025 |
{ |
|
2026 |
if ($this->supports('sub_selects') === true) { |
|
2027 |
return $query; |
|
2028 |
} |
|
2029 |
|
|
2030 |
if (!$this->supports('sub_selects')) { |
|
2031 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2032 |
'subSelect: method not implemented'); |
|
2033 |
} |
|
2034 |
|
|
2035 |
$col = $this->queryCol($query, $type); |
|
2036 |
if (PEAR::isError($col)) { |
|
2037 |
return $col; |
|
2038 |
} |
|
2039 |
if (!is_array($col) || count($col) == 0) { |
|
2040 |
return 'NULL'; |
|
2041 |
} |
|
2042 |
if ($type) { |
|
2043 |
$this->loadModule('Datatype'); |
|
2044 |
return $this->datatype->implodeArray($col, $type); |
|
2045 |
} |
|
2046 |
return implode(', ', $col); |
|
2047 |
} |
|
2048 |
|
|
2049 |
// }}} |
|
2050 |
// {{{ replace() |
|
2051 |
|
|
2052 |
/** |
|
2053 |
* Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT |
|
2054 |
* query, except that if there is already a row in the table with the same |
|
2055 |
* key field values, the REPLACE query just updates its values instead of |
|
2056 |
* inserting a new row. |
|
2057 |
* |
|
2058 |
* The REPLACE type of query does not make part of the SQL standards. Since |
|
2059 |
* pratically only MySQL implements it natively, this type of query is |
|
2060 |
* emulated through this method for other DBMS using standard types of |
|
2061 |
* queries inside a transaction to assure the atomicity of the operation. |
|
2062 |
* |
|
2063 |
* @param string $table name of the table on which the REPLACE query will |
|
2064 |
* be executed. |
|
2065 |
* @param array $fields associative array that describes the fields and the |
|
2066 |
* values that will be inserted or updated in the specified table. The |
|
2067 |
* indexes of the array are the names of all the fields of the table. |
|
2068 |
* The values of the array are also associative arrays that describe |
|
2069 |
* the values and other properties of the table fields. |
|
2070 |
* |
|
2071 |
* Here follows a list of field properties that need to be specified: |
|
2072 |
* |
|
2073 |
* value |
|
2074 |
* Value to be assigned to the specified field. This value may be |
|
2075 |
* of specified in database independent type format as this |
|
2076 |
* function can perform the necessary datatype conversions. |
|
2077 |
* |
|
2078 |
* Default: this property is required unless the Null property is |
|
2079 |
* set to 1. |
|
2080 |
* |
|
2081 |
* type |
|
2082 |
* Name of the type of the field. Currently, all types Metabase |
|
2083 |
* are supported except for clob and blob. |
|
2084 |
* |
|
2085 |
* Default: no type conversion |
|
2086 |
* |
|
2087 |
* null |
|
2088 |
* Boolean property that indicates that the value for this field |
|
2089 |
* should be set to null. |
|
2090 |
* |
|
2091 |
* The default value for fields missing in INSERT queries may be |
|
2092 |
* specified the definition of a table. Often, the default value |
|
2093 |
* is already null, but since the REPLACE may be emulated using |
|
2094 |
* an UPDATE query, make sure that all fields of the table are |
|
2095 |
* listed in this function argument array. |
|
2096 |
* |
|
2097 |
* Default: 0 |
|
2098 |
* |
|
2099 |
* key |
|
2100 |
* Boolean property that indicates that this field should be |
|
2101 |
* handled as a primary key or at least as part of the compound |
|
2102 |
* unique index of the table that will determine the row that will |
|
2103 |
* updated if it exists or inserted a new row otherwise. |
|
2104 |
* |
|
2105 |
* This function will fail if no key field is specified or if the |
|
2106 |
* value of a key field is set to null because fields that are |
|
2107 |
* part of unique index they may not be null. |
|
2108 |
* |
|
2109 |
* Default: 0 |
|
2110 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
2111 |
* @access public |
|
2112 |
*/ |
|
2113 |
function replace($table, $fields) |
|
2114 |
{ |
|
2115 |
if (!$this->supports('replace')) { |
|
2116 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2117 |
'replace: replace query is not supported'); |
|
2118 |
} |
|
2119 |
$count = count($fields); |
|
2120 |
$condition = $values = array(); |
|
2121 |
for ($colnum = 0, reset($fields); $colnum < $count; next($fields), $colnum++) { |
|
2122 |
$name = key($fields); |
|
2123 |
if (isset($fields[$name]['null']) && $fields[$name]['null']) { |
|
2124 |
$value = 'NULL'; |
|
2125 |
} else { |
|
2126 |
if (isset($fields[$name]['type'])) { |
|
2127 |
$value = $this->quote($fields[$name]['value'], $fields[$name]['type']); |
|
2128 |
} else { |
|
2129 |
$value = $fields[$name]['value']; |
|
2130 |
} |
|
2131 |
} |
|
2132 |
$values[$name] = $value; |
|
2133 |
if (isset($fields[$name]['key']) && $fields[$name]['key']) { |
|
2134 |
if ($value === 'NULL') { |
|
2135 |
return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null, |
|
2136 |
'replace: key value '.$name.' may not be NULL'); |
|
2137 |
} |
|
2138 |
$condition[] = $name . '=' . $value; |
|
2139 |
} |
|
2140 |
} |
|
2141 |
if (empty($condition)) { |
|
2142 |
return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null, |
|
2143 |
'replace: not specified which fields are keys'); |
|
2144 |
} |
|
2145 |
|
|
2146 |
$result = null; |
|
2147 |
$in_transaction = $this->in_transaction; |
|
2148 |
if (!$in_transaction && PEAR::isError($result = $this->beginTransaction())) { |
|
2149 |
return $result; |
|
2150 |
} |
|
2151 |
|
|
2152 |
$condition = ' WHERE '.implode(' AND ', $condition); |
|
2153 |
$query = "DELETE FROM $table$condition"; |
|
2154 |
$affected_rows = $result = $this->_doQuery($query, true); |
|
2155 |
if (!PEAR::isError($result)) { |
|
2156 |
$insert = implode(', ', array_keys($values)); |
|
2157 |
$values = implode(', ', $values); |
|
2158 |
$query = "INSERT INTO $table ($insert) VALUES ($values)"; |
|
2159 |
$result = $this->_doQuery($query, true); |
|
2160 |
if (!PEAR::isError($result)) { |
|
2161 |
$affected_rows += $result; |
|
2162 |
} |
|
2163 |
} |
|
2164 |
|
|
2165 |
if (!$in_transaction) { |
|
2166 |
if (PEAR::isError($result)) { |
|
2167 |
$this->rollback(); |
|
2168 |
} else { |
|
2169 |
$result = $this->commit(); |
|
2170 |
} |
|
2171 |
} |
|
2172 |
|
|
2173 |
if (PEAR::isError($result)) { |
|
2174 |
return $result; |
|
2175 |
} |
|
2176 |
|
|
2177 |
return $affected_rows; |
|
2178 |
} |
|
2179 |
|
|
2180 |
// }}} |
|
2181 |
// {{{ prepare() |
|
2182 |
|
|
2183 |
/** |
|
2184 |
* Prepares a query for multiple execution with execute(). |
|
2185 |
* With some database backends, this is emulated. |
|
2186 |
* prepare() requires a generic query as string like |
|
2187 |
* 'INSERT INTO numbers VALUES(?,?)' or |
|
2188 |
* 'INSERT INTO numbers VALUES(:foo,:bar)'. |
|
2189 |
* The ? and :[a-zA-Z] and are placeholders which can be set using |
|
2190 |
* bindParam() and the query can be send off using the execute() method. |
|
2191 |
* |
|
2192 |
* @param string $query the query to prepare |
|
2193 |
* @param mixed $types array that contains the types of the placeholders |
|
2194 |
* @param mixed $result_types array that contains the types of the columns in |
|
2195 |
* the result set |
|
2196 |
* @return mixed resource handle for the prepared query on success, a MDB2 |
|
2197 |
* error on failure |
|
2198 |
* @access public |
|
2199 |
* @see bindParam, execute |
|
2200 |
*/ |
|
2201 |
function &prepare($query, $types = null, $result_types = null) |
|
2202 |
{ |
|
2203 |
$this->debug($query, 'prepare'); |
|
2204 |
$positions = array(); |
|
2205 |
$placeholder_type_guess = $placeholder_type = null; |
|
2206 |
$question = '?'; |
|
2207 |
$colon = ':'; |
|
2208 |
$position = 0; |
|
2209 |
while ($position < strlen($query)) { |
|
2210 |
$q_position = strpos($query, $question, $position); |
|
2211 |
$c_position = strpos($query, $colon, $position); |
|
2212 |
if ($q_position && $c_position) { |
|
2213 |
$p_position = min($q_position, $c_position); |
|
2214 |
} elseif ($q_position) { |
|
2215 |
$p_position = $q_position; |
|
2216 |
} elseif ($c_position) { |
|
2217 |
$p_position = $c_position; |
|
2218 |
} else { |
|
2219 |
break; |
|
2220 |
} |
|
2221 |
if (is_null($placeholder_type)) { |
|
2222 |
$placeholder_type_guess = $query[$p_position]; |
|
2223 |
} |
|
2224 |
if (is_int($quote = strpos($query, "'", $position)) && $quote < $p_position) { |
|
2225 |
if (!is_int($end_quote = strpos($query, "'", $quote + 1))) { |
|
2226 |
$err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
2227 |
'prepare: query with an unterminated text string specified'); |
|
2228 |
return $err; |
|
2229 |
} |
|
2230 |
switch ($this->escape_quotes) { |
|
2231 |
case '': |
|
2232 |
case "'": |
|
2233 |
$position = $end_quote + 1; |
|
2234 |
break; |
|
2235 |
default: |
|
2236 |
if ($end_quote == $quote + 1) { |
|
2237 |
$position = $end_quote + 1; |
|
2238 |
} else { |
|
2239 |
if ($query[$end_quote-1] == $this->escape_quotes) { |
|
2240 |
$position = $end_quote; |
|
2241 |
} else { |
|
2242 |
$position = $end_quote + 1; |
|
2243 |
} |
|
2244 |
} |
|
2245 |
break; |
|
2246 |
} |
|
2247 |
} elseif ($query[$position] == $placeholder_type_guess) { |
|
2248 |
if (is_null($placeholder_type)) { |
|
2249 |
$placeholder_type = $query[$p_position]; |
|
2250 |
$question = $colon = $placeholder_type; |
|
2251 |
} |
|
2252 |
if ($placeholder_type == ':') { |
|
2253 |
$parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query); |
|
2254 |
if ($parameter === '') { |
|
2255 |
$err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
2256 |
'prepare: named parameter with an empty name'); |
|
2257 |
return $err; |
|
2258 |
} |
|
2259 |
$positions[$parameter] = $p_position; |
|
2260 |
$query = substr_replace($query, '?', $position, strlen($parameter)+1); |
|
2261 |
} else { |
|
2262 |
$positions[] = $p_position; |
|
2263 |
} |
|
2264 |
$position = $p_position + 1; |
|
2265 |
} else { |
|
2266 |
$position = $p_position; |
|
2267 |
} |
|
2268 |
} |
|
2269 |
$class_name = 'MDB2_Statement_'.$this->phptype; |
|
2270 |
$obj =& new $class_name($this, $positions, $query, $types, $result_types); |
|
2271 |
return $obj; |
|
2272 |
} |
|
2273 |
|
|
2274 |
// }}} |
|
2275 |
// {{{ quote() |
|
2276 |
|
|
2277 |
/** |
|
2278 |
* Convert a text value into a DBMS specific format that is suitable to |
|
2279 |
* compose query statements. |
|
2280 |
* |
|
2281 |
* @param string $value text string value that is intended to be converted. |
|
2282 |
* @param string $type type to which the value should be converted to |
|
2283 |
* @return string text string that represents the given argument value in |
|
2284 |
* a DBMS specific format. |
|
2285 |
* @access public |
|
2286 |
*/ |
|
2287 |
function quote($value, $type = null, $quote = true) |
|
2288 |
{ |
|
2289 |
$result = $this->loadModule('Datatype'); |
|
2290 |
if (PEAR::isError($result)) { |
|
2291 |
return $result; |
|
2292 |
} |
|
2293 |
return $this->datatype->quote($value, $type, $quote); |
|
2294 |
} |
|
2295 |
|
|
2296 |
// }}} |
|
2297 |
// {{{ getDeclaration() |
|
2298 |
|
|
2299 |
/** |
|
2300 |
* Obtain DBMS specific SQL code portion needed to declare |
|
2301 |
* of the given type |
|
2302 |
* |
|
2303 |
* @param string $type type to which the value should be converted to |
|
2304 |
* @param string $name name the field to be declared. |
|
2305 |
* @param string $field definition of the field |
|
2306 |
* @return string DBMS specific SQL code portion that should be used to |
|
2307 |
* declare the specified field. |
|
2308 |
* @access public |
|
2309 |
*/ |
|
2310 |
function getDeclaration($type, $name, $field) |
|
2311 |
{ |
|
2312 |
$result = $this->loadModule('Datatype'); |
|
2313 |
if (PEAR::isError($result)) { |
|
2314 |
return $result; |
|
2315 |
} |
|
2316 |
return $this->datatype->getDeclaration($type, $name, $field); |
|
2317 |
} |
|
2318 |
|
|
2319 |
// }}} |
|
2320 |
// {{{ compareDefinition() |
|
2321 |
|
|
2322 |
/** |
|
2323 |
* Obtain an array of changes that may need to applied |
|
2324 |
* |
|
2325 |
* @param array $current new definition |
|
2326 |
* @param array $previous old definition |
|
2327 |
* @return array containg all changes that will need to be applied |
|
2328 |
* @access public |
|
2329 |
*/ |
|
2330 |
function compareDefinition($current, $previous) |
|
2331 |
{ |
|
2332 |
$result = $this->loadModule('Datatype'); |
|
2333 |
if (PEAR::isError($result)) { |
|
2334 |
return $result; |
|
2335 |
} |
|
2336 |
return $this->datatype->compareDefinition($current, $previous); |
|
2337 |
} |
|
2338 |
|
|
2339 |
// }}} |
|
2340 |
// {{{ supports() |
|
2341 |
|
|
2342 |
/** |
|
2343 |
* Tell whether a DB implementation or its backend extension |
|
2344 |
* supports a given feature. |
|
2345 |
* |
|
2346 |
* @param string $feature name of the feature (see the MDB2 class doc) |
|
2347 |
* @return boolean|string whether this DB implementation supports $feature |
|
2348 |
* false means no, true means native, 'emulated' means emulated |
|
2349 |
* @access public |
|
2350 |
*/ |
|
2351 |
function supports($feature) |
|
2352 |
{ |
|
2353 |
if (array_key_exists($feature, $this->supported)) { |
|
2354 |
return $this->supported[$feature]; |
|
2355 |
} |
|
2356 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2357 |
"unknown support feature $feature"); |
|
2358 |
} |
|
2359 |
|
|
2360 |
// }}} |
|
2361 |
// {{{ getSequenceName() |
|
2362 |
|
|
2363 |
/** |
|
2364 |
* adds sequence name formating to a sequence name |
|
2365 |
* |
|
2366 |
* @param string $sqn name of the sequence |
|
2367 |
* @return string formatted sequence name |
|
2368 |
* @access public |
|
2369 |
*/ |
|
2370 |
function getSequenceName($sqn) |
|
2371 |
{ |
|
2372 |
return sprintf($this->options['seqname_format'], |
|
2373 |
preg_replace('/[^a-z0-9_]/i', '_', $sqn)); |
|
2374 |
} |
|
2375 |
|
|
2376 |
// }}} |
|
2377 |
// {{{ nextID() |
|
2378 |
|
|
2379 |
/** |
|
2380 |
* returns the next free id of a sequence |
|
2381 |
* |
|
2382 |
* @param string $seq_name name of the sequence |
|
2383 |
* @param boolean $ondemand when true the seqence is |
|
2384 |
* automatic created, if it |
|
2385 |
* not exists |
|
2386 |
* @return mixed MDB2 Error Object or id |
|
2387 |
* @access public |
|
2388 |
*/ |
|
2389 |
function nextID($seq_name, $ondemand = true) |
|
2390 |
{ |
|
2391 |
return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2392 |
'nextID: method not implemented'); |
|
2393 |
} |
|
2394 |
|
|
2395 |
// }}} |
|
2396 |
// {{{ lastInsertID() |
|
2397 |
|
|
2398 |
/** |
|
2399 |
* returns the autoincrement ID if supported or $id |
|
2400 |
* |
|
2401 |
* @param mixed $id value as returned by getBeforeId() |
|
2402 |
* @param string $table name of the table into which a new row was inserted |
|
2403 |
* @return mixed MDB2 Error Object or id |
|
2404 |
* @access public |
|
2405 |
*/ |
|
2406 |
function lastInsertID($table = null, $field = null) |
|
2407 |
{ |
|
2408 |
$seq = $table.(empty($field) ? '' : '_'.$field); |
|
2409 |
return $this->currID($seq); |
|
2410 |
} |
|
2411 |
|
|
2412 |
// }}} |
|
2413 |
// {{{ currID() |
|
2414 |
|
|
2415 |
/** |
|
2416 |
* returns the current id of a sequence |
|
2417 |
* |
|
2418 |
* @param string $seq_name name of the sequence |
|
2419 |
* @return mixed MDB2 Error Object or id |
|
2420 |
* @access public |
|
2421 |
*/ |
|
2422 |
function currID($seq_name) |
|
2423 |
{ |
|
2424 |
$this->warnings[] = 'database does not support getting current |
|
2425 |
sequence value, the sequence value was incremented'; |
|
2426 |
return $this->nextID($seq_name); |
|
2427 |
} |
|
2428 |
|
|
2429 |
// }}} |
|
2430 |
// {{{ queryOne() |
|
2431 |
|
|
2432 |
/** |
|
2433 |
* Execute the specified query, fetch the value from the first column of |
|
2434 |
* the first row of the result set and then frees |
|
2435 |
* the result set. |
|
2436 |
* |
|
2437 |
* @param string $query the SELECT query statement to be executed. |
|
2438 |
* @param string $type optional argument that specifies the expected |
|
2439 |
* datatype of the result set field, so that an eventual conversion |
|
2440 |
* may be performed. The default datatype is text, meaning that no |
|
2441 |
* conversion is performed |
|
2442 |
* @return mixed MDB2_OK or field value on success, a MDB2 error on failure |
|
2443 |
* @access public |
|
2444 |
*/ |
|
2445 |
function queryOne($query, $type = null) |
|
2446 |
{ |
|
2447 |
$result = $this->query($query, $type); |
|
2448 |
if (!MDB2::isResultCommon($result)) { |
|
2449 |
return $result; |
|
2450 |
} |
|
2451 |
|
|
2452 |
$one = $result->fetchOne(); |
|
2453 |
$result->free(); |
|
2454 |
return $one; |
|
2455 |
} |
|
2456 |
|
|
2457 |
// }}} |
|
2458 |
// {{{ queryRow() |
|
2459 |
|
|
2460 |
/** |
|
2461 |
* Execute the specified query, fetch the values from the first |
|
2462 |
* row of the result set into an array and then frees |
|
2463 |
* the result set. |
|
2464 |
* |
|
2465 |
* @param string $query the SELECT query statement to be executed. |
|
2466 |
* @param array $types optional array argument that specifies a list of |
|
2467 |
* expected datatypes of the result set columns, so that the eventual |
|
2468 |
* conversions may be performed. The default list of datatypes is |
|
2469 |
* empty, meaning that no conversion is performed. |
|
2470 |
* @param int $fetchmode how the array data should be indexed |
|
2471 |
* @return mixed MDB2_OK or data array on success, a MDB2 error on failure |
|
2472 |
* @access public |
|
2473 |
*/ |
|
2474 |
function queryRow($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT) |
|
2475 |
{ |
|
2476 |
$result = $this->query($query, $types); |
|
2477 |
if (!MDB2::isResultCommon($result)) { |
|
2478 |
return $result; |
|
2479 |
} |
|
2480 |
|
|
2481 |
$row = $result->fetchRow($fetchmode); |
|
2482 |
$result->free(); |
|
2483 |
return $row; |
|
2484 |
} |
|
2485 |
|
|
2486 |
// }}} |
|
2487 |
// {{{ queryCol() |
|
2488 |
|
|
2489 |
/** |
|
2490 |
* Execute the specified query, fetch the value from the first column of |
|
2491 |
* each row of the result set into an array and then frees the result set. |
|
2492 |
* |
|
2493 |
* @param string $query the SELECT query statement to be executed. |
|
2494 |
* @param string $type optional argument that specifies the expected |
|
2495 |
* datatype of the result set field, so that an eventual conversion |
|
2496 |
* may be performed. The default datatype is text, meaning that no |
|
2497 |
* conversion is performed |
|
2498 |
* @param int $colnum the row number to fetch |
|
2499 |
* @return mixed MDB2_OK or data array on success, a MDB2 error on failure |
|
2500 |
* @access public |
|
2501 |
*/ |
|
2502 |
function queryCol($query, $type = null, $colnum = 0) |
|
2503 |
{ |
|
2504 |
$result = $this->query($query, $type); |
|
2505 |
if (!MDB2::isResultCommon($result)) { |
|
2506 |
return $result; |
|
2507 |
} |
|
2508 |
|
|
2509 |
$col = $result->fetchCol($colnum); |
|
2510 |
$result->free(); |
|
2511 |
return $col; |
|
2512 |
} |
|
2513 |
|
|
2514 |
// }}} |
|
2515 |
// {{{ queryAll() |
|
2516 |
|
|
2517 |
/** |
|
2518 |
* Execute the specified query, fetch all the rows of the result set into |
|
2519 |
* a two dimensional array and then frees the result set. |
|
2520 |
* |
|
2521 |
* @param string $query the SELECT query statement to be executed. |
|
2522 |
* @param array $types optional array argument that specifies a list of |
|
2523 |
* expected datatypes of the result set columns, so that the eventual |
|
2524 |
* conversions may be performed. The default list of datatypes is |
|
2525 |
* empty, meaning that no conversion is performed. |
|
2526 |
* @param int $fetchmode how the array data should be indexed |
|
2527 |
* @param boolean $rekey if set to true, the $all will have the first |
|
2528 |
* column as its first dimension |
|
2529 |
* @param boolean $force_array used only when the query returns exactly |
|
2530 |
* two columns. If true, the values of the returned array will be |
|
2531 |
* one-element arrays instead of scalars. |
|
2532 |
* @param boolean $group if true, the values of the returned array is |
|
2533 |
* wrapped in another array. If the same key value (in the first |
|
2534 |
* column) repeats itself, the values will be appended to this array |
|
2535 |
* instead of overwriting the existing values. |
|
2536 |
* @return mixed MDB2_OK or data array on success, a MDB2 error on failure |
|
2537 |
* @access public |
|
2538 |
*/ |
|
2539 |
function queryAll($query, $types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, |
|
2540 |
$rekey = false, $force_array = false, $group = false) |
|
2541 |
{ |
|
2542 |
$result = $this->query($query, $types); |
|
2543 |
if (!MDB2::isResultCommon($result)) { |
|
2544 |
return $result; |
|
2545 |
} |
|
2546 |
|
|
2547 |
$all = $result->fetchAll($fetchmode, $rekey, $force_array, $group); |
|
2548 |
$result->free(); |
|
2549 |
return $all; |
|
2550 |
} |
|
2551 |
} |
|
2552 |
|
|
2553 |
class MDB2_Result |
|
2554 |
{ |
|
2555 |
} |
|
2556 |
|
|
2557 |
class MDB2_Result_Common extends MDB2_Result |
|
2558 |
{ |
|
2559 |
var $db; |
|
2560 |
var $result; |
|
2561 |
var $rownum = -1; |
|
2562 |
var $types; |
|
2563 |
var $values; |
|
2564 |
var $offset; |
|
2565 |
var $offset_count = 0; |
|
2566 |
var $limit; |
|
2567 |
var $column_names; |
|
2568 |
|
|
2569 |
// {{{ constructor |
|
2570 |
|
|
2571 |
/** |
|
2572 |
* Constructor |
|
2573 |
*/ |
|
2574 |
function __construct(&$db, &$result, $limit = 0, $offset = 0) |
|
2575 |
{ |
|
2576 |
$this->db =& $db; |
|
2577 |
$this->result =& $result; |
|
2578 |
$this->offset = $offset; |
|
2579 |
$this->limit = max(0, $limit - 1); |
|
2580 |
} |
|
2581 |
|
|
2582 |
function MDB2_Result_Common(&$db, &$result, $limit = 0, $offset = 0) |
|
2583 |
{ |
|
2584 |
$this->__construct($db, $result, $limit, $offset); |
|
2585 |
} |
|
2586 |
|
|
2587 |
// }}} |
|
2588 |
// {{{ setResultTypes() |
|
2589 |
|
|
2590 |
/** |
|
2591 |
* Define the list of types to be associated with the columns of a given |
|
2592 |
* result set. |
|
2593 |
* |
|
2594 |
* This function may be called before invoking fetchRow(), fetchOne(), |
|
2595 |
* fetchCol() and fetchAll() so that the necessary data type |
|
2596 |
* conversions are performed on the data to be retrieved by them. If this |
|
2597 |
* function is not called, the type of all result set columns is assumed |
|
2598 |
* to be text, thus leading to not perform any conversions. |
|
2599 |
* |
|
2600 |
* @param string $types array variable that lists the |
|
2601 |
* data types to be expected in the result set columns. If this array |
|
2602 |
* contains less types than the number of columns that are returned |
|
2603 |
* in the result set, the remaining columns are assumed to be of the |
|
2604 |
* type text. Currently, the types clob and blob are not fully |
|
2605 |
* supported. |
|
2606 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
2607 |
* @access public |
|
2608 |
*/ |
|
2609 |
function setResultTypes($types) |
|
2610 |
{ |
|
2611 |
$load = $this->db->loadModule('Datatype'); |
|
2612 |
if (PEAR::isError($load)) { |
|
2613 |
return $load; |
|
2614 |
} |
|
2615 |
return $this->db->datatype->setResultTypes($this, $types); |
|
2616 |
} |
|
2617 |
|
|
2618 |
// }}} |
|
2619 |
// {{{ seek() |
|
2620 |
|
|
2621 |
/** |
|
2622 |
* seek to a specific row in a result set |
|
2623 |
* |
|
2624 |
* @param int $rownum number of the row where the data can be found |
|
2625 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
2626 |
* @access public |
|
2627 |
*/ |
|
2628 |
function seek($rownum = 0) |
|
2629 |
{ |
|
2630 |
$target_rownum = $rownum - 1; |
|
2631 |
if ($this->rownum > $target_rownum) { |
|
2632 |
return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2633 |
'seek: seeking to previous rows not implemented'); |
|
2634 |
} |
|
2635 |
while ($this->rownum < $target_rownum) { |
|
2636 |
$this->fetchRow(); |
|
2637 |
} |
|
2638 |
return MDB2_OK; |
|
2639 |
} |
|
2640 |
|
|
2641 |
// }}} |
|
2642 |
// {{{ fetchRow() |
|
2643 |
|
|
2644 |
/** |
|
2645 |
* Fetch and return a row of data |
|
2646 |
* |
|
2647 |
* @param int $fetchmode how the array data should be indexed |
|
2648 |
* @param int $rownum number of the row where the data can be found |
|
2649 |
* @return int data array on success, a MDB2 error on failure |
|
2650 |
* @access public |
|
2651 |
*/ |
|
2652 |
function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null) |
|
2653 |
{ |
|
2654 |
$err =& $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2655 |
'fetch: method not implemented'); |
|
2656 |
return $err; |
|
2657 |
} |
|
2658 |
|
|
2659 |
// }}} |
|
2660 |
// {{{ fetchOne() |
|
2661 |
|
|
2662 |
/** |
|
2663 |
* fetch single column from the first row from a result set |
|
2664 |
* |
|
2665 |
* @param int $colnum the column number to fetch |
|
2666 |
* @return string data on success, a MDB2 error on failure |
|
2667 |
* @access public |
|
2668 |
*/ |
|
2669 |
function fetchOne($colnum = 0) |
|
2670 |
{ |
|
2671 |
$fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC; |
|
2672 |
$row = $this->fetchRow($fetchmode); |
|
2673 |
if (!is_array($row) || PEAR::isError($row)) { |
|
2674 |
return $row; |
|
2675 |
} |
|
2676 |
if (!array_key_exists($colnum, $row)) { |
|
2677 |
return $this->db->raiseError(MDB2_ERROR_TRUNCATED); |
|
2678 |
} |
|
2679 |
return $row[$colnum]; |
|
2680 |
} |
|
2681 |
|
|
2682 |
// }}} |
|
2683 |
// {{{ fetchCol() |
|
2684 |
|
|
2685 |
/** |
|
2686 |
* Fetch and return a column of data (it uses current for that) |
|
2687 |
* |
|
2688 |
* @param int $colnum the column number to fetch |
|
2689 |
* @return mixed data array on success, a MDB2 error on failure |
|
2690 |
* @access public |
|
2691 |
*/ |
|
2692 |
function fetchCol($colnum = 0) |
|
2693 |
{ |
|
2694 |
$column = array(); |
|
2695 |
$fetchmode = is_numeric($colnum) ? MDB2_FETCHMODE_ORDERED : MDB2_FETCHMODE_ASSOC; |
|
2696 |
$row = $this->fetchRow($fetchmode); |
|
2697 |
if (is_array($row)) { |
|
2698 |
if (!array_key_exists($colnum, $row)) { |
|
2699 |
return $this->db->raiseError(MDB2_ERROR_TRUNCATED); |
|
2700 |
} |
|
2701 |
do { |
|
2702 |
$column[] = $row[$colnum]; |
|
2703 |
} while (is_array($row = $this->fetchRow($fetchmode))); |
|
2704 |
} |
|
2705 |
if (PEAR::isError($row)) { |
|
2706 |
return $row; |
|
2707 |
} |
|
2708 |
return $column; |
|
2709 |
} |
|
2710 |
|
|
2711 |
// }}} |
|
2712 |
// {{{ fetchAll() |
|
2713 |
|
|
2714 |
/** |
|
2715 |
* Fetch and return a column of data (it uses fetchRow for that) |
|
2716 |
* |
|
2717 |
* @param int $fetchmode the fetch mode to use: |
|
2718 |
* + MDB2_FETCHMODE_ORDERED |
|
2719 |
* + MDB2_FETCHMODE_ASSOC |
|
2720 |
* + MDB2_FETCHMODE_ORDERED | MDB2_FETCHMODE_FLIPPED |
|
2721 |
* + MDB2_FETCHMODE_ASSOC | MDB2_FETCHMODE_FLIPPED |
|
2722 |
* @param boolean $rekey if set to true, the $all will have the first |
|
2723 |
* column as its first dimension |
|
2724 |
* @param boolean $force_array used only when the query returns exactly |
|
2725 |
* two columns. If true, the values of the returned array will be |
|
2726 |
* one-element arrays instead of scalars. |
|
2727 |
* @param boolean $group if true, the values of the returned array is |
|
2728 |
* wrapped in another array. If the same key value (in the first |
|
2729 |
* column) repeats itself, the values will be appended to this array |
|
2730 |
* instead of overwriting the existing values. |
|
2731 |
* @return mixed data array on success, a MDB2 error on failure |
|
2732 |
* @access public |
|
2733 |
* @see getAssoc() |
|
2734 |
*/ |
|
2735 |
function fetchAll($fetchmode = MDB2_FETCHMODE_DEFAULT, $rekey = false, |
|
2736 |
$force_array = false, $group = false) |
|
2737 |
{ |
|
2738 |
$all = array(); |
|
2739 |
$row = $this->fetchRow($fetchmode); |
|
2740 |
if (PEAR::isError($row)) { |
|
2741 |
return $row; |
|
2742 |
} elseif (!$row) { |
|
2743 |
return $all; |
|
2744 |
} |
|
2745 |
|
|
2746 |
$shift_array = $rekey ? false : null; |
|
2747 |
if (!is_null($shift_array)) { |
|
2748 |
if (is_object($row)) { |
|
2749 |
$colnum = count(get_object_vars($row)); |
|
2750 |
} else { |
|
2751 |
$colnum = count($row); |
|
2752 |
} |
|
2753 |
if ($colnum < 2) { |
|
2754 |
return $this->db->raiseError(MDB2_ERROR_TRUNCATED); |
|
2755 |
} |
|
2756 |
$shift_array = (!$force_array && $colnum == 2); |
|
2757 |
} |
|
2758 |
|
|
2759 |
if ($rekey) { |
|
2760 |
do { |
|
2761 |
if (is_object($row)) { |
|
2762 |
$arr = get_object_vars($row); |
|
2763 |
$key = reset($arr); |
|
2764 |
unset($row->{$key}); |
|
2765 |
} else { |
|
2766 |
if ($fetchmode & MDB2_FETCHMODE_ASSOC) { |
|
2767 |
$key = reset($row); |
|
2768 |
unset($row[key($row)]); |
|
2769 |
} else { |
|
2770 |
$key = array_shift($row); |
|
2771 |
} |
|
2772 |
if ($shift_array) { |
|
2773 |
$row = array_shift($row); |
|
2774 |
} |
|
2775 |
} |
|
2776 |
if ($group) { |
|
2777 |
$all[$key][] = $row; |
|
2778 |
} else { |
|
2779 |
$all[$key] = $row; |
|
2780 |
} |
|
2781 |
} while (($row = $this->fetchRow($fetchmode))); |
|
2782 |
} elseif ($fetchmode & MDB2_FETCHMODE_FLIPPED) { |
|
2783 |
do { |
|
2784 |
foreach ($row as $key => $val) { |
|
2785 |
$all[$key][] = $val; |
|
2786 |
} |
|
2787 |
} while (($row = $this->fetchRow($fetchmode))); |
|
2788 |
} else { |
|
2789 |
do { |
|
2790 |
$all[] = $row; |
|
2791 |
} while (($row = $this->fetchRow($fetchmode))); |
|
2792 |
} |
|
2793 |
|
|
2794 |
return $all; |
|
2795 |
} |
|
2796 |
|
|
2797 |
// }}} |
|
2798 |
// {{{ rowCount() |
|
2799 |
|
|
2800 |
/** |
|
2801 |
* returns the actual row number that was last fetched (count from 0) |
|
2802 |
* @return integer |
|
2803 |
*/ |
|
2804 |
function rowCount() |
|
2805 |
{ |
|
2806 |
return $this->rownum + 1; |
|
2807 |
} |
|
2808 |
|
|
2809 |
// }}} |
|
2810 |
// {{{ numRows() |
|
2811 |
|
|
2812 |
/** |
|
2813 |
* returns the number of rows in a result object |
|
2814 |
* |
|
2815 |
* @return mixed MDB2 Error Object or the number of rows |
|
2816 |
* @access public |
|
2817 |
*/ |
|
2818 |
function numRows() |
|
2819 |
{ |
|
2820 |
return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2821 |
'numRows: method not implemented'); |
|
2822 |
} |
|
2823 |
|
|
2824 |
// }}} |
|
2825 |
// {{{ nextResult() |
|
2826 |
|
|
2827 |
/** |
|
2828 |
* Move the internal result pointer to the next available result |
|
2829 |
* |
|
2830 |
* @param a valid result resource |
|
2831 |
* @return true on success or an error object on failure |
|
2832 |
* @access public |
|
2833 |
*/ |
|
2834 |
function nextResult() |
|
2835 |
{ |
|
2836 |
return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2837 |
'nextResult: method not implemented'); |
|
2838 |
} |
|
2839 |
|
|
2840 |
// }}} |
|
2841 |
// {{{ getColumnNames() |
|
2842 |
|
|
2843 |
/** |
|
2844 |
* Retrieve the names of columns returned by the DBMS in a query result or |
|
2845 |
* from the cache. |
|
2846 |
* |
|
2847 |
* @return mixed associative array variable |
|
2848 |
* that holds the names of columns. The indexes of the array are |
|
2849 |
* the column names mapped to lower case and the values are the |
|
2850 |
* respective numbers of the columns starting from 0. Some DBMS may |
|
2851 |
* not return any columns when the result set does not contain any |
|
2852 |
* rows. |
|
2853 |
* a MDB2 error on failure |
|
2854 |
* @access public |
|
2855 |
*/ |
|
2856 |
function getColumnNames() |
|
2857 |
{ |
|
2858 |
if (!isset($this->column_names)) { |
|
2859 |
$result = $this->_getColumnNames(); |
|
2860 |
if (PEAR::isError($result)) { |
|
2861 |
return $result; |
|
2862 |
} |
|
2863 |
$this->column_names = $result; |
|
2864 |
} |
|
2865 |
return $this->column_names; |
|
2866 |
} |
|
2867 |
|
|
2868 |
// }}} |
|
2869 |
// {{{ _getColumnNames() |
|
2870 |
|
|
2871 |
/** |
|
2872 |
* Retrieve the names of columns returned by the DBMS in a query result. |
|
2873 |
* |
|
2874 |
* @return mixed associative array variable |
|
2875 |
* that holds the names of columns. The indexes of the array are |
|
2876 |
* the column names mapped to lower case and the values are the |
|
2877 |
* respective numbers of the columns starting from 0. Some DBMS may |
|
2878 |
* not return any columns when the result set does not contain any |
|
2879 |
* rows. |
|
2880 |
* a MDB2 error on failure |
|
2881 |
* @access private |
|
2882 |
*/ |
|
2883 |
function _getColumnNames() |
|
2884 |
{ |
|
2885 |
return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2886 |
'getColumnNames: method not implemented'); |
|
2887 |
} |
|
2888 |
|
|
2889 |
// }}} |
|
2890 |
// {{{ numCols() |
|
2891 |
|
|
2892 |
/** |
|
2893 |
* Count the number of columns returned by the DBMS in a query result. |
|
2894 |
* |
|
2895 |
* @return mixed integer value with the number of columns, a MDB2 error |
|
2896 |
* on failure |
|
2897 |
* @access public |
|
2898 |
*/ |
|
2899 |
function numCols() |
|
2900 |
{ |
|
2901 |
return $this->db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, |
|
2902 |
'numCols: method not implemented'); |
|
2903 |
} |
|
2904 |
|
|
2905 |
// }}} |
|
2906 |
// {{{ getResource() |
|
2907 |
|
|
2908 |
/** |
|
2909 |
* return the resource associated with the result object |
|
2910 |
* |
|
2911 |
* @return resource |
|
2912 |
* @access public |
|
2913 |
*/ |
|
2914 |
function getResource() |
|
2915 |
{ |
|
2916 |
return $this->result; |
|
2917 |
} |
|
2918 |
|
|
2919 |
|
|
2920 |
// }}} |
|
2921 |
// {{{ bindColumn() |
|
2922 |
|
|
2923 |
/** |
|
2924 |
* Set bind variable to a column. |
|
2925 |
* |
|
2926 |
* @param int $column |
|
2927 |
* @param mixed $value |
|
2928 |
* @param string $type specifies the type of the field |
|
2929 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
2930 |
* @access public |
|
2931 |
*/ |
|
2932 |
function bindColumn($column, &$value, $type = null) |
|
2933 |
{ |
|
2934 |
if (!is_numeric($column)) { |
|
2935 |
$column_names = $this->getColumnNames(); |
|
2936 |
if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) { |
|
2937 |
if ($this->db->options['field_case'] == CASE_LOWER) { |
|
2938 |
$column = strtolower($column); |
|
2939 |
} else { |
|
2940 |
$column = strtoupper($column); |
|
2941 |
} |
|
2942 |
} |
|
2943 |
$column = $column_names[$column]; |
|
2944 |
} |
|
2945 |
$this->values[$column] =& $value; |
|
2946 |
if (!is_null($type)) { |
|
2947 |
$this->types[$column] = $type; |
|
2948 |
} |
|
2949 |
return MDB2_OK; |
|
2950 |
} |
|
2951 |
|
|
2952 |
// }}} |
|
2953 |
// {{{ _assignBindColumns() |
|
2954 |
|
|
2955 |
/** |
|
2956 |
* Bind a variable to a value in the result row. |
|
2957 |
* |
|
2958 |
* @param array $row |
|
2959 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
2960 |
* @access private |
|
2961 |
*/ |
|
2962 |
function _assignBindColumns($row) |
|
2963 |
{ |
|
2964 |
$row = array_values($row); |
|
2965 |
foreach ($row as $column => $value) { |
|
2966 |
if (array_key_exists($column, $this->values)) { |
|
2967 |
$this->values[$column] = $value; |
|
2968 |
} |
|
2969 |
} |
|
2970 |
return MDB2_OK; |
|
2971 |
} |
|
2972 |
|
|
2973 |
// }}} |
|
2974 |
// {{{ free() |
|
2975 |
|
|
2976 |
/** |
|
2977 |
* Free the internal resources associated with result. |
|
2978 |
* |
|
2979 |
* @return boolean true on success, false if result is invalid |
|
2980 |
* @access public |
|
2981 |
*/ |
|
2982 |
function free() |
|
2983 |
{ |
|
2984 |
$this->result = null; |
|
2985 |
return MDB2_OK; |
|
2986 |
} |
|
2987 |
} |
|
2988 |
|
|
2989 |
// }}} |
|
2990 |
// {{{ class MDB2_Row |
|
2991 |
|
|
2992 |
/** |
|
2993 |
* Pear MDB2 Row Object |
|
2994 |
* @see MDB2_Driver_Common::setFetchMode() |
|
2995 |
*/ |
|
2996 |
class MDB2_Row |
|
2997 |
{ |
|
2998 |
// {{{ constructor |
|
2999 |
|
|
3000 |
/** |
|
3001 |
* constructor |
|
3002 |
* |
|
3003 |
* @param resource row data as array |
|
3004 |
*/ |
|
3005 |
function __construct(&$row) |
|
3006 |
{ |
|
3007 |
foreach ($row as $key => $value) { |
|
3008 |
$this->$key = &$row[$key]; |
|
3009 |
} |
|
3010 |
} |
|
3011 |
|
|
3012 |
function MDB2_Row(&$row) |
|
3013 |
{ |
|
3014 |
$this->__construct($row); |
|
3015 |
} |
|
3016 |
} |
|
3017 |
|
|
3018 |
class MDB2_Statement_Common |
|
3019 |
{ |
|
3020 |
var $db; |
|
3021 |
var $statement; |
|
3022 |
var $query; |
|
3023 |
var $result_types; |
|
3024 |
var $types; |
|
3025 |
var $values; |
|
3026 |
var $row_limit; |
|
3027 |
var $row_offset; |
|
3028 |
|
|
3029 |
// {{{ constructor |
|
3030 |
|
|
3031 |
/** |
|
3032 |
* Constructor |
|
3033 |
*/ |
|
3034 |
function __construct(&$db, &$statement, $query, $types, $result_types, $limit = null, $offset = null) |
|
3035 |
{ |
|
3036 |
$this->db =& $db; |
|
3037 |
$this->statement =& $statement; |
|
3038 |
$this->query = $query; |
|
3039 |
$this->types = (array)$types; |
|
3040 |
$this->result_types = (array)$result_types; |
|
3041 |
$this->row_limit = $limit; |
|
3042 |
$this->row_offset = $offset; |
|
3043 |
} |
|
3044 |
|
|
3045 |
function MDB2_Statement_Common(&$db, &$statement, $query, $types, $result_types, $limit = null, $offset = null) |
|
3046 |
{ |
|
3047 |
$this->__construct($db, $statement, $query, $types, $result_types, $limit, $offset); |
|
3048 |
} |
|
3049 |
|
|
3050 |
// }}} |
|
3051 |
// {{{ bindParam() |
|
3052 |
|
|
3053 |
/** |
|
3054 |
* Set the value of a parameter of a prepared query. |
|
3055 |
* |
|
3056 |
* @param int $parameter the order number of the parameter in the query |
|
3057 |
* statement. The order number of the first parameter is 1. |
|
3058 |
* @param mixed $value value that is meant to be assigned to specified |
|
3059 |
* parameter. The type of the value depends on the $type argument. |
|
3060 |
* @param string $type specifies the type of the field |
|
3061 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
3062 |
* @access public |
|
3063 |
*/ |
|
3064 |
function bindParam($parameter, &$value, $type = null) |
|
3065 |
{ |
|
3066 |
if (!is_numeric($parameter)) { |
|
3067 |
$parameter = preg_replace('/^:(.*)$/', '\\1', $parameter); |
|
3068 |
} |
|
3069 |
$this->values[$parameter] =& $value; |
|
3070 |
if (!is_null($type)) { |
|
3071 |
$this->types[$parameter] = $type; |
|
3072 |
} |
|
3073 |
return MDB2_OK; |
|
3074 |
} |
|
3075 |
|
|
3076 |
// }}} |
|
3077 |
// {{{ bindParamArray() |
|
3078 |
|
|
3079 |
/** |
|
3080 |
* Set the values of multiple a parameter of a prepared query in bulk. |
|
3081 |
* |
|
3082 |
* @param array $values array thats specifies all necessary infromation |
|
3083 |
* for bindParam() the array elements must use keys corresponding to |
|
3084 |
* the number of the position of the parameter. |
|
3085 |
* @param array $types specifies the types of the fields |
|
3086 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
3087 |
* @access public |
|
3088 |
* @see bindParam() |
|
3089 |
*/ |
|
3090 |
function bindParamArray(&$values, $types = null) |
|
3091 |
{ |
|
3092 |
$types = is_array($types) ? array_values($types) : array_fill(0, count($values), null); |
|
3093 |
$parameters = array_keys($values); |
|
3094 |
foreach ($parameters as $key => $parameter) { |
|
3095 |
$this->bindParam($parameter, $values[$parameter], $types[$key]); |
|
3096 |
} |
|
3097 |
return MDB2_OK; |
|
3098 |
} |
|
3099 |
|
|
3100 |
// }}} |
|
3101 |
// {{{ execute() |
|
3102 |
|
|
3103 |
/** |
|
3104 |
* Execute a prepared query statement. |
|
3105 |
* |
|
3106 |
* @param array $values array thats specifies all necessary infromation |
|
3107 |
* for bindParam() the array elements must use keys corresponding to |
|
3108 |
* the number of the position of the parameter. |
|
3109 |
* @param mixed $result_class string which specifies which result class to use |
|
3110 |
* @param mixed $result_wrap_class string which specifies which class to wrap results in |
|
3111 |
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure |
|
3112 |
* @access public |
|
3113 |
*/ |
|
3114 |
function &execute($values = null, $result_class = true, $result_wrap_class = false) |
|
3115 |
{ |
|
3116 |
if (!empty($values)) { |
|
3117 |
$this->bindParamArray($values); |
|
3118 |
} |
|
3119 |
$result =& $this->_execute($result_class, $result_wrap_class); |
|
3120 |
if (is_numeric($result)) { |
|
3121 |
$this->rownum = $result - 1; |
|
3122 |
} |
|
3123 |
return $result; |
|
3124 |
} |
|
3125 |
|
|
3126 |
// }}} |
|
3127 |
// {{{ _execute() |
|
3128 |
|
|
3129 |
/** |
|
3130 |
* Execute a prepared query statement helper method. |
|
3131 |
* |
|
3132 |
* @param mixed $result_class string which specifies which result class to use |
|
3133 |
* @param mixed $result_wrap_class string which specifies which class to wrap results in |
|
3134 |
* @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure |
|
3135 |
* @access private |
|
3136 |
*/ |
|
3137 |
function &_execute($result_class = true, $result_wrap_class = false) |
|
3138 |
{ |
|
3139 |
$query = ''; |
|
3140 |
$last_position = $i = 0; |
|
3141 |
foreach ($this->values as $parameter => $value) { |
|
3142 |
$current_position = $this->statement[$parameter]; |
|
3143 |
$query.= substr($this->query, $last_position, $current_position - $last_position); |
|
3144 |
if (!isset($value)) { |
|
3145 |
$value_quoted = 'NULL'; |
|
3146 |
} else { |
|
3147 |
$type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null; |
|
3148 |
$value_quoted = $this->db->quote($value, $type); |
|
3149 |
if (PEAR::isError($value_quoted)) { |
|
3150 |
return $value_quoted; |
|
3151 |
} |
|
3152 |
} |
|
3153 |
$query.= $value_quoted; |
|
3154 |
$last_position = $current_position + 1; |
|
3155 |
++$i; |
|
3156 |
} |
|
3157 |
$query.= substr($this->query, $last_position); |
|
3158 |
|
|
3159 |
$this->db->row_offset = $this->row_offset; |
|
3160 |
$this->db->row_limit = $this->row_limit; |
|
3161 |
$result =& $this->db->query($query, $this->result_types, $result_class, $result_wrap_class); |
|
3162 |
return $result; |
|
3163 |
} |
|
3164 |
|
|
3165 |
// }}} |
|
3166 |
// {{{ free() |
|
3167 |
|
|
3168 |
/** |
|
3169 |
* Release resources allocated for the specified prepared query. |
|
3170 |
* |
|
3171 |
* @return mixed MDB2_OK on success, a MDB2 error on failure |
|
3172 |
* @access public |
|
3173 |
*/ |
|
3174 |
function free() |
|
3175 |
{ |
|
3176 |
return MDB2_OK; |
|
3177 |
} |
|
3178 |
} |
|
3179 |
|
|
3180 |
class MDB2_Module_Common |
|
3181 |
{ |
|
3182 |
/** |
|
3183 |
* contains the key to the global MDB2 instance array of the associated |
|
3184 |
* MDB2 instance |
|
3185 |
* |
|
3186 |
* @var integer |
|
3187 |
* @access protected |
|
3188 |
*/ |
|
3189 |
var $db_index; |
|
3190 |
|
|
3191 |
// {{{ constructor |
|
3192 |
|
|
3193 |
/** |
|
3194 |
* Constructor |
|
3195 |
*/ |
|
3196 |
function __construct($db_index) |
|
3197 |
{ |
|
3198 |
$this->db_index = $db_index; |
|
3199 |
} |
|
3200 |
|
|
3201 |
function MDB2_Module_Common($db_index) |
|
3202 |
{ |
|
3203 |
$this->__construct($db_index); |
|
3204 |
} |
|
3205 |
|
|
3206 |
|
|
3207 |
// }}} |
|
3208 |
// {{{ getDBInstance() |
|
3209 |
|
|
3210 |
/** |
|
3211 |
* get the instance of MDB2 associated with the module instance |
|
3212 |
* |
|
3213 |
* @return object MDB2 instance or a MDB2 error on failure |
|
3214 |
* @access public |
|
3215 |
*/ |
|
3216 |
function &getDBInstance() |
|
3217 |
{ |
|
3218 |
if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) { |
|
3219 |
$result =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
3220 |
} else { |
|
3221 |
$result =& MDB2::raiseError(MDB2_ERROR, null, null, 'could not find MDB2 instance'); |
|
3222 |
} |
|
3223 |
return $result; |
|
3224 |
} |
|
3225 |
} |
|
3226 |
|
|
3227 |
// }}} |
|
3228 |
// {{{ MDB2_closeOpenTransactions() |
|
3229 |
|
|
3230 |
/** |
|
3231 |
* close any open transactions form persistant connections |
|
3232 |
* |
|
3233 |
* @return void |
|
3234 |
* @access public |
|
3235 |
*/ |
|
3236 |
function MDB2_closeOpenTransactions() |
|
3237 |
{ |
|
3238 |
reset($GLOBALS['_MDB2_databases']); |
|
3239 |
while (next($GLOBALS['_MDB2_databases'])) { |
|
3240 |
$key = key($GLOBALS['_MDB2_databases']); |
|
3241 |
if ($GLOBALS['_MDB2_databases'][$key]->opened_persistent |
|
3242 |
&& $GLOBALS['_MDB2_databases'][$key]->in_transaction |
|
3243 |
) { |
|
3244 |
$GLOBALS['_MDB2_databases'][$key]->rollback(); |
|
3245 |
} |
|
3246 |
} |
|
3247 |
} |
|
3248 |
|
|
3249 |
// }}} |
|
3250 |
// {{{ MDB2_defaultDebugOutput() |
|
3251 |
|
|
3252 |
/** |
|
3253 |
* default debug output handler |
|
3254 |
* |
|
3255 |
* @param object $db reference to an MDB2 database object |
|
3256 |
* @param string $message message that should be appended to the debug |
|
3257 |
* variable |
|
3258 |
* @return string the corresponding error message, of false |
|
3259 |
* if the error code was unknown |
|
3260 |
* @access public |
|
3261 |
*/ |
|
3262 |
function MDB2_defaultDebugOutput(&$db, $scope, $message) |
|
3263 |
{ |
|
3264 |
$db->debug_output.= $scope.'('.$db->db_index.'): '; |
|
3265 |
$db->debug_output.= $message.$db->getOption('log_line_break'); |
|
3266 |
} |
|
3267 |
|
|
3268 |
?> |