Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

705 rivejä
25 KiB

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