PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/bak/PEAR.OLD/MDB2/Extended.php

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