Extended.php 26 KB

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