PageRenderTime 70ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/pear/MDB2/Driver/pgsql.php

https://bitbucket.org/renaatdemuynck/chamilo
PHP | 1711 lines | 1140 code | 146 blank | 425 comment | 212 complexity | 4c3c7eb45ba619a234aaad69e42d7e8f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT, GPL-2.0

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: Paul Cooper <pgc@ucecom.com> |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: pgsql.php 137 2009-11-09 13:24:37Z vanpouckesven $
  47. /**
  48. * MDB2 PostGreSQL driver
  49. *
  50. * @package MDB2
  51. * @category Database
  52. * @author Paul Cooper <pgc@ucecom.com>
  53. */
  54. class MDB2_Driver_pgsql extends MDB2_Driver_Common
  55. {
  56. // {{{ properties
  57. var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => "'", 'escape_pattern' => '\\');
  58. var $identifier_quoting = array('start' => '"', 'end' => '"', 'escape' => '"');
  59. // }}}
  60. // {{{ constructor
  61. /**
  62. * Constructor
  63. */
  64. function __construct()
  65. {
  66. parent :: __construct();
  67. $this->phptype = 'pgsql';
  68. $this->dbsyntax = 'pgsql';
  69. $this->supported['sequences'] = true;
  70. $this->supported['indexes'] = true;
  71. $this->supported['affected_rows'] = true;
  72. $this->supported['summary_functions'] = true;
  73. $this->supported['order_by_text'] = true;
  74. $this->supported['transactions'] = true;
  75. $this->supported['savepoints'] = true;
  76. $this->supported['current_id'] = true;
  77. $this->supported['limit_queries'] = true;
  78. $this->supported['LOBs'] = true;
  79. $this->supported['replace'] = 'emulated';
  80. $this->supported['sub_selects'] = true;
  81. $this->supported['triggers'] = true;
  82. $this->supported['auto_increment'] = 'emulated';
  83. $this->supported['primary_key'] = true;
  84. $this->supported['result_introspection'] = true;
  85. $this->supported['prepared_statements'] = true;
  86. $this->supported['identifier_quoting'] = true;
  87. $this->supported['pattern_escaping'] = true;
  88. $this->supported['new_link'] = true;
  89. $this->options['DBA_username'] = false;
  90. $this->options['DBA_password'] = false;
  91. $this->options['multi_query'] = false;
  92. $this->options['disable_smart_seqname'] = true;
  93. $this->options['max_identifiers_length'] = 63;
  94. }
  95. // }}}
  96. // {{{ errorInfo()
  97. /**
  98. * This method is used to collect information about an error
  99. *
  100. * @param integer $error
  101. * @return array
  102. * @access public
  103. */
  104. function errorInfo($error = null)
  105. {
  106. // Fall back to MDB2_ERROR if there was no mapping.
  107. $error_code = MDB2_ERROR;
  108. $native_msg = '';
  109. if (is_resource($error))
  110. {
  111. $native_msg = @pg_result_error($error);
  112. }
  113. elseif ($this->connection)
  114. {
  115. $native_msg = @pg_last_error($this->connection);
  116. if (! $native_msg && @pg_connection_status($this->connection) === PGSQL_CONNECTION_BAD)
  117. {
  118. $native_msg = 'Database connection has been lost.';
  119. $error_code = MDB2_ERROR_CONNECT_FAILED;
  120. }
  121. }
  122. else
  123. {
  124. $native_msg = @pg_last_error();
  125. }
  126. static $error_regexps;
  127. if (empty($error_regexps))
  128. {
  129. $error_regexps = array(
  130. '/column .* (of relation .*)?does not exist/i' => MDB2_ERROR_NOSUCHFIELD,
  131. '/(relation|sequence|table).*does not exist|class .* not found/i' => MDB2_ERROR_NOSUCHTABLE,
  132. '/database .* does not exist/' => MDB2_ERROR_NOT_FOUND,
  133. '/constraint .* does not exist/' => MDB2_ERROR_NOT_FOUND,
  134. '/index .* does not exist/' => MDB2_ERROR_NOT_FOUND,
  135. '/database .* already exists/i' => MDB2_ERROR_ALREADY_EXISTS,
  136. '/relation .* already exists/i' => MDB2_ERROR_ALREADY_EXISTS,
  137. '/(divide|division) by zero$/i' => MDB2_ERROR_DIVZERO,
  138. '/pg_atoi: error in .*: can\'t parse /i' => MDB2_ERROR_INVALID_NUMBER,
  139. '/invalid input syntax for( type)? (integer|numeric)/i' => MDB2_ERROR_INVALID_NUMBER,
  140. '/value .* is out of range for type \w*int/i' => MDB2_ERROR_INVALID_NUMBER,
  141. '/integer out of range/i' => MDB2_ERROR_INVALID_NUMBER,
  142. '/value too long for type character/i' => MDB2_ERROR_INVALID,
  143. '/attribute .* not found|relation .* does not have attribute/i' => MDB2_ERROR_NOSUCHFIELD,
  144. '/column .* specified in USING clause does not exist in (left|right) table/i' => MDB2_ERROR_NOSUCHFIELD,
  145. '/parser: parse error at or near/i' => MDB2_ERROR_SYNTAX,
  146. '/syntax error at/' => MDB2_ERROR_SYNTAX,
  147. '/column reference .* is ambiguous/i' => MDB2_ERROR_SYNTAX,
  148. '/permission denied/' => MDB2_ERROR_ACCESS_VIOLATION,
  149. '/violates not-null constraint/' => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  150. '/violates [\w ]+ constraint/' => MDB2_ERROR_CONSTRAINT,
  151. '/referential integrity violation/' => MDB2_ERROR_CONSTRAINT,
  152. '/more expressions than target columns/i' => MDB2_ERROR_VALUE_COUNT_ON_ROW);
  153. }
  154. if (is_numeric($error) && $error < 0)
  155. {
  156. $error_code = $error;
  157. }
  158. else
  159. {
  160. foreach ($error_regexps as $regexp => $code)
  161. {
  162. if (preg_match($regexp, $native_msg))
  163. {
  164. $error_code = $code;
  165. break;
  166. }
  167. }
  168. }
  169. return array($error_code, null, $native_msg);
  170. }
  171. // }}}
  172. // {{{ escape()
  173. /**
  174. * Quotes a string so it can be safely used in a query. It will quote
  175. * the text so it can safely be used within a query.
  176. *
  177. * @param string the input string to quote
  178. * @param bool escape wildcards
  179. *
  180. * @return string quoted string
  181. *
  182. * @access public
  183. */
  184. function escape($text, $escape_wildcards = false)
  185. {
  186. if ($escape_wildcards)
  187. {
  188. $text = $this->escapePattern($text);
  189. }
  190. $connection = $this->getConnection();
  191. if (PEAR :: isError($connection))
  192. {
  193. return $connection;
  194. }
  195. if (is_resource($connection) && version_compare(PHP_VERSION, '5.2.0RC5', '>='))
  196. {
  197. $text = @pg_escape_string($connection, $text);
  198. }
  199. else
  200. {
  201. $text = @pg_escape_string($text);
  202. }
  203. return $text;
  204. }
  205. // }}}
  206. // {{{ beginTransaction()
  207. /**
  208. * Start a transaction or set a savepoint.
  209. *
  210. * @param string name of a savepoint to set
  211. * @return mixed MDB2_OK on success, a MDB2 error on failure
  212. *
  213. * @access public
  214. */
  215. function beginTransaction($savepoint = null)
  216. {
  217. $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true,
  218. 'savepoint' => $savepoint));
  219. if (! is_null($savepoint))
  220. {
  221. if (! $this->in_transaction)
  222. {
  223. return $this->raiseError(MDB2_ERROR_INVALID, null, null, 'savepoint cannot be released when changes are auto committed', __FUNCTION__);
  224. }
  225. $query = 'SAVEPOINT ' . $savepoint;
  226. return $this->_doQuery($query, true);
  227. }
  228. elseif ($this->in_transaction)
  229. {
  230. return MDB2_OK; //nothing to do
  231. }
  232. if (! $this->destructor_registered && $this->opened_persistent)
  233. {
  234. $this->destructor_registered = true;
  235. register_shutdown_function('MDB2_closeOpenTransactions');
  236. }
  237. $result = & $this->_doQuery('BEGIN', true);
  238. if (PEAR :: isError($result))
  239. {
  240. return $result;
  241. }
  242. $this->in_transaction = true;
  243. return MDB2_OK;
  244. }
  245. // }}}
  246. // {{{ commit()
  247. /**
  248. * Commit the database changes done during a transaction that is in
  249. * progress or release a savepoint. This function may only be called when
  250. * auto-committing is disabled, otherwise it will fail. Therefore, a new
  251. * transaction is implicitly started after committing the pending changes.
  252. *
  253. * @param string name of a savepoint to release
  254. * @return mixed MDB2_OK on success, a MDB2 error on failure
  255. *
  256. * @access public
  257. */
  258. function commit($savepoint = null)
  259. {
  260. $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true,
  261. 'savepoint' => $savepoint));
  262. if (! $this->in_transaction)
  263. {
  264. return $this->raiseError(MDB2_ERROR_INVALID, null, null, 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
  265. }
  266. if (! is_null($savepoint))
  267. {
  268. $query = 'RELEASE SAVEPOINT ' . $savepoint;
  269. return $this->_doQuery($query, true);
  270. }
  271. $result = & $this->_doQuery('COMMIT', true);
  272. if (PEAR :: isError($result))
  273. {
  274. return $result;
  275. }
  276. $this->in_transaction = false;
  277. return MDB2_OK;
  278. }
  279. // }}}
  280. // {{{ rollback()
  281. /**
  282. * Cancel any database changes done during a transaction or since a specific
  283. * savepoint that is in progress. This function may only be called when
  284. * auto-committing is disabled, otherwise it will fail. Therefore, a new
  285. * transaction is implicitly started after canceling the pending changes.
  286. *
  287. * @param string name of a savepoint to rollback to
  288. * @return mixed MDB2_OK on success, a MDB2 error on failure
  289. *
  290. * @access public
  291. */
  292. function rollback($savepoint = null)
  293. {
  294. $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true,
  295. 'savepoint' => $savepoint));
  296. if (! $this->in_transaction)
  297. {
  298. return $this->raiseError(MDB2_ERROR_INVALID, null, null, 'rollback cannot be done changes are auto committed', __FUNCTION__);
  299. }
  300. if (! is_null($savepoint))
  301. {
  302. $query = 'ROLLBACK TO SAVEPOINT ' . $savepoint;
  303. return $this->_doQuery($query, true);
  304. }
  305. $query = 'ROLLBACK';
  306. $result = & $this->_doQuery($query, true);
  307. if (PEAR :: isError($result))
  308. {
  309. return $result;
  310. }
  311. $this->in_transaction = false;
  312. return MDB2_OK;
  313. }
  314. // }}}
  315. // {{{ function setTransactionIsolation()
  316. /**
  317. * Set the transacton isolation level.
  318. *
  319. * @param string standard isolation level
  320. * READ UNCOMMITTED (allows dirty reads)
  321. * READ COMMITTED (prevents dirty reads)
  322. * REPEATABLE READ (prevents nonrepeatable reads)
  323. * SERIALIZABLE (prevents phantom reads)
  324. * @return mixed MDB2_OK on success, a MDB2 error on failure
  325. *
  326. * @access public
  327. * @since 2.1.1
  328. */
  329. function setTransactionIsolation($isolation)
  330. {
  331. $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
  332. switch ($isolation)
  333. {
  334. case 'READ UNCOMMITTED' :
  335. case 'READ COMMITTED' :
  336. case 'REPEATABLE READ' :
  337. case 'SERIALIZABLE' :
  338. break;
  339. default :
  340. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, 'isolation level is not supported: ' . $isolation, __FUNCTION__);
  341. }
  342. $query = "SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL $isolation";
  343. return $this->_doQuery($query, true);
  344. }
  345. // }}}
  346. // {{{ _doConnect()
  347. /**
  348. * Do the grunt work of connecting to the database
  349. *
  350. * @return mixed connection resource on success, MDB2 Error Object on failure
  351. * @access protected
  352. */
  353. function _doConnect($username, $password, $database_name, $persistent = false)
  354. {
  355. if (! PEAR :: loadExtension($this->phptype))
  356. {
  357. return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, 'extension ' . $this->phptype . ' is not compiled into PHP', __FUNCTION__);
  358. }
  359. if ($database_name == '')
  360. {
  361. $database_name = 'template1';
  362. }
  363. $protocol = $this->dsn['protocol'] ? $this->dsn['protocol'] : 'tcp';
  364. $params = array('');
  365. if ($protocol == 'tcp')
  366. {
  367. if ($this->dsn['hostspec'])
  368. {
  369. $params[0] .= 'host=' . $this->dsn['hostspec'];
  370. }
  371. if ($this->dsn['port'])
  372. {
  373. $params[0] .= ' port=' . $this->dsn['port'];
  374. }
  375. }
  376. elseif ($protocol == 'unix')
  377. {
  378. // Allow for pg socket in non-standard locations.
  379. if ($this->dsn['socket'])
  380. {
  381. $params[0] .= 'host=' . $this->dsn['socket'];
  382. }
  383. if ($this->dsn['port'])
  384. {
  385. $params[0] .= ' port=' . $this->dsn['port'];
  386. }
  387. }
  388. if ($database_name)
  389. {
  390. $params[0] .= ' dbname=\'' . addslashes($database_name) . '\'';
  391. }
  392. if ($username)
  393. {
  394. $params[0] .= ' user=\'' . addslashes($username) . '\'';
  395. }
  396. if ($password)
  397. {
  398. $params[0] .= ' password=\'' . addslashes($password) . '\'';
  399. }
  400. if (! empty($this->dsn['options']))
  401. {
  402. $params[0] .= ' options=' . $this->dsn['options'];
  403. }
  404. if (! empty($this->dsn['tty']))
  405. {
  406. $params[0] .= ' tty=' . $this->dsn['tty'];
  407. }
  408. if (! empty($this->dsn['connect_timeout']))
  409. {
  410. $params[0] .= ' connect_timeout=' . $this->dsn['connect_timeout'];
  411. }
  412. if (! empty($this->dsn['sslmode']))
  413. {
  414. $params[0] .= ' sslmode=' . $this->dsn['sslmode'];
  415. }
  416. if (! empty($this->dsn['service']))
  417. {
  418. $params[0] .= ' service=' . $this->dsn['service'];
  419. }
  420. if ($this->_isNewLinkSet())
  421. {
  422. if (version_compare(phpversion(), '4.3.0', '>='))
  423. {
  424. $params[] = PGSQL_CONNECT_FORCE_NEW;
  425. }
  426. }
  427. $connect_function = $persistent ? 'pg_pconnect' : 'pg_connect';
  428. $connection = @call_user_func_array($connect_function, $params);
  429. if (! $connection)
  430. {
  431. return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null, 'unable to establish a connection', __FUNCTION__);
  432. }
  433. if (empty($this->dsn['disable_iso_date']))
  434. {
  435. if (! @pg_query($connection, "SET SESSION DATESTYLE = 'ISO'"))
  436. {
  437. return $this->raiseError(null, null, null, 'Unable to set date style to iso', __FUNCTION__);
  438. }
  439. }
  440. if (! empty($this->dsn['charset']))
  441. {
  442. $result = $this->setCharset($this->dsn['charset'], $connection);
  443. if (PEAR :: isError($result))
  444. {
  445. return $result;
  446. }
  447. }
  448. return $connection;
  449. }
  450. // }}}
  451. // {{{ connect()
  452. /**
  453. * Connect to the database
  454. *
  455. * @return true on success, MDB2 Error Object on failure
  456. * @access public
  457. */
  458. function connect()
  459. {
  460. if (is_resource($this->connection))
  461. {
  462. //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
  463. if (MDB2 :: areEquals($this->connected_dsn, $this->dsn) && $this->connected_database_name == $this->database_name && ($this->opened_persistent == $this->options['persistent']))
  464. {
  465. return MDB2_OK;
  466. }
  467. $this->disconnect(false);
  468. }
  469. if ($this->database_name)
  470. {
  471. $connection = $this->_doConnect($this->dsn['username'], $this->dsn['password'], $this->database_name, $this->options['persistent']);
  472. if (PEAR :: isError($connection))
  473. {
  474. return $connection;
  475. }
  476. $this->connection = $connection;
  477. $this->connected_dsn = $this->dsn;
  478. $this->connected_database_name = $this->database_name;
  479. $this->opened_persistent = $this->options['persistent'];
  480. $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
  481. }
  482. return MDB2_OK;
  483. }
  484. // }}}
  485. // {{{ setCharset()
  486. /**
  487. * Set the charset on the current connection
  488. *
  489. * @param string charset
  490. * @param resource connection handle
  491. *
  492. * @return true on success, MDB2 Error Object on failure
  493. */
  494. function setCharset($charset, $connection = null)
  495. {
  496. if (is_null($connection))
  497. {
  498. $connection = $this->getConnection();
  499. if (PEAR :: isError($connection))
  500. {
  501. return $connection;
  502. }
  503. }
  504. if (is_array($charset))
  505. {
  506. $charset = array_shift($charset);
  507. $this->warnings[] = 'postgresql does not support setting client collation';
  508. }
  509. $result = @pg_set_client_encoding($connection, $charset);
  510. if ($result == - 1)
  511. {
  512. return $this->raiseError(null, null, null, 'Unable to set client charset: ' . $charset, __FUNCTION__);
  513. }
  514. return MDB2_OK;
  515. }
  516. // }}}
  517. // {{{ databaseExists()
  518. /**
  519. * check if given database name is exists?
  520. *
  521. * @param string $name name of the database that should be checked
  522. *
  523. * @return mixed true/false on success, a MDB2 error on failure
  524. * @access public
  525. */
  526. function databaseExists($name)
  527. {
  528. $res = $this->_doConnect($this->dsn['username'], $this->dsn['password'], $this->escape($name), $this->options['persistent']);
  529. if (! PEAR :: isError($res))
  530. {
  531. return true;
  532. }
  533. return false;
  534. }
  535. // }}}
  536. // {{{ disconnect()
  537. /**
  538. * Log out and disconnect from the database.
  539. *
  540. * @param boolean $force if the disconnect should be forced even if the
  541. * connection is opened persistently
  542. * @return mixed true on success, false if not connected and error
  543. * object on error
  544. * @access public
  545. */
  546. function disconnect($force = true)
  547. {
  548. if (is_resource($this->connection))
  549. {
  550. if ($this->in_transaction)
  551. {
  552. $dsn = $this->dsn;
  553. $database_name = $this->database_name;
  554. $persistent = $this->options['persistent'];
  555. $this->dsn = $this->connected_dsn;
  556. $this->database_name = $this->connected_database_name;
  557. $this->options['persistent'] = $this->opened_persistent;
  558. $this->rollback();
  559. $this->dsn = $dsn;
  560. $this->database_name = $database_name;
  561. $this->options['persistent'] = $persistent;
  562. }
  563. if (! $this->opened_persistent || $force)
  564. {
  565. $ok = @pg_close($this->connection);
  566. if (! $ok)
  567. {
  568. return $this->raiseError(MDB2_ERROR_DISCONNECT_FAILED, null, null, null, __FUNCTION__);
  569. }
  570. }
  571. }
  572. else
  573. {
  574. return false;
  575. }
  576. return parent :: disconnect($force);
  577. }
  578. // }}}
  579. // {{{ standaloneQuery()
  580. /**
  581. * execute a query as DBA
  582. *
  583. * @param string $query the SQL query
  584. * @param mixed $types array that contains the types of the columns in
  585. * the result set
  586. * @param boolean $is_manip if the query is a manipulation query
  587. * @return mixed MDB2_OK on success, a MDB2 error on failure
  588. * @access public
  589. */
  590. function &standaloneQuery($query, $types = null, $is_manip = false)
  591. {
  592. $user = $this->options['DBA_username'] ? $this->options['DBA_username'] : $this->dsn['username'];
  593. $pass = $this->options['DBA_password'] ? $this->options['DBA_password'] : $this->dsn['password'];
  594. $connection = $this->_doConnect($user, $pass, $this->database_name, $this->options['persistent']);
  595. if (PEAR :: isError($connection))
  596. {
  597. return $connection;
  598. }
  599. $offset = $this->offset;
  600. $limit = $this->limit;
  601. $this->offset = $this->limit = 0;
  602. $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
  603. $result = & $this->_doQuery($query, $is_manip, $connection, $this->database_name);
  604. if (! PEAR :: isError($result))
  605. {
  606. if ($is_manip)
  607. {
  608. $result = $this->_affectedRows($connection, $result);
  609. }
  610. else
  611. {
  612. $result = & $this->_wrapResult($result, $types, true, false, $limit, $offset);
  613. }
  614. }
  615. @pg_close($connection);
  616. return $result;
  617. }
  618. // }}}
  619. // {{{ _doQuery()
  620. /**
  621. * Execute a query
  622. * @param string $query query
  623. * @param boolean $is_manip if the query is a manipulation query
  624. * @param resource $connection
  625. * @param string $database_name
  626. * @return result or error object
  627. * @access protected
  628. */
  629. function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
  630. {
  631. $this->last_query = $query;
  632. $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
  633. if ($result)
  634. {
  635. if (PEAR :: isError($result))
  636. {
  637. return $result;
  638. }
  639. $query = $result;
  640. }
  641. if ($this->options['disable_query'])
  642. {
  643. $result = $is_manip ? 0 : null;
  644. return $result;
  645. }
  646. if (is_null($connection))
  647. {
  648. $connection = $this->getConnection();
  649. if (PEAR :: isError($connection))
  650. {
  651. return $connection;
  652. }
  653. }
  654. $function = $this->options['multi_query'] ? 'pg_send_query' : 'pg_query';
  655. $result = @$function($connection, $query);
  656. if (! $result)
  657. {
  658. $err = & $this->raiseError(null, null, null, 'Could not execute statement', __FUNCTION__);
  659. return $err;
  660. }
  661. elseif ($this->options['multi_query'])
  662. {
  663. if (! ($result = @pg_get_result($connection)))
  664. {
  665. $err = & $this->raiseError(null, null, null, 'Could not get the first result from a multi query', __FUNCTION__);
  666. return $err;
  667. }
  668. }
  669. $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
  670. return $result;
  671. }
  672. // }}}
  673. // {{{ _affectedRows()
  674. /**
  675. * Returns the number of rows affected
  676. *
  677. * @param resource $result
  678. * @param resource $connection
  679. * @return mixed MDB2 Error Object or the number of rows affected
  680. * @access private
  681. */
  682. function _affectedRows($connection, $result = null)
  683. {
  684. if (is_null($connection))
  685. {
  686. $connection = $this->getConnection();
  687. if (PEAR :: isError($connection))
  688. {
  689. return $connection;
  690. }
  691. }
  692. return @pg_affected_rows($result);
  693. }
  694. // }}}
  695. // {{{ _modifyQuery()
  696. /**
  697. * Changes a query string for various DBMS specific reasons
  698. *
  699. * @param string $query query to modify
  700. * @param boolean $is_manip if it is a DML query
  701. * @param integer $limit limit the number of rows
  702. * @param integer $offset start reading from given offset
  703. * @return string modified query
  704. * @access protected
  705. */
  706. function _modifyQuery($query, $is_manip, $limit, $offset)
  707. {
  708. if ($limit > 0 && ! preg_match('/LIMIT\s*\d(?:\s*(?:,|OFFSET)\s*\d+)?(?:[^\)]*)?$/i', $query))
  709. {
  710. $query = rtrim($query);
  711. if (substr($query, - 1) == ';')
  712. {
  713. $query = substr($query, 0, - 1);
  714. }
  715. if ($is_manip)
  716. {
  717. $query = $this->_modifyManipQuery($query, $limit);
  718. }
  719. else
  720. {
  721. $query .= " LIMIT $limit OFFSET $offset";
  722. }
  723. }
  724. return $query;
  725. }
  726. // }}}
  727. // {{{ _modifyManipQuery()
  728. /**
  729. * Changes a manip query string for various DBMS specific reasons
  730. *
  731. * @param string $query query to modify
  732. * @param integer $limit limit the number of rows
  733. * @return string modified query
  734. * @access protected
  735. */
  736. function _modifyManipQuery($query, $limit)
  737. {
  738. $pos = strpos(strtolower($query), 'where');
  739. $where = $pos ? substr($query, $pos) : '';
  740. $manip_clause = '(\bDELETE\b\s+(?:\*\s+)?\bFROM\b|\bUPDATE\b)';
  741. $from_clause = '([\w\.]+)';
  742. $where_clause = '(?:(.*)\bWHERE\b\s+(.*))|(.*)';
  743. $pattern = '/^' . $manip_clause . '\s+' . $from_clause . '(?:\s)*(?:' . $where_clause . ')?$/i';
  744. $matches = preg_match($pattern, $query, $match);
  745. if ($matches)
  746. {
  747. $manip = $match[1];
  748. $from = $match[2];
  749. $what = (count($matches) == 6) ? $match[5] : $match[3];
  750. return $manip . ' ' . $from . ' ' . $what . ' WHERE ctid=(SELECT ctid FROM ' . $from . ' ' . $where . ' LIMIT ' . $limit . ')';
  751. }
  752. //return error?
  753. return $query;
  754. }
  755. // }}}
  756. // {{{ getServerVersion()
  757. /**
  758. * return version information about the server
  759. *
  760. * @param bool $native determines if the raw version string should be returned
  761. * @return mixed array/string with version information or MDB2 error object
  762. * @access public
  763. */
  764. function getServerVersion($native = false)
  765. {
  766. $query = 'SHOW SERVER_VERSION';
  767. if ($this->connected_server_info)
  768. {
  769. $server_info = $this->connected_server_info;
  770. }
  771. else
  772. {
  773. $server_info = $this->queryOne($query, 'text');
  774. if (PEAR :: isError($server_info))
  775. {
  776. return $server_info;
  777. }
  778. }
  779. // cache server_info
  780. $this->connected_server_info = $server_info;
  781. if (! $native && ! PEAR :: isError($server_info))
  782. {
  783. $tmp = explode('.', $server_info, 3);
  784. if (empty($tmp[2]) && isset($tmp[1]) && preg_match('/(\d+)(.*)/', $tmp[1], $tmp2))
  785. {
  786. $server_info = array('major' => $tmp[0], 'minor' => $tmp2[1], 'patch' => null, 'extra' => $tmp2[2],
  787. 'native' => $server_info);
  788. }
  789. else
  790. {
  791. $server_info = array('major' => isset($tmp[0]) ? $tmp[0] : null,
  792. 'minor' => isset($tmp[1]) ? $tmp[1] : null, 'patch' => isset($tmp[2]) ? $tmp[2] : null,
  793. 'extra' => null, 'native' => $server_info);
  794. }
  795. }
  796. return $server_info;
  797. }
  798. // }}}
  799. // {{{ prepare()
  800. /**
  801. * Prepares a query for multiple execution with execute().
  802. * With some database backends, this is emulated.
  803. * prepare() requires a generic query as string like
  804. * 'INSERT INTO numbers VALUES(?,?)' or
  805. * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  806. * The ? and :name and are placeholders which can be set using
  807. * bindParam() and the query can be sent off using the execute() method.
  808. * The allowed format for :name can be set with the 'bindname_format' option.
  809. *
  810. * @param string $query the query to prepare
  811. * @param mixed $types array that contains the types of the placeholders
  812. * @param mixed $result_types array that contains the types of the columns in
  813. * the result set or MDB2_PREPARE_RESULT, if set to
  814. * MDB2_PREPARE_MANIP the query is handled as a manipulation query
  815. * @param mixed $lobs key (field) value (parameter) pair for all lob placeholders
  816. * @return mixed resource handle for the prepared query on success, a MDB2
  817. * error on failure
  818. * @access public
  819. * @see bindParam, execute
  820. */
  821. function &prepare($query, $types = null, $result_types = null, $lobs = array())
  822. {
  823. if ($this->options['emulate_prepared'])
  824. {
  825. $obj = & parent :: prepare($query, $types, $result_types, $lobs);
  826. return $obj;
  827. }
  828. $is_manip = ($result_types === MDB2_PREPARE_MANIP);
  829. $offset = $this->offset;
  830. $limit = $this->limit;
  831. $this->offset = $this->limit = 0;
  832. $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
  833. if ($result)
  834. {
  835. if (PEAR :: isError($result))
  836. {
  837. return $result;
  838. }
  839. $query = $result;
  840. }
  841. $pgtypes = function_exists('pg_prepare') ? false : array();
  842. if ($pgtypes !== false && ! empty($types))
  843. {
  844. $this->loadModule('Datatype', null, true);
  845. }
  846. $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
  847. $placeholder_type_guess = $placeholder_type = null;
  848. $question = '?';
  849. $colon = ':';
  850. $positions = array();
  851. $position = $parameter = 0;
  852. while ($position < strlen($query))
  853. {
  854. $q_position = strpos($query, $question, $position);
  855. $c_position = strpos($query, $colon, $position);
  856. //skip "::type" cast ("select id::varchar(20) from sometable where name=?")
  857. $doublecolon_position = strpos($query, '::', $position);
  858. if ($doublecolon_position !== false && $doublecolon_position == $c_position)
  859. {
  860. $c_position = strpos($query, $colon, $position + 2);
  861. }
  862. if ($q_position && $c_position)
  863. {
  864. $p_position = min($q_position, $c_position);
  865. }
  866. elseif ($q_position)
  867. {
  868. $p_position = $q_position;
  869. }
  870. elseif ($c_position)
  871. {
  872. $p_position = $c_position;
  873. }
  874. else
  875. {
  876. break;
  877. }
  878. if (is_null($placeholder_type))
  879. {
  880. $placeholder_type_guess = $query[$p_position];
  881. }
  882. $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
  883. if (PEAR :: isError($new_pos))
  884. {
  885. return $new_pos;
  886. }
  887. if ($new_pos != $position)
  888. {
  889. $position = $new_pos;
  890. continue; //evaluate again starting from the new position
  891. }
  892. if ($query[$position] == $placeholder_type_guess)
  893. {
  894. if (is_null($placeholder_type))
  895. {
  896. $placeholder_type = $query[$p_position];
  897. $question = $colon = $placeholder_type;
  898. if (! empty($types) && is_array($types))
  899. {
  900. if ($placeholder_type == ':')
  901. {
  902. }
  903. else
  904. {
  905. $types = array_values($types);
  906. }
  907. }
  908. }
  909. if ($placeholder_type_guess == '?')
  910. {
  911. $length = 1;
  912. $name = $parameter;
  913. }
  914. else
  915. {
  916. $regexp = '/^.{' . ($position + 1) . '}(' . $this->options['bindname_format'] . ').*$/s';
  917. $param = preg_replace($regexp, '\\1', $query);
  918. if ($param === '')
  919. {
  920. $err = & $this->raiseError(MDB2_ERROR_SYNTAX, null, null, 'named parameter name must match "bindname_format" option', __FUNCTION__);
  921. return $err;
  922. }
  923. $length = strlen($param) + 1;
  924. $name = $param;
  925. }
  926. if ($pgtypes !== false)
  927. {
  928. if (is_array($types) && array_key_exists($name, $types))
  929. {
  930. $pgtypes[] = $this->datatype->mapPrepareDatatype($types[$name]);
  931. }
  932. elseif (is_array($types) && array_key_exists($parameter, $types))
  933. {
  934. $pgtypes[] = $this->datatype->mapPrepareDatatype($types[$parameter]);
  935. }
  936. else
  937. {
  938. $pgtypes[] = 'text';
  939. }
  940. }
  941. if (($key_parameter = array_search($name, $positions)))
  942. {
  943. $next_parameter = 1;
  944. foreach ($positions as $key => $value)
  945. {
  946. if ($key_parameter == $key)
  947. {
  948. break;
  949. }
  950. ++ $next_parameter;
  951. }
  952. }
  953. else
  954. {
  955. ++ $parameter;
  956. $next_parameter = $parameter;
  957. $positions[] = $name;
  958. }
  959. $query = substr_replace($query, '$' . $parameter, $position, $length);
  960. $position = $p_position + strlen($parameter);
  961. }
  962. else
  963. {
  964. $position = $p_position;
  965. }
  966. }
  967. $connection = $this->getConnection();
  968. if (PEAR :: isError($connection))
  969. {
  970. return $connection;
  971. }
  972. static $prep_statement_counter = 1;
  973. $statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter ++ . sha1(microtime() + mt_rand()));
  974. $statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']);
  975. if ($pgtypes === false)
  976. {
  977. $result = @pg_prepare($connection, $statement_name, $query);
  978. if (! $result)
  979. {
  980. $err = & $this->raiseError(null, null, null, 'Unable to create prepared statement handle', __FUNCTION__);
  981. return $err;
  982. }
  983. }
  984. else
  985. {
  986. $types_string = '';
  987. if ($pgtypes)
  988. {
  989. $types_string = ' (' . implode(', ', $pgtypes) . ') ';
  990. }
  991. $query = 'PREPARE ' . $statement_name . $types_string . ' AS ' . $query;
  992. $statement = & $this->_doQuery($query, true, $connection);
  993. if (PEAR :: isError($statement))
  994. {
  995. return $statement;
  996. }
  997. }
  998. $class_name = 'MDB2_Statement_' . $this->phptype;
  999. $obj = new $class_name($this, $statement_name, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
  1000. $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
  1001. return $obj;
  1002. }
  1003. // }}}
  1004. // {{{ function getSequenceName($sqn)
  1005. /**
  1006. * adds sequence name formatting to a sequence name
  1007. *
  1008. * @param string name of the sequence
  1009. *
  1010. * @return string formatted sequence name
  1011. *
  1012. * @access public
  1013. */
  1014. function getSequenceName($sqn)
  1015. {
  1016. if (false === $this->options['disable_smart_seqname'])
  1017. {
  1018. if (strpos($sqn, '_') !== false)
  1019. {
  1020. list($table, $field) = explode('_', $sqn, 2);
  1021. }
  1022. $schema_list = $this->queryOne("SELECT array_to_string(current_schemas(false), ',')");
  1023. if (PEAR :: isError($schema_list) || empty($schema_list) || count($schema_list) < 2)
  1024. {
  1025. $order_by = ' a.attnum';
  1026. $schema_clause = ' AND n.nspname=current_schema()';
  1027. }
  1028. else
  1029. {
  1030. $schemas = explode(',', $schema_list);
  1031. $schema_clause = ' AND n.nspname IN (' . $schema_list . ')';
  1032. $counter = 1;
  1033. $order_by = ' CASE ';
  1034. foreach ($schemas as $schema)
  1035. {
  1036. $order_by .= ' WHEN n.nspname=' . $schema . ' THEN ' . $counter ++;
  1037. }
  1038. $order_by .= ' ELSE ' . $counter . ' END, a.attnum';
  1039. }
  1040. $query = "SELECT substring((SELECT substring(pg_get_expr(d.adbin, d.adrelid) for 128)
  1041. FROM pg_attrdef d
  1042. WHERE d.adrelid = a.attrelid
  1043. AND d.adnum = a.attnum
  1044. AND a.atthasdef
  1045. ) FROM 'nextval[^'']*''([^'']*)')
  1046. FROM pg_attribute a
  1047. LEFT JOIN pg_class c ON c.oid = a.attrelid
  1048. LEFT JOIN pg_attrdef d ON d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef
  1049. LEFT JOIN pg_namespace n ON c.relnamespace = n.oid
  1050. WHERE (c.relname = " . $this->quote($sqn, 'text');
  1051. if (! empty($field))
  1052. {
  1053. $query .= " OR (c.relname = " . $this->quote($table, 'text') . " AND a.attname = " . $this->quote($field, 'text') . ")";
  1054. }
  1055. $query .= " )" . $schema_clause . "
  1056. AND NOT a.attisdropped
  1057. AND a.attnum > 0
  1058. AND pg_get_expr(d.adbin, d.adrelid) LIKE 'nextval%'
  1059. ORDER BY " . $order_by;
  1060. $seqname = $this->queryOne($query);
  1061. if (! PEAR :: isError($seqname) && ! empty($seqname) && is_string($seqname))
  1062. {
  1063. return $seqname;
  1064. }
  1065. }
  1066. return parent :: getSequenceName($sqn);
  1067. }
  1068. // }}}
  1069. // {{{ nextID()
  1070. /**
  1071. * Returns the next free id of a sequence
  1072. *
  1073. * @param string $seq_name name of the sequence
  1074. * @param boolean $ondemand when true the sequence is
  1075. * automatic created, if it
  1076. * not exists
  1077. * @return mixed MDB2 Error Object or id
  1078. * @access public
  1079. */
  1080. function nextID($seq_name, $ondemand = true)
  1081. {
  1082. $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
  1083. $query = "SELECT NEXTVAL('$sequence_name')";
  1084. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  1085. $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  1086. $result = $this->queryOne($query, 'integer');
  1087. $this->popExpect();
  1088. $this->popErrorHandling();
  1089. if (PEAR :: isError($result))
  1090. {
  1091. if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE)
  1092. {
  1093. $this->loadModule('Manager', null, true);
  1094. $result = $this->manager->createSequence($seq_name);
  1095. if (PEAR :: isError($result))
  1096. {
  1097. return $this->raiseError($result, null, null, 'on demand sequence could not be created', __FUNCTION__);
  1098. }
  1099. return $this->nextId($seq_name, false);
  1100. }
  1101. }
  1102. return $result;
  1103. }
  1104. // }}}
  1105. // {{{ lastInsertID()
  1106. /**
  1107. * Returns the autoincrement ID if supported or $id or fetches the current
  1108. * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  1109. *
  1110. * @param string $table name of the table into which a new row was inserted
  1111. * @param string $field name of the field into which a new row was inserted
  1112. * @return mixed MDB2 Error Object or id
  1113. * @access public
  1114. */
  1115. function lastInsertID($table = null, $field = null)
  1116. {
  1117. if (empty($table) && empty($field))
  1118. {
  1119. return $this->queryOne('SELECT lastval()', 'integer');
  1120. }
  1121. $seq = $table . (empty($field) ? '' : '_' . $field);
  1122. $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq), true);
  1123. return $this->queryOne("SELECT currval('$sequence_name')", 'integer');
  1124. }
  1125. // }}}
  1126. // {{{ currID()
  1127. /**
  1128. * Returns the current id of a sequence
  1129. *
  1130. * @param string $seq_name name of the sequence
  1131. * @return mixed MDB2 Error Object or id
  1132. * @access public
  1133. */
  1134. function currID($seq_name)
  1135. {
  1136. $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
  1137. return $this->queryOne("SELECT last_value FROM $sequence_name", 'integer');
  1138. }
  1139. }
  1140. /**
  1141. * MDB2 PostGreSQL result driver
  1142. *
  1143. * @package MDB2
  1144. * @category Database
  1145. * @author Paul Cooper <pgc@ucecom.com>
  1146. */
  1147. class MDB2_Result_pgsql extends MDB2_Result_Common
  1148. {
  1149. // }}}
  1150. // {{{ fetchRow()
  1151. /**
  1152. * Fetch a row and insert the data into an existing array.
  1153. *
  1154. * @param int $fetchmode how the array data should be indexed
  1155. * @param int $rownum number of the row where the data can be found
  1156. * @return int data array on success, a MDB2 error on failure
  1157. * @access public
  1158. */
  1159. function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
  1160. {
  1161. if (! is_null($rownum))
  1162. {
  1163. $seek = $this->seek($rownum);
  1164. if (PEAR :: isError($seek))
  1165. {
  1166. return $seek;
  1167. }
  1168. }
  1169. if ($fetchmode == MDB2_FETCHMODE_DEFAULT)
  1170. {
  1171. $fetchmode = $this->db->fetchmode;
  1172. }
  1173. if ($fetchmode & MDB2_FETCHMODE_ASSOC)
  1174. {
  1175. $row = @pg_fetch_array($this->result, null, PGSQL_ASSOC);
  1176. if (is_array($row) && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE)
  1177. {
  1178. $row = array_change_key_case($row, $this->db->options['field_case']);
  1179. }
  1180. }
  1181. else
  1182. {
  1183. $row = @pg_fetch_row($this->result);
  1184. }
  1185. if (! $row)
  1186. {
  1187. if ($this->result === false)
  1188. {
  1189. $err = & $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'resultset has already been freed', __FUNCTION__);
  1190. return $err;
  1191. }
  1192. $null = null;
  1193. return $null;
  1194. }
  1195. $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL;
  1196. $rtrim = false;
  1197. if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM)
  1198. {
  1199. if (empty($this->types))
  1200. {
  1201. $mode += MDB2_PORTABILITY_RTRIM;
  1202. }
  1203. else
  1204. {
  1205. $rtrim = true;
  1206. }
  1207. }
  1208. if ($mode)
  1209. {
  1210. $this->db->_fixResultArrayValues($row, $mode);
  1211. }
  1212. if (! ($fetchmode & MDB2_FETCHMODE_ASSOC) && ! empty($this->types))
  1213. {
  1214. $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
  1215. }
  1216. elseif (($fetchmode & MDB2_FETCHMODE_ASSOC) && ! empty($this->types_assoc))
  1217. {
  1218. $row = $this->db->datatype->convertResultRow($this->types_assoc, $row, $rtrim);
  1219. }
  1220. if (! empty($this->values))
  1221. {
  1222. $this->_assignBindColumns($row);
  1223. }
  1224. if ($fetchmode === MDB2_FETCHMODE_OBJECT)
  1225. {
  1226. $object_class = $this->db->options['fetch_class'];
  1227. if ($object_class == 'stdClass')
  1228. {
  1229. $row = (object) $row;
  1230. }
  1231. else
  1232. {
  1233. $row = &new $object_class($row);
  1234. }
  1235. }
  1236. ++ $this->rownum;
  1237. return $row;
  1238. }
  1239. // }}}
  1240. // {{{ _getColumnNames()
  1241. /**
  1242. * Retrieve the names of columns returned by the DBMS in a query result.
  1243. *
  1244. * @return mixed Array variable that holds the names of columns as keys
  1245. * or an MDB2 error on failure.
  1246. * Some DBMS may not return any columns when the result set
  1247. * does not contain any rows.
  1248. * @access private
  1249. */
  1250. function _getColumnNames()
  1251. {
  1252. $columns = array();
  1253. $numcols = $this->numCols();
  1254. if (PEAR :: isError($numcols))
  1255. {
  1256. return $numcols;
  1257. }
  1258. for($column = 0; $column < $numcols; $column ++)
  1259. {
  1260. $column_name = @pg_field_name($this->result, $column);
  1261. $columns[$column_name] = $column;
  1262. }
  1263. if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE)
  1264. {
  1265. $columns = array_change_key_case($columns, $this->db->options['field_case']);
  1266. }
  1267. return $columns;
  1268. }
  1269. // }}}
  1270. // {{{ numCols()
  1271. /**
  1272. * Count the number of columns returned by the DBMS in a query result.
  1273. *
  1274. * @access public
  1275. * @return mixed integer value with the number of columns, a MDB2 error
  1276. * on failure
  1277. */
  1278. function numCols()
  1279. {
  1280. $cols = @pg_num_fields($this->result);
  1281. if (is_null($cols))
  1282. {
  1283. if ($this->result === false)
  1284. {
  1285. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 'resultset has already been freed', __FUNCTION__);
  1286. }
  1287. elseif (is_null($this->result))
  1288. {
  1289. return count($this->types);
  1290. }
  1291. return $this->db->raiseError(null, null, null, 'Could not get column count', __FUNCTION__);
  1292. }

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