PageRenderTime 68ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/external/MDB2/Driver/mysql.php

https://bitbucket.org/djl/flyspray-mirror
PHP | 1685 lines | 1049 code | 127 blank | 509 comment | 219 complexity | caa2ef4042b76a764125cc8a4a911f16 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
  7. // | Stig. S. Bakken, Lukas Smith |
  8. // | All rights reserved. |
  9. // +----------------------------------------------------------------------+
  10. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  11. // | API as well as database abstraction for PHP applications. |
  12. // | This LICENSE is in the BSD license style. |
  13. // | |
  14. // | Redistribution and use in source and binary forms, with or without |
  15. // | modification, are permitted provided that the following conditions |
  16. // | are met: |
  17. // | |
  18. // | Redistributions of source code must retain the above copyright |
  19. // | notice, this list of conditions and the following disclaimer. |
  20. // | |
  21. // | Redistributions in binary form must reproduce the above copyright |
  22. // | notice, this list of conditions and the following disclaimer in the |
  23. // | documentation and/or other materials provided with the distribution. |
  24. // | |
  25. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  26. // | Lukas Smith nor the names of his contributors may be used to endorse |
  27. // | or promote products derived from this software without specific prior|
  28. // | written permission. |
  29. // | |
  30. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  31. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  32. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  33. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  34. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  35. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  36. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  37. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  38. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  39. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  40. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  41. // | POSSIBILITY OF SUCH DAMAGE. |
  42. // +----------------------------------------------------------------------+
  43. // | Author: Lukas Smith <smith@pooteeweet.org> |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mysql.php,v 1.208 2008/03/13 03:31:55 afz Exp $
  47. //
  48. /**
  49. * MDB2 MySQL driver
  50. *
  51. * @package MDB2
  52. * @category Database
  53. * @author Lukas Smith <smith@pooteeweet.org>
  54. */
  55. class MDB2_Driver_mysql extends MDB2_Driver_Common
  56. {
  57. // {{{ properties
  58. var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => '\\', 'escape_pattern' => '\\');
  59. var $identifier_quoting = array('start' => '`', 'end' => '`', 'escape' => '`');
  60. var $sql_comments = array(
  61. array('start' => '-- ', 'end' => "\n", 'escape' => false),
  62. array('start' => '#', 'end' => "\n", 'escape' => false),
  63. array('start' => '/*', 'end' => '*/', 'escape' => false),
  64. );
  65. var $server_capabilities_checked = false;
  66. var $start_transaction = false;
  67. var $varchar_max_length = 255;
  68. // }}}
  69. // {{{ constructor
  70. /**
  71. * Constructor
  72. */
  73. function __construct()
  74. {
  75. parent::__construct();
  76. $this->phptype = 'mysql';
  77. $this->dbsyntax = 'mysql';
  78. $this->supported['sequences'] = 'emulated';
  79. $this->supported['indexes'] = true;
  80. $this->supported['affected_rows'] = true;
  81. $this->supported['transactions'] = false;
  82. $this->supported['savepoints'] = false;
  83. $this->supported['summary_functions'] = true;
  84. $this->supported['order_by_text'] = true;
  85. $this->supported['current_id'] = 'emulated';
  86. $this->supported['limit_queries'] = true;
  87. $this->supported['LOBs'] = true;
  88. $this->supported['replace'] = true;
  89. $this->supported['sub_selects'] = 'emulated';
  90. $this->supported['triggers'] = false;
  91. $this->supported['auto_increment'] = true;
  92. $this->supported['primary_key'] = true;
  93. $this->supported['result_introspection'] = true;
  94. $this->supported['prepared_statements'] = 'emulated';
  95. $this->supported['identifier_quoting'] = true;
  96. $this->supported['pattern_escaping'] = true;
  97. $this->supported['new_link'] = true;
  98. $this->options['DBA_username'] = false;
  99. $this->options['DBA_password'] = false;
  100. $this->options['default_table_type'] = '';
  101. $this->options['max_identifiers_length'] = 64;
  102. $this->_reCheckSupportedOptions();
  103. }
  104. // }}}
  105. // {{{ _reCheckSupportedOptions()
  106. /**
  107. * If the user changes certain options, other capabilities may depend
  108. * on the new settings, so we need to check them (again).
  109. *
  110. * @access private
  111. */
  112. function _reCheckSupportedOptions()
  113. {
  114. $this->supported['transactions'] = $this->options['use_transactions'];
  115. $this->supported['savepoints'] = $this->options['use_transactions'];
  116. if ($this->options['default_table_type']) {
  117. switch (strtoupper($this->options['default_table_type'])) {
  118. case 'BLACKHOLE':
  119. case 'MEMORY':
  120. case 'ARCHIVE':
  121. case 'CSV':
  122. case 'HEAP':
  123. case 'ISAM':
  124. case 'MERGE':
  125. case 'MRG_ISAM':
  126. case 'ISAM':
  127. case 'MRG_MYISAM':
  128. case 'MYISAM':
  129. $this->supported['savepoints'] = false;
  130. $this->supported['transactions'] = false;
  131. $this->warnings[] = $this->options['default_table_type'] .
  132. ' is not a supported default table type';
  133. break;
  134. }
  135. }
  136. }
  137. // }}}
  138. // {{{ function setOption($option, $value)
  139. /**
  140. * set the option for the db class
  141. *
  142. * @param string option name
  143. * @param mixed value for the option
  144. *
  145. * @return mixed MDB2_OK or MDB2 Error Object
  146. *
  147. * @access public
  148. */
  149. function setOption($option, $value)
  150. {
  151. $res = parent::setOption($option, $value);
  152. $this->_reCheckSupportedOptions();
  153. }
  154. // }}}
  155. // {{{ errorInfo()
  156. /**
  157. * This method is used to collect information about an error
  158. *
  159. * @param integer $error
  160. * @return array
  161. * @access public
  162. */
  163. function errorInfo($error = null)
  164. {
  165. if ($this->connection) {
  166. $native_code = @mysql_errno($this->connection);
  167. $native_msg = @mysql_error($this->connection);
  168. } else {
  169. $native_code = @mysql_errno();
  170. $native_msg = @mysql_error();
  171. }
  172. if (is_null($error)) {
  173. static $ecode_map;
  174. if (empty($ecode_map)) {
  175. $ecode_map = array(
  176. 1000 => MDB2_ERROR_INVALID, //hashchk
  177. 1001 => MDB2_ERROR_INVALID, //isamchk
  178. 1004 => MDB2_ERROR_CANNOT_CREATE,
  179. 1005 => MDB2_ERROR_CANNOT_CREATE,
  180. 1006 => MDB2_ERROR_CANNOT_CREATE,
  181. 1007 => MDB2_ERROR_ALREADY_EXISTS,
  182. 1008 => MDB2_ERROR_CANNOT_DROP,
  183. 1009 => MDB2_ERROR_CANNOT_DROP,
  184. 1010 => MDB2_ERROR_CANNOT_DROP,
  185. 1011 => MDB2_ERROR_CANNOT_DELETE,
  186. 1022 => MDB2_ERROR_ALREADY_EXISTS,
  187. 1029 => MDB2_ERROR_NOT_FOUND,
  188. 1032 => MDB2_ERROR_NOT_FOUND,
  189. 1044 => MDB2_ERROR_ACCESS_VIOLATION,
  190. 1045 => MDB2_ERROR_ACCESS_VIOLATION,
  191. 1046 => MDB2_ERROR_NODBSELECTED,
  192. 1048 => MDB2_ERROR_CONSTRAINT,
  193. 1049 => MDB2_ERROR_NOSUCHDB,
  194. 1050 => MDB2_ERROR_ALREADY_EXISTS,
  195. 1051 => MDB2_ERROR_NOSUCHTABLE,
  196. 1054 => MDB2_ERROR_NOSUCHFIELD,
  197. 1060 => MDB2_ERROR_ALREADY_EXISTS,
  198. 1061 => MDB2_ERROR_ALREADY_EXISTS,
  199. 1062 => MDB2_ERROR_ALREADY_EXISTS,
  200. 1064 => MDB2_ERROR_SYNTAX,
  201. 1067 => MDB2_ERROR_INVALID,
  202. 1072 => MDB2_ERROR_NOT_FOUND,
  203. 1086 => MDB2_ERROR_ALREADY_EXISTS,
  204. 1091 => MDB2_ERROR_NOT_FOUND,
  205. 1100 => MDB2_ERROR_NOT_LOCKED,
  206. 1109 => MDB2_ERROR_NOT_FOUND,
  207. 1125 => MDB2_ERROR_ALREADY_EXISTS,
  208. 1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  209. 1138 => MDB2_ERROR_INVALID,
  210. 1142 => MDB2_ERROR_ACCESS_VIOLATION,
  211. 1143 => MDB2_ERROR_ACCESS_VIOLATION,
  212. 1146 => MDB2_ERROR_NOSUCHTABLE,
  213. 1149 => MDB2_ERROR_SYNTAX,
  214. 1169 => MDB2_ERROR_CONSTRAINT,
  215. 1176 => MDB2_ERROR_NOT_FOUND,
  216. 1177 => MDB2_ERROR_NOSUCHTABLE,
  217. 1213 => MDB2_ERROR_DEADLOCK,
  218. 1216 => MDB2_ERROR_CONSTRAINT,
  219. 1217 => MDB2_ERROR_CONSTRAINT,
  220. 1227 => MDB2_ERROR_ACCESS_VIOLATION,
  221. 1235 => MDB2_ERROR_CANNOT_CREATE,
  222. 1299 => MDB2_ERROR_INVALID_DATE,
  223. 1300 => MDB2_ERROR_INVALID,
  224. 1304 => MDB2_ERROR_ALREADY_EXISTS,
  225. 1305 => MDB2_ERROR_NOT_FOUND,
  226. 1306 => MDB2_ERROR_CANNOT_DROP,
  227. 1307 => MDB2_ERROR_CANNOT_CREATE,
  228. 1334 => MDB2_ERROR_CANNOT_ALTER,
  229. 1339 => MDB2_ERROR_NOT_FOUND,
  230. 1356 => MDB2_ERROR_INVALID,
  231. 1359 => MDB2_ERROR_ALREADY_EXISTS,
  232. 1360 => MDB2_ERROR_NOT_FOUND,
  233. 1363 => MDB2_ERROR_NOT_FOUND,
  234. 1365 => MDB2_ERROR_DIVZERO,
  235. 1451 => MDB2_ERROR_CONSTRAINT,
  236. 1452 => MDB2_ERROR_CONSTRAINT,
  237. 1542 => MDB2_ERROR_CANNOT_DROP,
  238. 1546 => MDB2_ERROR_CONSTRAINT,
  239. 1582 => MDB2_ERROR_CONSTRAINT,
  240. 2003 => MDB2_ERROR_CONNECT_FAILED,
  241. 2019 => MDB2_ERROR_INVALID,
  242. );
  243. }
  244. if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) {
  245. $ecode_map[1022] = MDB2_ERROR_CONSTRAINT;
  246. $ecode_map[1048] = MDB2_ERROR_CONSTRAINT_NOT_NULL;
  247. $ecode_map[1062] = MDB2_ERROR_CONSTRAINT;
  248. } else {
  249. // Doing this in case mode changes during runtime.
  250. $ecode_map[1022] = MDB2_ERROR_ALREADY_EXISTS;
  251. $ecode_map[1048] = MDB2_ERROR_CONSTRAINT;
  252. $ecode_map[1062] = MDB2_ERROR_ALREADY_EXISTS;
  253. }
  254. if (isset($ecode_map[$native_code])) {
  255. $error = $ecode_map[$native_code];
  256. }
  257. }
  258. return array($error, $native_code, $native_msg);
  259. }
  260. // }}}
  261. // {{{ escape()
  262. /**
  263. * Quotes a string so it can be safely used in a query. It will quote
  264. * the text so it can safely be used within a query.
  265. *
  266. * @param string the input string to quote
  267. * @param bool escape wildcards
  268. *
  269. * @return string quoted string
  270. *
  271. * @access public
  272. */
  273. function escape($text, $escape_wildcards = false)
  274. {
  275. if ($escape_wildcards) {
  276. $text = $this->escapePattern($text);
  277. }
  278. $connection = $this->getConnection();
  279. if (PEAR::isError($connection)) {
  280. return $connection;
  281. }
  282. $text = @mysql_real_escape_string($text, $connection);
  283. return $text;
  284. }
  285. // }}}
  286. // {{{ beginTransaction()
  287. /**
  288. * Start a transaction or set a savepoint.
  289. *
  290. * @param string name of a savepoint to set
  291. * @return mixed MDB2_OK on success, a MDB2 error on failure
  292. *
  293. * @access public
  294. */
  295. function beginTransaction($savepoint = null)
  296. {
  297. $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
  298. $this->_getServerCapabilities();
  299. if (!is_null($savepoint)) {
  300. if (!$this->supports('savepoints')) {
  301. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  302. 'savepoints are not supported', __FUNCTION__);
  303. }
  304. if (!$this->in_transaction) {
  305. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  306. 'savepoint cannot be released when changes are auto committed', __FUNCTION__);
  307. }
  308. $query = 'SAVEPOINT '.$savepoint;
  309. return $this->_doQuery($query, true);
  310. } elseif ($this->in_transaction) {
  311. return MDB2_OK; //nothing to do
  312. }
  313. if (!$this->destructor_registered && $this->opened_persistent) {
  314. $this->destructor_registered = true;
  315. register_shutdown_function('MDB2_closeOpenTransactions');
  316. }
  317. $query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 1';
  318. $result =& $this->_doQuery($query, true);
  319. if (PEAR::isError($result)) {
  320. return $result;
  321. }
  322. $this->in_transaction = true;
  323. return MDB2_OK;
  324. }
  325. // }}}
  326. // {{{ commit()
  327. /**
  328. * Commit the database changes done during a transaction that is in
  329. * progress or release a savepoint. This function may only be called when
  330. * auto-committing is disabled, otherwise it will fail. Therefore, a new
  331. * transaction is implicitly started after committing the pending changes.
  332. *
  333. * @param string name of a savepoint to release
  334. * @return mixed MDB2_OK on success, a MDB2 error on failure
  335. *
  336. * @access public
  337. */
  338. function commit($savepoint = null)
  339. {
  340. $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
  341. if (!$this->in_transaction) {
  342. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  343. 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
  344. }
  345. if (!is_null($savepoint)) {
  346. if (!$this->supports('savepoints')) {
  347. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  348. 'savepoints are not supported', __FUNCTION__);
  349. }
  350. $server_info = $this->getServerVersion();
  351. if (version_compare($server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'], '5.0.3', '<')) {
  352. return MDB2_OK;
  353. }
  354. $query = 'RELEASE SAVEPOINT '.$savepoint;
  355. return $this->_doQuery($query, true);
  356. }
  357. if (!$this->supports('transactions')) {
  358. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  359. 'transactions are not supported', __FUNCTION__);
  360. }
  361. $result =& $this->_doQuery('COMMIT', true);
  362. if (PEAR::isError($result)) {
  363. return $result;
  364. }
  365. if (!$this->start_transaction) {
  366. $query = 'SET AUTOCOMMIT = 0';
  367. $result =& $this->_doQuery($query, true);
  368. if (PEAR::isError($result)) {
  369. return $result;
  370. }
  371. }
  372. $this->in_transaction = false;
  373. return MDB2_OK;
  374. }
  375. // }}}
  376. // {{{ rollback()
  377. /**
  378. * Cancel any database changes done during a transaction or since a specific
  379. * savepoint that is in progress. This function may only be called when
  380. * auto-committing is disabled, otherwise it will fail. Therefore, a new
  381. * transaction is implicitly started after canceling the pending changes.
  382. *
  383. * @param string name of a savepoint to rollback to
  384. * @return mixed MDB2_OK on success, a MDB2 error on failure
  385. *
  386. * @access public
  387. */
  388. function rollback($savepoint = null)
  389. {
  390. $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
  391. if (!$this->in_transaction) {
  392. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  393. 'rollback cannot be done changes are auto committed', __FUNCTION__);
  394. }
  395. if (!is_null($savepoint)) {
  396. if (!$this->supports('savepoints')) {
  397. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  398. 'savepoints are not supported', __FUNCTION__);
  399. }
  400. $query = 'ROLLBACK TO SAVEPOINT '.$savepoint;
  401. return $this->_doQuery($query, true);
  402. }
  403. $query = 'ROLLBACK';
  404. $result =& $this->_doQuery($query, true);
  405. if (PEAR::isError($result)) {
  406. return $result;
  407. }
  408. if (!$this->start_transaction) {
  409. $query = 'SET AUTOCOMMIT = 0';
  410. $result =& $this->_doQuery($query, true);
  411. if (PEAR::isError($result)) {
  412. return $result;
  413. }
  414. }
  415. $this->in_transaction = false;
  416. return MDB2_OK;
  417. }
  418. // }}}
  419. // {{{ function setTransactionIsolation()
  420. /**
  421. * Set the transacton isolation level.
  422. *
  423. * @param string standard isolation level
  424. * READ UNCOMMITTED (allows dirty reads)
  425. * READ COMMITTED (prevents dirty reads)
  426. * REPEATABLE READ (prevents nonrepeatable reads)
  427. * SERIALIZABLE (prevents phantom reads)
  428. * @return mixed MDB2_OK on success, a MDB2 error on failure
  429. *
  430. * @access public
  431. * @since 2.1.1
  432. */
  433. function setTransactionIsolation($isolation)
  434. {
  435. $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
  436. if (!$this->supports('transactions')) {
  437. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  438. 'transactions are not supported', __FUNCTION__);
  439. }
  440. switch ($isolation) {
  441. case 'READ UNCOMMITTED':
  442. case 'READ COMMITTED':
  443. case 'REPEATABLE READ':
  444. case 'SERIALIZABLE':
  445. break;
  446. default:
  447. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  448. 'isolation level is not supported: '.$isolation, __FUNCTION__);
  449. }
  450. $query = "SET SESSION TRANSACTION ISOLATION LEVEL $isolation";
  451. return $this->_doQuery($query, true);
  452. }
  453. // }}}
  454. // {{{ _doConnect()
  455. /**
  456. * do the grunt work of the connect
  457. *
  458. * @return connection on success or MDB2 Error Object on failure
  459. * @access protected
  460. */
  461. function _doConnect($username, $password, $persistent = false)
  462. {
  463. if (!PEAR::loadExtension($this->phptype)) {
  464. return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  465. 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
  466. }
  467. $params = array();
  468. if ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix') {
  469. $params[0] = ':' . $this->dsn['socket'];
  470. } else {
  471. $params[0] = $this->dsn['hostspec'] ? $this->dsn['hostspec']
  472. : 'localhost';
  473. if ($this->dsn['port']) {
  474. $params[0].= ':' . $this->dsn['port'];
  475. }
  476. }
  477. $params[] = $username ? $username : null;
  478. $params[] = $password ? $password : null;
  479. if (!$persistent) {
  480. if (isset($this->dsn['new_link'])
  481. && ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true)
  482. ) {
  483. $params[] = true;
  484. } else {
  485. $params[] = false;
  486. }
  487. }
  488. if (version_compare(phpversion(), '4.3.0', '>=')) {
  489. $params[] = isset($this->dsn['client_flags'])
  490. ? $this->dsn['client_flags'] : null;
  491. }
  492. $connect_function = $persistent ? 'mysql_pconnect' : 'mysql_connect';
  493. $connection = @call_user_func_array($connect_function, $params);
  494. if (!$connection) {
  495. if (($err = @mysql_error()) != '') {
  496. return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
  497. $err, __FUNCTION__);
  498. } else {
  499. return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
  500. 'unable to establish a connection', __FUNCTION__);
  501. }
  502. }
  503. if (!empty($this->dsn['charset'])) {
  504. $result = $this->setCharset($this->dsn['charset'], $connection);
  505. if (PEAR::isError($result)) {
  506. $this->disconnect(false);
  507. return $result;
  508. }
  509. }
  510. return $connection;
  511. }
  512. // }}}
  513. // {{{ connect()
  514. /**
  515. * Connect to the database
  516. *
  517. * @return MDB2_OK on success, MDB2 Error Object on failure
  518. * @access public
  519. */
  520. function connect()
  521. {
  522. if (is_resource($this->connection)) {
  523. //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
  524. if (MDB2::areEquals($this->connected_dsn, $this->dsn)
  525. && $this->opened_persistent == $this->options['persistent']
  526. ) {
  527. return MDB2_OK;
  528. }
  529. $this->disconnect(false);
  530. }
  531. $connection = $this->_doConnect(
  532. $this->dsn['username'],
  533. $this->dsn['password'],
  534. $this->options['persistent']
  535. );
  536. if (PEAR::isError($connection)) {
  537. return $connection;
  538. }
  539. $this->connection = $connection;
  540. $this->connected_dsn = $this->dsn;
  541. $this->connected_database_name = '';
  542. $this->opened_persistent = $this->options['persistent'];
  543. $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
  544. if ($this->database_name) {
  545. if ($this->database_name != $this->connected_database_name) {
  546. if (!@mysql_select_db($this->database_name, $connection)) {
  547. $err = $this->raiseError(null, null, null,
  548. 'Could not select the database: '.$this->database_name, __FUNCTION__);
  549. return $err;
  550. }
  551. $this->connected_database_name = $this->database_name;
  552. }
  553. }
  554. $this->_getServerCapabilities();
  555. return MDB2_OK;
  556. }
  557. // }}}
  558. // {{{ setCharset()
  559. /**
  560. * Set the charset on the current connection
  561. *
  562. * @param string charset (or array(charset, collation))
  563. * @param resource connection handle
  564. *
  565. * @return true on success, MDB2 Error Object on failure
  566. */
  567. function setCharset($charset, $connection = null)
  568. {
  569. if (is_null($connection)) {
  570. $connection = $this->getConnection();
  571. if (PEAR::isError($connection)) {
  572. return $connection;
  573. }
  574. }
  575. $collation = null;
  576. if (is_array($charset) && 2 == count($charset)) {
  577. $collation = array_pop($charset);
  578. $charset = array_pop($charset);
  579. }
  580. $query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'";
  581. if (!is_null($collation)) {
  582. $query .= " COLLATE '".mysqli_real_escape_string($connection, $collation)."'";
  583. }
  584. return $this->_doQuery($query, true, $connection);
  585. }
  586. // }}}
  587. // {{{ databaseExists()
  588. /**
  589. * check if given database name is exists?
  590. *
  591. * @param string $name name of the database that should be checked
  592. *
  593. * @return mixed true/false on success, a MDB2 error on failure
  594. * @access public
  595. */
  596. function databaseExists($name)
  597. {
  598. $connection = $this->_doConnect($this->dsn['username'],
  599. $this->dsn['password'],
  600. $this->options['persistent']);
  601. if (PEAR::isError($connection)) {
  602. return $connection;
  603. }
  604. $result = @mysql_select_db($name, $connection);
  605. @mysql_close($connection);
  606. return $result;
  607. }
  608. // }}}
  609. // {{{ disconnect()
  610. /**
  611. * Log out and disconnect from the database.
  612. *
  613. * @param boolean $force if the disconnect should be forced even if the
  614. * connection is opened persistently
  615. * @return mixed true on success, false if not connected and error
  616. * object on error
  617. * @access public
  618. */
  619. function disconnect($force = true)
  620. {
  621. if (is_resource($this->connection)) {
  622. if ($this->in_transaction) {
  623. $dsn = $this->dsn;
  624. $database_name = $this->database_name;
  625. $persistent = $this->options['persistent'];
  626. $this->dsn = $this->connected_dsn;
  627. $this->database_name = $this->connected_database_name;
  628. $this->options['persistent'] = $this->opened_persistent;
  629. $this->rollback();
  630. $this->dsn = $dsn;
  631. $this->database_name = $database_name;
  632. $this->options['persistent'] = $persistent;
  633. }
  634. if (!$this->opened_persistent || $force) {
  635. @mysql_close($this->connection);
  636. }
  637. }
  638. return parent::disconnect($force);
  639. }
  640. // }}}
  641. // {{{ standaloneQuery()
  642. /**
  643. * execute a query as DBA
  644. *
  645. * @param string $query the SQL query
  646. * @param mixed $types array that contains the types of the columns in
  647. * the result set
  648. * @param boolean $is_manip if the query is a manipulation query
  649. * @return mixed MDB2_OK on success, a MDB2 error on failure
  650. * @access public
  651. */
  652. function &standaloneQuery($query, $types = null, $is_manip = false)
  653. {
  654. $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username'];
  655. $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password'];
  656. $connection = $this->_doConnect($user, $pass, $this->options['persistent']);
  657. if (PEAR::isError($connection)) {
  658. return $connection;
  659. }
  660. $offset = $this->offset;
  661. $limit = $this->limit;
  662. $this->offset = $this->limit = 0;
  663. $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
  664. $result =& $this->_doQuery($query, $is_manip, $connection, $this->database_name);
  665. if (!PEAR::isError($result)) {
  666. $result = $this->_affectedRows($connection, $result);
  667. }
  668. @mysql_close($connection);
  669. return $result;
  670. }
  671. // }}}
  672. // {{{ _doQuery()
  673. /**
  674. * Execute a query
  675. * @param string $query query
  676. * @param boolean $is_manip if the query is a manipulation query
  677. * @param resource $connection
  678. * @param string $database_name
  679. * @return result or error object
  680. * @access protected
  681. */
  682. function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
  683. {
  684. $this->last_query = $query;
  685. $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
  686. if ($result) {
  687. if (PEAR::isError($result)) {
  688. return $result;
  689. }
  690. $query = $result;
  691. }
  692. if ($this->options['disable_query']) {
  693. $result = $is_manip ? 0 : null;
  694. return $result;
  695. }
  696. if (is_null($connection)) {
  697. $connection = $this->getConnection();
  698. if (PEAR::isError($connection)) {
  699. return $connection;
  700. }
  701. }
  702. if (is_null($database_name)) {
  703. $database_name = $this->database_name;
  704. }
  705. if ($database_name) {
  706. if ($database_name != $this->connected_database_name) {
  707. if (!@mysql_select_db($database_name, $connection)) {
  708. $err = $this->raiseError(null, null, null,
  709. 'Could not select the database: '.$database_name, __FUNCTION__);
  710. return $err;
  711. }
  712. $this->connected_database_name = $database_name;
  713. }
  714. }
  715. $function = $this->options['result_buffering']
  716. ? 'mysql_query' : 'mysql_unbuffered_query';
  717. $result = @$function($query, $connection);
  718. if (!$result) {
  719. $err =& $this->raiseError(null, null, null,
  720. 'Could not execute statement', __FUNCTION__);
  721. return $err;
  722. }
  723. $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
  724. return $result;
  725. }
  726. // }}}
  727. // {{{ _affectedRows()
  728. /**
  729. * Returns the number of rows affected
  730. *
  731. * @param resource $result
  732. * @param resource $connection
  733. * @return mixed MDB2 Error Object or the number of rows affected
  734. * @access private
  735. */
  736. function _affectedRows($connection, $result = null)
  737. {
  738. if (is_null($connection)) {
  739. $connection = $this->getConnection();
  740. if (PEAR::isError($connection)) {
  741. return $connection;
  742. }
  743. }
  744. return @mysql_affected_rows($connection);
  745. }
  746. // }}}
  747. // {{{ _modifyQuery()
  748. /**
  749. * Changes a query string for various DBMS specific reasons
  750. *
  751. * @param string $query query to modify
  752. * @param boolean $is_manip if it is a DML query
  753. * @param integer $limit limit the number of rows
  754. * @param integer $offset start reading from given offset
  755. * @return string modified query
  756. * @access protected
  757. */
  758. function _modifyQuery($query, $is_manip, $limit, $offset)
  759. {
  760. if ($this->options['portability'] & MDB2_PORTABILITY_DELETE_COUNT) {
  761. // "DELETE FROM table" gives 0 affected rows in MySQL.
  762. // This little hack lets you know how many rows were deleted.
  763. if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) {
  764. $query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/',
  765. 'DELETE FROM \1 WHERE 1=1', $query);
  766. }
  767. }
  768. if ($limit > 0
  769. && !preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query)
  770. ) {
  771. $query = rtrim($query);
  772. if (substr($query, -1) == ';') {
  773. $query = substr($query, 0, -1);
  774. }
  775. // LIMIT doesn't always come last in the query
  776. // @see http://dev.mysql.com/doc/refman/5.0/en/select.html
  777. $after = '';
  778. if (preg_match('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', $query, $matches)) {
  779. $after = $matches[0];
  780. $query = preg_replace('/(\s+INTO\s+(?:OUT|DUMP)FILE\s.*)$/ims', '', $query);
  781. } elseif (preg_match('/(\s+FOR\s+UPDATE\s*)$/i', $query, $matches)) {
  782. $after = $matches[0];
  783. $query = preg_replace('/(\s+FOR\s+UPDATE\s*)$/im', '', $query);
  784. } elseif (preg_match('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', $query, $matches)) {
  785. $after = $matches[0];
  786. $query = preg_replace('/(\s+LOCK\s+IN\s+SHARE\s+MODE\s*)$/im', '', $query);
  787. }
  788. if ($is_manip) {
  789. return $query . " LIMIT $limit" . $after;
  790. } else {
  791. return $query . " LIMIT $offset, $limit" . $after;
  792. }
  793. }
  794. return $query;
  795. }
  796. // }}}
  797. // {{{ getServerVersion()
  798. /**
  799. * return version information about the server
  800. *
  801. * @param bool $native determines if the raw version string should be returned
  802. * @return mixed array/string with version information or MDB2 error object
  803. * @access public
  804. */
  805. function getServerVersion($native = false)
  806. {
  807. $connection = $this->getConnection();
  808. if (PEAR::isError($connection)) {
  809. return $connection;
  810. }
  811. if ($this->connected_server_info) {
  812. $server_info = $this->connected_server_info;
  813. } else {
  814. $server_info = @mysql_get_server_info($connection);
  815. }
  816. if (!$server_info) {
  817. return $this->raiseError(null, null, null,
  818. 'Could not get server information', __FUNCTION__);
  819. }
  820. // cache server_info
  821. $this->connected_server_info = $server_info;
  822. if (!$native) {
  823. $tmp = explode('.', $server_info, 3);
  824. if (isset($tmp[2]) && strpos($tmp[2], '-')) {
  825. $tmp2 = explode('-', @$tmp[2], 2);
  826. } else {
  827. $tmp2[0] = isset($tmp[2]) ? $tmp[2] : null;
  828. $tmp2[1] = null;
  829. }
  830. $server_info = array(
  831. 'major' => isset($tmp[0]) ? $tmp[0] : null,
  832. 'minor' => isset($tmp[1]) ? $tmp[1] : null,
  833. 'patch' => $tmp2[0],
  834. 'extra' => $tmp2[1],
  835. 'native' => $server_info,
  836. );
  837. }
  838. return $server_info;
  839. }
  840. // }}}
  841. // {{{ _getServerCapabilities()
  842. /**
  843. * Fetch some information about the server capabilities
  844. * (transactions, subselects, prepared statements, etc).
  845. *
  846. * @access private
  847. */
  848. function _getServerCapabilities()
  849. {
  850. if (!$this->server_capabilities_checked) {
  851. $this->server_capabilities_checked = true;
  852. //set defaults
  853. $this->supported['sub_selects'] = 'emulated';
  854. $this->supported['prepared_statements'] = 'emulated';
  855. $this->supported['triggers'] = false;
  856. $this->start_transaction = false;
  857. $this->varchar_max_length = 255;
  858. $server_info = $this->getServerVersion();
  859. if (is_array($server_info)) {
  860. $server_version = $server_info['major'].'.'.$server_info['minor'].'.'.$server_info['patch'];
  861. if (!version_compare($server_version, '4.1.0', '<')) {
  862. $this->supported['sub_selects'] = true;
  863. $this->supported['prepared_statements'] = true;
  864. }
  865. // SAVEPOINTs were introduced in MySQL 4.0.14 and 4.1.1 (InnoDB)
  866. if (version_compare($server_version, '4.1.0', '>=')) {
  867. if (version_compare($server_version, '4.1.1', '<')) {
  868. $this->supported['savepoints'] = false;
  869. }
  870. } elseif (version_compare($server_version, '4.0.14', '<')) {
  871. $this->supported['savepoints'] = false;
  872. }
  873. if (!version_compare($server_version, '4.0.11', '<')) {
  874. $this->start_transaction = true;
  875. }
  876. if (!version_compare($server_version, '5.0.3', '<')) {
  877. $this->varchar_max_length = 65532;
  878. }
  879. if (!version_compare($server_version, '5.0.2', '<')) {
  880. $this->supported['triggers'] = true;
  881. }
  882. }
  883. }
  884. }
  885. // }}}
  886. // {{{ function _skipUserDefinedVariable($query, $position)
  887. /**
  888. * Utility method, used by prepare() to avoid misinterpreting MySQL user
  889. * defined variables (SELECT @x:=5) for placeholders.
  890. * Check if the placeholder is a false positive, i.e. if it is an user defined
  891. * variable instead. If so, skip it and advance the position, otherwise
  892. * return the current position, which is valid
  893. *
  894. * @param string $query
  895. * @param integer $position current string cursor position
  896. * @return integer $new_position
  897. * @access protected
  898. */
  899. function _skipUserDefinedVariable($query, $position)
  900. {
  901. $found = strpos(strrev(substr($query, 0, $position)), '@');
  902. if ($found === false) {
  903. return $position;
  904. }
  905. $pos = strlen($query) - strlen(substr($query, $position)) - $found - 1;
  906. $substring = substr($query, $pos, $position - $pos + 2);
  907. if (preg_match('/^@\w+\s*:=$/', $substring)) {
  908. return $position + 1; //found an user defined variable: skip it
  909. }
  910. return $position;
  911. }
  912. // }}}
  913. // {{{ prepare()
  914. /**
  915. * Prepares a query for multiple execution with execute().
  916. * With some database backends, this is emulated.
  917. * prepare() requires a generic query as string like
  918. * 'INSERT INTO numbers VALUES(?,?)' or
  919. * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  920. * The ? and :name and are placeholders which can be set using
  921. * bindParam() and the query can be sent off using the execute() method.
  922. * The allowed format for :name can be set with the 'bindname_format' option.
  923. *
  924. * @param string $query the query to prepare
  925. * @param mixed $types array that contains the types of the placeholders
  926. * @param mixed $result_types array that contains the types of the columns in
  927. * the result set or MDB2_PREPARE_RESULT, if set to
  928. * MDB2_PREPARE_MANIP the query is handled as a manipulation query
  929. * @param mixed $lobs key (field) value (parameter) pair for all lob placeholders
  930. * @return mixed resource handle for the prepared query on success, a MDB2
  931. * error on failure
  932. * @access public
  933. * @see bindParam, execute
  934. */
  935. function &prepare($query, $types = null, $result_types = null, $lobs = array())
  936. {
  937. if ($this->options['emulate_prepared']
  938. || $this->supported['prepared_statements'] !== true
  939. ) {
  940. $obj =& parent::prepare($query, $types, $result_types, $lobs);
  941. return $obj;
  942. }
  943. $is_manip = ($result_types === MDB2_PREPARE_MANIP);
  944. $offset = $this->offset;
  945. $limit = $this->limit;
  946. $this->offset = $this->limit = 0;
  947. $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
  948. $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
  949. if ($result) {
  950. if (PEAR::isError($result)) {
  951. return $result;
  952. }
  953. $query = $result;
  954. }
  955. $placeholder_type_guess = $placeholder_type = null;
  956. $question = '?';
  957. $colon = ':';
  958. $positions = array();
  959. $position = 0;
  960. while ($position < strlen($query)) {
  961. $q_position = strpos($query, $question, $position);
  962. $c_position = strpos($query, $colon, $position);
  963. if ($q_position && $c_position) {
  964. $p_position = min($q_position, $c_position);
  965. } elseif ($q_position) {
  966. $p_position = $q_position;
  967. } elseif ($c_position) {
  968. $p_position = $c_position;
  969. } else {
  970. break;
  971. }
  972. if (is_null($placeholder_type)) {
  973. $placeholder_type_guess = $query[$p_position];
  974. }
  975. $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
  976. if (PEAR::isError($new_pos)) {
  977. return $new_pos;
  978. }
  979. if ($new_pos != $position) {
  980. $position = $new_pos;
  981. continue; //evaluate again starting from the new position
  982. }
  983. //make sure this is not part of an user defined variable
  984. $new_pos = $this->_skipUserDefinedVariable($query, $position);
  985. if ($new_pos != $position) {
  986. $position = $new_pos;
  987. continue; //evaluate again starting from the new position
  988. }
  989. if ($query[$position] == $placeholder_type_guess) {
  990. if (is_null($placeholder_type)) {
  991. $placeholder_type = $query[$p_position];
  992. $question = $colon = $placeholder_type;
  993. }
  994. if ($placeholder_type == ':') {
  995. $regexp = '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
  996. $parameter = preg_replace($regexp, '\\1', $query);
  997. if ($parameter === '') {
  998. $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
  999. 'named parameter name must match "bindname_format" option', __FUNCTION__);
  1000. return $err;
  1001. }
  1002. $positions[$p_position] = $parameter;
  1003. $query = substr_replace($query, '?', $position, strlen($parameter)+1);
  1004. } else {
  1005. $positions[$p_position] = count($positions);
  1006. }
  1007. $position = $p_position + 1;
  1008. } else {
  1009. $position = $p_position;
  1010. }
  1011. }
  1012. $connection = $this->getConnection();
  1013. if (PEAR::isError($connection)) {
  1014. return $connection;
  1015. }
  1016. static $prep_statement_counter = 1;
  1017. $statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter++ . sha1(microtime() + mt_rand()));
  1018. $statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']);
  1019. $query = "PREPARE $statement_name FROM ".$this->quote($query, 'text');
  1020. $statement =& $this->_doQuery($query, true, $connection);
  1021. if (PEAR::isError($statement)) {
  1022. return $statement;
  1023. }
  1024. $class_name = 'MDB2_Statement_'.$this->phptype;
  1025. $obj = new $class_name($this, $statement_name, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
  1026. $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
  1027. return $obj;
  1028. }
  1029. // }}}
  1030. // {{{ replace()
  1031. /**
  1032. * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT
  1033. * query, except that if there is already a row in the table with the same
  1034. * key field values, the REPLACE query just updates its values instead of
  1035. * inserting a new row.
  1036. *
  1037. * The REPLACE type of query does not make part of the SQL standards. Since
  1038. * practically only MySQL implements it natively, this type of query is
  1039. * emulated through this method for other DBMS using standard types of
  1040. * queries inside a transaction to assure the atomicity of the operation.
  1041. *
  1042. * @access public
  1043. *
  1044. * @param string $table name of the table on which the REPLACE query will
  1045. * be executed.
  1046. * @param array $fields associative array that describes the fields and the
  1047. * values that will be inserted or updated in the specified table. The
  1048. * indexes of the array are the names of all the fields of the table. The
  1049. * values of the array are also associative arrays that describe the
  1050. * values and other properties of the table fields.
  1051. *
  1052. * Here follows a list of field properties that need to be specified:
  1053. *
  1054. * value:
  1055. * Value to be assigned to the specified field. This value may be
  1056. * of specified in database independent type format as this
  1057. * function can perform the necessary datatype conversions.
  1058. *
  1059. * Default:
  1060. * this property is required unless the Null property
  1061. * is set to 1.
  1062. *
  1063. * type
  1064. * Name of the type of the field. Currently, all types Metabase
  1065. * are supported except for clob and blob.
  1066. *
  1067. * Default: no type conversion
  1068. *
  1069. * null
  1070. * Boolean property that indicates that the value for this field
  1071. * should be set to null.
  1072. *
  1073. * The default value for fields missing in INSERT queries may be
  1074. * specified the definition of a table. Often, the default value
  1075. * is already null, but since the REPLACE may be emulated using
  1076. * an UPDATE query, make sure that all fields of the table are
  1077. * listed in this function argument array.
  1078. *
  1079. * Default: 0
  1080. *
  1081. * key
  1082. * Boolean property that indicates that this field should be
  1083. * handled as a primary key or at least as part of the compound
  1084. * unique index of the table that will determine the row that will
  1085. * updated if it exists or inserted a new row otherwise.
  1086. *
  1087. * This function will fail if no key field is specified or if the
  1088. * value of a key field is set to null because fields that are
  1089. * part of unique index they may not be null.
  1090. *
  1091. * Default: 0
  1092. *
  1093. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1094. */
  1095. function replace($table, $fields)
  1096. {
  1097. $count = count($fields);
  1098. $query = $values = '';
  1099. $keys = $colnum = 0;
  1100. for (reset($fields); $colnum < $count; next($fields), $colnum++) {
  1101. $name = key($fields);
  1102. if ($colnum > 0) {
  1103. $query .= ',';
  1104. $values.= ',';
  1105. }
  1106. $query.= $this->quoteIdentifier($name, true);
  1107. if (isset($fields[$name]['null']) && $fields[$name]['null']) {
  1108. $value = 'NULL';
  1109. } else {
  1110. $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null;
  1111. $value = $this->quote($fields[$name]['value'], $type);
  1112. if (PEAR::isError($value)) {
  1113. return $value;
  1114. }
  1115. }
  1116. $values.= $value;
  1117. if (isset($fields[$name]['key']) && $fields[$name]['key']) {
  1118. if ($value === 'NULL') {
  1119. return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
  1120. 'key value '.$name.' may not be NULL', __FUNCTION__);
  1121. }
  1122. $keys++;
  1123. }
  1124. }
  1125. if ($keys == 0) {
  1126. return $this->raiseError(MDB2_ERROR_CANNOT_REPLACE, null, null,
  1127. 'not specified which fields are keys', __FUNCTION__);
  1128. }
  1129. $connection = $this->getConnection();
  1130. if (PEAR::isError($connection)) {
  1131. return $connection;
  1132. }
  1133. $table = $this->quoteIdentifier($table, true);
  1134. $query = "REPLACE INTO $table ($query) VALUES ($values)";
  1135. $result =& $this->_doQuery($query, true, $connection);
  1136. if (PEAR::isError($result)) {
  1137. return $result;
  1138. }
  1139. return $this->_affectedRows($connection, $result);
  1140. }
  1141. // }}}
  1142. // {{{ nextID()
  1143. /**
  1144. * Returns the next free id of a sequence
  1145. *
  1146. * @param string $seq_name name of the sequence
  1147. * @param boolean $ondemand when true the sequence is
  1148. * automatic created, if it
  1149. * not exists
  1150. *
  1151. * @return mixed MDB2 Error Object or id
  1152. * @access public
  1153. */
  1154. function nextID($seq_name, $ondemand = true)
  1155. {
  1156. $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
  1157. $seqcol_name = $this->quoteIdentifier($this->options['seqcol_name'], true);
  1158. $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL)";
  1159. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  1160. $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  1161. $result =& $this->_doQuery($query, true);
  1162. $this->popExpect();
  1163. $this->popErrorHandling();
  1164. if (PEAR::isError($result)) {
  1165. if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
  1166. $this->loadModule('Manager', null, true);
  1167. $result = $this->manager->createSequence($seq_name);
  1168. if (PEAR::isError($result)) {
  1169. return $this->raiseError($result, null, null,
  1170. 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__);
  1171. } else {
  1172. return $this->nextID($seq_name, false);
  1173. }
  1174. }
  1175. return $result;
  1176. }
  1177. $value = $this->lastInsertID();
  1178. if (is_numeric($value)) {
  1179. $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  1180. $result =& $this->_doQuery($query, true);
  1181. if (PEAR::isError($result)) {
  1182. $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name;
  1183. }
  1184. }
  1185. return $value;
  1186. }
  1187. // }}}
  1188. // {{{ lastInsertID()
  1189. /**
  1190. * Returns the autoincrement ID if supported or $id or fetches the current
  1191. * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  1192. *
  1193. * @param string $table name of the table in…

Large files files are truncated, but you can click here to view the full file