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

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