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

Source for file ibase.php

Documentation is available at ibase.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, Lorenzo Alberton                       |
  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: Lorenzo Alberton <l.alberton@quipo.it>                       |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: ibase.php,v 1.204 2007/01/06 21:48:37 quipo Exp $
  47.  
  48. /**
  49.  * MDB2 FireBird/InterBase driver
  50.  *
  51.  * @package MDB2
  52.  * @category Database
  53.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  54.  */
  55. class MDB2_Driver_ibase extends MDB2_Driver_Common
  56. {
  57.     // {{{ properties
  58.     var $string_quoting = array('start' => "'"'end' => "'"'escape' => "'"'escape_pattern' => '\\');
  59.  
  60.     var $identifier_quoting = array('start' => '''end' => '''escape' => false);
  61.  
  62.     var $transaction_id = 0;
  63.  
  64.     var $query_parameters = array();
  65.  
  66.     var $query_parameter_values = array();
  67.     // }}}
  68.     // {{{ constructor
  69.  
  70.     /**
  71.      * Constructor
  72.      */
  73.     function __construct()
  74.     {
  75.         parent::__construct();
  76.  
  77.         $this->phptype  'ibase';
  78.         $this->dbsyntax 'ibase';
  79.  
  80.         $this->supported['sequences'= true;
  81.         $this->supported['indexes'= true;
  82.         $this->supported['affected_rows'function_exists('ibase_affected_rows');
  83.         $this->supported['summary_functions'= true;
  84.         $this->supported['order_by_text'= true;
  85.         $this->supported['transactions'= true;
  86.         $this->supported['savepoints'= true;
  87.         $this->supported['current_id'= true;
  88.         $this->supported['limit_queries''emulated';
  89.         $this->supported['LOBs'= true;
  90.         $this->supported['replace'= false;
  91.         $this->supported['sub_selects'= true;
  92.         $this->supported['auto_increment'= true;
  93.         $this->supported['primary_key'= true;
  94.         $this->supported['result_introspection'= true;
  95.         $this->supported['prepared_statements'= true;
  96.         $this->supported['identifier_quoting'= false;
  97.         $this->supported['pattern_escaping'= true;
  98.         $this->supported['new_link'= false;
  99.  
  100.         $this->options['DBA_username'= false;
  101.         $this->options['DBA_password'= false;
  102.         $this->options['database_path''';
  103.         $this->options['database_extension''.gdb';
  104.         $this->options['server_version''';
  105.     }
  106.  
  107.     // }}}
  108.     // {{{ errorInfo()
  109.  
  110.     /**
  111.      * This method is used to collect information about an error
  112.      *
  113.      * @param integer $error 
  114.      * @return array 
  115.      * @access public
  116.      */
  117.     function errorInfo($error = null)
  118.     {
  119.         $native_msg @ibase_errmsg();
  120.  
  121.         if (function_exists('ibase_errcode')) {
  122.             $native_code @ibase_errcode();
  123.         else {
  124.             // memo for the interbase php module hackers: we need something similar
  125.             // to mysql_errno() to retrieve error codes instead of this ugly hack
  126.             if (preg_match('/^([^0-9\-]+)([0-9\-]+)\s+(.*)$/'$native_msg$m)) {
  127.                 $native_code = (int)$m[2];
  128.             else {
  129.                 $native_code = null;
  130.             }
  131.         }
  132.         if (is_null($error)) {
  133.             $error = MDB2_ERROR;
  134.             if ($native_code{
  135.                 // try to interpret Interbase error code (that's why we need ibase_errno()
  136.                 // in the interbase module to return the real error code)
  137.                 switch ($native_code{
  138.                 case -204:
  139.                     if (isset($m[3]&& is_int(strpos($m[3]'Table unknown'))) {
  140.                         $errno = MDB2_ERROR_NOSUCHTABLE;
  141.                     }
  142.                 break;
  143.                 default:
  144.                     static $ecode_map;
  145.                     if (empty($ecode_map)) {
  146.                         $ecode_map = array(
  147.                             -104 => MDB2_ERROR_SYNTAX,
  148.                             -150 => MDB2_ERROR_ACCESS_VIOLATION,
  149.                             -151 => MDB2_ERROR_ACCESS_VIOLATION,
  150.                             -155 => MDB2_ERROR_NOSUCHTABLE,
  151.                             -157 => MDB2_ERROR_NOSUCHFIELD,
  152.                             -158 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  153.                             -170 => MDB2_ERROR_MISMATCH,
  154.                             -171 => MDB2_ERROR_MISMATCH,
  155.                             -172 => MDB2_ERROR_INVALID,
  156.                             // -204 =>  // Covers too many errors, need to use regex on msg
  157.                             -205 => MDB2_ERROR_NOSUCHFIELD,
  158.                             -206 => MDB2_ERROR_NOSUCHFIELD,
  159.                             -208 => MDB2_ERROR_INVALID,
  160.                             -219 => MDB2_ERROR_NOSUCHTABLE,
  161.                             -297 => MDB2_ERROR_CONSTRAINT,
  162.                             -303 => MDB2_ERROR_INVALID,
  163.                             -413 => MDB2_ERROR_INVALID_NUMBER,
  164.                             -530 => MDB2_ERROR_CONSTRAINT,
  165.                             -551 => MDB2_ERROR_ACCESS_VIOLATION,
  166.                             -552 => MDB2_ERROR_ACCESS_VIOLATION,
  167.                             // -607 =>  // Covers too many errors, need to use regex on msg
  168.                             -625 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  169.                             -803 => MDB2_ERROR_CONSTRAINT,
  170.                             -804 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  171.                             // -902 =>  // Covers too many errors, need to use regex on msg
  172.                             -904 => MDB2_ERROR_CONNECT_FAILED,
  173.                             -922 => MDB2_ERROR_NOSUCHDB,
  174.                             -923 => MDB2_ERROR_CONNECT_FAILED,
  175.                             -924 => MDB2_ERROR_CONNECT_FAILED
  176.                         );
  177.                     }
  178.                     if (isset($ecode_map[$native_code])) {
  179.                         $error $ecode_map[$native_code];
  180.                     }
  181.                     break;
  182.                 }
  183.             else {
  184.                 static $error_regexps;
  185.                 if (!isset($error_regexps)) {
  186.                     $error_regexps = array(
  187.                         '/generator .* is not defined/'
  188.                             => MDB2_ERROR_SYNTAX,  // for compat. w ibase_errcode()
  189.                         '/table.*(not exist|not found|unknown)/i'
  190.                             => MDB2_ERROR_NOSUCHTABLE,
  191.                         '/table .* already exists/i'
  192.                             => MDB2_ERROR_ALREADY_EXISTS,
  193.                         '/unsuccessful metadata update .* failed attempt to store duplicate value/i'
  194.                             => MDB2_ERROR_ALREADY_EXISTS,
  195.                         '/unsuccessful metadata update .* not found/i'
  196.                             => MDB2_ERROR_NOT_FOUND,
  197.                         '/validation error for column .* value "\*\*\* null/i'
  198.                             => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  199.                         '/violation of [\w ]+ constraint/i'
  200.                             => MDB2_ERROR_CONSTRAINT,
  201.                         '/conversion error from string/i'
  202.                             => MDB2_ERROR_INVALID_NUMBER,
  203.                         '/no permission for/i'
  204.                             => MDB2_ERROR_ACCESS_VIOLATION,
  205.                         '/arithmetic exception, numeric overflow, or string truncation/i'
  206.                             => MDB2_ERROR_INVALID,
  207.                         '/feature is not supported/i'
  208.                             => MDB2_ERROR_NOT_CAPABLE,
  209.                     );
  210.                 }
  211.                 foreach ($error_regexps as $regexp => $code{
  212.                     if (preg_match($regexp$native_msg$m)) {
  213.                         $error $code;
  214.                         break;
  215.                     }
  216.                 }
  217.             }
  218.         }
  219.         return array($error$native_code$native_msg);
  220.     }
  221.  
  222.     // }}}
  223.     // {{{ quoteIdentifier()
  224.  
  225.     /**
  226.      * Delimited identifiers are a nightmare with InterBase, so they're disabled
  227.      *
  228.      * @param string $str  identifier name to be quoted
  229.      * @param bool   $check_option  check the 'quote_identifier' option
  230.      *
  231.      * @return string  quoted identifier string
  232.      *
  233.      * @access public
  234.      */
  235.     function quoteIdentifier($str$check_option = false)
  236.     {
  237.         if ($check_option && !$this->options['quote_identifier']{
  238.             return $str;
  239.         }
  240.         return strtoupper($str);
  241.     }
  242.  
  243.     // }}}
  244.     // {{{ getConnection()
  245.  
  246.     /**
  247.      * Returns a native connection
  248.      *
  249.      * @return  mixed   a valid MDB2 connection object,
  250.      *                   or a MDB2 error object on error
  251.      * @access  public
  252.      */
  253.     function getConnection()
  254.     {
  255.         $result $this->connect();
  256.         if (PEAR::isError($result)) {
  257.             return $result;
  258.         }
  259.         if ($this->in_transaction{
  260.             return $this->transaction_id;
  261.         }
  262.         return $this->connection;
  263.     }
  264.  
  265.     // }}}
  266.     // {{{ beginTransaction()
  267.  
  268.     /**
  269.      * Start a transaction or set a savepoint.
  270.      *
  271.      * @param   string  name of a savepoint to set
  272.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  273.      *
  274.      * @access  public
  275.      */
  276.     function beginTransaction($savepoint = null)
  277.     {
  278.         $this->debug('Starting transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  279.         if (!is_null($savepoint)) {
  280.             if (!$this->in_transaction{
  281.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  282.                     'savepoint cannot be released when changes are auto committed'__FUNCTION__);
  283.             }
  284.             $query 'SAVEPOINT '.$savepoint;
  285.             return $this->_doQuery($querytrue);
  286.         elseif ($this->in_transaction{
  287.             return MDB2_OK;  //nothing to do
  288.         }
  289.         $connection $this->getConnection();
  290.         if (PEAR::isError($connection)) {
  291.             return $connection;
  292.         }
  293.         $result @ibase_trans(IBASE_DEFAULT$connection);
  294.         if (!$result{
  295.             return $this->raiseError(nullnullnull,
  296.                 'could not start a transaction'__FUNCTION__);
  297.         }
  298.         $this->transaction_id = $result;
  299.         $this->in_transaction = true;
  300.         return MDB2_OK;
  301.     }
  302.  
  303.     // }}}
  304.     // {{{ commit()
  305.  
  306.     /**
  307.      * Commit the database changes done during a transaction that is in
  308.      * progress or release a savepoint. This function may only be called when
  309.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  310.      * transaction is implicitly started after committing the pending changes.
  311.      *
  312.      * @param   string  name of a savepoint to release
  313.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  314.      *
  315.      * @access  public
  316.      */
  317.     function commit($savepoint = null)
  318.     {
  319.         $this->debug('Committing transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  320.         if (!$this->in_transaction{
  321.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  322.                 'commit/release savepoint cannot be done changes are auto committed'__FUNCTION__);
  323.         }
  324.         if (!is_null($savepoint)) {
  325.             $query 'RELEASE SAVEPOINT '.$savepoint;
  326.             return $this->_doQuery($querytrue);
  327.         }
  328.  
  329.         if (!@ibase_commit($this->transaction_id)) {
  330.             return $this->raiseError(nullnullnull,
  331.                 'could not commit a transaction'__FUNCTION__);
  332.         }
  333.         $this->in_transaction = false;
  334.         $this->transaction_id = 0;
  335.         return MDB2_OK;
  336.     }
  337.  
  338.     // }}}
  339.     // {{{ rollback()
  340.  
  341.     /**
  342.      * Cancel any database changes done during a transaction or since a specific
  343.      * savepoint that is in progress. This function may only be called when
  344.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  345.      * transaction is implicitly started after canceling the pending changes.
  346.      *
  347.      * @param   string  name of a savepoint to rollback to
  348.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  349.      *
  350.      * @access  public
  351.      */
  352.     function rollback($savepoint = null)
  353.     {
  354.         $this->debug('Rolling back transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  355.         if (!$this->in_transaction{
  356.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  357.                 'rollback cannot be done changes are auto committed'__FUNCTION__);
  358.         }
  359.         if (!is_null($savepoint)) {
  360.             $query 'ROLLBACK TO SAVEPOINT '.$savepoint;
  361.             return $this->_doQuery($querytrue);
  362.         }
  363.  
  364.         if ($this->transaction_id && !@ibase_rollback($this->transaction_id)) {
  365.             return $this->raiseError(nullnullnull,
  366.                 'Could not rollback a pending transaction: '.@ibase_errmsg()__FUNCTION__);
  367.         }
  368.         $this->in_transaction = false;
  369.         $this->transaction_id = 0;
  370.         return MDB2_OK;
  371.     }
  372.  
  373.     // }}}
  374.     // {{{ setTransactionIsolation()
  375.  
  376.     /**
  377.      * Set the transacton isolation level.
  378.      *
  379.      * @param   string  standard isolation level (SQL-92)
  380.      *                   READ UNCOMMITTED (allows dirty reads)
  381.      *                   READ COMMITTED (prevents dirty reads)
  382.      *                   REPEATABLE READ (prevents nonrepeatable reads)
  383.      *                   SERIALIZABLE (prevents phantom reads)
  384.      * @param   array some transaction options:
  385.      *                   'wait' => 'WAIT' | 'NO WAIT'
  386.      *                   'rw'   => 'READ WRITE' | 'READ ONLY'
  387.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  388.      *
  389.      * @access  public
  390.      * @since   2.1.1
  391.      */
  392.     function setTransactionIsolation($isolation$options = array())
  393.     {
  394.         $this->debug('Setting transaction isolation level'__FUNCTION__array('is_manip' => true));
  395.         switch ($isolation{
  396.         case 'READ UNCOMMITTED':
  397.             $ibase_isolation 'READ COMMITTED RECORD_VERSION';
  398.             break;
  399.         case 'READ COMMITTED':
  400.             $ibase_isolation 'READ COMMITTED NO RECORD_VERSION';
  401.             break;
  402.         case 'REPEATABLE READ':
  403.             $ibase_isolation 'SNAPSHOT';
  404.             break;
  405.         case 'SERIALIZABLE':
  406.             $ibase_isolation 'SNAPSHOT TABLE STABILITY';
  407.             break;
  408.         default:
  409.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  410.                 'isolation level is not supported: '.$isolation__FUNCTION__);
  411.         }
  412.  
  413.         if (!empty($options['wait'])) {
  414.             switch ($options['wait']{
  415.             case 'WAIT':
  416.             case 'NO WAIT':
  417.                 $wait $options['wait'];
  418.                 break;
  419.             default:
  420.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  421.                     'wait option is not supported: '.$options['wait']__FUNCTION__);
  422.             }
  423.         }
  424.  
  425.         if (!empty($options['rw'])) {
  426.             switch ($options['rw']{
  427.             case 'READ ONLY':
  428.             case 'READ WRITE':
  429.                 $rw $options['wait'];
  430.                 break;
  431.             default:
  432.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  433.                     'wait option is not supported: '.$options['rw']__FUNCTION__);
  434.             }
  435.         }
  436.  
  437.         $query = "SET TRANSACTION $rw $wait ISOLATION LEVEL $ibase_isolation";
  438.         return $this->_doQuery($querytrue);
  439.     }
  440.  
  441.     // }}}
  442.     // {{{ getDatabaseFile($database_name)
  443.  
  444.     /**
  445.      * Builds the string with path+dbname+extension
  446.      *
  447.      * @return string full database path+file
  448.      * @access protected
  449.      */
  450.     function _getDatabaseFile($database_name)
  451.     {
  452.         if ($database_name == ''{
  453.             return $database_name;
  454.         }
  455.         $ret $this->options['database_path'$database_name;
  456.         if (   strpos($database_name'.fdb'=== false
  457.             && strpos($database_name'.gdb'=== false
  458.         {
  459.             $ret .= $this->options['database_extension'];
  460.         }
  461.         return $ret;
  462.     }
  463.  
  464.     // }}}
  465.     // {{{ _doConnect()
  466.  
  467.     /**
  468.      * Does the grunt work of connecting to the database
  469.      *
  470.      * @return mixed connection resource on success, MDB2 Error Object on failure
  471.      * @access protected
  472.      */
  473.     function _doConnect($database_name$persistent = false)
  474.     {
  475.         $user    $this->dsn['username'];
  476.         $pw      $this->dsn['password'];
  477.         $dbhost  $this->dsn['hostspec'?
  478.             ($this->dsn['hostspec'].':'.$database_name$database_name;
  479.  
  480.         $params = array();
  481.         $params[$dbhost;
  482.         $params[!empty($user$user : null;
  483.         $params[!empty($pw$pw : null;
  484.         $params[= isset($this->dsn['charset']$this->dsn['charset': null;
  485.         $params[= isset($this->dsn['buffers']$this->dsn['buffers': null;
  486.         $params[= isset($this->dsn['dialect']$this->dsn['dialect': null;
  487.         $params[= isset($this->dsn['role'])    $this->dsn['role': null;
  488.  
  489.         $connect_function $persistent 'ibase_pconnect' 'ibase_connect';
  490.         $connection @call_user_func_array($connect_function$params);
  491.         if ($connection <= 0{
  492.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  493.                 'unable to establish a connection'__FUNCTION__);
  494.         }
  495.  
  496.        if (empty($this->dsn['disable_iso_date'])) {
  497.             if (function_exists('ibase_timefmt')) {
  498.                 @ibase_timefmt("%Y-%m-%d %H:%M:%S"IBASE_TIMESTAMP);
  499.                 @ibase_timefmt("%Y-%m-%d"IBASE_DATE);
  500.             else {
  501.                 @ini_set("ibase.timestampformat""%Y-%m-%d %H:%M:%S");
  502.                 //@ini_set("ibase.timeformat", "%H:%M:%S");
  503.                 @ini_set("ibase.dateformat""%Y-%m-%d");
  504.             }
  505.        }
  506.  
  507.         return $connection;
  508.     }
  509.  
  510.     // }}}
  511.     // {{{ connect()
  512.  
  513.     /**
  514.      * Connect to the database
  515.      *
  516.      * @return true on success, MDB2 Error Object on failure
  517.      * @access public
  518.      */
  519.     function connect()
  520.     {
  521.         $database_file $this->_getDatabaseFile($this->database_name);
  522.         if (is_resource($this->connection)) {
  523.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  524.                 && $this->connected_database_name == $database_file
  525.                 && $this->opened_persistent == $this->options['persistent']
  526.             {
  527.                 return MDB2_OK;
  528.             }
  529.             $this->disconnect(false);
  530.         }
  531.  
  532.         if (!PEAR::loadExtension('interbase')) {
  533.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  534.                 'extension '.$this->phptype.' is not compiled into PHP'__FUNCTION__);
  535.         }
  536.  
  537.         if (!empty($this->database_name)) {
  538.             $connection $this->_doConnect($database_file$this->options['persistent']);
  539.             if (PEAR::isError($connection)) {
  540.                 return $connection;
  541.             }
  542.             $this->connection =$connection;
  543.             $this->connected_dsn $this->dsn;
  544.             $this->connected_database_name $database_file;
  545.             $this->opened_persistent $this->options['persistent'];
  546.             $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  547.             $this->supported['limit_queries'($this->dbsyntax == 'firebird'? true : 'emulated';
  548.         }
  549.         return MDB2_OK;
  550.     }
  551.  
  552.     // }}}
  553.     // {{{ setCharset()
  554.  
  555.     /**
  556.      * Set the charset on the current connection
  557.      *
  558.      * @param string    charset
  559.      * @param resource  connection handle
  560.      *
  561.      * @return true on success, MDB2 Error Object on failure
  562.      */
  563.     function setCharset($charset$connection = null)
  564.     {
  565.         if (is_null($connection)) {
  566.             $connection $this->getConnection();
  567.             if (PEAR::isError($connection)) {
  568.                 return $connection;
  569.             }
  570.         }
  571.  
  572.         $query 'SET NAMES '.$this->quote($charset'text');
  573.         $result @ibase_query($connection$query);
  574.         if (!$result{
  575.             return $this->raiseError(nullnullnull,
  576.                 'Unable to set client charset: '.$charset__FUNCTION__);
  577.         }
  578.  
  579.         return MDB2_OK;
  580.     }
  581.  
  582.     // }}}
  583.     // {{{ disconnect()
  584.  
  585.     /**
  586.      * Log out and disconnect from the database.
  587.      *
  588.      * @param  boolean $force if the disconnect should be forced even if the
  589.      *                         connection is opened persistently
  590.      * @return mixed true on success, false if not connected and error
  591.      *                 object on error
  592.      * @access public
  593.      */
  594.     function disconnect($force = true)
  595.     {
  596.         if (is_resource($this->connection)) {
  597.             if ($this->in_transaction{
  598.                 $dsn $this->dsn;
  599.                 $database_name $this->database_name;
  600.                 $persistent $this->options['persistent'];
  601.                 $this->dsn $this->connected_dsn;
  602.                 $this->database_name $this->connected_database_name;
  603.                 $this->options['persistent'$this->opened_persistent;
  604.                 $this->rollback();
  605.                 $this->dsn $dsn;
  606.                 $this->database_name $database_name;
  607.                 $this->options['persistent'$persistent;
  608.             }
  609.  
  610.             if (!$this->opened_persistent || $force{
  611.                 @ibase_close($this->connection);
  612.             }
  613.         }
  614.         return parent::disconnect($force);
  615.     }
  616.  
  617.     // }}}
  618.     // {{{ _doQuery()
  619.  
  620.     /**
  621.      * Execute a query
  622.      * @param string $query  query
  623.      * @param boolean $is_manip  if the query is a manipulation query
  624.      * @param resource $connection 
  625.      * @param string $database_name 
  626.      * @return result or error object
  627.      * @access protected
  628.      */
  629.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  630.     {
  631.         $this->last_query $query;
  632.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  633.         if ($result{
  634.             if (PEAR::isError($result)) {
  635.                 return $result;
  636.             }
  637.             $query $result;
  638.         }
  639.         if ($this->getOption('disable_query')) {
  640.             if ($is_manip{
  641.                 return 0;
  642.             }
  643.             return null;
  644.         }
  645.  
  646.         if (is_null($connection)) {
  647.             $connection $this->getConnection();
  648.             if (PEAR::isError($connection)) {
  649.                 return $connection;
  650.             }
  651.         }
  652.         $result @ibase_query($connection$query);
  653.  
  654.         if ($result === false{
  655.             $err =$this->raiseError(nullnullnull,
  656.                 'Could not execute statement'__FUNCTION__);
  657.             return $err;
  658.         }
  659.  
  660.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  661.         return $result;
  662.     }
  663.  
  664.     // }}}
  665.     // {{{ _affectedRows()
  666.  
  667.     /**
  668.      * Returns the number of rows affected
  669.      *
  670.      * @param resource $result 
  671.      * @param resource $connection 
  672.      * @return mixed MDB2 Error Object or the number of rows affected
  673.      * @access private
  674.      */
  675.     function _affectedRows($connection$result = null)
  676.     {
  677.         if (is_null($connection)) {
  678.             $connection $this->getConnection();
  679.             if (PEAR::isError($connection)) {
  680.                 return $connection;
  681.             }
  682.         }
  683.         return (function_exists('ibase_affected_rows'@ibase_affected_rows($connection: 0);
  684.     }
  685.  
  686.     // }}}
  687.     // {{{ _modifyQuery()
  688.  
  689.     /**
  690.      * Changes a query string for various DBMS specific reasons
  691.      *
  692.      * @param string $query  query to modify
  693.      * @param boolean $is_manip  if it is a DML query
  694.      * @param integer $limit  limit the number of rows
  695.      * @param integer $offset  start reading from given offset
  696.      * @return string modified query
  697.      * @access protected
  698.      */
  699.     function _modifyQuery($query$is_manip$limit$offset)
  700.     {
  701.         if ($limit > 0 && $this->supports('limit_queries'=== true{
  702.             $query preg_replace('/^([\s(])*SELECT(?!\s*FIRST\s*\d+)/i',
  703.                 "SELECT FIRST $limit SKIP $offset"$query);
  704.         }
  705.         return $query;
  706.     }
  707.  
  708.     // }}}
  709.     // {{{ getServerVersion()
  710.  
  711.     /**
  712.      * return version information about the server
  713.      *
  714.      * @param bool   $native  determines if the raw version string should be returned
  715.      * @return mixed array/string with version information or MDB2 error object
  716.      * @access public
  717.      */
  718.     function getServerVersion($native = false)
  719.     {
  720.         $server_info = false;
  721.         if ($this->connected_server_info{
  722.             $server_info $this->connected_server_info;
  723.         elseif ($this->options['server_version']{
  724.             $server_info $this->options['server_version'];
  725.         elseif ($this->options['DBA_username']{
  726.             $ibserv @ibase_service_attach($this->dsn['hostspec']$this->options['DBA_username']$this->options['DBA_password']);
  727.             $server_info @ibase_server_info($ibservIBASE_SVC_SERVER_VERSION);
  728.             @ibase_service_detach($ibserv);
  729.         }
  730.         if (!$server_info{
  731.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  732.                 'Requires either "server_version" or "DBA_username"/"DBA_password" option'__FUNCTION__);
  733.         }
  734.         // cache server_info
  735.         $this->connected_server_info $server_info;
  736.         if (!$native{
  737.             //WI-V1.5.3.4854 Firebird 1.5
  738.             if (!preg_match('/-V([\d\.]*)/'$server_info$matches)) {
  739.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  740.                     'Could not parse version information:'.$server_info__FUNCTION__);
  741.             }
  742.             $tmp explode('.'$matches[1]4);
  743.             $server_info = array(
  744.                 'major' => isset($tmp[0]$tmp[0: null,
  745.                 'minor' => isset($tmp[1]$tmp[1: null,
  746.                 'patch' => isset($tmp[2]$tmp[2: null,
  747.                 'extra' => isset($tmp[3]$tmp[3: null,
  748.                 'native' => $server_info,
  749.             );
  750.         }
  751.         return $server_info;
  752.     }
  753.  
  754.     // }}}
  755.     // {{{ prepare()
  756.  
  757.     /**
  758.      * Prepares a query for multiple execution with execute().
  759.      * With some database backends, this is emulated.
  760.      * prepare() requires a generic query as string like
  761.      * 'INSERT INTO numbers VALUES(?,?)' or
  762.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  763.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  764.      * bindParam() and the query can be send off using the execute() method.
  765.      *
  766.      * @param string $query the query to prepare
  767.      * @param mixed   $types  array that contains the types of the placeholders
  768.      * @param mixed   $result_types  array that contains the types of the columns in
  769.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  770.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  771.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  772.      * @return mixed resource handle for the prepared query on success, a MDB2
  773.      *         error on failure
  774.      * @access public
  775.      * @see bindParam, execute
  776.      */
  777.     function &prepare($query$types = null$result_types = null$lobs = array())
  778.     {
  779.         if ($this->options['emulate_prepared']{
  780.             $obj =parent::prepare($query$types$result_types$lobs);
  781.             return $obj;
  782.         }
  783.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  784.         $offset $this->offset;
  785.         $limit  $this->limit;
  786.         $this->offset $this->limit = 0;
  787.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  788.         if ($result{
  789.             if (PEAR::isError($result)) {
  790.                 return $result;
  791.             }
  792.             $query $result;
  793.         }
  794.         $placeholder_type_guess $placeholder_type = null;
  795.         $question '?';
  796.         $colon ':';
  797.         $positions = array();
  798.         $position = 0;
  799.         while ($position strlen($query)) {
  800.             $q_position strpos($query$question$position);
  801.             $c_position strpos($query$colon$position);
  802.             if ($q_position && $c_position{
  803.                 $p_position min($q_position$c_position);
  804.             elseif ($q_position{
  805.                 $p_position $q_position;
  806.             elseif ($c_position{
  807.                 $p_position $c_position;
  808.             else {
  809.                 break;
  810.             }
  811.             if (is_null($placeholder_type)) {
  812.                 $placeholder_type_guess $query[$p_position];
  813.             }
  814.             
  815.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  816.             if (PEAR::isError($new_pos)) {
  817.                 return $new_pos;
  818.             }
  819.             if ($new_pos != $position{
  820.                 $position $new_pos;
  821.                 continue; //evaluate again starting from the new position
  822.             }
  823.             
  824.             if ($query[$position== $placeholder_type_guess{
  825.                 if (is_null($placeholder_type)) {
  826.                     $placeholder_type $query[$p_position];
  827.                     $question $colon $placeholder_type;
  828.                 }
  829.                 if ($placeholder_type == ':'{
  830.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  831.                     if ($parameter === ''{
  832.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  833.                             'named parameter with an empty name'__FUNCTION__);
  834.                         return $err;
  835.                     }
  836.                     $positions[$parameter;
  837.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  838.                 else {
  839.                     $positions[count($positions);
  840.                 }
  841.                 $position $p_position + 1;
  842.             else {
  843.                 $position $p_position;
  844.             }
  845.         }
  846.         $connection $this->getConnection();
  847.         if (PEAR::isError($connection)) {
  848.             return $connection;
  849.         }
  850.         $statement @ibase_prepare($connection$query);
  851.         if (!$statement{
  852.             $err =$this->raiseError(nullnullnull,
  853.                 'Could not create statement'__FUNCTION__);
  854.             return $err;
  855.         }
  856.  
  857.         $class_name 'MDB2_Statement_'.$this->phptype;
  858.         $obj =new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  859.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  860.         return $obj;
  861.     }
  862.  
  863.     // }}}
  864.     // {{{ getSequenceName()
  865.  
  866.     /**
  867.      * adds sequence name formatting to a sequence name
  868.      *
  869.      * @param string $sqn name of the sequence
  870.      * @return string formatted sequence name
  871.      * @access public
  872.      */
  873.     function getSequenceName($sqn)
  874.     {
  875.         return strtoupper(parent::getSequenceName($sqn));
  876.     }
  877.  
  878.     // }}}
  879.     // {{{ nextID()
  880.  
  881.     /**
  882.      * Returns the next free id of a sequence
  883.      *
  884.      * @param string $seq_name name of the sequence
  885.      * @param boolean $ondemand when true the sequence is
  886.      *                           automatic created, if it
  887.      *                           not exists
  888.      * @return mixed MDB2 Error Object or id
  889.      * @access public
  890.      */
  891.     function nextID($seq_name$ondemand = true)
  892.     {
  893.         $sequence_name $this->getSequenceName($seq_name);
  894.         $query 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
  895.         $this->expectError('*');
  896.         $result $this->queryOne($query'integer');
  897.         $this->popExpect();
  898.         if (PEAR::isError($result)) {
  899.             if ($ondemand{
  900.                 $this->loadModule('Manager'nulltrue);
  901.                 $result $this->manager->createSequence($seq_name);
  902.                 if (PEAR::isError($result)) {
  903.                     return $this->raiseError($resultnullnull,
  904.                         'on demand sequence could not be created'__FUNCTION__);
  905.                 else {
  906.                     return $this->nextID($seq_namefalse);
  907.                 }
  908.             }
  909.         }
  910.         return $result;
  911.     }
  912.  
  913.     // }}}
  914.     // {{{ lastInsertID()
  915.  
  916.     /**
  917.      * Returns the autoincrement ID if supported or $id or fetches the current
  918.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  919.      *
  920.      * @param string $table name of the table into which a new row was inserted
  921.      * @param string $field name of the field into which a new row was inserted
  922.      * @return mixed MDB2 Error Object or id
  923.      * @access public
  924.      */
  925.     function lastInsertID($table = null$field = null)
  926.     {
  927.         $seq $table.(empty($field'' '_'.$field);
  928.         return $this->currID($seq);
  929.     }
  930.     
  931.     // }}}
  932.     // {{{ currID()
  933.  
  934.     /**
  935.      * Returns the current id of a sequence
  936.      *
  937.      * @param string $seq_name name of the sequence
  938.      * @return mixed MDB2 Error Object or id
  939.      * @access public
  940.      */
  941.     function currID($seq_name)
  942.     {
  943.         $sequence_name $this->getSequenceName($seq_name);
  944.         $query 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
  945.         $value $this->queryOne($query);
  946.         if (PEAR::isError($value)) {
  947.             return $this->raiseError($valuenullnull,
  948.                 'Unable to select from ' $seq_name__FUNCTION__);
  949.         }
  950.         if (!is_numeric($value)) {
  951.             return $this->raiseError(MDB2_ERRORnullnull,
  952.                 'could not find value in sequence table'__FUNCTION__);
  953.         }
  954.         return $value;
  955.     }
  956.  
  957.     // }}}
  958. }
  959.  
  960. /**
  961.  * MDB2 FireBird/InterBase result driver
  962.  *
  963.  * @package MDB2
  964.  * @category Database
  965.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  966.  */
  967. class MDB2_Result_ibase extends MDB2_Result_Common
  968. {
  969.     // {{{ _skipLimitOffset()
  970.  
  971.     /**
  972.      * Skip the first row of a result set.
  973.      *
  974.      * @param resource $result 
  975.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  976.      * @access protected
  977.      */
  978.     function _skipLimitOffset()
  979.     {
  980.         if ($this->db->supports('limit_queries'=== true{
  981.             return true;
  982.         }
  983.         if ($this->limit{
  984.             if ($this->rownum $this->limit{
  985.                 return false;
  986.             }
  987.         }
  988.         if ($this->offset{
  989.             while ($this->offset_count $this->offset{
  990.                 ++$this->offset_count;
  991.                 if (!is_array(@ibase_fetch_row($this->result))) {
  992.                     $this->offset_count $this->offset;
  993.                     return false;
  994.                 }
  995.             }
  996.         }
  997.         return true;
  998.     }
  999.  
  1000.     // }}}
  1001.     // {{{ fetchRow()
  1002.  
  1003.     /**
  1004.      * Fetch a row and insert the data into an existing array.
  1005.      *
  1006.      * @param int  $fetchmode how the array data should be indexed
  1007.      * @param int  $rownum    number of the row where the data can be found
  1008.      * @return int data array on success, a MDB2 error on failure
  1009.      * @access public
  1010.      */
  1011.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1012.     {
  1013.         if ($this->result === true{
  1014.             //query successfully executed, but without results...
  1015.             $null = null;
  1016.             return $null;
  1017.         }
  1018.         if (!$this->_skipLimitOffset()) {
  1019.             $null = null;
  1020.             return $null;
  1021.         }
  1022.         if (!is_null($rownum)) {
  1023.             $seek $this->seek($rownum);
  1024.             if (PEAR::isError($seek)) {
  1025.                 return $seek;
  1026.             }
  1027.         }
  1028.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1029.             $fetchmode $this->db->fetchmode;
  1030.         }
  1031.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1032.             $row @ibase_fetch_assoc($this->result);
  1033.             if (is_array($row)
  1034.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1035.             {
  1036.                 $row array_change_key_case($row$this->db->options['field_case']);
  1037.             }
  1038.         else {
  1039.             $row @ibase_fetch_row($this->result);
  1040.         }
  1041.         if (!$row{
  1042.             if ($this->result === false{
  1043.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1044.                     'resultset has already been freed'__FUNCTION__);
  1045.                 return $err;
  1046.             }
  1047.             $null = null;
  1048.             return $null;
  1049.         }
  1050.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1051.         $rtrim = false;
  1052.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1053.             if (empty($this->types)) {
  1054.                 $mode += MDB2_PORTABILITY_RTRIM;
  1055.             else {
  1056.                 $rtrim = true;
  1057.             }
  1058.         }
  1059.         if ($mode{
  1060.             $this->db->_fixResultArrayValues($row$mode);
  1061.         }
  1062.         if (!empty($this->types)) {
  1063.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1064.         }
  1065.         if (!empty($this->values)) {
  1066.             $this->_assignBindColumns($row);
  1067.         }
  1068.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1069.             $object_class $this->db->options['fetch_class'];
  1070.             if ($object_class == 'stdClass'{
  1071.                 $row = (object) $row;
  1072.             else {
  1073.                 $row &new $object_class($row);
  1074.             }
  1075.         }
  1076.         ++$this->rownum;
  1077.         return $row;
  1078.     }
  1079.  
  1080.     // }}}
  1081.     // {{{ _getColumnNames()
  1082.  
  1083.     /**
  1084.      * Retrieve the names of columns returned by the DBMS in a query result.
  1085.      *
  1086.      * @return  mixed   Array variable that holds the names of columns as keys
  1087.      *                   or an MDB2 error on failure.
  1088.      *                   Some DBMS may not return any columns when the result set
  1089.      *                   does not contain any rows.
  1090.      * @access private
  1091.      */
  1092.     function _getColumnNames()
  1093.     {
  1094.         $columns = array();
  1095.         $numcols $this->numCols();
  1096.         if (PEAR::isError($numcols)) {
  1097.             return $numcols;
  1098.         }
  1099.         for ($column = 0; $column $numcols$column++{
  1100.             $column_info @ibase_field_info($this->result$column);
  1101.             $columns[$column_info['alias']] $column;
  1102.         }
  1103.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1104.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1105.         }
  1106.         return $columns;
  1107.     }
  1108.  
  1109.     // }}}
  1110.     // {{{ numCols()
  1111.  
  1112.     /**
  1113.      * Count the number of columns returned by the DBMS in a query result.
  1114.      *
  1115.      * @return mixed integer value with the number of columns, a MDB2 error
  1116.      *       on failure
  1117.      * @access public
  1118.      */
  1119.     function numCols()
  1120.     {
  1121.         if ($this->result === true{
  1122.             //query successfully executed, but without results...
  1123.             return 0;
  1124.         }
  1125.  
  1126.         if (!is_resource($this->result)) {
  1127.             return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1128.                 'numCols(): not a valid ibase resource'__FUNCTION__);
  1129.         }
  1130.         $cols @ibase_num_fields($this->result);
  1131.         if (is_null($cols)) {
  1132.             if ($this->result === false{
  1133.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1134.                     'resultset has already been freed'__FUNCTION__);
  1135.             elseif (is_null($this->result)) {
  1136.                 return count($this->types);
  1137.             }
  1138.             return $this->db->raiseError(nullnullnull,
  1139.                 'Could not get column count'__FUNCTION__);
  1140.         }
  1141.         return $cols;
  1142.     }
  1143.  
  1144.     // }}}
  1145.     // {{{ free()
  1146.  
  1147.     /**
  1148.      * Free the internal resources associated with $result.
  1149.      *
  1150.      * @return boolean true on success, false if $result is invalid
  1151.      * @access public
  1152.      */
  1153.     function free()
  1154.     {
  1155.         if (is_resource($this->result&& $this->db->connection{
  1156.             $free @ibase_free_result($this->result);
  1157.             if ($free === false{
  1158.                 return $this->db->raiseError(nullnullnull,
  1159.                     'Could not free result'__FUNCTION__);
  1160.             }
  1161.         }
  1162.         $this->result = false;
  1163.         return MDB2_OK;
  1164.     }
  1165.  
  1166.     // }}}
  1167. }
  1168.  
  1169. /**
  1170.  * MDB2 FireBird/InterBase buffered result driver
  1171.  *
  1172.  * @package MDB2
  1173.  * @category Database
  1174.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1175.  */
  1176. {
  1177.     // {{{ class vars
  1178.  
  1179.     var $buffer;
  1180.     var $buffer_rownum = - 1;
  1181.  
  1182.     // }}}
  1183.     // {{{ _fillBuffer()
  1184.  
  1185.     /**
  1186.      * Fill the row buffer
  1187.      *
  1188.      * @param int $rownum   row number upto which the buffer should be filled
  1189.      *                       if the row number is null all rows are ready into the buffer
  1190.      * @return boolean true on success, false on failure
  1191.      * @access protected
  1192.      */
  1193.     function _fillBuffer($rownum = null)
  1194.     {
  1195.         if (isset($this->buffer&& is_array($this->buffer)) {
  1196.             if (is_null($rownum)) {
  1197.                 if (!end($this->buffer)) {
  1198.                     return false;
  1199.                 }
  1200.             elseif (isset($this->buffer[$rownum])) {
  1201.                 return (bool) $this->buffer[$rownum];
  1202.             }
  1203.         }
  1204.  
  1205.         if (!$this->_skipLimitOffset()) {
  1206.             return false;
  1207.         }
  1208.  
  1209.         $buffer = true;
  1210.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  1211.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  1212.             && ($buffer @ibase_fetch_row($this->result))
  1213.         {
  1214.             ++$this->buffer_rownum;
  1215.             $this->buffer[$this->buffer_rownum$buffer;
  1216.         }
  1217.  
  1218.         if (!$buffer{
  1219.             ++$this->buffer_rownum;
  1220.             $this->buffer[$this->buffer_rownum= false;
  1221.             return false;
  1222.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  1223.             ++$this->buffer_rownum;
  1224.             $this->buffer[$this->buffer_rownum= false;
  1225.         }
  1226.         return true;
  1227.     }
  1228.  
  1229.     // }}}
  1230.     // {{{ fetchRow()
  1231.  
  1232.     /**
  1233.      * Fetch a row and insert the data into an existing array.
  1234.      *
  1235.      * @param int       $fetchmode  how the array data should be indexed
  1236.      * @param int    $rownum    number of the row where the data can be found
  1237.      * @return int data array on success, a MDB2 error on failure
  1238.      * @access public
  1239.      */
  1240.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1241.     {
  1242.         if ($this->result === true || is_null($this->result)) {
  1243.             //query successfully executed, but without results...
  1244.             $null = null;
  1245.             return $null;
  1246.         }
  1247.         if ($this->result === false{
  1248.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1249.                 'resultset has already been freed'__FUNCTION__);
  1250.             return $err;
  1251.         }
  1252.         if (!is_null($rownum)) {
  1253.             $seek $this->seek($rownum);
  1254.             if (PEAR::isError($seek)) {
  1255.                 return $seek;
  1256.             }
  1257.         }
  1258.         $target_rownum $this->rownum + 1;
  1259.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1260.             $fetchmode $this->db->fetchmode;
  1261.         }
  1262.         if (!$this->_fillBuffer($target_rownum)) {
  1263.             $null = null;
  1264.             return $null;
  1265.         }
  1266.         $row $this->buffer[$target_rownum];
  1267.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1268.             $column_names $this->getColumnNames();
  1269.             foreach ($column_names as $name => $i{
  1270.                 $column_names[$name$row[$i];
  1271.             }
  1272.             $row $column_names;
  1273.         }
  1274.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1275.         $rtrim = false;
  1276.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1277.             if (empty($this->types)) {
  1278.                 $mode += MDB2_PORTABILITY_RTRIM;
  1279.             else {
  1280.                 $rtrim = true;
  1281.             }
  1282.         }
  1283.         if ($mode{
  1284.             $this->db->_fixResultArrayValues($row$mode);
  1285.         }
  1286.         if (!empty($this->types)) {
  1287.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1288.         }
  1289.         if (!empty($this->values)) {
  1290.             $this->_assignBindColumns($row);
  1291.         }
  1292.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1293.             $object_class $this->db->options['fetch_class'];
  1294.             if ($object_class == 'stdClass'{
  1295.                 $row = (object) $row;
  1296.             else {
  1297.                 $row &new $object_class($row);
  1298.             }
  1299.         }
  1300.         ++$this->rownum;
  1301.         return $row;
  1302.     }
  1303.  
  1304.     // }}}
  1305.     // {{{ seek()
  1306.  
  1307.     /**
  1308.      * Seek to a specific row in a result set
  1309.      *
  1310.      * @param int    $rownum    number of the row where the data can be found
  1311.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1312.      * @access public
  1313.      */
  1314.     function seek($rownum = 0)
  1315.     {
  1316.         if ($this->result === false{
  1317.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1318.                 'resultset has already been freed'__FUNCTION__);
  1319.         }
  1320.         $this->rownum $rownum - 1;
  1321.         return MDB2_OK;
  1322.     }
  1323.  
  1324.     // }}}
  1325.     // {{{ valid()
  1326.  
  1327.     /**
  1328.      * Check if the end of the result set has been reached
  1329.      *
  1330.      * @return mixed true or false on sucess, a MDB2 error on failure
  1331.      * @access public
  1332.      */
  1333.     function valid()
  1334.     {
  1335.         if ($this->result === false{
  1336.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1337.                 'resultset has already been freed'__FUNCTION__);
  1338.         elseif (is_null($this->result)) {
  1339.             return true;
  1340.         }
  1341.         if ($this->_fillBuffer($this->rownum + 1)) {
  1342.             return true;
  1343.         }
  1344.         return false;
  1345.     }
  1346.  
  1347.     // }}}
  1348.     // {{{ numRows()
  1349.  
  1350.     /**
  1351.      * Returns the number of rows in a result object
  1352.      *
  1353.      * @return mixed MDB2 Error Object or the number of rows
  1354.      * @access public
  1355.      */
  1356.     function numRows()
  1357.     {
  1358.         if ($this->result === false{
  1359.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1360.                 'resultset has already been freed'__FUNCTION__);
  1361.         elseif (is_null($this->result)) {
  1362.             return 0;
  1363.         }
  1364.         $this->_fillBuffer();
  1365.         return $this->buffer_rownum;
  1366.     }
  1367.  
  1368.     // }}}
  1369.     // {{{ free()
  1370.  
  1371.     /**
  1372.      * Free the internal resources associated with $result.
  1373.      *
  1374.      * @return boolean true on success, false if $result is invalid
  1375.      * @access public
  1376.      */
  1377.     function free()
  1378.     {
  1379.         $this->buffer = null;
  1380.         $this->buffer_rownum = null;
  1381.         return parent::free();
  1382.     }
  1383.  
  1384.     // }}}
  1385. }
  1386.  
  1387. /**
  1388.  * MDB2 FireBird/InterBase statement driver
  1389.  *
  1390.  * @package MDB2
  1391.  * @category Database
  1392.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1393.  */
  1394. class MDB2_Statement_ibase extends MDB2_Statement_Common
  1395. {
  1396.     // {{{ _execute()
  1397.  
  1398.     /**
  1399.      * Execute a prepared query statement helper method.
  1400.      *
  1401.      * @param mixed $result_class string which specifies which result class to use
  1402.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1403.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1404.      * @access private
  1405.      */
  1406.     function &_execute($result_class = true$result_wrap_class = false)
  1407.     {
  1408.         if (is_null($this->statement)) {
  1409.             $result =parent::_execute($result_class$result_wrap_class);
  1410.             return $result;
  1411.         }
  1412.         $this->db->last_query = $this->query;
  1413.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1414.         if ($this->db->getOption('disable_query')) {
  1415.             $result $this->is_manip ? 0 : null;
  1416.             return $result;
  1417.         }
  1418.  
  1419.         $connection $this->db->getConnection();
  1420.         if (PEAR::isError($connection)) {
  1421.             return $connection;
  1422.         }
  1423.  
  1424.         $parameters = array(0 => $this->statement);
  1425.         foreach ($this->positions as $parameter{
  1426.             if (!array_key_exists($parameter$this->values)) {
  1427.                 return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1428.                     'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1429.             }
  1430.             $value $this->values[$parameter];
  1431.             $type !empty($this->types[$parameter]$this->types[$parameter: null;
  1432.             $parameters[$this->db->quote($value$typefalse);
  1433.         }
  1434.  
  1435.         $result @call_user_func_array('ibase_execute'$parameters);
  1436.         if ($result === false{
  1437.             $err =$this->db->raiseError(nullnullnull,
  1438.                 'Could not execute statement'__FUNCTION__);
  1439.             return $err;
  1440.         }
  1441.  
  1442.         if ($this->is_manip{
  1443.             $affected_rows $this->db->_affectedRows($connection);
  1444.             return $affected_rows;
  1445.         }
  1446.  
  1447.         $result =$this->db->_wrapResult($result$this->result_types,
  1448.             $result_class$result_wrap_class$this->limit$this->offset);
  1449.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1450.         return $result;
  1451.     }
  1452.  
  1453.     // }}}
  1454.  
  1455.     // }}}
  1456.     // {{{ free()
  1457.  
  1458.     /**
  1459.      * Release resources allocated for the specified prepared query.
  1460.      *
  1461.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1462.      * @access public
  1463.      */
  1464.     function free()
  1465.     {
  1466.         if (is_null($this->positions)) {
  1467.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1468.                 'Prepared statement has already been freed'__FUNCTION__);
  1469.         }
  1470.         $result = MDB2_OK;
  1471.  
  1472.         if (!is_null($this->statement&& !@ibase_free_query($this->statement)) {
  1473.             $result $this->db->raiseError(nullnullnull,
  1474.                 'Could not free statement'__FUNCTION__);
  1475.         }
  1476.  
  1477.         parent::free();
  1478.         return $result;
  1479.     }
  1480. }
  1481. ?>

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