PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/chamilo/chamilo/
PHP | 721 lines | 314 code | 61 blank | 346 comment | 62 complexity | 26b3a3f28fed7b8eac3433616275ced1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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,
  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 ($where !== false && !is_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. $result =& $db->query($query, $types, $result_class, $result_wrap_class);
  270. return $result;
  271. }
  272. // }}}
  273. // {{{ execParam()
  274. /**
  275. * Execute a parameterized DML statement.
  276. *
  277. * @param string the SQL query
  278. * @param array if supplied, prepare/execute will be used
  279. * with this array as execute parameters
  280. * @param array that contains the types of the values defined in $params
  281. *
  282. * @return int|MDB2_Error affected rows on success, a MDB2 error on failure
  283. * @access public
  284. */
  285. function execParam($query, $params = array(), $param_types = null)
  286. {
  287. $db =& $this->getDBInstance();
  288. if (PEAR::isError($db)) {
  289. return $db;
  290. }
  291. settype($params, 'array');
  292. if (empty($params)) {
  293. return $db->exec($query);
  294. }
  295. $stmt = $db->prepare($query, $param_types, MDB2_PREPARE_MANIP);
  296. if (PEAR::isError($stmt)) {
  297. return $stmt;
  298. }
  299. $result = $stmt->execute($params);
  300. if (PEAR::isError($result)) {
  301. return $result;
  302. }
  303. $stmt->free();
  304. return $result;
  305. }
  306. // }}}
  307. // {{{ getOne()
  308. /**
  309. * Fetch the first column of the first row of data returned from a query.
  310. * Takes care of doing the query and freeing the results when finished.
  311. *
  312. * @param string the SQL query
  313. * @param string that contains the type of the column in the result set
  314. * @param array if supplied, prepare/execute will be used
  315. * with this array as execute parameters
  316. * @param array that contains the types of the values defined in $params
  317. * @param int|string which column to return
  318. *
  319. * @return scalar|MDB2_Error data on success, a MDB2 error on failure
  320. * @access public
  321. */
  322. function getOne($query, $type = null, $params = array(),
  323. $param_types = null, $colnum = 0)
  324. {
  325. $db =& $this->getDBInstance();
  326. if (PEAR::isError($db)) {
  327. return $db;
  328. }
  329. settype($params, 'array');
  330. settype($type, 'array');
  331. if (empty($params)) {
  332. return $db->queryOne($query, $type, $colnum);
  333. }
  334. $stmt = $db->prepare($query, $param_types, $type);
  335. if (PEAR::isError($stmt)) {
  336. return $stmt;
  337. }
  338. $result = $stmt->execute($params);
  339. if (!MDB2::isResultCommon($result)) {
  340. return $result;
  341. }
  342. $one = $result->fetchOne($colnum);
  343. $stmt->free();
  344. $result->free();
  345. return $one;
  346. }
  347. // }}}
  348. // {{{ getRow()
  349. /**
  350. * Fetch the first row of data returned from a query. Takes care
  351. * of doing the query and freeing the results when finished.
  352. *
  353. * @param string the SQL query
  354. * @param array that contains the types of the columns in the result set
  355. * @param array if supplied, prepare/execute will be used
  356. * with this array as execute parameters
  357. * @param array that contains the types of the values defined in $params
  358. * @param int the fetch mode to use
  359. *
  360. * @return array|MDB2_Error data on success, a MDB2 error on failure
  361. * @access public
  362. */
  363. function getRow($query, $types = null, $params = array(),
  364. $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
  365. {
  366. $db =& $this->getDBInstance();
  367. if (PEAR::isError($db)) {
  368. return $db;
  369. }
  370. settype($params, 'array');
  371. if (empty($params)) {
  372. return $db->queryRow($query, $types, $fetchmode);
  373. }
  374. $stmt = $db->prepare($query, $param_types, $types);
  375. if (PEAR::isError($stmt)) {
  376. return $stmt;
  377. }
  378. $result = $stmt->execute($params);
  379. if (!MDB2::isResultCommon($result)) {
  380. return $result;
  381. }
  382. $row = $result->fetchRow($fetchmode);
  383. $stmt->free();
  384. $result->free();
  385. return $row;
  386. }
  387. // }}}
  388. // {{{ getCol()
  389. /**
  390. * Fetch a single column from a result set and return it as an
  391. * indexed array.
  392. *
  393. * @param string the SQL query
  394. * @param string that contains the type of the column in the result set
  395. * @param array if supplied, prepare/execute will be used
  396. * with this array as execute parameters
  397. * @param array that contains the types of the values defined in $params
  398. * @param int|string which column to return
  399. *
  400. * @return array|MDB2_Error data on success, a MDB2 error on failure
  401. * @access public
  402. */
  403. function getCol($query, $type = null, $params = array(),
  404. $param_types = null, $colnum = 0)
  405. {
  406. $db =& $this->getDBInstance();
  407. if (PEAR::isError($db)) {
  408. return $db;
  409. }
  410. settype($params, 'array');
  411. settype($type, 'array');
  412. if (empty($params)) {
  413. return $db->queryCol($query, $type, $colnum);
  414. }
  415. $stmt = $db->prepare($query, $param_types, $type);
  416. if (PEAR::isError($stmt)) {
  417. return $stmt;
  418. }
  419. $result = $stmt->execute($params);
  420. if (!MDB2::isResultCommon($result)) {
  421. return $result;
  422. }
  423. $col = $result->fetchCol($colnum);
  424. $stmt->free();
  425. $result->free();
  426. return $col;
  427. }
  428. // }}}
  429. // {{{ getAll()
  430. /**
  431. * Fetch all the rows returned from a query.
  432. *
  433. * @param string the SQL query
  434. * @param array that contains the types of the columns in the result set
  435. * @param array if supplied, prepare/execute will be used
  436. * with this array as execute parameters
  437. * @param array that contains the types of the values defined in $params
  438. * @param int the fetch mode to use
  439. * @param bool if set to true, the $all will have the first
  440. * column as its first dimension
  441. * @param bool $force_array used only when the query returns exactly
  442. * two columns. If true, the values of the returned array will be
  443. * one-element arrays instead of scalars.
  444. * @param bool $group if true, the values of the returned array is
  445. * wrapped in another array. If the same key value (in the first
  446. * column) repeats itself, the values will be appended to this array
  447. * instead of overwriting the existing values.
  448. *
  449. * @return array|MDB2_Error data on success, a MDB2 error on failure
  450. * @access public
  451. */
  452. function getAll($query, $types = null, $params = array(),
  453. $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT,
  454. $rekey = false, $force_array = false, $group = false)
  455. {
  456. $db =& $this->getDBInstance();
  457. if (PEAR::isError($db)) {
  458. return $db;
  459. }
  460. settype($params, 'array');
  461. if (empty($params)) {
  462. return $db->queryAll($query, $types, $fetchmode, $rekey, $force_array, $group);
  463. }
  464. $stmt = $db->prepare($query, $param_types, $types);
  465. if (PEAR::isError($stmt)) {
  466. return $stmt;
  467. }
  468. $result = $stmt->execute($params);
  469. if (!MDB2::isResultCommon($result)) {
  470. return $result;
  471. }
  472. $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group);
  473. $stmt->free();
  474. $result->free();
  475. return $all;
  476. }
  477. // }}}
  478. // {{{ getAssoc()
  479. /**
  480. * Fetch the entire result set of a query and return it as an
  481. * associative array using the first column as the key.
  482. *
  483. * If the result set contains more than two columns, the value
  484. * will be an array of the values from column 2-n. If the result
  485. * set contains only two columns, the returned value will be a
  486. * scalar with the value of the second column (unless forced to an
  487. * array with the $force_array parameter). A MDB2 error code is
  488. * returned on errors. If the result set contains fewer than two
  489. * columns, a MDB2_ERROR_TRUNCATED error is returned.
  490. *
  491. * For example, if the table 'mytable' contains:
  492. * <pre>
  493. * ID TEXT DATE
  494. * --------------------------------
  495. * 1 'one' 944679408
  496. * 2 'two' 944679408
  497. * 3 'three' 944679408
  498. * </pre>
  499. * Then the call getAssoc('SELECT id,text FROM mytable') returns:
  500. * <pre>
  501. * array(
  502. * '1' => 'one',
  503. * '2' => 'two',
  504. * '3' => 'three',
  505. * )
  506. * </pre>
  507. * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
  508. * <pre>
  509. * array(
  510. * '1' => array('one', '944679408'),
  511. * '2' => array('two', '944679408'),
  512. * '3' => array('three', '944679408')
  513. * )
  514. * </pre>
  515. *
  516. * If the more than one row occurs with the same value in the
  517. * first column, the last row overwrites all previous ones by
  518. * default. Use the $group parameter if you don't want to
  519. * overwrite like this. Example:
  520. * <pre>
  521. * getAssoc('SELECT category,id,name FROM mytable', null, null
  522. * MDB2_FETCHMODE_ASSOC, false, true) returns:
  523. * array(
  524. * '1' => array(array('id' => '4', 'name' => 'number four'),
  525. * array('id' => '6', 'name' => 'number six')
  526. * ),
  527. * '9' => array(array('id' => '4', 'name' => 'number four'),
  528. * array('id' => '6', 'name' => 'number six')
  529. * )
  530. * )
  531. * </pre>
  532. *
  533. * Keep in mind that database functions in PHP usually return string
  534. * values for results regardless of the database's internal type.
  535. *
  536. * @param string the SQL query
  537. * @param array that contains the types of the columns in the result set
  538. * @param array if supplied, prepare/execute will be used
  539. * with this array as execute parameters
  540. * @param array that contains the types of the values defined in $params
  541. * @param bool $force_array used only when the query returns
  542. * exactly two columns. If TRUE, the values of the returned array
  543. * will be one-element arrays instead of scalars.
  544. * @param bool $group if TRUE, the values of the returned array
  545. * is wrapped in another array. If the same key value (in the first
  546. * column) repeats itself, the values will be appended to this array
  547. * instead of overwriting the existing values.
  548. *
  549. * @return array|MDB2_Error data on success, a MDB2 error on failure
  550. * @access public
  551. */
  552. function getAssoc($query, $types = null, $params = array(), $param_types = null,
  553. $fetchmode = MDB2_FETCHMODE_DEFAULT, $force_array = false, $group = false)
  554. {
  555. $db =& $this->getDBInstance();
  556. if (PEAR::isError($db)) {
  557. return $db;
  558. }
  559. settype($params, 'array');
  560. if (empty($params)) {
  561. return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group);
  562. }
  563. $stmt = $db->prepare($query, $param_types, $types);
  564. if (PEAR::isError($stmt)) {
  565. return $stmt;
  566. }
  567. $result = $stmt->execute($params);
  568. if (!MDB2::isResultCommon($result)) {
  569. return $result;
  570. }
  571. $all = $result->fetchAll($fetchmode, true, $force_array, $group);
  572. $stmt->free();
  573. $result->free();
  574. return $all;
  575. }
  576. // }}}
  577. // {{{ executeMultiple()
  578. /**
  579. * This function does several execute() calls on the same statement handle.
  580. * $params must be an array indexed numerically from 0, one execute call is
  581. * done for every 'row' in the array.
  582. *
  583. * If an error occurs during execute(), executeMultiple() does not execute
  584. * the unfinished rows, but rather returns that error.
  585. *
  586. * @param resource query handle from prepare()
  587. * @param array numeric array containing the data to insert into the query
  588. *
  589. * @return bool|MDB2_Error true on success, a MDB2 error on failure
  590. * @access public
  591. * @see prepare(), execute()
  592. */
  593. function executeMultiple(&$stmt, $params = null)
  594. {
  595. for ($i = 0, $j = count($params); $i < $j; $i++) {
  596. $result = $stmt->execute($params[$i]);
  597. if (PEAR::isError($result)) {
  598. return $result;
  599. }
  600. }
  601. return MDB2_OK;
  602. }
  603. // }}}
  604. // {{{ getBeforeID()
  605. /**
  606. * Returns the next free id of a sequence if the RDBMS
  607. * does not support auto increment
  608. *
  609. * @param string name of the table into which a new row was inserted
  610. * @param string name of the field into which a new row was inserted
  611. * @param bool when true the sequence is automatic created, if it not exists
  612. * @param bool if the returned value should be quoted
  613. *
  614. * @return int|MDB2_Error id on success, a MDB2 error on failure
  615. * @access public
  616. */
  617. function getBeforeID($table, $field = null, $ondemand = true, $quote = true)
  618. {
  619. $db =& $this->getDBInstance();
  620. if (PEAR::isError($db)) {
  621. return $db;
  622. }
  623. if ($db->supports('auto_increment') !== true) {
  624. $seq = $table.(empty($field) ? '' : '_'.$field);
  625. $id = $db->nextID($seq, $ondemand);
  626. if (!$quote || PEAR::isError($id)) {
  627. return $id;
  628. }
  629. return $db->quote($id, 'integer');
  630. } elseif (!$quote) {
  631. return null;
  632. }
  633. return 'NULL';
  634. }
  635. // }}}
  636. // {{{ getAfterID()
  637. /**
  638. * Returns the autoincrement ID if supported or $id
  639. *
  640. * @param mixed value as returned by getBeforeId()
  641. * @param string name of the table into which a new row was inserted
  642. * @param string name of the field into which a new row was inserted
  643. *
  644. * @return int|MDB2_Error id on success, a MDB2 error on failure
  645. * @access public
  646. */
  647. function getAfterID($id, $table, $field = null)
  648. {
  649. $db =& $this->getDBInstance();
  650. if (PEAR::isError($db)) {
  651. return $db;
  652. }
  653. if ($db->supports('auto_increment') !== true) {
  654. return $id;
  655. }
  656. return $db->lastInsertID($table, $field);
  657. }
  658. // }}}
  659. }
  660. ?>