commit | author | age
|
2273d4
|
1 |
<?php |
A |
2 |
// +----------------------------------------------------------------------+ |
|
3 |
// | PHP versions 4 and 5 | |
|
4 |
// +----------------------------------------------------------------------+ |
|
5 |
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, | |
|
6 |
// | Stig. S. Bakken, Lukas Smith | |
|
7 |
// | All rights reserved. | |
|
8 |
// +----------------------------------------------------------------------+ |
|
9 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB | |
|
10 |
// | API as well as database abstraction for PHP applications. | |
|
11 |
// | This LICENSE is in the BSD license style. | |
|
12 |
// | | |
|
13 |
// | Redistribution and use in source and binary forms, with or without | |
|
14 |
// | modification, are permitted provided that the following conditions | |
|
15 |
// | are met: | |
|
16 |
// | | |
|
17 |
// | Redistributions of source code must retain the above copyright | |
|
18 |
// | notice, this list of conditions and the following disclaimer. | |
|
19 |
// | | |
|
20 |
// | Redistributions in binary form must reproduce the above copyright | |
|
21 |
// | notice, this list of conditions and the following disclaimer in the | |
|
22 |
// | documentation and/or other materials provided with the distribution. | |
|
23 |
// | | |
|
24 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, | |
|
25 |
// | Lukas Smith nor the names of his contributors may be used to endorse | |
|
26 |
// | or promote products derived from this software without specific prior| |
|
27 |
// | written permission. | |
|
28 |
// | | |
|
29 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
|
30 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
|
31 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
|
32 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
|
33 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
|
34 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
|
35 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS| |
|
36 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
|
37 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
|
38 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY| |
|
39 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
|
40 |
// | POSSIBILITY OF SUCH DAMAGE. | |
|
41 |
// +----------------------------------------------------------------------+ |
|
42 |
// | Author: Lukas Smith <smith@pooteeweet.org> | |
|
43 |
// +----------------------------------------------------------------------+ |
|
44 |
// |
|
45 |
// $Id: Extended.php 292715 2009-12-28 14:06:34Z quipo $ |
|
46 |
|
|
47 |
/** |
|
48 |
* @package MDB2 |
|
49 |
* @category Database |
|
50 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
51 |
*/ |
|
52 |
|
|
53 |
/** |
|
54 |
* Used by autoPrepare() |
|
55 |
*/ |
|
56 |
define('MDB2_AUTOQUERY_INSERT', 1); |
|
57 |
define('MDB2_AUTOQUERY_UPDATE', 2); |
|
58 |
define('MDB2_AUTOQUERY_DELETE', 3); |
|
59 |
define('MDB2_AUTOQUERY_SELECT', 4); |
|
60 |
|
|
61 |
/** |
|
62 |
* MDB2_Extended: class which adds several high level methods to MDB2 |
|
63 |
* |
|
64 |
* @package MDB2 |
|
65 |
* @category Database |
|
66 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
67 |
*/ |
|
68 |
class MDB2_Extended extends MDB2_Module_Common |
|
69 |
{ |
|
70 |
// {{{ autoPrepare() |
|
71 |
|
|
72 |
/** |
|
73 |
* Generate an insert, update or delete query and call prepare() on it |
|
74 |
* |
|
75 |
* @param string table |
|
76 |
* @param array the fields names |
|
77 |
* @param int type of query to build |
|
78 |
* MDB2_AUTOQUERY_INSERT |
|
79 |
* MDB2_AUTOQUERY_UPDATE |
|
80 |
* MDB2_AUTOQUERY_DELETE |
|
81 |
* MDB2_AUTOQUERY_SELECT |
|
82 |
* @param string (in case of update and delete queries, this string will be put after the sql WHERE statement) |
|
83 |
* @param array that contains the types of the placeholders |
|
84 |
* @param mixed array that contains the types of the columns in |
|
85 |
* the result set or MDB2_PREPARE_RESULT, if set to |
|
86 |
* MDB2_PREPARE_MANIP the query is handled as a manipulation query |
|
87 |
* |
|
88 |
* @return resource handle for the query |
|
89 |
* @see buildManipSQL |
|
90 |
* @access public |
|
91 |
*/ |
|
92 |
function autoPrepare($table, $table_fields, $mode = MDB2_AUTOQUERY_INSERT, |
|
93 |
$where = false, $types = null, $result_types = MDB2_PREPARE_MANIP) |
|
94 |
{ |
|
95 |
$query = $this->buildManipSQL($table, $table_fields, $mode, $where); |
|
96 |
if (PEAR::isError($query)) { |
|
97 |
return $query; |
|
98 |
} |
|
99 |
$db =& $this->getDBInstance(); |
|
100 |
if (PEAR::isError($db)) { |
|
101 |
return $db; |
|
102 |
} |
|
103 |
$lobs = array(); |
|
104 |
foreach ((array)$types as $param => $type) { |
|
105 |
if (($type == 'clob') || ($type == 'blob')) { |
|
106 |
$lobs[$param] = $table_fields[$param]; |
|
107 |
} |
|
108 |
} |
|
109 |
return $db->prepare($query, $types, $result_types, $lobs); |
|
110 |
} |
|
111 |
|
|
112 |
// }}} |
|
113 |
// {{{ autoExecute() |
|
114 |
|
|
115 |
/** |
|
116 |
* Generate an insert, update or delete query and call prepare() and execute() on it |
|
117 |
* |
|
118 |
* @param string name of the table |
|
119 |
* @param array assoc ($key=>$value) where $key is a field name and $value its value |
|
120 |
* @param int type of query to build |
|
121 |
* MDB2_AUTOQUERY_INSERT |
|
122 |
* MDB2_AUTOQUERY_UPDATE |
|
123 |
* MDB2_AUTOQUERY_DELETE |
|
124 |
* MDB2_AUTOQUERY_SELECT |
|
125 |
* @param string (in case of update and delete queries, this string will be put after the sql WHERE statement) |
|
126 |
* @param array that contains the types of the placeholders |
|
127 |
* @param string which specifies which result class to use |
|
128 |
* @param mixed array that contains the types of the columns in |
|
129 |
* the result set or MDB2_PREPARE_RESULT, if set to |
|
130 |
* MDB2_PREPARE_MANIP the query is handled as a manipulation query |
|
131 |
* |
|
132 |
* @return bool|MDB2_Error true on success, a MDB2 error on failure |
|
133 |
* @see buildManipSQL |
|
134 |
* @see autoPrepare |
|
135 |
* @access public |
|
136 |
*/ |
|
137 |
function &autoExecute($table, $fields_values, $mode = MDB2_AUTOQUERY_INSERT, |
|
138 |
$where = false, $types = null, $result_class = true, $result_types = MDB2_PREPARE_MANIP) |
|
139 |
{ |
|
140 |
$fields_values = (array)$fields_values; |
|
141 |
if ($mode == MDB2_AUTOQUERY_SELECT) { |
|
142 |
if (is_array($result_types)) { |
|
143 |
$keys = array_keys($result_types); |
|
144 |
} elseif (!empty($fields_values)) { |
|
145 |
$keys = $fields_values; |
|
146 |
} else { |
|
147 |
$keys = array(); |
|
148 |
} |
|
149 |
} else { |
|
150 |
$keys = array_keys($fields_values); |
|
151 |
} |
|
152 |
$params = array_values($fields_values); |
|
153 |
if (empty($params)) { |
|
154 |
$query = $this->buildManipSQL($table, $keys, $mode, $where); |
|
155 |
|
|
156 |
$db =& $this->getDBInstance(); |
|
157 |
if (PEAR::isError($db)) { |
|
158 |
return $db; |
|
159 |
} |
|
160 |
if ($mode == MDB2_AUTOQUERY_SELECT) { |
|
161 |
$result =& $db->query($query, $result_types, $result_class); |
|
162 |
} else { |
|
163 |
$result = $db->exec($query); |
|
164 |
} |
|
165 |
} else { |
|
166 |
$stmt = $this->autoPrepare($table, $keys, $mode, $where, $types, $result_types); |
|
167 |
if (PEAR::isError($stmt)) { |
|
168 |
return $stmt; |
|
169 |
} |
|
170 |
$result =& $stmt->execute($params, $result_class); |
|
171 |
$stmt->free(); |
|
172 |
} |
|
173 |
return $result; |
|
174 |
} |
|
175 |
|
|
176 |
// }}} |
|
177 |
// {{{ buildManipSQL() |
|
178 |
|
|
179 |
/** |
|
180 |
* Make automaticaly an sql query for prepare() |
|
181 |
* |
|
182 |
* Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), MDB2_AUTOQUERY_INSERT) |
|
183 |
* will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?) |
|
184 |
* NB : - This belongs more to a SQL Builder class, but this is a simple facility |
|
185 |
* - Be carefull ! If you don't give a $where param with an UPDATE/DELETE query, all |
|
186 |
* the records of the table will be updated/deleted ! |
|
187 |
* |
|
188 |
* @param string name of the table |
|
189 |
* @param ordered array containing the fields names |
|
190 |
* @param int type of query to build |
|
191 |
* MDB2_AUTOQUERY_INSERT |
|
192 |
* MDB2_AUTOQUERY_UPDATE |
|
193 |
* MDB2_AUTOQUERY_DELETE |
|
194 |
* MDB2_AUTOQUERY_SELECT |
|
195 |
* @param string (in case of update and delete queries, this string will be put after the sql WHERE statement) |
|
196 |
* |
|
197 |
* @return string sql query for prepare() |
|
198 |
* @access public |
|
199 |
*/ |
|
200 |
function buildManipSQL($table, $table_fields, $mode, $where = false) |
|
201 |
{ |
|
202 |
$db =& $this->getDBInstance(); |
|
203 |
if (PEAR::isError($db)) { |
|
204 |
return $db; |
|
205 |
} |
|
206 |
|
|
207 |
if ($db->options['quote_identifier']) { |
|
208 |
$table = $db->quoteIdentifier($table); |
|
209 |
} |
|
210 |
|
|
211 |
if (!empty($table_fields) && $db->options['quote_identifier']) { |
|
212 |
foreach ($table_fields as $key => $field) { |
|
213 |
$table_fields[$key] = $db->quoteIdentifier($field); |
|
214 |
} |
|
215 |
} |
|
216 |
|
|
217 |
if ((false !== $where) && (null !== $where)) { |
|
218 |
if (is_array($where)) { |
|
219 |
$where = implode(' AND ', $where); |
|
220 |
} |
|
221 |
$where = ' WHERE '.$where; |
|
222 |
} |
|
223 |
|
|
224 |
switch ($mode) { |
|
225 |
case MDB2_AUTOQUERY_INSERT: |
|
226 |
if (empty($table_fields)) { |
|
227 |
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
228 |
'Insert requires table fields', __FUNCTION__); |
|
229 |
} |
|
230 |
$cols = implode(', ', $table_fields); |
|
231 |
$values = '?'.str_repeat(', ?', (count($table_fields) - 1)); |
|
232 |
return 'INSERT INTO '.$table.' ('.$cols.') VALUES ('.$values.')'; |
|
233 |
break; |
|
234 |
case MDB2_AUTOQUERY_UPDATE: |
|
235 |
if (empty($table_fields)) { |
|
236 |
return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, |
|
237 |
'Update requires table fields', __FUNCTION__); |
|
238 |
} |
|
239 |
$set = implode(' = ?, ', $table_fields).' = ?'; |
|
240 |
$sql = 'UPDATE '.$table.' SET '.$set.$where; |
|
241 |
return $sql; |
|
242 |
break; |
|
243 |
case MDB2_AUTOQUERY_DELETE: |
|
244 |
$sql = 'DELETE FROM '.$table.$where; |
|
245 |
return $sql; |
|
246 |
break; |
|
247 |
case MDB2_AUTOQUERY_SELECT: |
|
248 |
$cols = !empty($table_fields) ? implode(', ', $table_fields) : '*'; |
|
249 |
$sql = 'SELECT '.$cols.' FROM '.$table.$where; |
|
250 |
return $sql; |
|
251 |
break; |
|
252 |
} |
|
253 |
return $db->raiseError(MDB2_ERROR_SYNTAX, null, null, |
|
254 |
'Non existant mode', __FUNCTION__); |
|
255 |
} |
|
256 |
|
|
257 |
// }}} |
|
258 |
// {{{ limitQuery() |
|
259 |
|
|
260 |
/** |
|
261 |
* Generates a limited query |
|
262 |
* |
|
263 |
* @param string query |
|
264 |
* @param array that contains the types of the columns in the result set |
|
265 |
* @param integer the numbers of rows to fetch |
|
266 |
* @param integer the row to start to fetching |
|
267 |
* @param string which specifies which result class to use |
|
268 |
* @param mixed string which specifies which class to wrap results in |
|
269 |
* |
|
270 |
* @return MDB2_Result|MDB2_Error result set on success, a MDB2 error on failure |
|
271 |
* @access public |
|
272 |
*/ |
|
273 |
function &limitQuery($query, $types, $limit, $offset = 0, $result_class = true, |
|
274 |
$result_wrap_class = false) |
|
275 |
{ |
|
276 |
$db =& $this->getDBInstance(); |
|
277 |
if (PEAR::isError($db)) { |
|
278 |
return $db; |
|
279 |
} |
|
280 |
|
|
281 |
$result = $db->setLimit($limit, $offset); |
|
282 |
if (PEAR::isError($result)) { |
|
283 |
return $result; |
|
284 |
} |
|
285 |
$result =& $db->query($query, $types, $result_class, $result_wrap_class); |
|
286 |
return $result; |
|
287 |
} |
|
288 |
|
|
289 |
// }}} |
|
290 |
// {{{ execParam() |
|
291 |
|
|
292 |
/** |
|
293 |
* Execute a parameterized DML statement. |
|
294 |
* |
|
295 |
* @param string the SQL query |
|
296 |
* @param array if supplied, prepare/execute will be used |
|
297 |
* with this array as execute parameters |
|
298 |
* @param array that contains the types of the values defined in $params |
|
299 |
* |
|
300 |
* @return int|MDB2_Error affected rows on success, a MDB2 error on failure |
|
301 |
* @access public |
|
302 |
*/ |
|
303 |
function execParam($query, $params = array(), $param_types = null) |
|
304 |
{ |
|
305 |
$db =& $this->getDBInstance(); |
|
306 |
if (PEAR::isError($db)) { |
|
307 |
return $db; |
|
308 |
} |
|
309 |
|
|
310 |
settype($params, 'array'); |
|
311 |
if (empty($params)) { |
|
312 |
return $db->exec($query); |
|
313 |
} |
|
314 |
|
|
315 |
$stmt = $db->prepare($query, $param_types, MDB2_PREPARE_MANIP); |
|
316 |
if (PEAR::isError($stmt)) { |
|
317 |
return $stmt; |
|
318 |
} |
|
319 |
|
|
320 |
$result = $stmt->execute($params); |
|
321 |
if (PEAR::isError($result)) { |
|
322 |
return $result; |
|
323 |
} |
|
324 |
|
|
325 |
$stmt->free(); |
|
326 |
return $result; |
|
327 |
} |
|
328 |
|
|
329 |
// }}} |
|
330 |
// {{{ getOne() |
|
331 |
|
|
332 |
/** |
|
333 |
* Fetch the first column of the first row of data returned from a query. |
|
334 |
* Takes care of doing the query and freeing the results when finished. |
|
335 |
* |
|
336 |
* @param string the SQL query |
|
337 |
* @param string that contains the type of the column in the result set |
|
338 |
* @param array if supplied, prepare/execute will be used |
|
339 |
* with this array as execute parameters |
|
340 |
* @param array that contains the types of the values defined in $params |
|
341 |
* @param int|string which column to return |
|
342 |
* |
|
343 |
* @return scalar|MDB2_Error data on success, a MDB2 error on failure |
|
344 |
* @access public |
|
345 |
*/ |
|
346 |
function getOne($query, $type = null, $params = array(), |
|
347 |
$param_types = null, $colnum = 0) |
|
348 |
{ |
|
349 |
$db =& $this->getDBInstance(); |
|
350 |
if (PEAR::isError($db)) { |
|
351 |
return $db; |
|
352 |
} |
|
353 |
|
|
354 |
settype($params, 'array'); |
|
355 |
settype($type, 'array'); |
|
356 |
if (empty($params)) { |
|
357 |
return $db->queryOne($query, $type, $colnum); |
|
358 |
} |
|
359 |
|
|
360 |
$stmt = $db->prepare($query, $param_types, $type); |
|
361 |
if (PEAR::isError($stmt)) { |
|
362 |
return $stmt; |
|
363 |
} |
|
364 |
|
|
365 |
$result = $stmt->execute($params); |
|
366 |
if (!MDB2::isResultCommon($result)) { |
|
367 |
return $result; |
|
368 |
} |
|
369 |
|
|
370 |
$one = $result->fetchOne($colnum); |
|
371 |
$stmt->free(); |
|
372 |
$result->free(); |
|
373 |
return $one; |
|
374 |
} |
|
375 |
|
|
376 |
// }}} |
|
377 |
// {{{ getRow() |
|
378 |
|
|
379 |
/** |
|
380 |
* Fetch the first row of data returned from a query. Takes care |
|
381 |
* of doing the query and freeing the results when finished. |
|
382 |
* |
|
383 |
* @param string the SQL query |
|
384 |
* @param array that contains the types of the columns in the result set |
|
385 |
* @param array if supplied, prepare/execute will be used |
|
386 |
* with this array as execute parameters |
|
387 |
* @param array that contains the types of the values defined in $params |
|
388 |
* @param int the fetch mode to use |
|
389 |
* |
|
390 |
* @return array|MDB2_Error data on success, a MDB2 error on failure |
|
391 |
* @access public |
|
392 |
*/ |
|
393 |
function getRow($query, $types = null, $params = array(), |
|
394 |
$param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT) |
|
395 |
{ |
|
396 |
$db =& $this->getDBInstance(); |
|
397 |
if (PEAR::isError($db)) { |
|
398 |
return $db; |
|
399 |
} |
|
400 |
|
|
401 |
settype($params, 'array'); |
|
402 |
if (empty($params)) { |
|
403 |
return $db->queryRow($query, $types, $fetchmode); |
|
404 |
} |
|
405 |
|
|
406 |
$stmt = $db->prepare($query, $param_types, $types); |
|
407 |
if (PEAR::isError($stmt)) { |
|
408 |
return $stmt; |
|
409 |
} |
|
410 |
|
|
411 |
$result = $stmt->execute($params); |
|
412 |
if (!MDB2::isResultCommon($result)) { |
|
413 |
return $result; |
|
414 |
} |
|
415 |
|
|
416 |
$row = $result->fetchRow($fetchmode); |
|
417 |
$stmt->free(); |
|
418 |
$result->free(); |
|
419 |
return $row; |
|
420 |
} |
|
421 |
|
|
422 |
// }}} |
|
423 |
// {{{ getCol() |
|
424 |
|
|
425 |
/** |
|
426 |
* Fetch a single column from a result set and return it as an |
|
427 |
* indexed array. |
|
428 |
* |
|
429 |
* @param string the SQL query |
|
430 |
* @param string that contains the type of the column in the result set |
|
431 |
* @param array if supplied, prepare/execute will be used |
|
432 |
* with this array as execute parameters |
|
433 |
* @param array that contains the types of the values defined in $params |
|
434 |
* @param int|string which column to return |
|
435 |
* |
|
436 |
* @return array|MDB2_Error data on success, a MDB2 error on failure |
|
437 |
* @access public |
|
438 |
*/ |
|
439 |
function getCol($query, $type = null, $params = array(), |
|
440 |
$param_types = null, $colnum = 0) |
|
441 |
{ |
|
442 |
$db =& $this->getDBInstance(); |
|
443 |
if (PEAR::isError($db)) { |
|
444 |
return $db; |
|
445 |
} |
|
446 |
|
|
447 |
settype($params, 'array'); |
|
448 |
settype($type, 'array'); |
|
449 |
if (empty($params)) { |
|
450 |
return $db->queryCol($query, $type, $colnum); |
|
451 |
} |
|
452 |
|
|
453 |
$stmt = $db->prepare($query, $param_types, $type); |
|
454 |
if (PEAR::isError($stmt)) { |
|
455 |
return $stmt; |
|
456 |
} |
|
457 |
|
|
458 |
$result = $stmt->execute($params); |
|
459 |
if (!MDB2::isResultCommon($result)) { |
|
460 |
return $result; |
|
461 |
} |
|
462 |
|
|
463 |
$col = $result->fetchCol($colnum); |
|
464 |
$stmt->free(); |
|
465 |
$result->free(); |
|
466 |
return $col; |
|
467 |
} |
|
468 |
|
|
469 |
// }}} |
|
470 |
// {{{ getAll() |
|
471 |
|
|
472 |
/** |
|
473 |
* Fetch all the rows returned from a query. |
|
474 |
* |
|
475 |
* @param string the SQL query |
|
476 |
* @param array that contains the types of the columns in the result set |
|
477 |
* @param array if supplied, prepare/execute will be used |
|
478 |
* with this array as execute parameters |
|
479 |
* @param array that contains the types of the values defined in $params |
|
480 |
* @param int the fetch mode to use |
|
481 |
* @param bool if set to true, the $all will have the first |
|
482 |
* column as its first dimension |
|
483 |
* @param bool $force_array used only when the query returns exactly |
|
484 |
* two columns. If true, the values of the returned array will be |
|
485 |
* one-element arrays instead of scalars. |
|
486 |
* @param bool $group if true, the values of the returned array is |
|
487 |
* wrapped in another array. If the same key value (in the first |
|
488 |
* column) repeats itself, the values will be appended to this array |
|
489 |
* instead of overwriting the existing values. |
|
490 |
* |
|
491 |
* @return array|MDB2_Error data on success, a MDB2 error on failure |
|
492 |
* @access public |
|
493 |
*/ |
|
494 |
function getAll($query, $types = null, $params = array(), |
|
495 |
$param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT, |
|
496 |
$rekey = false, $force_array = false, $group = false) |
|
497 |
{ |
|
498 |
$db =& $this->getDBInstance(); |
|
499 |
if (PEAR::isError($db)) { |
|
500 |
return $db; |
|
501 |
} |
|
502 |
|
|
503 |
settype($params, 'array'); |
|
504 |
if (empty($params)) { |
|
505 |
return $db->queryAll($query, $types, $fetchmode, $rekey, $force_array, $group); |
|
506 |
} |
|
507 |
|
|
508 |
$stmt = $db->prepare($query, $param_types, $types); |
|
509 |
if (PEAR::isError($stmt)) { |
|
510 |
return $stmt; |
|
511 |
} |
|
512 |
|
|
513 |
$result = $stmt->execute($params); |
|
514 |
if (!MDB2::isResultCommon($result)) { |
|
515 |
return $result; |
|
516 |
} |
|
517 |
|
|
518 |
$all = $result->fetchAll($fetchmode, $rekey, $force_array, $group); |
|
519 |
$stmt->free(); |
|
520 |
$result->free(); |
|
521 |
return $all; |
|
522 |
} |
|
523 |
|
|
524 |
// }}} |
|
525 |
// {{{ getAssoc() |
|
526 |
|
|
527 |
/** |
|
528 |
* Fetch the entire result set of a query and return it as an |
|
529 |
* associative array using the first column as the key. |
|
530 |
* |
|
531 |
* If the result set contains more than two columns, the value |
|
532 |
* will be an array of the values from column 2-n. If the result |
|
533 |
* set contains only two columns, the returned value will be a |
|
534 |
* scalar with the value of the second column (unless forced to an |
|
535 |
* array with the $force_array parameter). A MDB2 error code is |
|
536 |
* returned on errors. If the result set contains fewer than two |
|
537 |
* columns, a MDB2_ERROR_TRUNCATED error is returned. |
|
538 |
* |
|
539 |
* For example, if the table 'mytable' contains: |
|
540 |
* <pre> |
|
541 |
* ID TEXT DATE |
|
542 |
* -------------------------------- |
|
543 |
* 1 'one' 944679408 |
|
544 |
* 2 'two' 944679408 |
|
545 |
* 3 'three' 944679408 |
|
546 |
* </pre> |
|
547 |
* Then the call getAssoc('SELECT id,text FROM mytable') returns: |
|
548 |
* <pre> |
|
549 |
* array( |
|
550 |
* '1' => 'one', |
|
551 |
* '2' => 'two', |
|
552 |
* '3' => 'three', |
|
553 |
* ) |
|
554 |
* </pre> |
|
555 |
* ...while the call getAssoc('SELECT id,text,date FROM mytable') returns: |
|
556 |
* <pre> |
|
557 |
* array( |
|
558 |
* '1' => array('one', '944679408'), |
|
559 |
* '2' => array('two', '944679408'), |
|
560 |
* '3' => array('three', '944679408') |
|
561 |
* ) |
|
562 |
* </pre> |
|
563 |
* |
|
564 |
* If the more than one row occurs with the same value in the |
|
565 |
* first column, the last row overwrites all previous ones by |
|
566 |
* default. Use the $group parameter if you don't want to |
|
567 |
* overwrite like this. Example: |
|
568 |
* <pre> |
|
569 |
* getAssoc('SELECT category,id,name FROM mytable', null, null |
|
570 |
* MDB2_FETCHMODE_ASSOC, false, true) returns: |
|
571 |
* array( |
|
572 |
* '1' => array(array('id' => '4', 'name' => 'number four'), |
|
573 |
* array('id' => '6', 'name' => 'number six') |
|
574 |
* ), |
|
575 |
* '9' => array(array('id' => '4', 'name' => 'number four'), |
|
576 |
* array('id' => '6', 'name' => 'number six') |
|
577 |
* ) |
|
578 |
* ) |
|
579 |
* </pre> |
|
580 |
* |
|
581 |
* Keep in mind that database functions in PHP usually return string |
|
582 |
* values for results regardless of the database's internal type. |
|
583 |
* |
|
584 |
* @param string the SQL query |
|
585 |
* @param array that contains the types of the columns in the result set |
|
586 |
* @param array if supplied, prepare/execute will be used |
|
587 |
* with this array as execute parameters |
|
588 |
* @param array that contains the types of the values defined in $params |
|
589 |
* @param bool $force_array used only when the query returns |
|
590 |
* exactly two columns. If TRUE, the values of the returned array |
|
591 |
* will be one-element arrays instead of scalars. |
|
592 |
* @param bool $group if TRUE, the values of the returned array |
|
593 |
* is wrapped in another array. If the same key value (in the first |
|
594 |
* column) repeats itself, the values will be appended to this array |
|
595 |
* instead of overwriting the existing values. |
|
596 |
* |
|
597 |
* @return array|MDB2_Error data on success, a MDB2 error on failure |
|
598 |
* @access public |
|
599 |
*/ |
|
600 |
function getAssoc($query, $types = null, $params = array(), $param_types = null, |
|
601 |
$fetchmode = MDB2_FETCHMODE_DEFAULT, $force_array = false, $group = false) |
|
602 |
{ |
|
603 |
$db =& $this->getDBInstance(); |
|
604 |
if (PEAR::isError($db)) { |
|
605 |
return $db; |
|
606 |
} |
|
607 |
|
|
608 |
settype($params, 'array'); |
|
609 |
if (empty($params)) { |
|
610 |
return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group); |
|
611 |
} |
|
612 |
|
|
613 |
$stmt = $db->prepare($query, $param_types, $types); |
|
614 |
if (PEAR::isError($stmt)) { |
|
615 |
return $stmt; |
|
616 |
} |
|
617 |
|
|
618 |
$result = $stmt->execute($params); |
|
619 |
if (!MDB2::isResultCommon($result)) { |
|
620 |
return $result; |
|
621 |
} |
|
622 |
|
|
623 |
$all = $result->fetchAll($fetchmode, true, $force_array, $group); |
|
624 |
$stmt->free(); |
|
625 |
$result->free(); |
|
626 |
return $all; |
|
627 |
} |
|
628 |
|
|
629 |
// }}} |
|
630 |
// {{{ executeMultiple() |
|
631 |
|
|
632 |
/** |
|
633 |
* This function does several execute() calls on the same statement handle. |
|
634 |
* $params must be an array indexed numerically from 0, one execute call is |
|
635 |
* done for every 'row' in the array. |
|
636 |
* |
|
637 |
* If an error occurs during execute(), executeMultiple() does not execute |
|
638 |
* the unfinished rows, but rather returns that error. |
|
639 |
* |
|
640 |
* @param resource query handle from prepare() |
|
641 |
* @param array numeric array containing the data to insert into the query |
|
642 |
* |
|
643 |
* @return bool|MDB2_Error true on success, a MDB2 error on failure |
|
644 |
* @access public |
|
645 |
* @see prepare(), execute() |
|
646 |
*/ |
|
647 |
function executeMultiple(&$stmt, $params = null) |
|
648 |
{ |
|
649 |
for ($i = 0, $j = count($params); $i < $j; $i++) { |
|
650 |
$result = $stmt->execute($params[$i]); |
|
651 |
if (PEAR::isError($result)) { |
|
652 |
return $result; |
|
653 |
} |
|
654 |
} |
|
655 |
return MDB2_OK; |
|
656 |
} |
|
657 |
|
|
658 |
// }}} |
|
659 |
// {{{ getBeforeID() |
|
660 |
|
|
661 |
/** |
|
662 |
* Returns the next free id of a sequence if the RDBMS |
|
663 |
* does not support auto increment |
|
664 |
* |
|
665 |
* @param string name of the table into which a new row was inserted |
|
666 |
* @param string name of the field into which a new row was inserted |
|
667 |
* @param bool when true the sequence is automatic created, if it not exists |
|
668 |
* @param bool if the returned value should be quoted |
|
669 |
* |
|
670 |
* @return int|MDB2_Error id on success, a MDB2 error on failure |
|
671 |
* @access public |
|
672 |
*/ |
|
673 |
function getBeforeID($table, $field = null, $ondemand = true, $quote = true) |
|
674 |
{ |
|
675 |
$db =& $this->getDBInstance(); |
|
676 |
if (PEAR::isError($db)) { |
|
677 |
return $db; |
|
678 |
} |
|
679 |
|
|
680 |
if ($db->supports('auto_increment') !== true) { |
|
681 |
$seq = $table.(empty($field) ? '' : '_'.$field); |
|
682 |
$id = $db->nextID($seq, $ondemand); |
|
683 |
if (!$quote || PEAR::isError($id)) { |
|
684 |
return $id; |
|
685 |
} |
|
686 |
return $db->quote($id, 'integer'); |
|
687 |
} elseif (!$quote) { |
|
688 |
return null; |
|
689 |
} |
|
690 |
return 'NULL'; |
|
691 |
} |
|
692 |
|
|
693 |
// }}} |
|
694 |
// {{{ getAfterID() |
|
695 |
|
|
696 |
/** |
|
697 |
* Returns the autoincrement ID if supported or $id |
|
698 |
* |
|
699 |
* @param mixed value as returned by getBeforeId() |
|
700 |
* @param string name of the table into which a new row was inserted |
|
701 |
* @param string name of the field into which a new row was inserted |
|
702 |
* |
|
703 |
* @return int|MDB2_Error id on success, a MDB2 error on failure |
|
704 |
* @access public |
|
705 |
*/ |
|
706 |
function getAfterID($id, $table, $field = null) |
|
707 |
{ |
|
708 |
$db =& $this->getDBInstance(); |
|
709 |
if (PEAR::isError($db)) { |
|
710 |
return $db; |
|
711 |
} |
|
712 |
|
|
713 |
if ($db->supports('auto_increment') !== true) { |
|
714 |
return $id; |
|
715 |
} |
|
716 |
return $db->lastInsertID($table, $field); |
|
717 |
} |
|
718 |
|
|
719 |
// }}} |
|
720 |
} |
95ebbc
|
721 |
?> |