PageRenderTime 90ms CodeModel.GetById 13ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 1ms

/ railoapacheportable/php/PEAR/MDB2/Driver/ibase.php

http://railoapacheportable.googlecode.com/
PHP | 1480 lines | 918 code | 115 blank | 447 comment | 183 complexity | 6bc9cb90113fe68e8bbca26c41c4407d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file