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-2004 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.64 2005/04/25 13:09: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.     // }}}
  61.     // {{{ constructor
  62.  
  63.     /**
  64.     * Constructor
  65.     */
  66.     function __construct()
  67.     {
  68.         parent::__construct();
  69.  
  70.         $this->phptype 'pgsql';
  71.         $this->dbsyntax 'pgsql';
  72.  
  73.         $this->supported['sequences'= true;
  74.         $this->supported['indexes'= true;
  75.         $this->supported['affected_rows'= true;
  76.         $this->supported['summary_functions'= true;
  77.         $this->supported['order_by_text'= true;
  78.         $this->supported['transactions'= true;
  79.         $this->supported['current_id'= true;
  80.         $this->supported['limit_queries'= true;
  81.         $this->supported['LOBs'= true;
  82.         $this->supported['replace'= true;
  83.         $this->supported['sub_selects'= true;
  84.         $this->supported['auto_increment'= false;
  85.     }
  86.  
  87.     // }}}
  88.     // {{{ errorInfo()
  89.  
  90.     /**
  91.      * This method is used to collect information about an error
  92.      *
  93.      * @param integer $error 
  94.      * @return array 
  95.      * @access public
  96.      */
  97.     function errorInfo($error = null)
  98.     {
  99.         $native_msg '';
  100.         if (is_resource($error)) {
  101.             $native_msg @pg_result_error($error);
  102.         elseif ($this->connection{
  103.             $native_msg @pg_last_error($this->connection);
  104.         }
  105.  
  106.         // Fall back to MDB2_ERROR if there was no mapping.
  107.         $error = MDB2_ERROR;
  108.  
  109.         static $error_regexps;
  110.         if (empty($error_regexps)) {
  111.             $error_regexps = array(
  112.                 '/(relation|sequence|table).*does not exist|class .* not found/i'
  113.                     => MDB2_ERROR_NOSUCHTABLE,
  114.                 '/index .* does not exist/'
  115.                     => MDB2_ERROR_NOT_FOUND,
  116.                 '/column .* does not exist/i'
  117.                     => MDB2_ERROR_NOSUCHFIELD,
  118.                 '/relation .* already exists/i'
  119.                     => MDB2_ERROR_ALREADY_EXISTS,
  120.                 '/(divide|division) by zero$/i'
  121.                     => MDB2_ERROR_DIVZERO,
  122.                 '/pg_atoi: error in .*: can\'t parse /i'
  123.                     => MDB2_ERROR_INVALID_NUMBER,
  124.                 '/invalid input syntax for( type)? (integer|numeric)/i'
  125.                     => MDB2_ERROR_INVALID_NUMBER,
  126.                 '/value .* is out of range for type \w*int/i'
  127.                     => MDB2_ERROR_INVALID_NUMBER,
  128.                 '/integer out of range/i'
  129.                     => MDB2_ERROR_INVALID_NUMBER,
  130.                 '/value too long for type character/i'
  131.                     => MDB2_ERROR_INVALID,
  132.                 '/attribute .* not found|relation .* does not have attribute/i'
  133.                     => MDB2_ERROR_NOSUCHFIELD,
  134.                 '/column .* specified in USING clause does not exist in (left|right) table/i'
  135.                     => MDB2_ERROR_NOSUCHFIELD,
  136.                 '/parser: parse error at or near/i'
  137.                     => MDB2_ERROR_SYNTAX,
  138.                 '/syntax error at/'
  139.                     => MDB2_ERROR_SYNTAX,
  140.                 '/column reference .* is ambiguous/i'
  141.                     => MDB2_ERROR_SYNTAX,
  142.                 '/permission denied/'
  143.                     => MDB2_ERROR_ACCESS_VIOLATION,
  144.                 '/violates not-null constraint/'
  145.                     => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  146.                 '/violates [\w ]+ constraint/'
  147.                     => MDB2_ERROR_CONSTRAINT,
  148.                 '/referential integrity violation/'
  149.                     => MDB2_ERROR_CONSTRAINT,
  150.                 '/more expressions than target columns/i'
  151.                     => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  152.             );
  153.         }
  154.         foreach ($error_regexps as $regexp => $code{
  155.             if (preg_match($regexp$native_msg)) {
  156.                 $error $code;
  157.                 break;
  158.             }
  159.         }
  160.  
  161.         return array($errornull$native_msg);
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ beginTransaction()
  166.  
  167.     /**
  168.      * Start a transaction.
  169.      *
  170.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  171.      * @access public
  172.      */
  173.     function beginTransaction()
  174.     {
  175.         $this->debug('starting transaction''beginTransaction');
  176.         if ($this->in_transaction{
  177.             return MDB2_OK;  //nothing to do
  178.         }
  179.         if (!$this->destructor_registered && $this->opened_persistent{
  180.             $this->destructor_registered = true;
  181.             register_shutdown_function('MDB2_closeOpenTransactions');
  182.         }
  183.         $result $this->_doQuery('BEGIN'true);
  184.         if (PEAR::isError($result)) {
  185.             return $result;
  186.         }
  187.         $this->in_transaction = true;
  188.         return MDB2_OK;
  189.     }
  190.  
  191.     // }}}
  192.     // {{{ commit()
  193.  
  194.     /**
  195.      * Commit the database changes done during a transaction that is in
  196.      * progress.
  197.      *
  198.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  199.      * @access public
  200.      */
  201.     function commit()
  202.     {
  203.         $this->debug('commit transaction''commit');
  204.         if (!$this->in_transaction{
  205.             return $this->raiseError(MDB2_ERRORnullnull,
  206.                 'commit: transaction changes are being auto committed');
  207.         }
  208.         $result $this->_doQuery('COMMIT'true);
  209.         if (PEAR::isError($result)) {
  210.             return $result;
  211.         }
  212.         $this->in_transaction = false;
  213.         return MDB2_OK;
  214.     }
  215.  
  216.     // }}}
  217.     // {{{ rollback()
  218.  
  219.     /**
  220.      * Cancel any database changes done during a transaction that is in
  221.      * progress.
  222.      *
  223.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  224.      * @access public
  225.      */
  226.     function rollback()
  227.     {
  228.         $this->debug('rolling back transaction''rollback');
  229.         if (!$this->in_transaction{
  230.             return $this->raiseError(MDB2_ERRORnullnull,
  231.                 'rollback: transactions can not be rolled back when changes are auto committed');
  232.         }
  233.         $result $this->_doQuery('ROLLBACK'true);
  234.         if (PEAR::isError($result)) {
  235.             return $result;
  236.         }
  237.         $this->in_transaction = false;
  238.         return MDB2_OK;
  239.     }
  240.  
  241.     // }}}
  242.     // {{{ _doConnect()
  243.  
  244.     /**
  245.      * Does the grunt work of connecting to the database
  246.      *
  247.      * @return mixed connection resource on success, MDB2 Error Object on failure
  248.      * @access protected
  249.      ***/
  250.     function _doConnect($database_name$persistent = false)
  251.     {
  252.         if ($database_name == ''{
  253.             $database_name 'template1';
  254.         }
  255.  
  256.         $protocol $this->dsn['protocol'$this->dsn['protocol''tcp';
  257.  
  258.         $params = array('');
  259.         if ($protocol == 'tcp'{
  260.             if ($this->dsn['hostspec']{
  261.                 $params[0.= 'host=' $this->dsn['hostspec'];
  262.             }
  263.             if ($this->dsn['port']{
  264.                 $params[0.= ' port=' $this->dsn['port'];
  265.             }
  266.         elseif ($protocol == 'unix'{
  267.             // Allow for pg socket in non-standard locations.
  268.             if ($this->dsn['socket']{
  269.                 $params[0.= 'host=' $this->dsn['socket'];
  270.             }
  271.             if ($this->dsn['port']{
  272.                 $params[0.= ' port=' $this->dsn['port'];
  273.             }
  274.         }
  275.         if ($database_name{
  276.             $params[0.= ' dbname=\'' addslashes($database_name'\'';
  277.         }
  278.         if ($this->dsn['username']{
  279.             $params[0.= ' user=\'' addslashes($this->dsn['username']'\'';
  280.         }
  281.         if ($this->dsn['password']{
  282.             $params[0.= ' password=\'' addslashes($this->dsn['password']'\'';
  283.         }
  284.         if (!empty($this->dsn['options'])) {
  285.             $params[0.= ' options=' $this->dsn['options'];
  286.         }
  287.         if (!empty($this->dsn['tty'])) {
  288.             $params[0.= ' tty=' $this->dsn['tty'];
  289.         }
  290.         if (!empty($this->dsn['connect_timeout'])) {
  291.             $params[0.= ' connect_timeout=' $this->dsn['connect_timeout'];
  292.         }
  293.         if (!empty($this->dsn['sslmode'])) {
  294.             $params[0.= ' sslmode=' $this->dsn['sslmode'];
  295.         }
  296.         if (!empty($this->dsn['service'])) {
  297.             $params[0.= ' service=' $this->dsn['service'];
  298.         }
  299.  
  300.         if (isset($this->dsn['new_link'])
  301.             && ($this->dsn['new_link'== 'true' || $this->dsn['new_link'=== true))
  302.         {
  303.             if (version_compare(phpversion()'4.3.0''>=')) {
  304.                 $params[= PGSQL_CONNECT_FORCE_NEW;
  305.             }
  306.         }
  307.  
  308.         $connect_function $persistent 'pg_pconnect' 'pg_connect';
  309.  
  310.         putenv('PGDATESTYLE=ISO');
  311.  
  312.         @ini_set('track_errors'true);
  313.         $php_errormsg '';
  314.         $connection @call_user_func_array($connect_function$params);
  315.         @ini_restore('track_errors');
  316.         if (!$connection{
  317.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILED,
  318.                 nullnullstrip_tags($php_errormsg));
  319.         }
  320.         return $connection;
  321.     }
  322.  
  323.     // }}}
  324.     // {{{ connect()
  325.  
  326.     /**
  327.      * Connect to the database
  328.      *
  329.      * @return true on success, MDB2 Error Object on failure
  330.      * @access public
  331.      ***/
  332.     function connect()
  333.     {
  334.         if (is_resource($this->connection)) {
  335.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  336.                 && $this->connected_database_name == $this->database_name
  337.                 && ($this->opened_persistent == $this->options['persistent'])
  338.             {
  339.                 return MDB2_OK;
  340.             }
  341.             $this->disconnect(false);
  342.         }
  343.  
  344.         if (!PEAR::loadExtension($this->phptype)) {
  345.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  346.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  347.         }
  348.  
  349.         if ($this->database_name{
  350.             $connection $this->_doConnect($this->database_name$this->options['persistent']);
  351.             if (PEAR::isError($connection)) {
  352.                 return $connection;
  353.             }
  354.             $this->connection $connection;
  355.             $this->connected_dsn $this->dsn;
  356.             $this->connected_database_name $this->database_name;
  357.             $this->opened_persistent $this->options['persistent'];
  358.             $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  359.         }
  360.         return MDB2_OK;
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ disconnect()
  365.  
  366.     /**
  367.      * Log out and disconnect from the database.
  368.      *
  369.      * @return mixed true on success, false if not connected and error
  370.      *                 object on error
  371.      * @access public
  372.      */
  373.     function disconnect($force = true)
  374.     {
  375.         if (is_resource($this->connection)) {
  376.             if (!$this->opened_persistent || $force{
  377.                 @pg_close($this->connection);
  378.             }
  379.             $this->connection = 0;
  380.         }
  381.         return MDB2_OK;
  382.     }
  383.  
  384.     // }}}
  385.     // {{{ standaloneQuery()
  386.  
  387.    /**
  388.      * execute a query as DBA
  389.      *
  390.      * @param string $query the SQL query
  391.      * @param mixed   $types  array that contains the types of the columns in
  392.      *                         the result set
  393.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  394.      * @access public
  395.      */
  396.     function &standaloneQuery($query$types = null)
  397.     {
  398.         $connection $this->_doConnect('template1'false);
  399.         if (PEAR::isError($connection)) {
  400.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  401.                 'Cannot connect to template1');
  402.         }
  403.  
  404.         $isManip = MDB2::isManip($query);
  405.         $offset $this->row_offset;
  406.         $limit $this->row_limit;
  407.         $this->row_offset $this->row_limit = 0;
  408.         $query $this->_modifyQuery($query$isManip$limit$offset);
  409.  
  410.         $result $this->_doQuery($query$isManip$connectionfalse);
  411.         @pg_close($connection);
  412.         if (PEAR::isError($result)) {
  413.             return $result;
  414.         }
  415.  
  416.         if ($isManip{
  417.             return $result;
  418.         }
  419.  
  420.         return $this->_wrapResult($result$typestruefalse$limit$offset);
  421.     }
  422.  
  423.     // }}}
  424.     // {{{ _doQuery()
  425.  
  426.     /**
  427.      * Execute a query
  428.      * @param string $query  query
  429.      * @param boolean $isManip  if the query is a manipulation query
  430.      * @param resource $connection 
  431.      * @param string $database_name 
  432.      * @return result or error object
  433.      * @access protected
  434.      */
  435.     function _doQuery($query$isManip = false$connection = null$database_name = null)
  436.     {
  437.         $this->last_query $query;
  438.         $this->debug($query'query');
  439.         if ($this->options['disable_query']{
  440.             if ($isManip{
  441.                 return MDB2_OK;
  442.             }
  443.             return null;
  444.         }
  445.  
  446.         if (is_null($connection)) {
  447.             $error $this->connect();
  448.             if (PEAR::isError($error)) {
  449.                 return $error;
  450.             }
  451.             $connection $this->connection;
  452.         }
  453.  
  454.         $result @pg_query($connection$query);
  455.         if (!$result{
  456.             return $this->raiseError();
  457.         }
  458.  
  459.         if ($isManip{
  460.             return @pg_affected_rows($result);
  461.         }
  462.         return $result;
  463.     }
  464.  
  465.     // }}}
  466.     // {{{ _modifyQuery()
  467.  
  468.     /**
  469.      * Changes a query string for various DBMS specific reasons
  470.      *
  471.      * @param string $query  query to modify
  472.      * @return the new (modified) query
  473.      * @access protected
  474.      */
  475.     function _modifyQuery($query$isManip$limit$offset)
  476.     {
  477.         if ($limit > 0
  478.             && !preg_match('/LIMIT\s*\d(\s*(,|OFFSET)\s*\d+)?/i'$query)
  479.         {
  480.             $query rtrim($query);
  481.             if (substr($query-1== ';'{
  482.                 $query substr($query0-1);
  483.             }
  484.             if ($isManip{
  485.                 $manip preg_replace('/^(DELETE FROM|UPDATE).*$/''\\1'$query);
  486.                 $from $match[2];
  487.                 $where $match[3];
  488.                 $query $manip.' '.$from.' WHERE ctid=(SELECT ctid FROM '.$from.' '.$where.' LIMIT '.$limit.')';
  489.             else {
  490.                 $query .= " LIMIT $limit OFFSET $offset";
  491.             }
  492.         }
  493.         return $query;
  494.     }
  495.  
  496.     // }}}
  497.     // {{{ nextID()
  498.  
  499.     /**
  500.      * returns the next free id of a sequence
  501.      *
  502.      * @param string  $seq_name name of the sequence
  503.      * @param boolean $ondemand when true the seqence is
  504.      *                           automatic created, if it
  505.      *                           not exists
  506.      * @return mixed MDB2 Error Object or id
  507.      * @access public
  508.      */
  509.     function nextID($seq_name$ondemand = true)
  510.     {
  511.         $sequence_name $this->getSequenceName($seq_name);
  512.         $query = "SELECT NEXTVAL('$sequence_name')";
  513.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  514.         $result $this->queryOne($query'integer');
  515.         $this->popExpect();
  516.         if (PEAR::isError($result)) {
  517.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  518.                 $this->loadModule('Manager');
  519.                 $result $this->manager->createSequence($seq_name1);
  520.                 if (PEAR::isError($result)) {
  521.                     return $this->raiseError(MDB2_ERRORnullnull,
  522.                         'nextID: on demand sequence could not be created');
  523.                 }
  524.                 return $this->nextId($seq_namefalse);
  525.             }
  526.         }
  527.         return $result;
  528.     }
  529.  
  530.     // }}}
  531.     // {{{ currID()
  532.  
  533.     /**
  534.      * returns the current id of a sequence
  535.      *
  536.      * @param string  $seq_name name of the sequence
  537.      * @return mixed MDB2 Error Object or id
  538.      * @access public
  539.      */
  540.     function currID($seq_name)
  541.     {
  542.         $sequence_name $this->getSequenceName($seq_name);
  543.         return $this->queryOne("SELECT last_value FROM $sequence_name"'integer');
  544.     }
  545. }
  546.  
  547. class MDB2_Result_pgsql extends MDB2_Result_Common
  548. {
  549.     // }}}
  550.     // {{{ fetchRow()
  551.  
  552.     /**
  553.      * Fetch a row and insert the data into an existing array.
  554.      *
  555.      * @param int       $fetchmode  how the array data should be indexed
  556.      * @param int    $rownum    number of the row where the data can be found
  557.      * @return int data array on success, a MDB2 error on failure
  558.      * @access public
  559.      */
  560.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  561.     {
  562.         if (!is_null($rownum)) {
  563.             $seek $this->seek($rownum);
  564.             if (PEAR::isError($seek)) {
  565.                 return $seek;
  566.             }
  567.         }
  568.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  569.             $fetchmode $this->db->fetchmode;
  570.         }
  571.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  572.             $row @pg_fetch_array($this->resultnullPGSQL_ASSOC);
  573.             if (is_array($row)
  574.                 && $this->db->options['portability'MDB2_PORTABILITY_LOWERCASE
  575.             {
  576.                 $row array_change_key_case($rowCASE_LOWER);
  577.             }
  578.         else {
  579.             $row @pg_fetch_row($this->result);
  580.         }
  581.         if (!$row{
  582.             if (is_null($this->result)) {
  583.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  584.                     'fetchRow: resultset has already been freed');
  585.             }
  586.             return null;
  587.         }
  588.         if ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL{
  589.             $this->db->_convertEmptyArrayValuesToNull($row);
  590.         }
  591.         if (isset($this->values)) {
  592.             $this->_assignBindColumns($row);
  593.         }
  594.         if (isset($this->types)) {
  595.             $row $this->db->datatype->convertResultRow($this->types$row);
  596.         }
  597.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  598.             $object_class $this->db->options['fetch_class'];
  599.             if ($object_class == 'stdClass'{
  600.                 $row = (object) $row;
  601.             else {
  602.                 $row &new $object_class($row);
  603.             }
  604.         }
  605.         ++$this->rownum;
  606.         return $row;
  607.     }
  608.  
  609.     // }}}
  610.     // {{{ _getColumnNames()
  611.  
  612.     /**
  613.      * Retrieve the names of columns returned by the DBMS in a query result.
  614.      *
  615.      * @return mixed                an associative array variable
  616.      *                               that will hold the names of columns. The
  617.      *                               indexes of the array are the column names
  618.      *                               mapped to lower case and the values are the
  619.      *                               respective numbers of the columns starting
  620.      *                               from 0. Some DBMS may not return any
  621.      *                               columns when the result set does not
  622.      *                               contain any rows.
  623.      *
  624.      *                               a MDB2 error on failure
  625.      * @access private
  626.      */
  627.     function _getColumnNames()
  628.     {
  629.         $columns = array();
  630.         $numcols $this->numCols();
  631.         if (PEAR::isError($numcols)) {
  632.             return $numcols;
  633.         }
  634.         for ($column = 0; $column $numcols$column++{
  635.             $column_name @pg_field_name($this->result$column);
  636.             $columns[$column_name$column;
  637.         }
  638.         if ($this->db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  639.             $columns array_change_key_case($columnsCASE_LOWER);
  640.         }
  641.         return $columns;
  642.     }
  643.  
  644.     // }}}
  645.     // {{{ numCols()
  646.  
  647.     /**
  648.      * Count the number of columns returned by the DBMS in a query result.
  649.      *
  650.      * @access public
  651.      * @return mixed integer value with the number of columns, a MDB2 error
  652.      *                        on failure
  653.      */
  654.     function numCols()
  655.     {
  656.         $cols @pg_num_fields($this->result);
  657.         if (is_null($cols)) {
  658.             if (is_null($this->result)) {
  659.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  660.                     'numCols: resultset has already been freed');
  661.             }
  662.             return $this->db->raiseError();
  663.         }
  664.         return $cols;
  665.     }
  666.  
  667.     // }}}
  668.     // {{{ free()
  669.  
  670.     /**
  671.      * Free the internal resources associated with result.
  672.      *
  673.      * @return boolean true on success, false if result is invalid
  674.      * @access public
  675.      */
  676.     function free()
  677.     {
  678.         $free @pg_free_result($this->result);
  679.         if (!$free{
  680.             if (is_null($this->result)) {
  681.                 return MDB2_OK;
  682.             }
  683.             return $this->db->raiseError();
  684.         }
  685.         $this->result = null;
  686.         return MDB2_OK;
  687.     }
  688. }
  689.  
  690. {
  691.     // {{{ seek()
  692.  
  693.     /**
  694.     * seek to a specific row in a result set
  695.     *
  696.     * @param int    $rownum    number of the row where the data can be found
  697.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  698.     * @access public
  699.     */
  700.     function seek($rownum = 0)
  701.     {
  702.         if ($this->rownum != ($rownum - 1&& !@pg_result_seek($this->result$rownum)) {
  703.             if (is_null($this->result)) {
  704.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  705.                     'seek: resultset has already been freed');
  706.             }
  707.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  708.                 'seek: tried to seek to an invalid row number ('.$rownum.')');
  709.         }
  710.         $this->rownum $rownum - 1;
  711.         return MDB2_OK;
  712.     }
  713.  
  714.     // }}}
  715.     // {{{ valid()
  716.  
  717.     /**
  718.     * check if the end of the result set has been reached
  719.     *
  720.     * @return mixed true or false on sucess, a MDB2 error on failure
  721.     * @access public
  722.     */
  723.     function valid()
  724.     {
  725.         $numrows $this->numRows();
  726.         if (PEAR::isError($numrows)) {
  727.             return $numrows;
  728.         }
  729.         return $this->rownum ($numrows - 1);
  730.     }
  731.  
  732.     // }}}
  733.     // {{{ numRows()
  734.  
  735.     /**
  736.     * returns the number of rows in a result object
  737.     *
  738.     * @return mixed MDB2 Error Object or the number of rows
  739.     * @access public
  740.     */
  741.     function numRows()
  742.     {
  743.         $rows @pg_num_rows($this->result);
  744.         if (is_null($rows)) {
  745.             if (is_null($this->result)) {
  746.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  747.                     'numRows: resultset has already been freed');
  748.             }
  749.             return $this->raiseError();
  750.         }
  751.         return $rows;
  752.     }
  753. }
  754.  
  755. class MDB2_Statement_pgsql extends MDB2_Statement_Common
  756. {
  757.  
  758. }
  759. ?>

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