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

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