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.183 2006/07/22 08:01:56 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 $escape_quotes = "'";
  59.  
  60.     var $escape_pattern = "\\";
  61.  
  62.     var $escape_identifier = '';
  63.  
  64.     var $transaction_id = 0;
  65.  
  66.     var $query_parameters = array();
  67.     var $query_parameter_values = array();
  68.  
  69.     // }}}
  70.     // {{{ constructor
  71.  
  72.     /**
  73.      * Constructor
  74.      */
  75.     function __construct()
  76.     {
  77.         parent::__construct();
  78.  
  79.         $this->phptype  'ibase';
  80.         $this->dbsyntax 'ibase';
  81.  
  82.         $this->supported['sequences'= true;
  83.         $this->supported['indexes'= true;
  84.         $this->supported['affected_rows'function_exists('ibase_affected_rows');
  85.         $this->supported['summary_functions'= true;
  86.         $this->supported['order_by_text'= true;
  87.         $this->supported['transactions'= true;
  88.         $this->supported['savepoints'= true;
  89.         $this->supported['current_id'= true;
  90.         $this->supported['limit_queries''emulated';
  91.         $this->supported['LOBs'= true;
  92.         $this->supported['replace'= false;
  93.         $this->supported['sub_selects'= true;
  94.         $this->supported['auto_increment'= true;
  95.         $this->supported['primary_key'= true;
  96.         $this->supported['result_introspection'= true;
  97.         $this->supported['prepared_statements'= true;
  98.         $this->supported['identifier_quoting'= false;
  99.         $this->supported['pattern_escaping'= true;
  100.  
  101.         $this->options['DBA_username'= false;
  102.         $this->options['DBA_password'= false;
  103.         $this->options['database_path''';
  104.         $this->options['database_extension''.gdb';
  105.         $this->options['server_version''';
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ errorInfo()
  110.  
  111.     /**
  112.      * This method is used to collect information about an error
  113.      *
  114.      * @param integer $error 
  115.      * @return array 
  116.      * @access public
  117.      */
  118.     function errorInfo($error = null)
  119.     {
  120.         $native_msg @ibase_errmsg();
  121.  
  122.         if (function_exists('ibase_errcode')) {
  123.             $native_code @ibase_errcode();
  124.         else {
  125.             // memo for the interbase php module hackers: we need something similar
  126.             // to mysql_errno() to retrieve error codes instead of this ugly hack
  127.             if (preg_match('/^([^0-9\-]+)([0-9\-]+)\s+(.*)$/'$native_msg$m)) {
  128.                 $native_code = (int)$m[2];
  129.             else {
  130.                 $native_code = null;
  131.             }
  132.         }
  133.         if (is_null($error)) {
  134.             $error = MDB2_ERROR;
  135.             if ($native_code{
  136.                 // try to interpret Interbase error code (that's why we need ibase_errno()
  137.                 // in the interbase module to return the real error code)
  138.                 switch ($native_code{
  139.                 case -204:
  140.                     if (isset($m[3]&& is_int(strpos($m[3]'Table unknown'))) {
  141.                         $errno = MDB2_ERROR_NOSUCHTABLE;
  142.                     }
  143.                 break;
  144.                 default:
  145.                     static $ecode_map;
  146.                     if (empty($ecode_map)) {
  147.                         $ecode_map = array(
  148.                             -104 => MDB2_ERROR_SYNTAX,
  149.                             -150 => MDB2_ERROR_ACCESS_VIOLATION,
  150.                             -151 => MDB2_ERROR_ACCESS_VIOLATION,
  151.                             -155 => MDB2_ERROR_NOSUCHTABLE,
  152.                             -157 => MDB2_ERROR_NOSUCHFIELD,
  153.                             -158 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  154.                             -170 => MDB2_ERROR_MISMATCH,
  155.                             -171 => MDB2_ERROR_MISMATCH,
  156.                             -172 => MDB2_ERROR_INVALID,
  157.                             // -204 =>  // Covers too many errors, need to use regex on msg
  158.                             -205 => MDB2_ERROR_NOSUCHFIELD,
  159.                             -206 => MDB2_ERROR_NOSUCHFIELD,
  160.                             -208 => MDB2_ERROR_INVALID,
  161.                             -219 => MDB2_ERROR_NOSUCHTABLE,
  162.                             -297 => MDB2_ERROR_CONSTRAINT,
  163.                             -303 => MDB2_ERROR_INVALID,
  164.                             -413 => MDB2_ERROR_INVALID_NUMBER,
  165.                             -530 => MDB2_ERROR_CONSTRAINT,
  166.                             -551 => MDB2_ERROR_ACCESS_VIOLATION,
  167.                             -552 => MDB2_ERROR_ACCESS_VIOLATION,
  168.                             // -607 =>  // Covers too many errors, need to use regex on msg
  169.                             -625 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  170.                             -803 => MDB2_ERROR_CONSTRAINT,
  171.                             -804 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  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.                     );
  208.                 }
  209.                 foreach ($error_regexps as $regexp => $code{
  210.                     if (preg_match($regexp$native_msg$m)) {
  211.                         $error $code;
  212.                         break;
  213.                     }
  214.                 }
  215.             }
  216.         }
  217.         return array($error$native_code$native_msg);
  218.     }
  219.  
  220.     // }}}
  221.     // {{{ quoteIdentifier()
  222.  
  223.     /**
  224.      * Delimited identifiers are a nightmare with InterBase, so they're disabled
  225.      *
  226.      * @param string $str  identifier name to be quoted
  227.      * @param bool   $check_option  check the 'quote_identifier' option
  228.      *
  229.      * @return string  quoted identifier string
  230.      *
  231.      * @access public
  232.      */
  233.     function quoteIdentifier($str$check_option = false)
  234.     {
  235.         if ($check_option && !$this->options['quote_identifier']{
  236.             return $str;
  237.         }
  238.         return strtoupper($str);
  239.     }
  240.  
  241.     // }}}
  242.     // {{{ getConnection()
  243.  
  244.     /**
  245.      * Returns a native connection
  246.      *
  247.      * @return  mixed   a valid MDB2 connection object,
  248.      *                   or a MDB2 error object on error
  249.      * @access  public
  250.      */
  251.     function getConnection()
  252.     {
  253.         $result $this->connect();
  254.         if (PEAR::isError($result)) {
  255.             return $result;
  256.         }
  257.         if ($this->in_transaction{
  258.             return $this->transaction_id;
  259.         }
  260.         return $this->connection;
  261.     }
  262.  
  263.     // }}}
  264.     // {{{ beginTransaction()
  265.  
  266.     /**
  267.      * Start a transaction or set a savepoint.
  268.      *
  269.      * @param   string  name of a savepoint to set
  270.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  271.      *
  272.      * @access  public
  273.      */
  274.     function beginTransaction($savepoint = null)
  275.     {
  276.         $this->debug('Starting transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  277.         if (!is_null($savepoint)) {
  278.             if (!$this->in_transaction{
  279.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  280.                     'savepoint cannot be released when changes are auto committed'__FUNCTION__);
  281.             }
  282.             $query 'SAVEPOINT '.$savepoint;
  283.             return $this->_doQuery($querytrue);
  284.         elseif ($this->in_transaction{
  285.             return MDB2_OK;  //nothing to do
  286.         }
  287.         $connection $this->getConnection();
  288.         if (PEAR::isError($connection)) {
  289.             return $connection;
  290.         }
  291.         $result @ibase_trans(IBASE_DEFAULT$connection);
  292.         if (!$result{
  293.             return $this->raiseError(nullnullnull,
  294.                 'could not start a transaction'__FUNCTION__);
  295.         }
  296.         $this->transaction_id = $result;
  297.         $this->in_transaction = true;
  298.         return MDB2_OK;
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ commit()
  303.  
  304.     /**
  305.      * Commit the database changes done during a transaction that is in
  306.      * progress or release a savepoint. This function may only be called when
  307.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  308.      * transaction is implicitly started after committing the pending changes.
  309.      *
  310.      * @param   string  name of a savepoint to release
  311.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  312.      *
  313.      * @access  public
  314.      */
  315.     function commit($savepoint = null)
  316.     {
  317.         $this->debug('Committing transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  318.         if (!$this->in_transaction{
  319.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  320.                 'commit/release savepoint cannot be done changes are auto committed'__FUNCTION__);
  321.         }
  322.         if (!is_null($savepoint)) {
  323.             $query 'RELEASE SAVEPOINT '.$savepoint;
  324.             return $this->_doQuery($querytrue);
  325.         }
  326.  
  327.         if (!@ibase_commit($this->transaction_id)) {
  328.             return $this->raiseError(nullnullnull,
  329.                 'could not commit a transaction'__FUNCTION__);
  330.         }
  331.         $this->in_transaction = false;
  332.         $this->transaction_id = 0;
  333.         return MDB2_OK;
  334.     }
  335.  
  336.     // }}}
  337.     // {{{ rollback()
  338.  
  339.     /**
  340.      * Cancel any database changes done during a transaction or since a specific
  341.      * savepoint that is in progress. This function may only be called when
  342.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  343.      * transaction is implicitly started after canceling the pending changes.
  344.      *
  345.      * @param   string  name of a savepoint to rollback to
  346.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  347.      *
  348.      * @access  public
  349.      */
  350.     function rollback($savepoint = null)
  351.     {
  352.         $this->debug('Rolling back transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  353.         if (!$this->in_transaction{
  354.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  355.                 'rollback cannot be done changes are auto committed'__FUNCTION__);
  356.         }
  357.         if (!is_null($savepoint)) {
  358.             $query 'ROLLBACK TO SAVEPOINT '.$savepoint;
  359.             return $this->_doQuery($querytrue);
  360.         }
  361.  
  362.         if ($this->transaction_id && !@ibase_rollback($this->transaction_id)) {
  363.             return $this->raiseError(nullnullnull,
  364.                 'Could not rollback a pending transaction: '.@ibase_errmsg()__FUNCTION__);
  365.         }
  366.         $this->in_transaction = false;
  367.         $this->transaction_id = 0;
  368.         return MDB2_OK;
  369.     }
  370.  
  371.     // }}}
  372.     // {{{ setTransactionIsolation()
  373.  
  374.     /**
  375.      * Set the transacton isolation level.
  376.      *
  377.      * @param   string  standard isolation level (SQL-92)
  378.      *                   READ UNCOMMITTED (allows dirty reads)
  379.      *                   READ COMMITTED (prevents dirty reads)
  380.      *                   REPEATABLE READ (prevents nonrepeatable reads)
  381.      *                   SERIALIZABLE (prevents phantom reads)
  382.      * @param   array some transaction options:
  383.      *                   'wait' => 'WAIT' | 'NO WAIT'
  384.      *                   'rw'   => 'READ WRITE' | 'READ ONLY'
  385.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  386.      *
  387.      * @access  public
  388.      * @since   2.1.1
  389.      */
  390.     function setTransactionIsolation($isolation$options = array())
  391.     {
  392.         $this->debug('Setting transaction isolation level'__FUNCTION__array('is_manip' => true));
  393.         switch ($isolation{
  394.         case 'READ UNCOMMITTED':
  395.             $ibase_isolation 'READ COMMITTED RECORD_VERSION';
  396.             break;
  397.         case 'READ COMMITTED':
  398.             $ibase_isolation 'READ COMMITTED NO RECORD_VERSION';
  399.             break;
  400.         case 'REPEATABLE READ':
  401.             $ibase_isolation 'SNAPSHOT';
  402.             break;
  403.         case 'SERIALIZABLE':
  404.             $ibase_isolation 'SNAPSHOT TABLE STABILITY';
  405.             break;
  406.         default:
  407.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  408.                 'isolation level is not supported: '.$isolation__FUNCTION__);
  409.         }
  410.  
  411.         if (!empty($options['wait'])) {
  412.             switch ($options['wait']{
  413.             case 'WAIT':
  414.             case 'NO WAIT':
  415.                 $wait $options['wait'];
  416.                 break;
  417.             default:
  418.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  419.                     'wait option is not supported: '.$options['wait']__FUNCTION__);
  420.             }
  421.         }
  422.  
  423.         if (!empty($options['rw'])) {
  424.             switch ($options['rw']{
  425.             case 'READ ONLY':
  426.             case 'READ WRITE':
  427.                 $rw $options['wait'];
  428.                 break;
  429.             default:
  430.                 return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  431.                     'wait option is not supported: '.$options['rw']__FUNCTION__);
  432.             }
  433.         }
  434.  
  435.         $query = "SET TRANSACTION $rw $wait ISOLATION LEVEL $ibase_isolation";
  436.         return $this->_doQuery($querytrue);
  437.     }
  438.  
  439.     // }}}
  440.     // {{{ getDatabaseFile($database_name)
  441.  
  442.     /**
  443.      * Builds the string with path+dbname+extension
  444.      *
  445.      * @return string full database path+file
  446.      * @access protected
  447.      */
  448.     function _getDatabaseFile($database_name)
  449.     {
  450.         if ($database_name == ''{
  451.             return $database_name;
  452.         }
  453.         $ret $this->options['database_path'$database_name;
  454.         if (   strpos($database_name'.fdb'=== false
  455.             && strpos($database_name'.gdb'=== false
  456.         {
  457.             $ret .= $this->options['database_extension'];
  458.         }
  459.         return $ret;
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ _doConnect()
  464.  
  465.     /**
  466.      * Does the grunt work of connecting to the database
  467.      *
  468.      * @return mixed connection resource on success, MDB2 Error Object on failure
  469.      * @access protected
  470.      */
  471.     function _doConnect($database_name$persistent = false)
  472.     {
  473.         $user    $this->dsn['username'];
  474.         $pw      $this->dsn['password'];
  475.         $dbhost  $this->dsn['hostspec'?
  476.             ($this->dsn['hostspec'].':'.$database_name$database_name;
  477.  
  478.         $params = array();
  479.         $params[$dbhost;
  480.         $params[!empty($user$user : null;
  481.         $params[!empty($pw$pw : null;
  482.         $params[= isset($this->dsn['charset']$this->dsn['charset': null;
  483.         $params[= isset($this->dsn['buffers']$this->dsn['buffers': null;
  484.         $params[= isset($this->dsn['dialect']$this->dsn['dialect': null;
  485.         $params[= isset($this->dsn['role'])    $this->dsn['role': null;
  486.  
  487.         $connect_function $persistent 'ibase_pconnect' 'ibase_connect';
  488.  
  489.         $connection @call_user_func_array($connect_function$params);
  490.         if ($connection <= 0{
  491.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  492.                 'unable to establish a connection'__FUNCTION__);
  493.         }
  494.  
  495.         if (function_exists('ibase_timefmt')) {
  496.             @ibase_timefmt("%Y-%m-%d %H:%M:%S"IBASE_TIMESTAMP);
  497.             @ibase_timefmt("%Y-%m-%d"IBASE_DATE);
  498.         else {
  499.             @ini_set("ibase.timestampformat""%Y-%m-%d %H:%M:%S");
  500.             //@ini_set("ibase.timeformat", "%H:%M:%S");
  501.             @ini_set("ibase.dateformat""%Y-%m-%d");
  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.             if (is_int($quote strpos($query"'"$position)) && $quote $p_position{
  812.                 if (!is_int($end_quote strpos($query"'"$quote + 1))) {
  813.                     $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  814.                         'query with an unterminated text string specified'__FUNCTION__);
  815.                     return $err;
  816.                 }
  817.                 switch ($this->escape_quotes{
  818.                 case '':
  819.                 case "'":
  820.                     $position $end_quote + 1;
  821.                     break;
  822.                 default:
  823.                     if ($end_quote == $quote + 1{
  824.                         $position $end_quote + 1;
  825.                     else {
  826.                         if ($query[$end_quote-1== $this->escape_quotes{
  827.                             $position $end_quote;
  828.                         else {
  829.                             $position $end_quote + 1;
  830.                         }
  831.                     }
  832.                     break;
  833.                 }
  834.             elseif ($query[$position== $placeholder_type_guess{
  835.                 if (is_null($placeholder_type)) {
  836.                     $placeholder_type $query[$p_position];
  837.                     $question $colon $placeholder_type;
  838.                 }
  839.                 if ($placeholder_type == ':'{
  840.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  841.                     if ($parameter === ''{
  842.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  843.                             'named parameter with an empty name'__FUNCTION__);
  844.                         return $err;
  845.                     }
  846.                     $positions[$parameter$p_position;
  847.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  848.                 else {
  849.                     $positions[$p_position;
  850.                 }
  851.                 $position $p_position + 1;
  852.             else {
  853.                 $position $p_position;
  854.             }
  855.         }
  856.         $connection $this->getConnection();
  857.         if (PEAR::isError($connection)) {
  858.             return $connection;
  859.         }
  860.         $statement @ibase_prepare($connection$query);
  861.         if (!$statement{
  862.             $err =$this->raiseError(nullnullnull,
  863.                 'Could not create statement'__FUNCTION__);
  864.             return $err;
  865.         }
  866.  
  867.         $class_name 'MDB2_Statement_'.$this->phptype;
  868.         $obj =new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  869.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  870.         return $obj;
  871.     }
  872.  
  873.     // }}}
  874.     // {{{ getSequenceName()
  875.  
  876.     /**
  877.      * adds sequence name formatting to a sequence name
  878.      *
  879.      * @param string $sqn name of the sequence
  880.      * @return string formatted sequence name
  881.      * @access public
  882.      */
  883.     function getSequenceName($sqn)
  884.     {
  885.         return strtoupper(parent::getSequenceName($sqn));
  886.     }
  887.  
  888.     // }}}
  889.     // {{{ nextID()
  890.  
  891.     /**
  892.      * Returns the next free id of a sequence
  893.      *
  894.      * @param string $seq_name name of the sequence
  895.      * @param boolean $ondemand when true the sequence is
  896.      *                           automatic created, if it
  897.      *                           not exists
  898.      * @return mixed MDB2 Error Object or id
  899.      * @access public
  900.      */
  901.     function nextID($seq_name$ondemand = true)
  902.     {
  903.         $sequence_name $this->getSequenceName($seq_name);
  904.         $query 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
  905.         $this->expectError('*');
  906.         $result $this->queryOne($query'integer');
  907.         $this->popExpect();
  908.         if (PEAR::isError($result)) {
  909.             if ($ondemand{
  910.                 $this->loadModule('Manager'nulltrue);
  911.                 // Since we are creating the sequence on demand
  912.                 // we know the first id = 1 so initialize the
  913.                 // sequence at 2
  914.                 $result $this->manager->createSequence($seq_name2);
  915.                 if (PEAR::isError($result)) {
  916.                     return $this->raiseError($resultnullnull,
  917.                         'on demand sequence could not be created'__FUNCTION__);
  918.                 else {
  919.                     // First ID of a newly created sequence is 1
  920.                     // return 1;
  921.                     // BUT generators are not always reset, so return the actual value
  922.                     return $this->currID($seq_name);
  923.                 }
  924.             }
  925.         }
  926.         return $result;
  927.     }
  928.  
  929.     // }}}
  930.     // {{{ currID()
  931.  
  932.     /**
  933.      * Returns the current id of a sequence
  934.      *
  935.      * @param string $seq_name name of the sequence
  936.      * @return mixed MDB2 Error Object or id
  937.      * @access public
  938.      */
  939.     function currID($seq_name)
  940.     {
  941.         $sequence_name $this->getSequenceName($seq_name);
  942.         $query 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
  943.         $value $this->queryOne($query);
  944.         if (PEAR::isError($value)) {
  945.             return $this->raiseError($resultnullnull,
  946.                 'Unable to select from ' $seq_name__FUNCTION__);
  947.         }
  948.         if (!is_numeric($value)) {
  949.             return $this->raiseError(MDB2_ERRORnullnull,
  950.                 'could not find value in sequence table'__FUNCTION__);
  951.         }
  952.         return $value;
  953.     }
  954.  
  955.     // }}}
  956. }
  957.  
  958. /**
  959.  * MDB2 FireBird/InterBase result driver
  960.  *
  961.  * @package MDB2
  962.  * @category Database
  963.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  964.  */
  965. class MDB2_Result_ibase extends MDB2_Result_Common
  966. {
  967.     // {{{ _skipLimitOffset()
  968.  
  969.     /**
  970.      * Skip the first row of a result set.
  971.      *
  972.      * @param resource $result 
  973.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  974.      * @access protected
  975.      */
  976.     function _skipLimitOffset()
  977.     {
  978.         if ($this->db->supports('limit_queries'=== true{
  979.             return true;
  980.         }
  981.         if ($this->limit{
  982.             if ($this->rownum $this->limit{
  983.                 return false;
  984.             }
  985.         }
  986.         if ($this->offset{
  987.             while ($this->offset_count $this->offset{
  988.                 ++$this->offset_count;
  989.                 if (!is_array(@ibase_fetch_row($this->result))) {
  990.                     $this->offset_count $this->offset;
  991.                     return false;
  992.                 }
  993.             }
  994.         }
  995.         return true;
  996.     }
  997.  
  998.     // }}}
  999.     // {{{ fetchRow()
  1000.  
  1001.     /**
  1002.      * Fetch a row and insert the data into an existing array.
  1003.      *
  1004.      * @param int  $fetchmode how the array data should be indexed
  1005.      * @param int  $rownum    number of the row where the data can be found
  1006.      * @return int data array on success, a MDB2 error on failure
  1007.      * @access public
  1008.      */
  1009.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1010.     {
  1011.         if ($this->result === true{
  1012.             //query successfully executed, but without results...
  1013.             $null = null;
  1014.             return $null;
  1015.         }
  1016.         if (!$this->_skipLimitOffset()) {
  1017.             $null = null;
  1018.             return $null;
  1019.         }
  1020.         if (!is_null($rownum)) {
  1021.             $seek $this->seek($rownum);
  1022.             if (PEAR::isError($seek)) {
  1023.                 return $seek;
  1024.             }
  1025.         }
  1026.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1027.             $fetchmode $this->db->fetchmode;
  1028.         }
  1029.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1030.             $row @ibase_fetch_assoc($this->result);
  1031.             if (is_array($row)
  1032.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1033.             {
  1034.                 $row array_change_key_case($row$this->db->options['field_case']);
  1035.             }
  1036.         else {
  1037.             $row @ibase_fetch_row($this->result);
  1038.         }
  1039.         if (!$row{
  1040.             if ($this->result === false{
  1041.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1042.                     'resultset has already been freed'__FUNCTION__);
  1043.                 return $err;
  1044.             }
  1045.             $null = null;
  1046.             return $null;
  1047.         }
  1048.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  1049.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  1050.         {
  1051.             $this->db->_fixResultArrayValues($row$mode);
  1052.         }
  1053.         if (!empty($this->values)) {
  1054.             $this->_assignBindColumns($row);
  1055.         }
  1056.         if (!empty($this->types)) {
  1057.             $row $this->db->datatype->convertResultRow($this->types$row);
  1058.         }
  1059.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1060.             $object_class $this->db->options['fetch_class'];
  1061.             if ($object_class == 'stdClass'{
  1062.                 $row = (object) $row;
  1063.             else {
  1064.                 $row &new $object_class($row);
  1065.             }
  1066.         }
  1067.         ++$this->rownum;
  1068.         return $row;
  1069.     }
  1070.  
  1071.     // }}}
  1072.     // {{{ _getColumnNames()
  1073.  
  1074.     /**
  1075.      * Retrieve the names of columns returned by the DBMS in a query result.
  1076.      *
  1077.      * @return  mixed   Array variable that holds the names of columns as keys
  1078.      *                   or an MDB2 error on failure.
  1079.      *                   Some DBMS may not return any columns when the result set
  1080.      *                   does not contain any rows.
  1081.      * @access private
  1082.      */
  1083.     function _getColumnNames()
  1084.     {
  1085.         $columns = array();
  1086.         $numcols $this->numCols();
  1087.         if (PEAR::isError($numcols)) {
  1088.             return $numcols;
  1089.         }
  1090.         for ($column = 0; $column $numcols$column++{
  1091.             $column_info @ibase_field_info($this->result$column);
  1092.             $columns[$column_info['alias']] $column;
  1093.         }
  1094.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1095.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1096.         }
  1097.         return $columns;
  1098.     }
  1099.  
  1100.     // }}}
  1101.     // {{{ numCols()
  1102.  
  1103.     /**
  1104.      * Count the number of columns returned by the DBMS in a query result.
  1105.      *
  1106.      * @return mixed integer value with the number of columns, a MDB2 error
  1107.      *       on failure
  1108.      * @access public
  1109.      */
  1110.     function numCols()
  1111.     {
  1112.         if ($this->result === true{
  1113.             //query successfully executed, but without results...
  1114.             return 0;
  1115.         }
  1116.  
  1117.         if (!is_resource($this->result)) {
  1118.             return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1119.                 'numCols(): not a valid ibase resource'__FUNCTION__);
  1120.         }
  1121.         $cols @ibase_num_fields($this->result);
  1122.         if (is_null($cols)) {
  1123.             if ($this->result === false{
  1124.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1125.                     'resultset has already been freed'__FUNCTION__);
  1126.             elseif (is_null($this->result)) {
  1127.                 return count($this->types);
  1128.             }
  1129.             return $this->db->raiseError(nullnullnull,
  1130.                 'Could not get column count'__FUNCTION__);
  1131.         }
  1132.         return $cols;
  1133.     }
  1134.  
  1135.     // }}}
  1136.     // {{{ free()
  1137.  
  1138.     /**
  1139.      * Free the internal resources associated with $result.
  1140.      *
  1141.      * @return boolean true on success, false if $result is invalid
  1142.      * @access public
  1143.      */
  1144.     function free()
  1145.     {
  1146.         if (is_resource($this->result&& $this->db->connection{
  1147.             $free @ibase_free_result($this->result);
  1148.             if ($free === false{
  1149.                 return $this->db->raiseError(nullnullnull,
  1150.                     'Could not free result'__FUNCTION__);
  1151.             }
  1152.         }
  1153.         $this->result = false;
  1154.         return MDB2_OK;
  1155.     }
  1156.  
  1157.     // }}}
  1158. }
  1159.  
  1160. /**
  1161.  * MDB2 FireBird/InterBase buffered result driver
  1162.  *
  1163.  * @package MDB2
  1164.  * @category Database
  1165.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1166.  */
  1167. {
  1168.     // {{{ class vars
  1169.  
  1170.     var $buffer;
  1171.     var $buffer_rownum = - 1;
  1172.  
  1173.     // }}}
  1174.     // {{{ _fillBuffer()
  1175.  
  1176.     /**
  1177.      * Fill the row buffer
  1178.      *
  1179.      * @param int $rownum   row number upto which the buffer should be filled
  1180.      *                       if the row number is null all rows are ready into the buffer
  1181.      * @return boolean true on success, false on failure
  1182.      * @access protected
  1183.      */
  1184.     function _fillBuffer($rownum = null)
  1185.     {
  1186.         if (isset($this->buffer&& is_array($this->buffer)) {
  1187.             if (is_null($rownum)) {
  1188.                 if (!end($this->buffer)) {
  1189.                     return false;
  1190.                 }
  1191.             elseif (isset($this->buffer[$rownum])) {
  1192.                 return (bool) $this->buffer[$rownum];
  1193.             }
  1194.         }
  1195.  
  1196.         if (!$this->_skipLimitOffset()) {
  1197.             return false;
  1198.         }
  1199.  
  1200.         $buffer = true;
  1201.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  1202.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  1203.             && ($buffer @ibase_fetch_row($this->result))
  1204.         {
  1205.             ++$this->buffer_rownum;
  1206.             $this->buffer[$this->buffer_rownum$buffer;
  1207.         }
  1208.  
  1209.         if (!$buffer{
  1210.             ++$this->buffer_rownum;
  1211.             $this->buffer[$this->buffer_rownum= false;
  1212.             return false;
  1213.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  1214.             ++$this->buffer_rownum;
  1215.             $this->buffer[$this->buffer_rownum= false;
  1216.         }
  1217.         return true;
  1218.     }
  1219.  
  1220.     // }}}
  1221.     // {{{ fetchRow()
  1222.  
  1223.     /**
  1224.      * Fetch a row and insert the data into an existing array.
  1225.      *
  1226.      * @param int       $fetchmode  how the array data should be indexed
  1227.      * @param int    $rownum    number of the row where the data can be found
  1228.      * @return int data array on success, a MDB2 error on failure
  1229.      * @access public
  1230.      */
  1231.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1232.     {
  1233.         if ($this->result === true || is_null($this->result)) {
  1234.             //query successfully executed, but without results...
  1235.             $null = null;
  1236.             return $null;
  1237.         }
  1238.         if ($this->result === false{
  1239.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1240.                 'resultset has already been freed'__FUNCTION__);
  1241.             return $err;
  1242.         }
  1243.         if (!is_null($rownum)) {
  1244.             $seek $this->seek($rownum);
  1245.             if (PEAR::isError($seek)) {
  1246.                 return $seek;
  1247.             }
  1248.         }
  1249.         $target_rownum $this->rownum + 1;
  1250.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1251.             $fetchmode $this->db->fetchmode;
  1252.         }
  1253.         if (!$this->_fillBuffer($target_rownum)) {
  1254.             $null = null;
  1255.             return $null;
  1256.         }
  1257.         $row $this->buffer[$target_rownum];
  1258.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1259.             $column_names $this->getColumnNames();
  1260.             foreach ($column_names as $name => $i{
  1261.                 $column_names[$name$row[$i];
  1262.             }
  1263.             $row $column_names;
  1264.         }
  1265.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  1266.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  1267.         {
  1268.             $this->db->_fixResultArrayValues($row$mode);
  1269.         }
  1270.         if (!empty($this->values)) {
  1271.             $this->_assignBindColumns($row);
  1272.         }
  1273.         if (!empty($this->types)) {
  1274.             $row $this->db->datatype->convertResultRow($this->types$row);
  1275.         }
  1276.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1277.             $object_class $this->db->options['fetch_class'];
  1278.             if ($object_class == 'stdClass'{
  1279.                 $row = (object) $row;
  1280.             else {
  1281.                 $row &new $object_class($row);
  1282.             }
  1283.         }
  1284.         ++$this->rownum;
  1285.         return $row;
  1286.     }
  1287.  
  1288.     // }}}
  1289.     // {{{ seek()
  1290.  
  1291.     /**
  1292.      * Seek to a specific row in a result set
  1293.      *
  1294.      * @param int    $rownum    number of the row where the data can be found
  1295.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1296.      * @access public
  1297.      */
  1298.     function seek($rownum = 0)
  1299.     {
  1300.         if ($this->result === false{
  1301.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1302.                 'resultset has already been freed'__FUNCTION__);
  1303.         }
  1304.         $this->rownum $rownum - 1;
  1305.         return MDB2_OK;
  1306.     }
  1307.  
  1308.     // }}}
  1309.     // {{{ valid()
  1310.  
  1311.     /**
  1312.      * Check if the end of the result set has been reached
  1313.      *
  1314.      * @return mixed true or false on sucess, a MDB2 error on failure
  1315.      * @access public
  1316.      */
  1317.     function valid()
  1318.     {
  1319.         if ($this->result === false{
  1320.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1321.                 'resultset has already been freed'__FUNCTION__);
  1322.         elseif (is_null($this->result)) {
  1323.             return true;
  1324.         }
  1325.         if ($this->_fillBuffer($this->rownum + 1)) {
  1326.             return true;
  1327.         }
  1328.         return false;
  1329.     }
  1330.  
  1331.     // }}}
  1332.     // {{{ numRows()
  1333.  
  1334.     /**
  1335.      * Returns the number of rows in a result object
  1336.      *
  1337.      * @return mixed MDB2 Error Object or the number of rows
  1338.      * @access public
  1339.      */
  1340.     function numRows()
  1341.     {
  1342.         if ($this->result === false{
  1343.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1344.                 'resultset has already been freed'__FUNCTION__);
  1345.         elseif (is_null($this->result)) {
  1346.             return 0;
  1347.         }
  1348.         $this->_fillBuffer();
  1349.         return $this->buffer_rownum;
  1350.     }
  1351.  
  1352.     // }}}
  1353.     // {{{ free()
  1354.  
  1355.     /**
  1356.      * Free the internal resources associated with $result.
  1357.      *
  1358.      * @return boolean true on success, false if $result is invalid
  1359.      * @access public
  1360.      */
  1361.     function free()
  1362.     {
  1363.         $this->buffer = null;
  1364.         $this->buffer_rownum = null;
  1365.         return parent::free();
  1366.     }
  1367.  
  1368.     // }}}
  1369. }
  1370.  
  1371. /**
  1372.  * MDB2 FireBird/InterBase statement driver
  1373.  *
  1374.  * @package MDB2
  1375.  * @category Database
  1376.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1377.  */
  1378. class MDB2_Statement_ibase extends MDB2_Statement_Common
  1379. {
  1380.     // {{{ _execute()
  1381.  
  1382.     /**
  1383.      * Execute a prepared query statement helper method.
  1384.      *
  1385.      * @param mixed $result_class string which specifies which result class to use
  1386.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1387.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1388.      * @access private
  1389.      */
  1390.     function &_execute($result_class = true$result_wrap_class = false)
  1391.     {
  1392.         if (is_null($this->statement)) {
  1393.             $result =parent::_execute($result_class$result_wrap_class);
  1394.             return $result;
  1395.         }
  1396.         $this->db->last_query = $this->query;
  1397.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1398.         if ($this->db->getOption('disable_query')) {
  1399.             if ($this->is_manip{
  1400.                 $return = 0;
  1401.                 return $return;
  1402.             }
  1403.             $null = null;
  1404.             return $null;
  1405.         }
  1406.  
  1407.         $connection $this->db->getConnection();
  1408.         if (PEAR::isError($connection)) {
  1409.             return $connection;
  1410.         }
  1411.  
  1412.         $parameters = array(0 => $this->statement);
  1413.         foreach ($this->positions as $parameter => $current_position{
  1414.             if (!array_key_exists($parameter$this->values)) {
  1415.                 return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1416.                     'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1417.             }
  1418.             $value $this->values[$parameter];
  1419.             $type !empty($this->types[$parameter]$this->types[$parameter: null;
  1420.             $parameters[$this->db->quote($value$typefalse);
  1421.         }
  1422.  
  1423.         $result @call_user_func_array('ibase_execute'$parameters);
  1424.         if ($result === false{
  1425.             $err =$this->db->raiseError(nullnullnull,
  1426.                 'Could not execute statement'__FUNCTION__);
  1427.             return $err;
  1428.         }
  1429.  
  1430.         if ($this->is_manip{
  1431.             $affected_rows $this->db->_affectedRows($connection);
  1432.             return $affected_rows;
  1433.         }
  1434.  
  1435.         $result =$this->db->_wrapResult($result$this->result_types,
  1436.             $result_class$result_wrap_class$this->limit$this->offset);
  1437.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1438.         return $result;
  1439.     }
  1440.  
  1441.     // }}}
  1442.  
  1443.     // }}}
  1444.     // {{{ free()
  1445.  
  1446.     /**
  1447.      * Release resources allocated for the specified prepared query.
  1448.      *
  1449.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1450.      * @access public
  1451.      */
  1452.     function free()
  1453.     {
  1454.         if (is_null($this->positions)) {
  1455.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1456.                 'Prepared statement has already been freed'__FUNCTION__);
  1457.         }
  1458.         $result = MDB2_OK;
  1459.  
  1460.         if (!is_null($this->statement&& !@ibase_free_query($this->statement)) {
  1461.             $result $this->db->raiseError(nullnullnull,
  1462.                 'Could not free statement'__FUNCTION__);
  1463.         }
  1464.  
  1465.         parent::free();
  1466.         return $result;
  1467.     }
  1468. }
  1469. ?>

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