PageRenderTime 72ms CodeModel.GetById 43ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/pear/MDB2/Extended.php

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