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.150 2006/07/22 08:01:57 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.             if ($is_manip{
  606.                 return 0;
  607.             }
  608.             return null;
  609.         }
  610.  
  611.         if (is_null($connection)) {
  612.             $connection $this->getConnection();
  613.             if (PEAR::isError($connection)) {
  614.                 return $connection;
  615.             }
  616.         }
  617.  
  618.         $function $this->options['multi_query''pg_send_query' 'pg_query';
  619.         $result @$function($connection$query);
  620.         if (!$result{
  621.             $err =$this->raiseError(nullnullnull,
  622.                 'Could not execute statement'__FUNCTION__);
  623.             return $err;
  624.         elseif ($this->options['multi_query']{
  625.             if (!($result @pg_get_result($connection))) {
  626.                 $err =$this->raiseError(nullnullnull,
  627.                         'Could not get the first result from a multi query'__FUNCTION__);
  628.                 return $err;
  629.             }
  630.         }
  631.  
  632.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  633.         return $result;
  634.     }
  635.  
  636.     // }}}
  637.     // {{{ _affectedRows()
  638.  
  639.     /**
  640.      * Returns the number of rows affected
  641.      *
  642.      * @param resource $result 
  643.      * @param resource $connection 
  644.      * @return mixed MDB2 Error Object or the number of rows affected
  645.      * @access private
  646.      */
  647.     function _affectedRows($connection$result = null)
  648.     {
  649.         if (is_null($connection)) {
  650.             $connection $this->getConnection();
  651.             if (PEAR::isError($connection)) {
  652.                 return $connection;
  653.             }
  654.         }
  655.         return @pg_affected_rows($result);
  656.     }
  657.  
  658.     // }}}
  659.     // {{{ _modifyQuery()
  660.  
  661.     /**
  662.      * Changes a query string for various DBMS specific reasons
  663.      *
  664.      * @param string $query  query to modify
  665.      * @param boolean $is_manip  if it is a DML query
  666.      * @param integer $limit  limit the number of rows
  667.      * @param integer $offset  start reading from given offset
  668.      * @return string modified query
  669.      * @access protected
  670.      */
  671.     function _modifyQuery($query$is_manip$limit$offset)
  672.     {
  673.         if ($limit > 0
  674.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  675.         {
  676.             $query rtrim($query);
  677.             if (substr($query-1== ';'{
  678.                 $query substr($query0-1);
  679.             }
  680.             if ($is_manip{
  681.                 $manip preg_replace('/^(DELETE FROM|UPDATE).*$/''\\1'$query);
  682.                 $from $match[2];
  683.                 $where $match[3];
  684.                 $query $manip.' '.$from.' WHERE ctid=(SELECT ctid FROM '.$from.' '.$where.' LIMIT '.$limit.')';
  685.             else {
  686.                 $query.= " LIMIT $limit OFFSET $offset";
  687.             }
  688.         }
  689.         return $query;
  690.     }
  691.  
  692.     // }}}
  693.     // {{{ getServerVersion()
  694.  
  695.     /**
  696.      * return version information about the server
  697.      *
  698.      * @param string     $native  determines if the raw version string should be returned
  699.      * @return mixed array/string with version information or MDB2 error object
  700.      * @access public
  701.      */
  702.     function getServerVersion($native = false)
  703.     {
  704.         $query 'SHOW SERVER_VERSION';
  705.         if ($this->connected_server_info{
  706.             $server_info $this->connected_server_info;
  707.         else {
  708.             $server_info $this->queryOne($query'text');
  709.             if (PEAR::isError($server_info)) {
  710.                 return $server_info;
  711.             }
  712.         }
  713.         // cache server_info
  714.         $this->connected_server_info $server_info;
  715.         if (!$native && !PEAR::isError($server_info)) {
  716.             $tmp explode('.'$server_info3);
  717.             if (empty($tmp[2])
  718.                 && isset($tmp[1])
  719.                 && preg_match('/(\d+)(.*)/'$tmp[1]$tmp2)
  720.             {
  721.                 $server_info = array(
  722.                     'major' => $tmp[0],
  723.                     'minor' => $tmp2[1],
  724.                     'patch' => null,
  725.                     'extra' => $tmp2[2],
  726.                     'native' => $server_info,
  727.                 );
  728.             else {
  729.                 $server_info = array(
  730.                     'major' => isset($tmp[0]$tmp[0: null,
  731.                     'minor' => isset($tmp[1]$tmp[1: null,
  732.                     'patch' => isset($tmp[2]$tmp[2: null,
  733.                     'extra' => null,
  734.                     'native' => $server_info,
  735.                 );
  736.             }
  737.         }
  738.         return $server_info;
  739.     }
  740.  
  741.     // }}}
  742.     // {{{ prepare()
  743.  
  744.     /**
  745.      * Prepares a query for multiple execution with execute().
  746.      * With some database backends, this is emulated.
  747.      * prepare() requires a generic query as string like
  748.      * 'INSERT INTO numbers VALUES(?,?)' or
  749.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  750.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  751.      * bindParam() and the query can be send off using the execute() method.
  752.      *
  753.      * @param string $query the query to prepare
  754.      * @param mixed   $types  array that contains the types of the placeholders
  755.      * @param mixed   $result_types  array that contains the types of the columns in
  756.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  757.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  758.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  759.      * @return mixed resource handle for the prepared query on success, a MDB2
  760.      *         error on failure
  761.      * @access public
  762.      * @see bindParam, execute
  763.      */
  764.     function &prepare($query$types = null$result_types = null$lobs = array())
  765.     {
  766.         if ($this->options['emulate_prepared']{
  767.             $obj =parent::prepare($query$types$result_types$lobs);
  768.             return $obj;
  769.         }
  770.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  771.         $offset $this->offset;
  772.         $limit $this->limit;
  773.         $this->offset $this->limit = 0;
  774.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  775.         if ($result{
  776.             if (PEAR::isError($result)) {
  777.                 return $result;
  778.             }
  779.             $query $result;
  780.         }
  781.         $pgtypes function_exists('pg_prepare'? false : array();
  782.         if ($pgtypes !== false && !empty($types)) {
  783.             $this->loadModule('Datatype'nulltrue);
  784.         }
  785.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  786.         $placeholder_type_guess $placeholder_type = null;
  787.         $question '?';
  788.         $colon ':';
  789.         $positions = array();
  790.         $position $parameter = 0;
  791.         while ($position strlen($query)) {
  792.             $q_position strpos($query$question$position);
  793.             $c_position strpos($query$colon$position);
  794.             if ($q_position && $c_position{
  795.                 $p_position min($q_position$c_position);
  796.             elseif ($q_position{
  797.                 $p_position $q_position;
  798.             elseif ($c_position{
  799.                 $p_position $c_position;
  800.             else {
  801.                 break;
  802.             }
  803.             if (is_null($placeholder_type)) {
  804.                 $placeholder_type_guess $query[$p_position];
  805.             }
  806.             if (is_int($quote strpos($query"'"$position)) && $quote $p_position{
  807.                 if (!is_int($end_quote strpos($query"'"$quote + 1))) {
  808.                     $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  809.                         'query with an unterminated text string specified'__FUNCTION__);
  810.                     return $err;
  811.                 }
  812.                 switch ($this->escape_quotes{
  813.                 case '':
  814.                 case "'":
  815.                     $position $end_quote + 1;
  816.                     break;
  817.                 default:
  818.                     if ($end_quote == $quote + 1{
  819.                         $position $end_quote + 1;
  820.                     else {
  821.                         if ($query[$end_quote-1== $this->escape_quotes{
  822.                             $position $end_quote;
  823.                         else {
  824.                             $position $end_quote + 1;
  825.                         }
  826.                     }
  827.                     break;
  828.                 }
  829.             elseif ($query[$position== $placeholder_type_guess{
  830.                 if (is_null($placeholder_type)) {
  831.                     $placeholder_type $query[$p_position];
  832.                     $question $colon $placeholder_type;
  833.                     if (!empty($types&& is_array($types)) {
  834.                         if ($placeholder_type == ':'{
  835.                         else {
  836.                             $types array_values($types);
  837.                         }
  838.                     }
  839.                 }
  840.                 if ($placeholder_type_guess == '?'{
  841.                     $length = 1;
  842.                     $name $parameter;
  843.                 else {
  844.                     $name preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  845.                     if ($name === ''{
  846.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  847.                             'named parameter with an empty name'__FUNCTION__);
  848.                         return $err;
  849.                     }
  850.                     $length strlen($name+ 1;
  851.                 }
  852.                 if ($pgtypes !== false{
  853.                     if (is_array($types&& array_key_exists($name$types)) {
  854.                         $pgtypes[$this->datatype->mapPrepareDatatype($types[$name]);
  855.                     elseif (is_array($types&& array_key_exists($parameter$types)) {
  856.                         $pgtypes[$this->datatype->mapPrepareDatatype($types[$parameter]);
  857.                     else {
  858.                         $pgtypes['text';
  859.                     }
  860.                 }
  861.                 $positions[$name$p_position;
  862.                 $query substr_replace($query'$'.++$parameter$position$length);
  863.                 $position $p_position strlen($parameter);
  864.             else {
  865.                 $position $p_position;
  866.             }
  867.         }
  868.         $connection $this->getConnection();
  869.         if (PEAR::isError($connection)) {
  870.             return $connection;
  871.         }
  872.  
  873.         $statement_name strtolower('MDB2_Statement_'.$this->phptype.md5(time(rand()));
  874.         if ($pgtypes === false{
  875.             $result @pg_prepare($connection$statement_name$query);
  876.             if (!$result{
  877.                 $err =$this->raiseError(nullnullnull,
  878.                     'Unable to create prepared statement handle'__FUNCTION__);
  879.                 return $err;
  880.             }
  881.         else {
  882.             $types_string '';
  883.             if ($pgtypes{
  884.                 $types_string ' ('.implode(', '$pgtypes).') ';
  885.             }
  886.             $query 'PREPARE '.$statement_name.$types_string.' AS '.$query;
  887.             $statement =$this->_doQuery($querytrue$connection);
  888.             if (PEAR::isError($statement)) {
  889.                 return $statement;
  890.             }
  891.         }
  892.  
  893.         $class_name 'MDB2_Statement_'.$this->phptype;
  894.         $obj =new $class_name($this$statement_name$positions$query$types$result_types$is_manip$limit$offset);
  895.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  896.         return $obj;
  897.     }
  898.  
  899.     // }}}
  900.     // {{{ nextID()
  901.  
  902.     /**
  903.      * Returns the next free id of a sequence
  904.      *
  905.      * @param string $seq_name name of the sequence
  906.      * @param boolean $ondemand when true the sequence is
  907.      *                           automatic created, if it
  908.      *                           not exists
  909.      * @return mixed MDB2 Error Object or id
  910.      * @access public
  911.      */
  912.     function nextID($seq_name$ondemand = true)
  913.     {
  914.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  915.         $query = "SELECT NEXTVAL('$sequence_name')";
  916.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  917.         $result $this->queryOne($query'integer');
  918.         $this->popExpect();
  919.         if (PEAR::isError($result)) {
  920.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  921.                 $this->loadModule('Manager'nulltrue);
  922.                 $result $this->manager->createSequence($seq_name1);
  923.                 if (PEAR::isError($result)) {
  924.                     return $this->raiseError($resultnullnull,
  925.                         'on demand sequence could not be created'__FUNCTION__);
  926.                 }
  927.                 return $this->nextId($seq_namefalse);
  928.             }
  929.         }
  930.         return $result;
  931.     }
  932.  
  933.     // }}}
  934.     // {{{ currID()
  935.  
  936.     /**
  937.      * Returns the current id of a sequence
  938.      *
  939.      * @param string $seq_name name of the sequence
  940.      * @return mixed MDB2 Error Object or id
  941.      * @access public
  942.      */
  943.     function currID($seq_name)
  944.     {
  945.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  946.         return $this->queryOne("SELECT last_value FROM $sequence_name"'integer');
  947.     }
  948. }
  949.  
  950. /**
  951.  * MDB2 PostGreSQL result driver
  952.  *
  953.  * @package MDB2
  954.  * @category Database
  955.  * @author  Paul Cooper <pgc@ucecom.com>
  956.  */
  957. class MDB2_Result_pgsql extends MDB2_Result_Common
  958. {
  959.     // }}}
  960.     // {{{ fetchRow()
  961.  
  962.     /**
  963.      * Fetch a row and insert the data into an existing array.
  964.      *
  965.      * @param int       $fetchmode  how the array data should be indexed
  966.      * @param int    $rownum    number of the row where the data can be found
  967.      * @return int data array on success, a MDB2 error on failure
  968.      * @access public
  969.      */
  970.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  971.     {
  972.         if (!is_null($rownum)) {
  973.             $seek $this->seek($rownum);
  974.             if (PEAR::isError($seek)) {
  975.                 return $seek;
  976.             }
  977.         }
  978.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  979.             $fetchmode $this->db->fetchmode;
  980.         }
  981.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  982.             $row @pg_fetch_array($this->resultnullPGSQL_ASSOC);
  983.             if (is_array($row)
  984.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  985.             {
  986.                 $row array_change_key_case($row$this->db->options['field_case']);
  987.             }
  988.         else {
  989.             $row @pg_fetch_row($this->result);
  990.         }
  991.         if (!$row{
  992.             if ($this->result === false{
  993.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  994.                     'resultset has already been freed'__FUNCTION__);
  995.                 return $err;
  996.             }
  997.             $null = null;
  998.             return $null;
  999.         }
  1000.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  1001.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  1002.         {
  1003.             $this->db->_fixResultArrayValues($row$mode);
  1004.         }
  1005.         if (!empty($this->values)) {
  1006.             $this->_assignBindColumns($row);
  1007.         }
  1008.         if (!empty($this->types)) {
  1009.             $row $this->db->datatype->convertResultRow($this->types$row);
  1010.         }
  1011.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1012.             $object_class $this->db->options['fetch_class'];
  1013.             if ($object_class == 'stdClass'{
  1014.                 $row = (object) $row;
  1015.             else {
  1016.                 $row &new $object_class($row);
  1017.             }
  1018.         }
  1019.         ++$this->rownum;
  1020.         return $row;
  1021.     }
  1022.  
  1023.     // }}}
  1024.     // {{{ _getColumnNames()
  1025.  
  1026.     /**
  1027.      * Retrieve the names of columns returned by the DBMS in a query result.
  1028.      *
  1029.      * @return  mixed   Array variable that holds the names of columns as keys
  1030.      *                   or an MDB2 error on failure.
  1031.      *                   Some DBMS may not return any columns when the result set
  1032.      *                   does not contain any rows.
  1033.      * @access private
  1034.      */
  1035.     function _getColumnNames()
  1036.     {
  1037.         $columns = array();
  1038.         $numcols $this->numCols();
  1039.         if (PEAR::isError($numcols)) {
  1040.             return $numcols;
  1041.         }
  1042.         for ($column = 0; $column $numcols$column++{
  1043.             $column_name @pg_field_name($this->result$column);
  1044.             $columns[$column_name$column;
  1045.         }
  1046.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1047.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1048.         }
  1049.         return $columns;
  1050.     }
  1051.  
  1052.     // }}}
  1053.     // {{{ numCols()
  1054.  
  1055.     /**
  1056.      * Count the number of columns returned by the DBMS in a query result.
  1057.      *
  1058.      * @access public
  1059.      * @return mixed integer value with the number of columns, a MDB2 error
  1060.      *                        on failure
  1061.      */
  1062.     function numCols()
  1063.     {
  1064.         $cols @pg_num_fields($this->result);
  1065.         if (is_null($cols)) {
  1066.             if ($this->result === false{
  1067.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1068.                     'resultset has already been freed'__FUNCTION__);
  1069.             elseif (is_null($this->result)) {
  1070.                 return count($this->types);
  1071.             }
  1072.             return $this->db->raiseError(nullnullnull,
  1073.                 'Could not get column count'__FUNCTION__);
  1074.         }
  1075.         return $cols;
  1076.     }
  1077.  
  1078.     // }}}
  1079.     // {{{ nextResult()
  1080.  
  1081.     /**
  1082.      * Move the internal result pointer to the next available result
  1083.      *
  1084.      * @param valid result resource
  1085.      * @return true on success, false if there is no more result set or an error object on failure
  1086.      * @access public
  1087.      */
  1088.     function nextResult()
  1089.     {
  1090.         $connection $this->db->getConnection();
  1091.         if (PEAR::isError($connection)) {
  1092.             return $connection;
  1093.         }
  1094.  
  1095.         if (!($this->result @pg_get_result($connection))) {
  1096.             return false;
  1097.         }
  1098.         return MDB2_OK;
  1099.     }
  1100.  
  1101.     // }}}
  1102.     // {{{ free()
  1103.  
  1104.     /**
  1105.      * Free the internal resources associated with result.
  1106.      *
  1107.      * @return boolean true on success, false if result is invalid
  1108.      * @access public
  1109.      */
  1110.     function free()
  1111.     {
  1112.         if (is_resource($this->result&& $this->db->connection{
  1113.             $free @pg_free_result($this->result);
  1114.             if ($free === false{
  1115.                 return $this->db->raiseError(nullnullnull,
  1116.                     'Could not free result'__FUNCTION__);
  1117.             }
  1118.         }
  1119.         $this->result = false;
  1120.         return MDB2_OK;
  1121.     }
  1122. }
  1123.  
  1124. /**
  1125.  * MDB2 PostGreSQL buffered result driver
  1126.  *
  1127.  * @package MDB2
  1128.  * @category Database
  1129.  * @author  Paul Cooper <pgc@ucecom.com>
  1130.  */
  1131. {
  1132.     // {{{ seek()
  1133.  
  1134.     /**
  1135.      * Seek to a specific row in a result set
  1136.      *
  1137.      * @param int    $rownum    number of the row where the data can be found
  1138.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1139.      * @access public
  1140.      */
  1141.     function seek($rownum = 0)
  1142.     {
  1143.         if ($this->rownum != ($rownum - 1&& !@pg_result_seek($this->result$rownum)) {
  1144.             if ($this->result === false{
  1145.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1146.                     'resultset has already been freed'__FUNCTION__);
  1147.             elseif (is_null($this->result)) {
  1148.                 return MDB2_OK;
  1149.             }
  1150.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  1151.                 'tried to seek to an invalid row number ('.$rownum.')'__FUNCTION__);
  1152.         }
  1153.         $this->rownum $rownum - 1;
  1154.         return MDB2_OK;
  1155.     }
  1156.  
  1157.     // }}}
  1158.     // {{{ valid()
  1159.  
  1160.     /**
  1161.      * Check if the end of the result set has been reached
  1162.      *
  1163.      * @return mixed true or false on sucess, a MDB2 error on failure
  1164.      * @access public
  1165.      */
  1166.     function valid()
  1167.     {
  1168.         $numrows $this->numRows();
  1169.         if (PEAR::isError($numrows)) {
  1170.             return $numrows;
  1171.         }
  1172.         return $this->rownum ($numrows - 1);
  1173.     }
  1174.  
  1175.     // }}}
  1176.     // {{{ numRows()
  1177.  
  1178.     /**
  1179.      * Returns the number of rows in a result object
  1180.      *
  1181.      * @return mixed MDB2 Error Object or the number of rows
  1182.      * @access public
  1183.      */
  1184.     function numRows()
  1185.     {
  1186.         $rows @pg_num_rows($this->result);
  1187.         if (is_null($rows)) {
  1188.             if ($this->result === false{
  1189.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1190.                     'resultset has already been freed'__FUNCTION__);
  1191.             elseif (is_null($this->result)) {
  1192.                 return 0;
  1193.             }
  1194.             return $this->db->raiseError(nullnullnull,
  1195.                 'Could not get row count'__FUNCTION__);
  1196.         }
  1197.         return $rows;
  1198.     }
  1199. }
  1200.  
  1201. /**
  1202.  * MDB2 PostGreSQL statement driver
  1203.  *
  1204.  * @package MDB2
  1205.  * @category Database
  1206.  * @author  Paul Cooper <pgc@ucecom.com>
  1207.  */
  1208. class MDB2_Statement_pgsql extends MDB2_Statement_Common
  1209. {
  1210.     // {{{ _execute()
  1211.  
  1212.     /**
  1213.      * Execute a prepared query statement helper method.
  1214.      *
  1215.      * @param mixed $result_class string which specifies which result class to use
  1216.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1217.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1218.      * @access private
  1219.      */
  1220.     function &_execute($result_class = true$result_wrap_class = false)
  1221.     {
  1222.         if (is_null($this->statement)) {
  1223.             $result =parent::_execute($result_class$result_wrap_class);
  1224.             return $result;
  1225.         }
  1226.         $this->db->last_query = $this->query;
  1227.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1228.         if ($this->db->getOption('disable_query')) {
  1229.             if ($this->is_manip{
  1230.                 $return = 0;
  1231.                 return $return;
  1232.             }
  1233.             $null = null;
  1234.             return $null;
  1235.         }
  1236.  
  1237.         $connection $this->db->getConnection();
  1238.         if (PEAR::isError($connection)) {
  1239.             return $connection;
  1240.         }
  1241.  
  1242.         $query = false;
  1243.         $parameters = array();
  1244.         // todo: disabled until pg_execute() bytea issues are cleared up
  1245.         if (true || !function_exists('pg_execute')) {
  1246.             $query 'EXECUTE '.$this->statement;
  1247.         }
  1248.         if (!empty($this->positions)) {
  1249.             foreach ($this->positions as $parameter => $current_position{
  1250.                 if (!array_key_exists($parameter$this->values)) {
  1251.                     return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1252.                         'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1253.                 }
  1254.                 $value $this->values[$parameter];
  1255.                 $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1256.                 if (is_resource($value|| $type == 'clob' || $type == 'blob'{
  1257.                     if (!is_resource($value&& preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1258.                         if ($match[1== 'file://'{
  1259.                             $value $match[2];
  1260.                         }
  1261.                         $value @fopen($value'r');
  1262.                         $close = true;
  1263.                     }
  1264.                     if (is_resource($value)) {
  1265.                         $data '';
  1266.                         while (!@feof($value)) {
  1267.                             $data.= @fread($value$this->db->options['lob_buffer_length']);
  1268.                         }
  1269.                         if ($close{
  1270.                             @fclose($value);
  1271.                         }
  1272.                         $value $data;
  1273.                     }
  1274.                 }
  1275.                 $parameters[$this->db->quote($value$type$query);
  1276.             }
  1277.             if ($query{
  1278.                 $query.= ' ('.implode(', '$parameters).')';
  1279.             }
  1280.         }
  1281.  
  1282.         if (!$query{
  1283.             $result @pg_execute($connection$this->statement$parameters);
  1284.             if (!$result{
  1285.                 $err =$this->db->raiseError(nullnullnull,
  1286.                     'Unable to execute statement'__FUNCTION__);
  1287.                 return $err;
  1288.             }
  1289.         else {
  1290.             $result $this->db->_doQuery($query$this->is_manip$connection);
  1291.             if (PEAR::isError($result)) {
  1292.                 return $result;
  1293.             }
  1294.         }
  1295.  
  1296.         if ($this->is_manip{
  1297.             $affected_rows $this->db->_affectedRows($connection$result);
  1298.             return $affected_rows;
  1299.         }
  1300.  
  1301.         $result =$this->db->_wrapResult($result$this->result_types,
  1302.             $result_class$result_wrap_class$this->limit$this->offset);
  1303.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1304.         return $result;
  1305.     }
  1306.  
  1307.     // }}}
  1308.     // {{{ free()
  1309.  
  1310.     /**
  1311.      * Release resources allocated for the specified prepared query.
  1312.      *
  1313.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1314.      * @access public
  1315.      */
  1316.     function free()
  1317.     {
  1318.         if (is_null($this->positions)) {
  1319.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1320.                 'Prepared statement has already been freed'__FUNCTION__);
  1321.         }
  1322.         $result = MDB2_OK;
  1323.  
  1324.         if (!is_null($this->statement)) {
  1325.             $connection $this->db->getConnection();
  1326.             if (PEAR::isError($connection)) {
  1327.                 return $connection;
  1328.             }
  1329.             $query 'DEALLOCATE PREPARE '.$this->statement;
  1330.             $result $this->db->_doQuery($querytrue$connection);
  1331.         }
  1332.  
  1333.         parent::free();
  1334.         return $result;
  1335.     }
  1336. }
  1337. ?>

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