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.219 2008/03/08 14:18:38 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.             $connection $this->_doConnect($this->dsn['username'],
  541.                                             $this->dsn['password'],
  542.                                             $this->database_name,
  543.                                             $this->options['persistent']);
  544.             if (PEAR::isError($connection)) {
  545.                 return $connection;
  546.             }
  547.             $this->connection =$connection;
  548.             $this->connected_dsn $this->dsn;
  549.             $this->connected_database_name $database_file;
  550.             $this->opened_persistent $this->options['persistent'];
  551.             $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  552.             $this->supported['limit_queries'($this->dbsyntax == 'firebird'? true : 'emulated';
  553.         }
  554.         return MDB2_OK;
  555.     }
  556.  
  557.     // }}}
  558.     // {{{ databaseExists()
  559.  
  560.     /**
  561.      * check if given database name is exists?
  562.      *
  563.      * @param string $name    name of the database that should be checked
  564.      *
  565.      * @return mixed true/false on success, a MDB2 error on failure
  566.      * @access public
  567.      */
  568.     function databaseExists($name)
  569.     {
  570.         $database_file $this->_getDatabaseFile($name);
  571.         $result file_exists($database_file);
  572.         return $result;
  573.     }
  574.  
  575.     // }}}
  576.     // {{{ disconnect()
  577.  
  578.     /**
  579.      * Log out and disconnect from the database.
  580.      *
  581.      * @param  boolean $force if the disconnect should be forced even if the
  582.      *                         connection is opened persistently
  583.      * @return mixed true on success, false if not connected and error
  584.      *                 object on error
  585.      * @access public
  586.      */
  587.     function disconnect($force = true)
  588.     {
  589.         if (is_resource($this->connection)) {
  590.             if ($this->in_transaction{
  591.                 $dsn $this->dsn;
  592.                 $database_name $this->database_name;
  593.                 $persistent $this->options['persistent'];
  594.                 $this->dsn $this->connected_dsn;
  595.                 $this->database_name $this->connected_database_name;
  596.                 $this->options['persistent'$this->opened_persistent;
  597.                 $this->rollback();
  598.                 $this->dsn $dsn;
  599.                 $this->database_name $database_name;
  600.                 $this->options['persistent'$persistent;
  601.             }
  602.  
  603.             if (!$this->opened_persistent || $force{
  604.                 @ibase_close($this->connection);
  605.             }
  606.         }
  607.         return parent::disconnect($force);
  608.     }
  609.  
  610.     // }}}
  611.     // {{{ standaloneQuery()
  612.  
  613.    /**
  614.      * execute a query as DBA
  615.      *
  616.      * @param string $query the SQL query
  617.      * @param mixed   $types  array that contains the types of the columns in
  618.      *                         the result set
  619.      * @param boolean $is_manip  if the query is a manipulation query
  620.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  621.      * @access public
  622.      */
  623.     function &standaloneQuery($query$types = null$is_manip = false)
  624.     {
  625.         $user $this->options['DBA_username']$this->options['DBA_username'$this->dsn['username'];
  626.         $pass $this->options['DBA_password']$this->options['DBA_password'$this->dsn['password'];
  627.         $connection $this->_doConnect($user$pass$this->database_name$this->options['persistent']);
  628.         if (PEAR::isError($connection)) {
  629.             return $connection;
  630.         }
  631.  
  632.         $offset $this->offset;
  633.         $limit $this->limit;
  634.         $this->offset $this->limit = 0;
  635.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  636.         
  637.         $result =$this->_doQuery($query$is_manip$connection);
  638.         if (!PEAR::isError($result)) {
  639.             $result $this->_affectedRows($connection$result);
  640.         }
  641.  
  642.         @mysql_close($connection);
  643.         return $result;
  644.     }
  645.  
  646.     // }}}
  647.     // {{{ _doQuery()
  648.  
  649.     /**
  650.      * Execute a query
  651.      * @param string $query  query
  652.      * @param boolean $is_manip  if the query is a manipulation query
  653.      * @param resource $connection 
  654.      * @param string $database_name 
  655.      * @return result or error object
  656.      * @access protected
  657.      */
  658.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  659.     {
  660.         $this->last_query $query;
  661.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  662.         if ($result{
  663.             if (PEAR::isError($result)) {
  664.                 return $result;
  665.             }
  666.             $query $result;
  667.         }
  668.         if ($this->getOption('disable_query')) {
  669.             if ($is_manip{
  670.                 return 0;
  671.             }
  672.             return null;
  673.         }
  674.  
  675.         if (is_null($connection)) {
  676.             $connection $this->getConnection();
  677.             if (PEAR::isError($connection)) {
  678.                 return $connection;
  679.             }
  680.         }
  681.         $result @ibase_query($connection$query);
  682.  
  683.         if ($result === false{
  684.             $err =$this->raiseError(nullnullnull,
  685.                 'Could not execute statement'__FUNCTION__);
  686.             return $err;
  687.         }
  688.  
  689.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  690.         return $result;
  691.     }
  692.  
  693.     // }}}
  694.     // {{{ _affectedRows()
  695.  
  696.     /**
  697.      * Returns the number of rows affected
  698.      *
  699.      * @param resource $result 
  700.      * @param resource $connection 
  701.      * @return mixed MDB2 Error Object or the number of rows affected
  702.      * @access private
  703.      */
  704.     function _affectedRows($connection$result = null)
  705.     {
  706.         if (is_null($connection)) {
  707.             $connection $this->getConnection();
  708.             if (PEAR::isError($connection)) {
  709.                 return $connection;
  710.             }
  711.         }
  712.         return (function_exists('ibase_affected_rows'@ibase_affected_rows($connection: 0);
  713.     }
  714.  
  715.     // }}}
  716.     // {{{ _modifyQuery()
  717.  
  718.     /**
  719.      * Changes a query string for various DBMS specific reasons
  720.      *
  721.      * @param string $query  query to modify
  722.      * @param boolean $is_manip  if it is a DML query
  723.      * @param integer $limit  limit the number of rows
  724.      * @param integer $offset  start reading from given offset
  725.      * @return string modified query
  726.      * @access protected
  727.      */
  728.     function _modifyQuery($query$is_manip$limit$offset)
  729.     {
  730.         if ($limit > 0 && $this->supports('limit_queries'=== true{
  731.             $query preg_replace('/^([\s(])*SELECT(?!\s*FIRST\s*\d+)/i',
  732.                 "SELECT FIRST $limit SKIP $offset"$query);
  733.         }
  734.         return $query;
  735.     }
  736.  
  737.     // }}}
  738.     // {{{ getServerVersion()
  739.  
  740.     /**
  741.      * return version information about the server
  742.      *
  743.      * @param bool   $native  determines if the raw version string should be returned
  744.      * @return mixed array/string with version information or MDB2 error object
  745.      * @access public
  746.      */
  747.     function getServerVersion($native = false)
  748.     {
  749.         $server_info = false;
  750.         if ($this->connected_server_info{
  751.             $server_info $this->connected_server_info;
  752.         elseif ($this->options['server_version']{
  753.             $server_info $this->options['server_version'];
  754.         else {
  755.             $username $this->options['DBA_username'$this->options['DBA_username'$this->dsn['username'];
  756.             $password $this->options['DBA_password'$this->options['DBA_password'$this->dsn['password'];
  757.             $ibserv @ibase_service_attach($this->dsn['hostspec']$username$password);
  758.             $server_info @ibase_server_info($ibservIBASE_SVC_SERVER_VERSION);
  759.             @ibase_service_detach($ibserv);
  760.         }
  761.         if (!$server_info{
  762.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  763.                 'Requires either "server_version" or "DBA_username"/"DBA_password" option'__FUNCTION__);
  764.         }
  765.         // cache server_info
  766.         $this->connected_server_info $server_info;
  767.         if (!$native{
  768.             //WI-V1.5.3.4854 Firebird 1.5
  769.             //WI-T2.1.0.16780 Firebird 2.1 Beta 2
  770.             if (!preg_match('/-[VT]([\d\.]*)/'$server_info$matches)) {
  771.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  772.                     'Could not parse version information:'.$server_info__FUNCTION__);
  773.             }
  774.             $tmp explode('.'$matches[1]4);
  775.             $server_info = array(
  776.                 'major' => isset($tmp[0]$tmp[0: null,
  777.                 'minor' => isset($tmp[1]$tmp[1: null,
  778.                 'patch' => isset($tmp[2]$tmp[2: null,
  779.                 'extra' => isset($tmp[3]$tmp[3: null,
  780.                 'native' => $server_info,
  781.             );
  782.         }
  783.         return $server_info;
  784.     }
  785.  
  786.     // }}}
  787.     // {{{ prepare()
  788.  
  789.     /**
  790.      * Prepares a query for multiple execution with execute().
  791.      * With some database backends, this is emulated.
  792.      * prepare() requires a generic query as string like
  793.      * 'INSERT INTO numbers VALUES(?,?)' or
  794.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  795.      * The ? and :name and are placeholders which can be set using
  796.      * bindParam() and the query can be sent off using the execute() method.
  797.      * The allowed format for :name can be set with the 'bindname_format' option.
  798.      *
  799.      * @param string $query the query to prepare
  800.      * @param mixed   $types  array that contains the types of the placeholders
  801.      * @param mixed   $result_types  array that contains the types of the columns in
  802.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  803.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  804.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  805.      * @return mixed resource handle for the prepared query on success, a MDB2
  806.      *         error on failure
  807.      * @access public
  808.      * @see bindParam, execute
  809.      */
  810.     function &prepare($query$types = null$result_types = null$lobs = array())
  811.     {
  812.         if ($this->options['emulate_prepared']{
  813.             $obj =parent::prepare($query$types$result_types$lobs);
  814.             return $obj;
  815.         }
  816.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  817.         $offset $this->offset;
  818.         $limit  $this->limit;
  819.         $this->offset $this->limit = 0;
  820.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  821.         if ($result{
  822.             if (PEAR::isError($result)) {
  823.                 return $result;
  824.             }
  825.             $query $result;
  826.         }
  827.         $placeholder_type_guess $placeholder_type = null;
  828.         $question '?';
  829.         $colon ':';
  830.         $positions = array();
  831.         $position = 0;
  832.         while ($position strlen($query)) {
  833.             $q_position strpos($query$question$position);
  834.             $c_position strpos($query$colon$position);
  835.             if ($q_position && $c_position{
  836.                 $p_position min($q_position$c_position);
  837.             elseif ($q_position{
  838.                 $p_position $q_position;
  839.             elseif ($c_position{
  840.                 $p_position $c_position;
  841.             else {
  842.                 break;
  843.             }
  844.             if (is_null($placeholder_type)) {
  845.                 $placeholder_type_guess $query[$p_position];
  846.             }
  847.             
  848.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  849.             if (PEAR::isError($new_pos)) {
  850.                 return $new_pos;
  851.             }
  852.             if ($new_pos != $position{
  853.                 $position $new_pos;
  854.                 continue; //evaluate again starting from the new position
  855.             }
  856.             
  857.             if ($query[$position== $placeholder_type_guess{
  858.                 if (is_null($placeholder_type)) {
  859.                     $placeholder_type $query[$p_position];
  860.                     $question $colon $placeholder_type;
  861.                 }
  862.                 if ($placeholder_type == ':'{
  863.                     $regexp '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
  864.                     $parameter preg_replace($regexp'\\1'$query);
  865.                     if ($parameter === ''{
  866.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  867.                             'named parameter name must match "bindname_format" option'__FUNCTION__);
  868.                         return $err;
  869.                     }
  870.                     $positions[$parameter;
  871.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  872.                 else {
  873.                     $positions[count($positions);
  874.                 }
  875.                 $position $p_position + 1;
  876.             else {
  877.                 $position $p_position;
  878.             }
  879.         }
  880.         $connection $this->getConnection();
  881.         if (PEAR::isError($connection)) {
  882.             return $connection;
  883.         }
  884.         $statement @ibase_prepare($connection$query);
  885.         if (!$statement{
  886.             $err =$this->raiseError(nullnullnull,
  887.                 'Could not create statement'__FUNCTION__);
  888.             return $err;
  889.         }
  890.  
  891.         $class_name 'MDB2_Statement_'.$this->phptype;
  892.         $obj = new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  893.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  894.         return $obj;
  895.     }
  896.  
  897.     // }}}
  898.     // {{{ getSequenceName()
  899.  
  900.     /**
  901.      * adds sequence name formatting to a sequence name
  902.      *
  903.      * @param string $sqn name of the sequence
  904.      * @return string formatted sequence name
  905.      * @access public
  906.      */
  907.     function getSequenceName($sqn)
  908.     {
  909.         return strtoupper(parent::getSequenceName($sqn));
  910.     }
  911.  
  912.     // }}}
  913.     // {{{ nextID()
  914.  
  915.     /**
  916.      * Returns the next free id of a sequence
  917.      *
  918.      * @param string $seq_name name of the sequence
  919.      * @param boolean $ondemand when true the sequence is
  920.      *                           automatic created, if it
  921.      *                           not exists
  922.      * @return mixed MDB2 Error Object or id
  923.      * @access public
  924.      */
  925.     function nextID($seq_name$ondemand = true)
  926.     {
  927.         $sequence_name $this->getSequenceName($seq_name);
  928.         $query 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
  929.         $this->pushErrorHandling(PEAR_ERROR_RETURN);
  930.         $this->expectError('*');
  931.         $result $this->queryOne($query'integer');
  932.         $this->popExpect();
  933.         $this->popErrorHandling();
  934.         if (PEAR::isError($result)) {
  935.             if ($ondemand{
  936.                 $this->loadModule('Manager'nulltrue);
  937.                 $result $this->manager->createSequence($seq_name);
  938.                 if (PEAR::isError($result)) {
  939.                     return $this->raiseError($resultnullnull,
  940.                         'on demand sequence could not be created'__FUNCTION__);
  941.                 else {
  942.                     return $this->nextID($seq_namefalse);
  943.                 }
  944.             }
  945.         }
  946.         return $result;
  947.     }
  948.  
  949.     // }}}
  950.     // {{{ lastInsertID()
  951.  
  952.     /**
  953.      * Returns the autoincrement ID if supported or $id or fetches the current
  954.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  955.      *
  956.      * @param string $table name of the table into which a new row was inserted
  957.      * @param string $field name of the field into which a new row was inserted
  958.      * @return mixed MDB2 Error Object or id
  959.      * @access public
  960.      */
  961.     function lastInsertID($table = null$field = null)
  962.     {
  963.         $seq $table.(empty($field'' '_'.$field);
  964.         return $this->currID($seq);
  965.     }
  966.     
  967.     // }}}
  968.     // {{{ currID()
  969.  
  970.     /**
  971.      * Returns the current id of a sequence
  972.      *
  973.      * @param string $seq_name name of the sequence
  974.      * @return mixed MDB2 Error Object or id
  975.      * @access public
  976.      */
  977.     function currID($seq_name)
  978.     {
  979.         $sequence_name $this->getSequenceName($seq_name);
  980.         $query 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
  981.         $value $this->queryOne($query);
  982.         if (PEAR::isError($value)) {
  983.             return $this->raiseError($valuenullnull,
  984.                 'Unable to select from ' $seq_name__FUNCTION__);
  985.         }
  986.         if (!is_numeric($value)) {
  987.             return $this->raiseError(MDB2_ERRORnullnull,
  988.                 'could not find value in sequence table'__FUNCTION__);
  989.         }
  990.         return $value;
  991.     }
  992.  
  993.     // }}}
  994. }
  995.  
  996. /**
  997.  * MDB2 FireBird/InterBase result driver
  998.  *
  999.  * @package MDB2
  1000.  * @category Database
  1001.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1002.  */
  1003. class MDB2_Result_ibase extends MDB2_Result_Common
  1004. {
  1005.     // {{{ _skipLimitOffset()
  1006.  
  1007.     /**
  1008.      * Skip the first row of a result set.
  1009.      *
  1010.      * @param resource $result 
  1011.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1012.      * @access protected
  1013.      */
  1014.     function _skipLimitOffset()
  1015.     {
  1016.         if ($this->db->supports('limit_queries'=== true{
  1017.             return true;
  1018.         }
  1019.         if ($this->limit{
  1020.             if ($this->rownum $this->limit{
  1021.                 return false;
  1022.             }
  1023.         }
  1024.         if ($this->offset{
  1025.             while ($this->offset_count $this->offset{
  1026.                 ++$this->offset_count;
  1027.                 if (!is_array(@ibase_fetch_row($this->result))) {
  1028.                     $this->offset_count $this->offset;
  1029.                     return false;
  1030.                 }
  1031.             }
  1032.         }
  1033.         return true;
  1034.     }
  1035.  
  1036.     // }}}
  1037.     // {{{ fetchRow()
  1038.  
  1039.     /**
  1040.      * Fetch a row and insert the data into an existing array.
  1041.      *
  1042.      * @param int  $fetchmode how the array data should be indexed
  1043.      * @param int  $rownum    number of the row where the data can be found
  1044.      * @return int data array on success, a MDB2 error on failure
  1045.      * @access public
  1046.      */
  1047.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1048.     {
  1049.         if ($this->result === true{
  1050.             //query successfully executed, but without results...
  1051.             $null = null;
  1052.             return $null;
  1053.         }
  1054.         if (!$this->_skipLimitOffset()) {
  1055.             $null = null;
  1056.             return $null;
  1057.         }
  1058.         if (!is_null($rownum)) {
  1059.             $seek $this->seek($rownum);
  1060.             if (PEAR::isError($seek)) {
  1061.                 return $seek;
  1062.             }
  1063.         }
  1064.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1065.             $fetchmode $this->db->fetchmode;
  1066.         }
  1067.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1068.             $row @ibase_fetch_assoc($this->result);
  1069.             if (is_array($row)
  1070.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1071.             {
  1072.                 $row array_change_key_case($row$this->db->options['field_case']);
  1073.             }
  1074.         else {
  1075.             $row @ibase_fetch_row($this->result);
  1076.         }
  1077.         if (!$row{
  1078.             if ($this->result === false{
  1079.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1080.                     'resultset has already been freed'__FUNCTION__);
  1081.                 return $err;
  1082.             }
  1083.             $null = null;
  1084.             return $null;
  1085.         }
  1086.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1087.         $rtrim = false;
  1088.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1089.             if (empty($this->types)) {
  1090.                 $mode += MDB2_PORTABILITY_RTRIM;
  1091.             else {
  1092.                 $rtrim = true;
  1093.             }
  1094.         }
  1095.         if ($mode{
  1096.             $this->db->_fixResultArrayValues($row$mode);
  1097.         }
  1098.         if (!empty($this->types)) {
  1099.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1100.         }
  1101.         if (!empty($this->values)) {
  1102.             $this->_assignBindColumns($row);
  1103.         }
  1104.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1105.             $object_class $this->db->options['fetch_class'];
  1106.             if ($object_class == 'stdClass'{
  1107.                 $row = (object) $row;
  1108.             else {
  1109.                 $row &new $object_class($row);
  1110.             }
  1111.         }
  1112.         ++$this->rownum;
  1113.         return $row;
  1114.     }
  1115.  
  1116.     // }}}
  1117.     // {{{ _getColumnNames()
  1118.  
  1119.     /**
  1120.      * Retrieve the names of columns returned by the DBMS in a query result.
  1121.      *
  1122.      * @return  mixed   Array variable that holds the names of columns as keys
  1123.      *                   or an MDB2 error on failure.
  1124.      *                   Some DBMS may not return any columns when the result set
  1125.      *                   does not contain any rows.
  1126.      * @access private
  1127.      */
  1128.     function _getColumnNames()
  1129.     {
  1130.         $columns = array();
  1131.         $numcols $this->numCols();
  1132.         if (PEAR::isError($numcols)) {
  1133.             return $numcols;
  1134.         }
  1135.         for ($column = 0; $column $numcols$column++{
  1136.             $column_info @ibase_field_info($this->result$column);
  1137.             $columns[$column_info['alias']] $column;
  1138.         }
  1139.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1140.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1141.         }
  1142.         return $columns;
  1143.     }
  1144.  
  1145.     // }}}
  1146.     // {{{ numCols()
  1147.  
  1148.     /**
  1149.      * Count the number of columns returned by the DBMS in a query result.
  1150.      *
  1151.      * @return mixed integer value with the number of columns, a MDB2 error
  1152.      *       on failure
  1153.      * @access public
  1154.      */
  1155.     function numCols()
  1156.     {
  1157.         if ($this->result === true{
  1158.             //query successfully executed, but without results...
  1159.             return 0;
  1160.         }
  1161.  
  1162.         if (!is_resource($this->result)) {
  1163.             return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1164.                 'numCols(): not a valid ibase resource'__FUNCTION__);
  1165.         }
  1166.         $cols @ibase_num_fields($this->result);
  1167.         if (is_null($cols)) {
  1168.             if ($this->result === false{
  1169.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1170.                     'resultset has already been freed'__FUNCTION__);
  1171.             elseif (is_null($this->result)) {
  1172.                 return count($this->types);
  1173.             }
  1174.             return $this->db->raiseError(nullnullnull,
  1175.                 'Could not get column count'__FUNCTION__);
  1176.         }
  1177.         return $cols;
  1178.     }
  1179.  
  1180.     // }}}
  1181.     // {{{ free()
  1182.  
  1183.     /**
  1184.      * Free the internal resources associated with $result.
  1185.      *
  1186.      * @return boolean true on success, false if $result is invalid
  1187.      * @access public
  1188.      */
  1189.     function free()
  1190.     {
  1191.         if (is_resource($this->result&& $this->db->connection{
  1192.             $free @ibase_free_result($this->result);
  1193.             if ($free === false{
  1194.                 return $this->db->raiseError(nullnullnull,
  1195.                     'Could not free result'__FUNCTION__);
  1196.             }
  1197.         }
  1198.         $this->result = false;
  1199.         return MDB2_OK;
  1200.     }
  1201.  
  1202.     // }}}
  1203. }
  1204.  
  1205. /**
  1206.  * MDB2 FireBird/InterBase buffered result driver
  1207.  *
  1208.  * @package MDB2
  1209.  * @category Database
  1210.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1211.  */
  1212. {
  1213.     // {{{ class vars
  1214.  
  1215.     var $buffer;
  1216.     var $buffer_rownum = - 1;
  1217.  
  1218.     // }}}
  1219.     // {{{ _fillBuffer()
  1220.  
  1221.     /**
  1222.      * Fill the row buffer
  1223.      *
  1224.      * @param int $rownum   row number upto which the buffer should be filled
  1225.      *                       if the row number is null all rows are ready into the buffer
  1226.      * @return boolean true on success, false on failure
  1227.      * @access protected
  1228.      */
  1229.     function _fillBuffer($rownum = null)
  1230.     {
  1231.         if (isset($this->buffer&& is_array($this->buffer)) {
  1232.             if (is_null($rownum)) {
  1233.                 if (!end($this->buffer)) {
  1234.                     return false;
  1235.                 }
  1236.             elseif (isset($this->buffer[$rownum])) {
  1237.                 return (bool) $this->buffer[$rownum];
  1238.             }
  1239.         }
  1240.  
  1241.         if (!$this->_skipLimitOffset()) {
  1242.             return false;
  1243.         }
  1244.  
  1245.         $buffer = true;
  1246.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  1247.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  1248.             && ($buffer @ibase_fetch_row($this->result))
  1249.         {
  1250.             ++$this->buffer_rownum;
  1251.             $this->buffer[$this->buffer_rownum$buffer;
  1252.         }
  1253.  
  1254.         if (!$buffer{
  1255.             ++$this->buffer_rownum;
  1256.             $this->buffer[$this->buffer_rownum= false;
  1257.             return false;
  1258.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  1259.             ++$this->buffer_rownum;
  1260.             $this->buffer[$this->buffer_rownum= false;
  1261.         }
  1262.         return true;
  1263.     }
  1264.  
  1265.     // }}}
  1266.     // {{{ fetchRow()
  1267.  
  1268.     /**
  1269.      * Fetch a row and insert the data into an existing array.
  1270.      *
  1271.      * @param int       $fetchmode  how the array data should be indexed
  1272.      * @param int    $rownum    number of the row where the data can be found
  1273.      * @return int data array on success, a MDB2 error on failure
  1274.      * @access public
  1275.      */
  1276.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1277.     {
  1278.         if ($this->result === true || is_null($this->result)) {
  1279.             //query successfully executed, but without results...
  1280.             $null = null;
  1281.             return $null;
  1282.         }
  1283.         if ($this->result === false{
  1284.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1285.                 'resultset has already been freed'__FUNCTION__);
  1286.             return $err;
  1287.         }
  1288.         if (!is_null($rownum)) {
  1289.             $seek $this->seek($rownum);
  1290.             if (PEAR::isError($seek)) {
  1291.                 return $seek;
  1292.             }
  1293.         }
  1294.         $target_rownum $this->rownum + 1;
  1295.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1296.             $fetchmode $this->db->fetchmode;
  1297.         }
  1298.         if (!$this->_fillBuffer($target_rownum)) {
  1299.             $null = null;
  1300.             return $null;
  1301.         }
  1302.         $row $this->buffer[$target_rownum];
  1303.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1304.             $column_names $this->getColumnNames();
  1305.             foreach ($column_names as $name => $i{
  1306.                 $column_names[$name$row[$i];
  1307.             }
  1308.             $row $column_names;
  1309.         }
  1310.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  1311.         $rtrim = false;
  1312.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1313.             if (empty($this->types)) {
  1314.                 $mode += MDB2_PORTABILITY_RTRIM;
  1315.             else {
  1316.                 $rtrim = true;
  1317.             }
  1318.         }
  1319.         if ($mode{
  1320.             $this->db->_fixResultArrayValues($row$mode);
  1321.         }
  1322.         if (!empty($this->types)) {
  1323.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1324.         }
  1325.         if (!empty($this->values)) {
  1326.             $this->_assignBindColumns($row);
  1327.         }
  1328.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1329.             $object_class $this->db->options['fetch_class'];
  1330.             if ($object_class == 'stdClass'{
  1331.                 $row = (object) $row;
  1332.             else {
  1333.                 $row &new $object_class($row);
  1334.             }
  1335.         }
  1336.         ++$this->rownum;
  1337.         return $row;
  1338.     }
  1339.  
  1340.     // }}}
  1341.     // {{{ seek()
  1342.  
  1343.     /**
  1344.      * Seek to a specific row in a result set
  1345.      *
  1346.      * @param int    $rownum    number of the row where the data can be found
  1347.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1348.      * @access public
  1349.      */
  1350.     function seek($rownum = 0)
  1351.     {
  1352.         if ($this->result === false{
  1353.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1354.                 'resultset has already been freed'__FUNCTION__);
  1355.         }
  1356.         $this->rownum $rownum - 1;
  1357.         return MDB2_OK;
  1358.     }
  1359.  
  1360.     // }}}
  1361.     // {{{ valid()
  1362.  
  1363.     /**
  1364.      * Check if the end of the result set has been reached
  1365.      *
  1366.      * @return mixed true or false on sucess, a MDB2 error on failure
  1367.      * @access public
  1368.      */
  1369.     function valid()
  1370.     {
  1371.         if ($this->result === false{
  1372.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1373.                 'resultset has already been freed'__FUNCTION__);
  1374.         elseif (is_null($this->result)) {
  1375.             return true;
  1376.         }
  1377.         if ($this->_fillBuffer($this->rownum + 1)) {
  1378.             return true;
  1379.         }
  1380.         return false;
  1381.     }
  1382.  
  1383.     // }}}
  1384.     // {{{ numRows()
  1385.  
  1386.     /**
  1387.      * Returns the number of rows in a result object
  1388.      *
  1389.      * @return mixed MDB2 Error Object or the number of rows
  1390.      * @access public
  1391.      */
  1392.     function numRows()
  1393.     {
  1394.         if ($this->result === false{
  1395.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1396.                 'resultset has already been freed'__FUNCTION__);
  1397.         elseif (is_null($this->result)) {
  1398.             return 0;
  1399.         }
  1400.         $this->_fillBuffer();
  1401.         return $this->buffer_rownum;
  1402.     }
  1403.  
  1404.     // }}}
  1405.     // {{{ free()
  1406.  
  1407.     /**
  1408.      * Free the internal resources associated with $result.
  1409.      *
  1410.      * @return boolean true on success, false if $result is invalid
  1411.      * @access public
  1412.      */
  1413.     function free()
  1414.     {
  1415.         $this->buffer = null;
  1416.         $this->buffer_rownum = null;
  1417.         return parent::free();
  1418.     }
  1419.  
  1420.     // }}}
  1421. }
  1422.  
  1423. /**
  1424.  * MDB2 FireBird/InterBase statement driver
  1425.  *
  1426.  * @package MDB2
  1427.  * @category Database
  1428.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  1429.  */
  1430. class MDB2_Statement_ibase extends MDB2_Statement_Common
  1431. {
  1432.     // {{{ _execute()
  1433.  
  1434.     /**
  1435.      * Execute a prepared query statement helper method.
  1436.      *
  1437.      * @param mixed $result_class string which specifies which result class to use
  1438.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1439.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1440.      * @access private
  1441.      */
  1442.     function &_execute($result_class = true$result_wrap_class = false)
  1443.     {
  1444.         if (is_null($this->statement)) {
  1445.             $result =parent::_execute($result_class$result_wrap_class);
  1446.             return $result;
  1447.         }
  1448.         $this->db->last_query = $this->query;
  1449.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1450.         if ($this->db->getOption('disable_query')) {
  1451.             $result $this->is_manip ? 0 : null;
  1452.             return $result;
  1453.         }
  1454.  
  1455.         $connection $this->db->getConnection();
  1456.         if (PEAR::isError($connection)) {
  1457.             return $connection;
  1458.         }
  1459.  
  1460.         $parameters = array(0 => $this->statement);
  1461.         foreach ($this->positions as $parameter{
  1462.             if (!array_key_exists($parameter$this->values)) {
  1463.                 return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1464.                     'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1465.             }
  1466.             $value $this->values[$parameter];
  1467.             $type !empty($this->types[$parameter]$this->types[$parameter: null;
  1468.             $quoted $this->db->quote($value$typefalse);
  1469.             if (PEAR::isError($quoted)) {
  1470.                 return $quoted;
  1471.             }
  1472.             $parameters[$quoted;
  1473.         }
  1474.  
  1475.         $result @call_user_func_array('ibase_execute'$parameters);
  1476.         if ($result === false{
  1477.             $err =$this->db->raiseError(nullnullnull,
  1478.                 'Could not execute statement'__FUNCTION__);
  1479.             return $err;
  1480.         }
  1481.  
  1482.         if ($this->is_manip{
  1483.             $affected_rows $this->db->_affectedRows($connection);
  1484.             return $affected_rows;
  1485.         }
  1486.  
  1487.         $result =$this->db->_wrapResult($result$this->result_types,
  1488.             $result_class$result_wrap_class$this->limit$this->offset);
  1489.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1490.         return $result;
  1491.     }
  1492.  
  1493.     // }}}
  1494.  
  1495.     // }}}
  1496.     // {{{ free()
  1497.  
  1498.     /**
  1499.      * Release resources allocated for the specified prepared query.
  1500.      *
  1501.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1502.      * @access public
  1503.      */
  1504.     function free()
  1505.     {
  1506.         if (is_null($this->positions)) {
  1507.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1508.                 'Prepared statement has already been freed'__FUNCTION__);
  1509.         }
  1510.         $result = MDB2_OK;
  1511.  
  1512.         if (!is_null($this->statement&& !@ibase_free_query($this->statement)) {
  1513.             $result $this->db->raiseError(nullnullnull,
  1514.                 'Could not free statement'__FUNCTION__);
  1515.         }
  1516.  
  1517.         parent::free();
  1518.         return $result;
  1519.     }
  1520. }
  1521. ?>

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