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-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith                                         |
  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.134 2006/01/22 18:30:16 lsmith Exp $
  47.  
  48. /**
  49.  * MDB2 FireBird/InterBase driver
  50.  *
  51.  * @package MDB2
  52.  * @category Database
  53.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  54.  */
  55. class MDB2_Driver_ibase extends MDB2_Driver_Common
  56. {
  57.     // {{{ properties
  58.     var $escape_quotes = "'";
  59.  
  60.     var $transaction_id = 0;
  61.  
  62.     var $query_parameters = array();
  63.     var $query_parameter_values = array();
  64.  
  65.     // }}}
  66.     // {{{ constructor
  67.  
  68.     /**
  69.      * Constructor
  70.      */
  71.     function __construct()
  72.     {
  73.         parent::__construct();
  74.  
  75.         $this->phptype  'ibase';
  76.         $this->dbsyntax 'ibase';
  77.  
  78.         $this->supported['sequences'= true;
  79.         $this->supported['indexes'= true;
  80.         $this->supported['affected_rows'function_exists('ibase_affected_rows');
  81.         $this->supported['summary_functions'= true;
  82.         $this->supported['order_by_text'= true;
  83.         $this->supported['transactions'= true;
  84.         $this->supported['current_id'= true;
  85.         // maybe this needs different handling for ibase and firebird?
  86.         $this->supported['limit_queries''emulated';
  87.         $this->supported['LOBs'= true;
  88.         $this->supported['replace'= false;
  89.         $this->supported['sub_selects'= true;
  90.         $this->supported['auto_increment'= true;
  91.         $this->supported['primary_key'= true;
  92.  
  93.         $this->options['DBA_username'= false;
  94.         $this->options['DBA_password'= false;
  95.         $this->options['database_path''';
  96.         $this->options['database_extension''.gdb';
  97.         $this->options['default_text_field_length'= 4096;
  98.     }
  99.  
  100.     // }}}
  101.     // {{{ errorInfo()
  102.  
  103.     /**
  104.      * This method is used to collect information about an error
  105.      *
  106.      * @param integer $error 
  107.      * @return array 
  108.      * @access public
  109.      */
  110.     function errorInfo($error = null)
  111.     {
  112.         $native_msg @ibase_errmsg();
  113.  
  114.         if (function_exists('ibase_errcode')) {
  115.             $native_code @ibase_errcode();
  116.         else {
  117.             // memo for the interbase php module hackers: we need something similar
  118.             // to mysql_errno() to retrieve error codes instead of this ugly hack
  119.             if (preg_match('/^([^0-9\-]+)([0-9\-]+)\s+(.*)$/'$native_msg$m)) {
  120.                 $native_code = (int)$m[2];
  121.             else {
  122.                 $native_code = null;
  123.             }
  124.         }
  125.         if (is_null($error)) {
  126.             $error = MDB2_ERROR;
  127.             if ($native_code{
  128.                 // try to interpret Interbase error code (that's why we need ibase_errno()
  129.                 // in the interbase module to return the real error code)
  130.                 switch ($native_code{
  131.                 case -204:
  132.                     if (isset($m[3]&& is_int(strpos($m[3]'Table unknown'))) {
  133.                         $errno = MDB2_ERROR_NOSUCHTABLE;
  134.                     }
  135.                 break;
  136.                 default:
  137.                     static $ecode_map;
  138.                     if (empty($ecode_map)) {
  139.                         $ecode_map = array(
  140.                             -104 => MDB2_ERROR_SYNTAX,
  141.                             -150 => MDB2_ERROR_ACCESS_VIOLATION,
  142.                             -151 => MDB2_ERROR_ACCESS_VIOLATION,
  143.                             -155 => MDB2_ERROR_NOSUCHTABLE,
  144.                             -157 => MDB2_ERROR_NOSUCHFIELD,
  145.                             -158 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  146.                             -170 => MDB2_ERROR_MISMATCH,
  147.                             -171 => MDB2_ERROR_MISMATCH,
  148.                             -172 => MDB2_ERROR_INVALID,
  149.                             // -204 =>  // Covers too many errors, need to use regex on msg
  150.                             -205 => MDB2_ERROR_NOSUCHFIELD,
  151.                             -206 => MDB2_ERROR_NOSUCHFIELD,
  152.                             -208 => MDB2_ERROR_INVALID,
  153.                             -219 => MDB2_ERROR_NOSUCHTABLE,
  154.                             -297 => MDB2_ERROR_CONSTRAINT,
  155.                             -303 => MDB2_ERROR_INVALID,
  156.                             -413 => MDB2_ERROR_INVALID_NUMBER,
  157.                             -530 => MDB2_ERROR_CONSTRAINT,
  158.                             -551 => MDB2_ERROR_ACCESS_VIOLATION,
  159.                             -552 => MDB2_ERROR_ACCESS_VIOLATION,
  160.                             // -607 =>  // Covers too many errors, need to use regex on msg
  161.                             -625 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  162.                             -803 => MDB2_ERROR_CONSTRAINT,
  163.                             -804 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  164.                             -904 => MDB2_ERROR_CONNECT_FAILED,
  165.                             -922 => MDB2_ERROR_NOSUCHDB,
  166.                             -923 => MDB2_ERROR_CONNECT_FAILED,
  167.                             -924 => MDB2_ERROR_CONNECT_FAILED
  168.                         );
  169.                     }
  170.                     if (isset($ecode_map[$native_code])) {
  171.                         $error $ecode_map[$native_code];
  172.                     }
  173.                     break;
  174.                 }
  175.             else {
  176.                 static $error_regexps;
  177.                 if (!isset($error_regexps)) {
  178.                     $error_regexps = array(
  179.                         '/generator .* is not defined/'
  180.                             => MDB2_ERROR_SYNTAX,  // for compat. w ibase_errcode()
  181.                         '/table.*(not exist|not found|unknown)/i'
  182.                             => MDB2_ERROR_NOSUCHTABLE,
  183.                         '/table .* already exists/i'
  184.                             => MDB2_ERROR_ALREADY_EXISTS,
  185.                         '/unsuccessful metadata update .* failed attempt to store duplicate value/i'
  186.                             => MDB2_ERROR_ALREADY_EXISTS,
  187.                         '/unsuccessful metadata update .* not found/i'
  188.                             => MDB2_ERROR_NOT_FOUND,
  189.                         '/validation error for column .* value "\*\*\* null/i'
  190.                             => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  191.                         '/violation of [\w ]+ constraint/i'
  192.                             => MDB2_ERROR_CONSTRAINT,
  193.                         '/conversion error from string/i'
  194.                             => MDB2_ERROR_INVALID_NUMBER,
  195.                         '/no permission for/i'
  196.                             => MDB2_ERROR_ACCESS_VIOLATION,
  197.                         '/arithmetic exception, numeric overflow, or string truncation/i'
  198.                             => MDB2_ERROR_INVALID,
  199.                     );
  200.                 }
  201.                 foreach ($error_regexps as $regexp => $code{
  202.                     if (preg_match($regexp$native_msg$m)) {
  203.                         $error $code;
  204.                         break;
  205.                     }
  206.                 }
  207.             }
  208.         }
  209.         return array($error$native_code$native_msg);
  210.     }
  211.  
  212.     // }}}
  213.     // {{{ quoteIdentifier()
  214.  
  215.     /**
  216.      * Delimited identifiers are a nightmare with InterBase, so they're disabled
  217.      *
  218.      * @param string $str  identifier name to be quoted
  219.      * @param bool   $check_option  check the 'quote_identifier' option
  220.      *
  221.      * @return string  quoted identifier string
  222.      *
  223.      * @access public
  224.      */
  225.     function quoteIdentifier($str$check_option = false)
  226.  
  227.     {
  228.         return strtoupper($str);
  229.     }
  230.  
  231.     // }}}
  232.     // {{{ getConnection()
  233.  
  234.     /**
  235.      * Returns a native connection
  236.      *
  237.      * @return  mixed   a valid MDB2 connection object,
  238.      *                   or a MDB2 error object on error
  239.      * @access  public
  240.      */
  241.     function getConnection()
  242.     {
  243.         $result $this->connect();
  244.         if (PEAR::isError($result)) {
  245.             return $result;
  246.         }
  247.         if ($this->in_transaction{
  248.             return $this->transaction_id;
  249.         }
  250.         return $this->connection;
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ beginTransaction()
  255.  
  256.     /**
  257.      * Start a transaction.
  258.      *
  259.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  260.      * @access public
  261.      */
  262.     function beginTransaction()
  263.     {
  264.         $this->debug('starting transaction''beginTransaction');
  265.         if ($this->in_transaction{
  266.             return MDB2_OK;  //nothing to do
  267.         }
  268.         $result = ibase_trans();
  269.         if (!$result{
  270.             return $this->raiseError(MDB2_ERRORnullnull,
  271.                 'beginTransaction: could not start a transaction');
  272.         }
  273.         $this->transaction_id = $result;
  274.         $this->in_transaction = true;
  275.         return MDB2_OK;
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ commit()
  280.  
  281.     /**
  282.      * Commit the database changes done during a transaction that is in
  283.      * progress.
  284.      *
  285.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  286.      * @access public
  287.      */
  288.     function commit()
  289.     {
  290.         $this->debug('commit transaction''commit');
  291.         if (!$this->in_transaction{
  292.             return $this->raiseError(MDB2_ERRORnullnull,
  293.                 'commit: transaction changes are being auto committed');
  294.         }
  295.         if (!ibase_commit($this->transaction_id)) {
  296.             return $this->raiseError(MDB2_ERRORnullnull,
  297.                 'commit: could not commit a transaction');
  298.         }
  299.         $this->in_transaction = false;
  300.         //$this->transaction_id = 0;
  301.         return MDB2_OK;
  302.     }
  303.  
  304.     // }}}
  305.     // {{{ rollback()
  306.  
  307.     /**
  308.      * Cancel any database changes done during a transaction that is in
  309.      * progress.
  310.      *
  311.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  312.      * @access public
  313.      */
  314.     function rollback()
  315.     {
  316.         $this->debug('rolling back transaction''rollback');
  317.         if (!$this->in_transaction{
  318.             return $this->raiseError(MDB2_ERRORnullnull,
  319.                 'rollback: transactions can not be rolled back when changes are auto committed');
  320.         }
  321.         if ($this->transaction_id && !ibase_rollback($this->transaction_id)) {
  322.             return $this->raiseError(MDB2_ERRORnullnull,
  323.                 'rollback: Could not rollback a pending transaction: '.ibase_errmsg());
  324.         }
  325.         $this->in_transaction = false;
  326.         $this->transaction_id = 0;
  327.         return MDB2_OK;
  328.     }
  329.  
  330.     // }}}
  331.     // {{{ getDatabaseFile()
  332.  
  333.     /**
  334.      * Builds the string with path+dbname+extension
  335.      *
  336.      * @return string full database path+file
  337.      * @access protected
  338.      */
  339.     function _getDatabaseFile($database_name)
  340.     {
  341.         if ($database_name == ''{
  342.             return $database_name;
  343.         }
  344.         $ret $this->options['database_path'$database_name;
  345.         if (   strpos($database_name'.fdb'=== false
  346.             && strpos($database_name'.gdb'=== false
  347.         {
  348.             $ret .= $this->options['database_extension'];
  349.         }
  350.         return $ret;
  351.     }
  352.  
  353.     // }}}
  354.     // {{{ _doConnect()
  355.  
  356.     /**
  357.      * Does the grunt work of connecting to the database
  358.      *
  359.      * @return mixed connection resource on success, MDB2 Error Object on failure
  360.      * @access protected
  361.      */
  362.     function _doConnect($database_name$persistent = false)
  363.     {
  364.         $user    $this->dsn['username'];
  365.         $pw      $this->dsn['password'];
  366.         $dbhost  $this->dsn['hostspec'?
  367.             ($this->dsn['hostspec'].':'.$database_name$database_name;
  368.  
  369.         $params = array();
  370.         $params[$dbhost;
  371.         $params[!empty($user$user : null;
  372.         $params[!empty($pw$pw : null;
  373.         $params[= isset($this->dsn['charset']$this->dsn['charset': null;
  374.         $params[= isset($this->dsn['buffers']$this->dsn['buffers': null;
  375.         $params[= isset($this->dsn['dialect']$this->dsn['dialect': null;
  376.         $params[= isset($this->dsn['role'])    $this->dsn['role': null;
  377.  
  378.         $connect_function $persistent 'ibase_pconnect' 'ibase_connect';
  379.  
  380.         $connection @call_user_func_array($connect_function$params);
  381.         if ($connection <= 0{
  382.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILED);
  383.         }
  384.         if (function_exists('ibase_timefmt')) {
  385.             @ibase_timefmt("%Y-%m-%d %H:%M:%S"IBASE_TIMESTAMP);
  386.             @ibase_timefmt("%Y-%m-%d"IBASE_DATE);
  387.         else {
  388.             @ini_set("ibase.timestampformat""%Y-%m-%d %H:%M:%S");
  389.             //@ini_set("ibase.timeformat", "%H:%M:%S");
  390.             @ini_set("ibase.dateformat""%Y-%m-%d");
  391.         }
  392.  
  393.         return $connection;
  394.     }
  395.  
  396.     // }}}
  397.     // {{{ connect()
  398.  
  399.     /**
  400.      * Connect to the database
  401.      *
  402.      * @return true on success, MDB2 Error Object on failure
  403.      * @access public
  404.      */
  405.     function connect()
  406.     {
  407.         $database_file $this->_getDatabaseFile($this->database_name);
  408.         if (is_resource($this->connection)) {
  409.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  410.                 && $this->connected_database_name == $database_file
  411.                 && $this->opened_persistent == $this->options['persistent']
  412.             {
  413.                 return MDB2_OK;
  414.             }
  415.             $this->disconnect(false);
  416.         }
  417.  
  418.         if (!PEAR::loadExtension('interbase')) {
  419.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  420.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  421.         }
  422.  
  423.         if (!empty($this->database_name)) {
  424.             $connection $this->_doConnect($database_file$this->options['persistent']);
  425.             if (PEAR::isError($connection)) {
  426.                 return $connection;
  427.             }
  428.             $this->connection =$connection;
  429.             $this->connected_dsn $this->dsn;
  430.             $this->connected_database_name $database_file;
  431.             $this->opened_persistent $this->options['persistent'];
  432.             $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  433.         }
  434.         return MDB2_OK;
  435.     }
  436.  
  437.     // }}}
  438.     // {{{ disconnect()
  439.  
  440.     /**
  441.      * Log out and disconnect from the database.
  442.      *
  443.      * @return mixed true on success, false if not connected and error
  444.      *                 object on error
  445.      * @access public
  446.      */
  447.     function disconnect($force = true)
  448.     {
  449.         if (is_resource($this->connection)) {
  450.             if ($this->in_transaction{
  451.                 $this->rollback();
  452.             }
  453.             if ($force || !$this->opened_persistent{
  454.                 @ibase_close($this->connection);
  455.             }
  456.             $this->connection = 0;
  457.             $this->in_transaction = false;
  458.         }
  459.         return MDB2_OK;
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ _doQuery()
  464.  
  465.     /**
  466.      * Execute a query
  467.      * @param string $query  query
  468.      * @param boolean $is_manip  if the query is a manipulation query
  469.      * @param resource $connection 
  470.      * @param string $database_name 
  471.      * @return result or error object
  472.      * @access protected
  473.      */
  474.     function _doQuery($query$is_manip = false$connection = null$database_name = null)
  475.     {
  476.         $this->last_query $query;
  477.         $this->debug($query'query');
  478.         if ($this->getOption('disable_query')) {
  479.             if ($is_manip{
  480.                 return 0;
  481.             }
  482.             return null;
  483.         }
  484.  
  485.         if (is_null($connection)) {
  486.             $connection $this->getConnection();
  487.             if (PEAR::isError($connection)) {
  488.                 return $connection;
  489.             }
  490.         }
  491.         $result = ibase_query($connection$query);
  492.  
  493.         if ($result === false{
  494.             return $this->raiseError();
  495.         }
  496.  
  497.         return $result;
  498.     }
  499.  
  500.     // }}}
  501.     // {{{ _affectedRows()
  502.  
  503.     /**
  504.      * returns the number of rows affected
  505.      *
  506.      * @param resource $result 
  507.      * @param resource $connection 
  508.      * @return mixed MDB2 Error Object or the number of rows affected
  509.      * @access private
  510.      */
  511.     function _affectedRows($connection$result = null)
  512.     {
  513.         if (is_null($connection)) {
  514.             $connection $this->getConnection();
  515.             if (PEAR::isError($connection)) {
  516.                 return $connection;
  517.             }
  518.         }
  519.         return (function_exists('ibase_affected_rows'? ibase_affected_rows($connection: 0);
  520.     }
  521.  
  522.     // }}}
  523.     // {{{ _modifyQuery()
  524.  
  525.     /**
  526.      * Changes a query string for various DBMS specific reasons
  527.      *
  528.      * @param string $query  query to modify
  529.      * @return the new (modified) query
  530.      * @access protected
  531.      */
  532.     function _modifyQuery($query$is_manip$limit$offset)
  533.     {
  534.         if ($limit > 0 && $this->dsn['dbsyntax'== 'firebird'{
  535.             $query preg_replace('/^([\s(])*SELECT(?!\s*FIRST\s*\d+)/i',
  536.                 "SELECT FIRST $limit SKIP $offset"$query);
  537.         }
  538.         return $query;
  539.     }
  540.  
  541.     // }}}
  542.     // {{{ getServerVersion()
  543.  
  544.     /**
  545.      * return version information about the server
  546.      *
  547.      * @param string     $native  determines if the raw version string should be returned
  548.      * @return mixed array/string with version information or MDB2 error object
  549.      * @access public
  550.      */
  551.     function getServerVersion($native = false)
  552.     {
  553.         $ibserv = ibase_service_attach($this->dsn['hostspec']$this->options['DBA_username']$this->options['DBA_password']);
  554.         $server_info = ibase_server_info($ibservIBASE_SVC_SERVER_VERSION);
  555.         ibase_service_detach($ibserv);
  556.         if (!$native{
  557.             //WI-V1.5.3.4854 Firebird 1.5
  558.             if (!preg_match('/-V([\d\.]*)/'$server_info$matches)) {
  559.                 return $this->raiseError(MDB2_ERRORnullnull,
  560.                     'Could not parse version information:'.$server_info);
  561.             }
  562.             $tmp explode('.'$matches[1]4);
  563.             $server_info = array(
  564.                 'major' => isset($tmp[0]$tmp[0: null,
  565.                 'minor' => isset($tmp[1]$tmp[1: null,
  566.                 'patch' => isset($tmp[2]$tmp[2: null,
  567.                 'extra' => isset($tmp[3]$tmp[3: null,
  568.                 'native' => $server_info,
  569.             );
  570.         }
  571.         return $server_info;
  572.     }
  573.  
  574.     // }}}
  575.     // {{{ prepare()
  576.  
  577.     /**
  578.      * Prepares a query for multiple execution with execute().
  579.      * With some database backends, this is emulated.
  580.      * prepare() requires a generic query as string like
  581.      * 'INSERT INTO numbers VALUES(?,?)' or
  582.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  583.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  584.      * bindParam() and the query can be send off using the execute() method.
  585.      *
  586.      * @param string $query the query to prepare
  587.      * @param mixed   $types  array that contains the types of the placeholders
  588.      * @param mixed   $result_types  array that contains the types of the columns in
  589.      *                         the result set, if set to MDB2_PREPARE_MANIP the
  590.                               query is handled as a manipulation query
  591.      * @return mixed resource handle for the prepared query on success, a MDB2
  592.      *         error on failure
  593.      * @access public
  594.      * @see bindParam, execute
  595.      */
  596.     function &prepare($query$types = null$result_types = null)
  597.     {
  598.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  599.         $offset $this->offset;
  600.         $limit $this->limit;
  601.         $this->offset $this->limit = 0;
  602.         $this->debug($query'prepare');
  603.         $placeholder_type_guess $placeholder_type = null;
  604.         $question '?';
  605.         $colon ':';
  606.         $positions = array();
  607.         $position = 0;
  608.         while ($position strlen($query)) {
  609.             $q_position strpos($query$question$position);
  610.             $c_position strpos($query$colon$position);
  611.             if ($q_position && $c_position{
  612.                 $p_position min($q_position$c_position);
  613.             elseif ($q_position{
  614.                 $p_position $q_position;
  615.             elseif ($c_position{
  616.                 $p_position $c_position;
  617.             else {
  618.                 break;
  619.             }
  620.             if (is_null($placeholder_type)) {
  621.                 $placeholder_type_guess $query[$p_position];
  622.             }
  623.             if (is_int($quote strpos($query"'"$position)) && $quote $p_position{
  624.                 if (!is_int($end_quote strpos($query"'"$quote + 1))) {
  625.                     $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  626.                         'prepare: query with an unterminated text string specified');
  627.                     return $err;
  628.                 }
  629.                 switch ($this->escape_quotes{
  630.                 case '':
  631.                 case "'":
  632.                     $position $end_quote + 1;
  633.                     break;
  634.                 default:
  635.                     if ($end_quote == $quote + 1{
  636.                         $position $end_quote + 1;
  637.                     else {
  638.                         if ($query[$end_quote-1== $this->escape_quotes{
  639.                             $position $end_quote;
  640.                         else {
  641.                             $position $end_quote + 1;
  642.                         }
  643.                     }
  644.                     break;
  645.                 }
  646.             elseif ($query[$position== $placeholder_type_guess{
  647.                 if (is_null($placeholder_type)) {
  648.                     $placeholder_type $query[$p_position];
  649.                     $question $colon $placeholder_type;
  650.                 }
  651.                 if ($placeholder_type == ':'{
  652.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  653.                     if ($parameter === ''{
  654.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  655.                             'prepare: named parameter with an empty name');
  656.                         return $err;
  657.                     }
  658.                     $positions[$parameter$p_position;
  659.                     $query substr_replace($query'?'$positionstrlen($parameter)+1);
  660.                 else {
  661.                     $positions[$p_position;
  662.                 }
  663.                 $position $p_position + 1;
  664.             else {
  665.                 $position $p_position;
  666.             }
  667.         }
  668.         $connection $this->getConnection();
  669.         if (PEAR::isError($connection)) {
  670.             return $connection;
  671.         }
  672.         $statement @ibase_prepare($connection$query);
  673.         if (!$statement{
  674.             $err =$this->raiseError(MDB2_ERRORnullnull,
  675.                 'Could not create statement');
  676.             return $err;
  677.         }
  678.  
  679.         $class_name 'MDB2_Statement_'.$this->phptype;
  680.         $obj =new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  681.         return $obj;
  682.     }
  683.  
  684.     // }}}
  685.     // {{{ getSequenceName()
  686.  
  687.     /**
  688.      * adds sequence name formatting to a sequence name
  689.      *
  690.      * @param string $sqn name of the sequence
  691.      * @return string formatted sequence name
  692.      * @access public
  693.      */
  694.     function getSequenceName($sqn)
  695.     {
  696.         return strtoupper(parent::getSequenceName($sqn));
  697.     }
  698.  
  699.     // }}}
  700.     // {{{ nextID()
  701.  
  702.     /**
  703.      * returns the next free id of a sequence
  704.      *
  705.      * @param string $seq_name name of the sequence
  706.      * @param boolean $ondemand when true the seqence is
  707.      *                           automatic created, if it
  708.      *                           not exists
  709.      * @return mixed MDB2 Error Object or id
  710.      * @access public
  711.      */
  712.     function nextID($seq_name$ondemand = true)
  713.     {
  714.         $sequence_name $this->getSequenceName($seq_name);
  715.         $query 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
  716.         $this->expectError('*');
  717.         $result $this->queryOne($query'integer');
  718.         $this->popExpect();
  719.         if (PEAR::isError($result)) {
  720.             if ($ondemand{
  721.                 $this->loadModule('Manager'nulltrue);
  722.                 // Since we are creating the sequence on demand
  723.                 // we know the first id = 1 so initialize the
  724.                 // sequence at 2
  725.                 $result $this->manager->createSequence($seq_name2);
  726.                 if (PEAR::isError($result)) {
  727.                     return $this->raiseError(MDB2_ERRORnullnull,
  728.                         'nextID: on demand sequence could not be created');
  729.                 else {
  730.                     // First ID of a newly created sequence is 1
  731.                     // return 1;
  732.                     // BUT generators are not always reset, so return the actual value
  733.                     return $this->currID($seq_name);
  734.                 }
  735.             }
  736.         }
  737.         return $result;
  738.     }
  739.  
  740.     // }}}
  741.     // {{{ currID()
  742.  
  743.     /**
  744.      * returns the current id of a sequence
  745.      *
  746.      * @param string $seq_name name of the sequence
  747.      * @return mixed MDB2 Error Object or id
  748.      * @access public
  749.      */
  750.     function currID($seq_name)
  751.     {
  752.         $sequence_name $this->getSequenceName($seq_name);
  753.         $query 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
  754.         $value @$this->queryOne($query);
  755.         if (PEAR::isError($value)) {
  756.             return $this->raiseError(MDB2_ERRORnullnull,
  757.                 'currID: Unable to select from ' $seq_name;
  758.         }
  759.         if (!is_numeric($value)) {
  760.             return $this->raiseError(MDB2_ERRORnullnull,
  761.                 'currID: could not find value in sequence table');
  762.         }
  763.         return $value;
  764.     }
  765.  
  766.     // }}}
  767. }
  768.  
  769. class MDB2_Result_ibase extends MDB2_Result_Common
  770. {
  771.     // {{{ _skipLimitOffset()
  772.  
  773.     /**
  774.      * Skip the first row of a result set.
  775.      *
  776.      * @param resource $result 
  777.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  778.      * @access protected
  779.      */
  780.     function _skipLimitOffset()
  781.     {
  782.         if ($this->db->dsn['dbsyntax'== 'firebird'{
  783.             return true;
  784.         }
  785.         if ($this->limit{
  786.             if ($this->rownum $this->limit{
  787.                 return false;
  788.             }
  789.         }
  790.         if ($this->offset{
  791.             while ($this->offset_count $this->offset{
  792.                 ++$this->offset_count;
  793.                 if (!is_array(@ibase_fetch_row($this->result))) {
  794.                     $this->offset_count $this->offset;
  795.                     return false;
  796.                 }
  797.             }
  798.         }
  799.         return true;
  800.     }
  801.  
  802.     // }}}
  803.     // {{{ fetchRow()
  804.  
  805.     /**
  806.      * Fetch a row and insert the data into an existing array.
  807.      *
  808.      * @param int  $fetchmode how the array data should be indexed
  809.      * @param int  $rownum    number of the row where the data can be found
  810.      * @return int data array on success, a MDB2 error on failure
  811.      * @access public
  812.      */
  813.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  814.     {
  815.         if ($this->result === true{
  816.             //query successfully executed, but without results...
  817.             $null = null;
  818.             return $null;
  819.         }
  820.         if (!$this->_skipLimitOffset()) {
  821.             $null = null;
  822.             return $null;
  823.         }
  824.         if (!is_null($rownum)) {
  825.             $seek $this->seek($rownum);
  826.             if (PEAR::isError($seek)) {
  827.                 return $seek;
  828.             }
  829.         }
  830.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  831.             $fetchmode $this->db->fetchmode;
  832.         }
  833.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  834.             $row @ibase_fetch_assoc($this->result);
  835.             if (is_array($row)
  836.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  837.             {
  838.                 $row array_change_key_case($row$this->db->options['field_case']);
  839.             }
  840.         else {
  841.             $row @ibase_fetch_row($this->result);
  842.         }
  843.         if (!$row{
  844.             if (is_null($this->result)) {
  845.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  846.                     'fetchRow: resultset has already been freed');
  847.                 return $err;
  848.             }
  849.             $null = null;
  850.             return $null;
  851.         }
  852.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  853.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  854.         {
  855.             $this->db->_fixResultArrayValues($row$mode);
  856.         }
  857.         if (!empty($this->values)) {
  858.             $this->_assignBindColumns($row);
  859.         }
  860.         if (!empty($this->types)) {
  861.             $row $this->db->datatype->convertResultRow($this->types$row);
  862.         }
  863.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  864.             $object_class $this->db->options['fetch_class'];
  865.             if ($object_class == 'stdClass'{
  866.                 $row = (object) $row;
  867.             else {
  868.                 $row &new $object_class($row);
  869.             }
  870.         }
  871.         ++$this->rownum;
  872.         return $row;
  873.     }
  874.  
  875.     // }}}
  876.     // {{{ _getColumnNames()
  877.  
  878.     /**
  879.      * Retrieve the names of columns returned by the DBMS in a query result.
  880.      *
  881.      * @return mixed associative array variable
  882.      *       that holds the names of columns. The indexes of the array are
  883.      *       the column names mapped to lower case and the values are the
  884.      *       respective numbers of the columns starting from 0. Some DBMS may
  885.      *       not return any columns when the result set does not contain any
  886.      *       rows.
  887.      * @access private
  888.      */
  889.     function _getColumnNames()
  890.     {
  891.         $columns = array();
  892.         $numcols $this->numCols();
  893.         if (PEAR::isError($numcols)) {
  894.             return $numcols;
  895.         }
  896.         for ($column = 0; $column $numcols$column++{
  897.             $column_info @ibase_field_info($this->result$column);
  898.             $columns[$column_info['alias']] $column;
  899.         }
  900.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  901.             $columns array_change_key_case($columns$this->db->options['field_case']);
  902.         }
  903.         return $columns;
  904.     }
  905.  
  906.     // }}}
  907.     // {{{ numCols()
  908.  
  909.     /**
  910.      * Count the number of columns returned by the DBMS in a query result.
  911.      *
  912.      * @return mixed integer value with the number of columns, a MDB2 error
  913.      *       on failure
  914.      * @access public
  915.      */
  916.     function numCols()
  917.     {
  918.         if ($this->result === true{
  919.             //query successfully executed, but without results...
  920.             return 0;
  921.         }
  922.  
  923.         if (!is_resource($this->result)) {
  924.             return $this->db->raiseError('numCols(): not a valid ibase resource');
  925.         }
  926.         $cols @ibase_num_fields($this->result);
  927.         if (is_null($cols)) {
  928.             if (is_null($this->result)) {
  929.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  930.                     'numCols: resultset has already been freed');
  931.             }
  932.             return $this->db->raiseError();
  933.         }
  934.         return $cols;
  935.     }
  936.  
  937.     // }}}
  938.     // {{{ free()
  939.  
  940.     /**
  941.      * Free the internal resources associated with $result.
  942.      *
  943.      * @return boolean true on success, false if $result is invalid
  944.      * @access public
  945.      */
  946.     function free()
  947.     {
  948.         if (is_resource($this->result)) {
  949.             $free @ibase_free_result($this->result);
  950.             if (!$free{
  951.                 if (is_null($this->result)) {
  952.                     return MDB2_OK;
  953.                 }
  954.                 return $this->db->raiseError();
  955.             }
  956.         }
  957.         $this->result = null;
  958.         return MDB2_OK;
  959.     }
  960.  
  961.     // }}}
  962. }
  963.  
  964. {
  965.     // {{{ class vars
  966.  
  967.     var $buffer;
  968.     var $buffer_rownum = - 1;
  969.  
  970.     // }}}
  971.     // {{{ _fillBuffer()
  972.  
  973.     /**
  974.      * Fill the row buffer
  975.      *
  976.      * @param int $rownum   row number upto which the buffer should be filled
  977.      *                       if the row number is null all rows are ready into the buffer
  978.      * @return boolean true on success, false on failure
  979.      * @access protected
  980.      */
  981.     function _fillBuffer($rownum = null)
  982.     {
  983.         if (isset($this->buffer&& is_array($this->buffer)) {
  984.             if (is_null($rownum)) {
  985.                 if (!end($this->buffer)) {
  986.                     return false;
  987.                 }
  988.             elseif (isset($this->buffer[$rownum])) {
  989.                 return (bool) $this->buffer[$rownum];
  990.             }
  991.         }
  992.  
  993.         if (!$this->_skipLimitOffset()) {
  994.             return false;
  995.         }
  996.  
  997.         $buffer = true;
  998.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  999.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  1000.             && ($buffer @ibase_fetch_row($this->result))
  1001.         {
  1002.             ++$this->buffer_rownum;
  1003.             $this->buffer[$this->buffer_rownum$buffer;
  1004.         }
  1005.  
  1006.         if (!$buffer{
  1007.             ++$this->buffer_rownum;
  1008.             $this->buffer[$this->buffer_rownum= false;
  1009.             return false;
  1010.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  1011.             ++$this->buffer_rownum;
  1012.             $this->buffer[$this->buffer_rownum= false;
  1013.         }
  1014.         return true;
  1015.     }
  1016.  
  1017.     // }}}
  1018.     // {{{ fetchRow()
  1019.  
  1020.     /**
  1021.      * Fetch a row and insert the data into an existing array.
  1022.      *
  1023.      * @param int       $fetchmode  how the array data should be indexed
  1024.      * @param int    $rownum    number of the row where the data can be found
  1025.      * @return int data array on success, a MDB2 error on failure
  1026.      * @access public
  1027.      */
  1028.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1029.     {
  1030.         if ($this->result === true{
  1031.             //query successfully executed, but without results...
  1032.             $null = null;
  1033.             return $null;
  1034.         }
  1035.         if (is_null($this->result)) {
  1036.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1037.                 'fetchRow: resultset has already been freed');
  1038.             return $err;
  1039.         }
  1040.         if (!is_null($rownum)) {
  1041.             $seek $this->seek($rownum);
  1042.             if (PEAR::isError($seek)) {
  1043.                 return $seek;
  1044.             }
  1045.         }
  1046.         $target_rownum $this->rownum + 1;
  1047.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1048.             $fetchmode $this->db->fetchmode;
  1049.         }
  1050.         if (!$this->_fillBuffer($target_rownum)) {
  1051.             $null = null;
  1052.             return $null;
  1053.         }
  1054.         $row $this->buffer[$target_rownum];
  1055.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1056.             $column_names $this->getColumnNames();
  1057.             foreach ($column_names as $name => $i{
  1058.                 $column_names[$name$row[$i];
  1059.             }
  1060.             $row $column_names;
  1061.         }
  1062.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  1063.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  1064.         {
  1065.             $this->db->_fixResultArrayValues($row$mode);
  1066.         }
  1067.         if (!empty($this->values)) {
  1068.             $this->_assignBindColumns($row);
  1069.         }
  1070.         if (!empty($this->types)) {
  1071.             $row $this->db->datatype->convertResultRow($this->types$row);
  1072.         }
  1073.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1074.             $object_class $this->db->options['fetch_class'];
  1075.             if ($object_class == 'stdClass'{
  1076.                 $row = (object) $row;
  1077.             else {
  1078.                 $row &new $object_class($row);
  1079.             }
  1080.         }
  1081.         ++$this->rownum;
  1082.         return $row;
  1083.     }
  1084.  
  1085.     // }}}
  1086.     // {{{ seek()
  1087.  
  1088.     /**
  1089.      * seek to a specific row in a result set
  1090.      *
  1091.      * @param int    $rownum    number of the row where the data can be found
  1092.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1093.      * @access public
  1094.      */
  1095.     function seek($rownum = 0)
  1096.     {
  1097.         if (is_null($this->result)) {
  1098.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1099.                 'seek: resultset has already been freed');
  1100.         }
  1101.         $this->rownum $rownum - 1;
  1102.         return MDB2_OK;
  1103.     }
  1104.  
  1105.     // }}}
  1106.     // {{{ valid()
  1107.  
  1108.     /**
  1109.      * check if the end of the result set has been reached
  1110.      *
  1111.      * @return mixed true or false on sucess, a MDB2 error on failure
  1112.      * @access public
  1113.      */
  1114.     function valid()
  1115.     {
  1116.         if (is_null($this->result)) {
  1117.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1118.                 'valid: resultset has already been freed');
  1119.         }
  1120.         if ($this->_fillBuffer($this->rownum + 1)) {
  1121.             return true;
  1122.         }
  1123.         return false;
  1124.     }
  1125.  
  1126.     // }}}
  1127.     // {{{ numRows()
  1128.  
  1129.     /**
  1130.      * returns the number of rows in a result object
  1131.      *
  1132.      * @return mixed MDB2 Error Object or the number of rows
  1133.      * @access public
  1134.      */
  1135.     function numRows()
  1136.     {
  1137.         if (is_null($this->result)) {
  1138.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1139.                 'seek: resultset has already been freed');
  1140.         }
  1141.         $this->_fillBuffer();
  1142.         return $this->buffer_rownum;
  1143.     }
  1144.  
  1145.     // }}}
  1146.     // {{{ free()
  1147.  
  1148.     /**
  1149.      * Free the internal resources associated with $result.
  1150.      *
  1151.      * @return boolean true on success, false if $result is invalid
  1152.      * @access public
  1153.      */
  1154.     function free()
  1155.     {
  1156.         $this->buffer = null;
  1157.         $this->buffer_rownum = null;
  1158.         $free = parent::free();
  1159.     }
  1160.  
  1161.     // }}}
  1162. }
  1163.  
  1164. class MDB2_Statement_ibase extends MDB2_Statement_Common
  1165. {
  1166.     // {{{ _execute()
  1167.  
  1168.     /**
  1169.      * Execute a prepared query statement helper method.
  1170.      *
  1171.      * @param mixed $result_class string which specifies which result class to use
  1172.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1173.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1174.      * @access private
  1175.      */
  1176.     function &_execute($result_class = true$result_wrap_class = false)
  1177.     {
  1178.         $this->db->last_query = $this->query;
  1179.         $this->db->debug($this->query'execute');
  1180.         if ($this->db->getOption('disable_query')) {
  1181.             if ($this->is_manip{
  1182.                 $return = 0;
  1183.                 return $return;
  1184.             }
  1185.             $null = null;
  1186.             return $null;
  1187.         }
  1188.  
  1189.         $connection $this->db->getConnection();
  1190.         if (PEAR::isError($connection)) {
  1191.             return $connection;
  1192.         }
  1193.  
  1194.         $parameters = array(0 => $this->statement);
  1195.         foreach ($this->positions as $parameter => $current_position{
  1196.             if (!array_key_exists($parameter$this->values)) {
  1197.                 return $this->db->raiseError();
  1198.             }
  1199.             $value $this->values[$parameter];
  1200.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1201.             $parameters[$this->db->quote($value$typefalse);
  1202.         }
  1203.  
  1204.         $result call_user_func_array('ibase_execute'$parameters);
  1205.         if ($result === false{
  1206.             $err =$this->db->raiseError();
  1207.             return $err;
  1208.         }
  1209.  
  1210.         if ($this->is_manip{
  1211.             $affected_rows $this->db->_affectedRows($connection);
  1212.             return $affected_rows;
  1213.         }
  1214.  
  1215.         $result =$this->db->_wrapResult($result$this->result_types,
  1216.             $result_class$result_wrap_class$this->limit$this->offset);
  1217.         return $result;
  1218.     }
  1219.  
  1220.     // }}}
  1221.  
  1222.     // }}}
  1223.     // {{{ free()
  1224.  
  1225.     /**
  1226.      * Release resources allocated for the specified prepared query.
  1227.      *
  1228.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1229.      * @access public
  1230.      */
  1231.     function free()
  1232.     {
  1233.         if (!@ibase_free_query($this->statement)) {
  1234.             return $this->db->raiseError();
  1235.         }
  1236.         return MDB2_OK;
  1237.     }
  1238. }
  1239. ?>

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