MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file pgsql.php

Documentation is available at pgsql.php

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

Documentation generated on Mon, 11 Mar 2019 14:44:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.