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

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