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.121 2005/12/13 13:14:26 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)
  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.         if (!$this->destructor_registered && $this->opened_persistent{
  269.             $this->destructor_registered = true;
  270.             register_shutdown_function('MDB2_closeOpenTransactions');
  271.         }
  272.         $result = ibase_trans();
  273.         if (!$result{
  274.             return $this->raiseError(MDB2_ERRORnullnull,
  275.                 'beginTransaction: could not start a transaction');
  276.         }
  277.         $this->transaction_id = $result;
  278.         $this->in_transaction = true;
  279.         return MDB2_OK;
  280.     }
  281.  
  282.     // }}}
  283.     // {{{ commit()
  284.  
  285.     /**
  286.      * Commit the database changes done during a transaction that is in
  287.      * progress.
  288.      *
  289.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  290.      * @access public
  291.      */
  292.     function commit()
  293.     {
  294.         $this->debug('commit transaction''commit');
  295.         if (!$this->in_transaction{
  296.             return $this->raiseError(MDB2_ERRORnullnull,
  297.                 'commit: transaction changes are being auto committed');
  298.         }
  299.         if (!ibase_commit($this->transaction_id)) {
  300.             return $this->raiseError(MDB2_ERRORnullnull,
  301.                 'commit: could not commit a transaction');
  302.         }
  303.         $this->in_transaction = false;
  304.         //$this->transaction_id = 0;
  305.         return MDB2_OK;
  306.     }
  307.  
  308.     // }}}
  309.     // {{{ rollback()
  310.  
  311.     /**
  312.      * Cancel any database changes done during a transaction that is in
  313.      * progress.
  314.      *
  315.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  316.      * @access public
  317.      */
  318.     function rollback()
  319.     {
  320.         $this->debug('rolling back transaction''rollback');
  321.         if (!$this->in_transaction{
  322.             return $this->raiseError(MDB2_ERRORnullnull,
  323.                 'rollback: transactions can not be rolled back when changes are auto committed');
  324.         }
  325.         if ($this->transaction_id && !ibase_rollback($this->transaction_id)) {
  326.             return $this->raiseError(MDB2_ERRORnullnull,
  327.                 'rollback: Could not rollback a pending transaction: '.ibase_errmsg());
  328.         }
  329.         $this->in_transaction = false;
  330.         $this->transaction_id = 0;
  331.         return MDB2_OK;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ getDatabaseFile()
  336.  
  337.     /**
  338.      * Builds the string with path+dbname+extension
  339.      *
  340.      * @return string full database path+file
  341.      * @access protected
  342.      */
  343.     function _getDatabaseFile($database_name)
  344.     {
  345.         if ($database_name == ''{
  346.             return $database_name;
  347.         }
  348.         return $this->options['database_path'].$database_name.$this->options['database_extension'];
  349.     }
  350.  
  351.     // }}}
  352.     // {{{ _doConnect()
  353.  
  354.     /**
  355.      * Does the grunt work of connecting to the database
  356.      *
  357.      * @return mixed connection resource on success, MDB2 Error Object on failure
  358.      * @access protected
  359.      */
  360.     function _doConnect($database_name$persistent = false)
  361.     {
  362.         $user    $this->dsn['username'];
  363.         $pw      $this->dsn['password'];
  364.         $dbhost  $this->dsn['hostspec'?
  365.             ($this->dsn['hostspec'].':'.$database_name$database_name;
  366.  
  367.         $params = array();
  368.         $params[$dbhost;
  369.         $params[!empty($user$user : null;
  370.         $params[!empty($pw$pw : null;
  371.         $params[= isset($this->dsn['charset']$this->dsn['charset': null;
  372.         $params[= isset($this->dsn['buffers']$this->dsn['buffers': null;
  373.         $params[= isset($this->dsn['dialect']$this->dsn['dialect': null;
  374.         $params[= isset($this->dsn['role'])    $this->dsn['role': null;
  375.  
  376.         $connect_function $persistent 'ibase_pconnect' 'ibase_connect';
  377.  
  378.         $connection @call_user_func_array($connect_function$params);
  379.         if ($connection <= 0{
  380.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILED);
  381.         }
  382.         if (function_exists('ibase_timefmt')) {
  383.             @ibase_timefmt("%Y-%m-%d %H:%M:%S"IBASE_TIMESTAMP);
  384.             @ibase_timefmt("%Y-%m-%d"IBASE_DATE);
  385.         else {
  386.             @ini_set("ibase.timestampformat""%Y-%m-%d %H:%M:%S");
  387.             //@ini_set("ibase.timeformat", "%H:%M:%S");
  388.             @ini_set("ibase.dateformat""%Y-%m-%d");
  389.         }
  390.  
  391.         return $connection;
  392.     }
  393.  
  394.     // }}}
  395.     // {{{ connect()
  396.  
  397.     /**
  398.      * Connect to the database
  399.      *
  400.      * @return true on success, MDB2 Error Object on failure
  401.      * @access public
  402.      */
  403.     function connect()
  404.     {
  405.         $database_file $this->_getDatabaseFile($this->database_name);
  406.         if (is_resource($this->connection)) {
  407.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  408.                 && $this->connected_database_name == $database_file
  409.                 && $this->opened_persistent == $this->options['persistent']
  410.             {
  411.                 return MDB2_OK;
  412.             }
  413.             $this->disconnect(false);
  414.         }
  415.  
  416.         if (!PEAR::loadExtension('interbase')) {
  417.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  418.                 'connect: extension '.$this->phptype.' is not compiled into PHP');
  419.         }
  420.  
  421.         if (!empty($this->database_name)) {
  422.             $connection $this->_doConnect($database_file$this->options['persistent']);
  423.             if (PEAR::isError($connection)) {
  424.                 return $connection;
  425.             }
  426.             $this->connection =$connection;
  427.             $this->connected_dsn $this->dsn;
  428.             $this->connected_database_name $database_file;
  429.             $this->opened_persistent $this->options['persistent'];
  430.             $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  431.         }
  432.         return MDB2_OK;
  433.     }
  434.  
  435.     // }}}
  436.     // {{{ disconnect()
  437.  
  438.     /**
  439.      * Log out and disconnect from the database.
  440.      *
  441.      * @return mixed true on success, false if not connected and error
  442.      *                 object on error
  443.      * @access public
  444.      */
  445.     function disconnect($force = true)
  446.     {
  447.         if (is_resource($this->connection)) {
  448.             if ($this->in_transaction{
  449.                 $this->rollback();
  450.             }
  451.             if ($force || !$this->opened_persistent{
  452.                 @ibase_close($this->connection);
  453.             }
  454.             $this->connection = 0;
  455.             $this->in_transaction = false;
  456.         }
  457.         return MDB2_OK;
  458.     }
  459.  
  460.     // }}}
  461.     // {{{ _doQuery()
  462.  
  463.     /**
  464.      * Execute a query
  465.      * @param string $query  query
  466.      * @param boolean $is_manip  if the query is a manipulation query
  467.      * @param resource $connection 
  468.      * @param string $database_name 
  469.      * @return result or error object
  470.      * @access protected
  471.      */
  472.     function _doQuery($query$is_manip = false$connection = null$database_name = null)
  473.     {
  474.         $this->last_query $query;
  475.         $this->debug($query'query');
  476.         if ($this->getOption('disable_query')) {
  477.             if ($is_manip{
  478.                 return 0;
  479.             }
  480.             return null;
  481.         }
  482.  
  483.         if (is_null($connection)) {
  484.             $connection $this->getConnection();
  485.             if (PEAR::isError($connection)) {
  486.                 return $connection;
  487.             }
  488.         }
  489.         $result = ibase_query($connection$query);
  490.  
  491.         if ($result === false{
  492.             return $this->raiseError();
  493.         }
  494.  
  495.         return $result;
  496.     }
  497.  
  498.     // }}}
  499.     // {{{ _affectedRows()
  500.  
  501.     /**
  502.      * returns the number of rows affected
  503.      *
  504.      * @param resource $result 
  505.      * @param resource $connection 
  506.      * @return mixed MDB2 Error Object or the number of rows affected
  507.      * @access private
  508.      */
  509.     function _affectedRows($connection$result = null)
  510.     {
  511.         if (is_null($connection)) {
  512.             $connection $this->getConnection();
  513.             if (PEAR::isError($connection)) {
  514.                 return $connection;
  515.             }
  516.         }
  517.         return (function_exists('ibase_affected_rows'? ibase_affected_rows($connection: 0);
  518.     }
  519.  
  520.     // }}}
  521.     // {{{ _modifyQuery()
  522.  
  523.     /**
  524.      * Changes a query string for various DBMS specific reasons
  525.      *
  526.      * @param string $query  query to modify
  527.      * @return the new (modified) query
  528.      * @access protected
  529.      */
  530.     function _modifyQuery($query$is_manip$limit$offset)
  531.     {
  532.         if ($limit > 0 && $this->dsn['dbsyntax'== 'firebird'{
  533.             $query preg_replace('/^([\s(])*SELECT(?!\s*FIRST\s*\d+)/i',
  534.                 "SELECT FIRST $limit SKIP $offset"$query);
  535.         }
  536.         return $query;
  537.     }
  538.  
  539.     // }}}
  540.     // {{{ getServerVersion()
  541.  
  542.     /**
  543.      * return version information about the server
  544.      *
  545.      * @param string     $native  determines if the raw version string should be returned
  546.      * @return mixed array with versoin information or row string
  547.      * @access public
  548.      */
  549.     function getServerVersion($native = false)
  550.     {
  551.         $ibserv = ibase_service_attach($this->dsn['hostspec']$this->options['DBA_username']$this->options['DBA_password']);
  552.         $server_info = ibase_server_info($ibservIBASE_SVC_SERVER_VERSION);
  553.         ibase_service_detach($ibserv);
  554.         if (!$native{
  555.             //WI-V1.5.3.4854 Firebird 1.5
  556.             preg_match('/-V([\d\.]*)/'$server_info$matches);
  557.             $tmp explode('.'$matches[1]);
  558.             $server_info = array(
  559.                 'major' => @$tmp[0],
  560.                 'minor' => @$tmp[1],
  561.                 'patch' => @$tmp[2],
  562.                 'extra' => @$tmp[3],
  563.                 'native' => $server_info,
  564.             );
  565.         }
  566.         return $server_info;
  567.     }
  568.  
  569.     // }}}
  570.     // {{{ prepare()
  571.  
  572.     /**
  573.      * Prepares a query for multiple execution with execute().
  574.      * With some database backends, this is emulated.
  575.      * prepare() requires a generic query as string like
  576.      * 'INSERT INTO numbers VALUES(?,?)' or
  577.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  578.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  579.      * bindParam() and the query can be send off using the execute() method.
  580.      *
  581.      * @param string $query the query to prepare
  582.      * @param mixed   $types  array that contains the types of the placeholders
  583.      * @param mixed   $result_types  array that contains the types of the columns in
  584.      *                         the result set, if set to MDB2_PREPARE_MANIP the
  585.                               query is handled as a manipulation query
  586.      * @return mixed resource handle for the prepared query on success, a MDB2
  587.      *         error on failure
  588.      * @access public
  589.      * @see bindParam, execute
  590.      */
  591.     function &prepare($query$types = null$result_types = null)
  592.     {
  593.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  594.         $this->debug($query'prepare');
  595.         $placeholder_type_guess $placeholder_type = null;
  596.         $question '?';
  597.         $colon ':';
  598.         $position = 0;
  599.         while ($position strlen($query)) {
  600.             $q_position strpos($query$question$position);
  601.             $c_position strpos($query$colon$position);
  602.             if ($q_position && $c_position{
  603.                 $p_position min($q_position$c_position);
  604.             elseif ($q_position{
  605.                 $p_position $q_position;
  606.             elseif ($c_position{
  607.                 $p_position $c_position;
  608.             else {
  609.                 break;
  610.             }
  611.             if (is_null($placeholder_type)) {
  612.                 $placeholder_type_guess $query[$p_position];
  613.             }
  614.             if (is_int($quote strpos($query"'"$position)) && $quote $p_position{
  615.                 if (!is_int($end_quote strpos($query"'"$quote + 1))) {
  616.                     $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  617.                         'prepare: query with an unterminated text string specified');
  618.                     return $err;
  619.                 }
  620.                 switch ($this->escape_quotes{
  621.                 case '':
  622.                 case "'":
  623.                     $position $end_quote + 1;
  624.                     break;
  625.                 default:
  626.                     if ($end_quote == $quote + 1{
  627.                         $position $end_quote + 1;
  628.                     else {
  629.                         if ($query[$end_quote-1== $this->escape_quotes{
  630.                             $position $end_quote;
  631.                         else {
  632.                             $position $end_quote + 1;
  633.                         }
  634.                     }
  635.                     break;
  636.                 }
  637.             elseif ($query[$position== $placeholder_type_guess{
  638.                 if ($placeholder_type_guess == '?'{
  639.                     break;
  640.                 }
  641.                 if (is_null($placeholder_type)) {
  642.                     $placeholder_type $query[$p_position];
  643.                     $question $colon $placeholder_type;
  644.                 }
  645.                 $name preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  646.                 if ($name === ''{
  647.                     $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  648.                         'prepare: named parameter with an empty name');
  649.                     return $err;
  650.                 }
  651.                 $query substr_replace($query'?'$positionstrlen($name)+1);
  652.                 $position $p_position + 1;
  653.             else {
  654.                 $position $p_position;
  655.             }
  656.         }
  657.         $connection $this->getConnection();
  658.         if (PEAR::isError($connection)) {
  659.             return $connection;
  660.         }
  661.         $statement = ibase_prepare($connection$query);
  662.  
  663.         $class_name 'MDB2_Statement_'.$this->phptype;
  664.         $obj =new $class_name($this$statement$query$types$result_types$is_manip$this->row_limit$this->row_offset);
  665.         return $obj;
  666.     }
  667.  
  668.     // }}}
  669.     // {{{ getSequenceName()
  670.  
  671.     /**
  672.      * adds sequence name formatting to a sequence name
  673.      *
  674.      * @param string $sqn name of the sequence
  675.      * @return string formatted sequence name
  676.      * @access public
  677.      */
  678.     function getSequenceName($sqn)
  679.     {
  680.         return strtoupper(parent::getSequenceName($sqn));
  681.     }
  682.  
  683.     // }}}
  684.     // {{{ nextID()
  685.  
  686.     /**
  687.      * returns the next free id of a sequence
  688.      *
  689.      * @param string $seq_name name of the sequence
  690.      * @param boolean $ondemand when true the seqence is
  691.      *                           automatic created, if it
  692.      *                           not exists
  693.      * @return mixed MDB2 Error Object or id
  694.      * @access public
  695.      */
  696.     function nextID($seq_name$ondemand = true)
  697.     {
  698.         $sequence_name $this->getSequenceName($seq_name);
  699.         $query 'SELECT GEN_ID('.$sequence_name.', 1) as the_value FROM RDB$DATABASE';
  700.         $this->expectError('*');
  701.         $result $this->queryOne($query'integer');
  702.         $this->popExpect();
  703.         if (PEAR::isError($result)) {
  704.             if ($ondemand{
  705.                 $this->loadModule('Manager');
  706.                 // Since we are creating the sequence on demand
  707.                 // we know the first id = 1 so initialize the
  708.                 // sequence at 2
  709.                 $result $this->manager->createSequence($seq_name2);
  710.                 if (PEAR::isError($result)) {
  711.                     return $this->raiseError(MDB2_ERRORnullnull,
  712.                         'nextID: on demand sequence could not be created');
  713.                 else {
  714.                     // First ID of a newly created sequence is 1
  715.                     // return 1;
  716.                     // BUT generators are not always reset, so return the actual value
  717.                     return $this->currID($seq_name);
  718.                 }
  719.             }
  720.         }
  721.         return $result;
  722.     }
  723.  
  724.     // }}}
  725.     // {{{ currID()
  726.  
  727.     /**
  728.      * returns the current id of a sequence
  729.      *
  730.      * @param string $seq_name name of the sequence
  731.      * @return mixed MDB2 Error Object or id
  732.      * @access public
  733.      */
  734.     function currID($seq_name)
  735.     {
  736.         $sequence_name $this->getSequenceName($seq_name);
  737.         $query 'SELECT GEN_ID('.$sequence_name.', 0) as the_value FROM RDB$DATABASE';
  738.         $value @$this->queryOne($query);
  739.         if (PEAR::isError($value)) {
  740.             return $this->raiseError(MDB2_ERRORnullnull,
  741.                 'currID: Unable to select from ' $seq_name;
  742.         }
  743.         if (!is_numeric($value)) {
  744.             return $this->raiseError(MDB2_ERRORnullnull,
  745.                 'currID: could not find value in sequence table');
  746.         }
  747.         return $value;
  748.     }
  749.  
  750.     // }}}
  751. }
  752.  
  753. class MDB2_Result_ibase extends MDB2_Result_Common
  754. {
  755.     // {{{ _skipLimitOffset()
  756.  
  757.     /**
  758.      * Skip the first row of a result set.
  759.      *
  760.      * @param resource $result 
  761.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  762.      * @access protected
  763.      */
  764.     function _skipLimitOffset()
  765.     {
  766.         if ($this->db->dsn['dbsyntax'== 'firebird'{
  767.             return true;
  768.         }
  769.         if ($this->limit{
  770.             if ($this->rownum $this->limit{
  771.                 return false;
  772.             }
  773.         }
  774.         if ($this->offset{
  775.             while ($this->offset_count $this->offset{
  776.                 ++$this->offset_count;
  777.                 if (!is_array(@ibase_fetch_row($this->result))) {
  778.                     $this->offset_count $this->offset;
  779.                     return false;
  780.                 }
  781.             }
  782.         }
  783.         return true;
  784.     }
  785.  
  786.     // }}}
  787.     // {{{ fetchRow()
  788.  
  789.     /**
  790.      * Fetch a row and insert the data into an existing array.
  791.      *
  792.      * @param int  $fetchmode how the array data should be indexed
  793.      * @param int  $rownum    number of the row where the data can be found
  794.      * @return int data array on success, a MDB2 error on failure
  795.      * @access public
  796.      */
  797.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  798.     {
  799.         if ($this->result === true{
  800.             //query successfully executed, but without results...
  801.             $null = null;
  802.             return $null;
  803.         }
  804.         if (!$this->_skipLimitOffset()) {
  805.             $null = null;
  806.             return $null;
  807.         }
  808.         if (!is_null($rownum)) {
  809.             $seek $this->seek($rownum);
  810.             if (PEAR::isError($seek)) {
  811.                 return $seek;
  812.             }
  813.         }
  814.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  815.             $fetchmode $this->db->fetchmode;
  816.         }
  817.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  818.             $row @ibase_fetch_assoc($this->result);
  819.             if (is_array($row)
  820.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  821.             {
  822.                 $row array_change_key_case($row$this->db->options['field_case']);
  823.             }
  824.         else {
  825.             $row @ibase_fetch_row($this->result);
  826.         }
  827.         if (!$row{
  828.             if (is_null($this->result)) {
  829.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  830.                     'fetchRow: resultset has already been freed');
  831.                 return $err;
  832.             }
  833.             $null = null;
  834.             return $null;
  835.         }
  836.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  837.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  838.         {
  839.             $this->db->_fixResultArrayValues($row$mode);
  840.         }
  841.         if (!empty($this->values)) {
  842.             $this->_assignBindColumns($row);
  843.         }
  844.         if (!empty($this->types)) {
  845.             $row $this->db->datatype->convertResultRow($this->types$row);
  846.         }
  847.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  848.             $object_class $this->db->options['fetch_class'];
  849.             if ($object_class == 'stdClass'{
  850.                 $row = (object) $row;
  851.             else {
  852.                 $row &new $object_class($row);
  853.             }
  854.         }
  855.         ++$this->rownum;
  856.         return $row;
  857.     }
  858.  
  859.     // }}}
  860.     // {{{ _getColumnNames()
  861.  
  862.     /**
  863.      * Retrieve the names of columns returned by the DBMS in a query result.
  864.      *
  865.      * @return mixed associative array variable
  866.      *       that holds the names of columns. The indexes of the array are
  867.      *       the column names mapped to lower case and the values are the
  868.      *       respective numbers of the columns starting from 0. Some DBMS may
  869.      *       not return any columns when the result set does not contain any
  870.      *       rows.
  871.      * @access private
  872.      */
  873.     function _getColumnNames()
  874.     {
  875.         $columns = array();
  876.         $numcols $this->numCols();
  877.         if (PEAR::isError($numcols)) {
  878.             return $numcols;
  879.         }
  880.         for ($column = 0; $column $numcols$column++{
  881.             $column_info @ibase_field_info($this->result$column);
  882.             $columns[$column_info['alias']] $column;
  883.         }
  884.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  885.             $columns array_change_key_case($columns$this->db->options['field_case']);
  886.         }
  887.         return $columns;
  888.     }
  889.  
  890.     // }}}
  891.     // {{{ numCols()
  892.  
  893.     /**
  894.      * Count the number of columns returned by the DBMS in a query result.
  895.      *
  896.      * @return mixed integer value with the number of columns, a MDB2 error
  897.      *       on failure
  898.      * @access public
  899.      */
  900.     function numCols()
  901.     {
  902.         if ($this->result === true{
  903.             //query successfully executed, but without results...
  904.             return 0;
  905.         }
  906.  
  907.         if (!is_resource($this->result)) {
  908.             return $this->db->raiseError('numCols(): not a valid ibase resource');
  909.         }
  910.         $cols @ibase_num_fields($this->result);
  911.         if (is_null($cols)) {
  912.             if (is_null($this->result)) {
  913.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  914.                     'numCols: resultset has already been freed');
  915.             }
  916.             return $this->db->raiseError();
  917.         }
  918.         return $cols;
  919.     }
  920.  
  921.     // }}}
  922.     // {{{ free()
  923.  
  924.     /**
  925.      * Free the internal resources associated with $result.
  926.      *
  927.      * @return boolean true on success, false if $result is invalid
  928.      * @access public
  929.      */
  930.     function free()
  931.     {
  932.         if (is_resource($this->result)) {
  933.             $free @ibase_free_result($this->result);
  934.             if (!$free{
  935.                 if (is_null($this->result)) {
  936.                     return MDB2_OK;
  937.                 }
  938.                 return $this->db->raiseError();
  939.             }
  940.         }
  941.         $this->result = null;
  942.         return MDB2_OK;
  943.     }
  944.  
  945.     // }}}
  946. }
  947.  
  948. {
  949.     // {{{ class vars
  950.  
  951.     var $buffer;
  952.     var $buffer_rownum = - 1;
  953.  
  954.     // }}}
  955.     // {{{ _fillBuffer()
  956.  
  957.     /**
  958.      * Fill the row buffer
  959.      *
  960.      * @param int $rownum   row number upto which the buffer should be filled
  961.      *                       if the row number is null all rows are ready into the buffer
  962.      * @return boolean true on success, false on failure
  963.      * @access protected
  964.      */
  965.     function _fillBuffer($rownum = null)
  966.     {
  967.         if (isset($this->buffer&& is_array($this->buffer)) {
  968.             if (is_null($rownum)) {
  969.                 if (!end($this->buffer)) {
  970.                     return false;
  971.                 }
  972.             elseif (isset($this->buffer[$rownum])) {
  973.                 return (bool) $this->buffer[$rownum];
  974.             }
  975.         }
  976.  
  977.         if (!$this->_skipLimitOffset()) {
  978.             return false;
  979.         }
  980.  
  981.         $buffer = true;
  982.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  983.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  984.             && ($buffer @ibase_fetch_row($this->result))
  985.         {
  986.             ++$this->buffer_rownum;
  987.             $this->buffer[$this->buffer_rownum$buffer;
  988.         }
  989.  
  990.         if (!$buffer{
  991.             ++$this->buffer_rownum;
  992.             $this->buffer[$this->buffer_rownum= false;
  993.             return false;
  994.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  995.             ++$this->buffer_rownum;
  996.             $this->buffer[$this->buffer_rownum= false;
  997.         }
  998.         return true;
  999.     }
  1000.  
  1001.     // }}}
  1002.     // {{{ fetchRow()
  1003.  
  1004.     /**
  1005.      * Fetch a row and insert the data into an existing array.
  1006.      *
  1007.      * @param int       $fetchmode  how the array data should be indexed
  1008.      * @param int    $rownum    number of the row where the data can be found
  1009.      * @return int data array on success, a MDB2 error on failure
  1010.      * @access public
  1011.      */
  1012.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1013.     {
  1014.         if ($this->result === true{
  1015.             //query successfully executed, but without results...
  1016.             $null = null;
  1017.             return $null;
  1018.         }
  1019.         if (is_null($this->result)) {
  1020.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1021.                 'fetchRow: resultset has already been freed');
  1022.             return $err;
  1023.         }
  1024.         if (!is_null($rownum)) {
  1025.             $seek $this->seek($rownum);
  1026.             if (PEAR::isError($seek)) {
  1027.                 return $seek;
  1028.             }
  1029.         }
  1030.         $target_rownum $this->rownum + 1;
  1031.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1032.             $fetchmode $this->db->fetchmode;
  1033.         }
  1034.         if (!$this->_fillBuffer($target_rownum)) {
  1035.             $null = null;
  1036.             return $null;
  1037.         }
  1038.         $row $this->buffer[$target_rownum];
  1039.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1040.             $column_names $this->getColumnNames();
  1041.             foreach ($column_names as $name => $i{
  1042.                 $column_names[$name$row[$i];
  1043.             }
  1044.             $row $column_names;
  1045.         }
  1046.         if (($mode ($this->db->options['portability'MDB2_PORTABILITY_RTRIM)
  1047.             + ($this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL))
  1048.         {
  1049.             $this->db->_fixResultArrayValues($row$mode);
  1050.         }
  1051.         if (!empty($this->values)) {
  1052.             $this->_assignBindColumns($row);
  1053.         }
  1054.         if (!empty($this->types)) {
  1055.             $row $this->db->datatype->convertResultRow($this->types$row);
  1056.         }
  1057.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1058.             $object_class $this->db->options['fetch_class'];
  1059.             if ($object_class == 'stdClass'{
  1060.                 $row = (object) $row;
  1061.             else {
  1062.                 $row &new $object_class($row);
  1063.             }
  1064.         }
  1065.         ++$this->rownum;
  1066.         return $row;
  1067.     }
  1068.  
  1069.     // }}}
  1070.     // {{{ seek()
  1071.  
  1072.     /**
  1073.      * seek to a specific row in a result set
  1074.      *
  1075.      * @param int    $rownum    number of the row where the data can be found
  1076.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1077.      * @access public
  1078.      */
  1079.     function seek($rownum = 0)
  1080.     {
  1081.         if (is_null($this->result)) {
  1082.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1083.                 'seek: resultset has already been freed');
  1084.         }
  1085.         $this->rownum $rownum - 1;
  1086.         return MDB2_OK;
  1087.     }
  1088.  
  1089.     // }}}
  1090.     // {{{ valid()
  1091.  
  1092.     /**
  1093.      * check if the end of the result set has been reached
  1094.      *
  1095.      * @return mixed true or false on sucess, a MDB2 error on failure
  1096.      * @access public
  1097.      */
  1098.     function valid()
  1099.     {
  1100.         if (is_null($this->result)) {
  1101.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1102.                 'valid: resultset has already been freed');
  1103.         }
  1104.         if ($this->_fillBuffer($this->rownum + 1)) {
  1105.             return true;
  1106.         }
  1107.         return false;
  1108.     }
  1109.  
  1110.     // }}}
  1111.     // {{{ numRows()
  1112.  
  1113.     /**
  1114.      * returns the number of rows in a result object
  1115.      *
  1116.      * @return mixed MDB2 Error Object or the number of rows
  1117.      * @access public
  1118.      */
  1119.     function numRows()
  1120.     {
  1121.         if (is_null($this->result)) {
  1122.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1123.                 'seek: resultset has already been freed');
  1124.         }
  1125.         $this->_fillBuffer();
  1126.         return $this->buffer_rownum;
  1127.     }
  1128.  
  1129.     // }}}
  1130.     // {{{ free()
  1131.  
  1132.     /**
  1133.      * Free the internal resources associated with $result.
  1134.      *
  1135.      * @return boolean true on success, false if $result is invalid
  1136.      * @access public
  1137.      */
  1138.     function free()
  1139.     {
  1140.         $this->buffer = null;
  1141.         $this->buffer_rownum = null;
  1142.         $free = parent::free();
  1143.     }
  1144.  
  1145.     // }}}
  1146. }
  1147.  
  1148. class MDB2_Statement_ibase extends MDB2_Statement_Common
  1149. {
  1150.     // {{{ _execute()
  1151.  
  1152.     /**
  1153.      * Execute a prepared query statement helper method.
  1154.      *
  1155.      * @param mixed $result_class string which specifies which result class to use
  1156.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1157.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1158.      * @access private
  1159.      */
  1160.     function &_execute($result_class = true$result_wrap_class = false)
  1161.     {
  1162.         $this->db->last_query = $this->query;
  1163.         $this->db->debug($this->query'execute');
  1164.         if ($this->db->getOption('disable_query')) {
  1165.             if ($this->is_manip{
  1166.                 $return = 0;
  1167.                 return $return;
  1168.             }
  1169.             $null = null;
  1170.             return $null;
  1171.         }
  1172.  
  1173.         $connection $this->db->getConnection();
  1174.         if (PEAR::isError($connection)) {
  1175.             return $connection;
  1176.         }
  1177.  
  1178.         $parameters = array(0 => $this->statement);
  1179.         $i = 0;
  1180.         foreach ($this->values as $parameter => $value{
  1181.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1182.             $parameters[$this->db->quote($value$typefalse);
  1183.             ++$i;
  1184.         }
  1185.  
  1186.         $result call_user_func_array('ibase_execute'$parameters);
  1187.         if ($result === false{
  1188.             $err =$this->db->raiseError();
  1189.             return $err;
  1190.         }
  1191.  
  1192.         if ($this->is_manip{
  1193.             $affected_rows $this->db->_affectedRows($connection);
  1194.             return $affected_rows;
  1195.         }
  1196.  
  1197.         $result =$this->db->_wrapResult($result$this->types,
  1198.             $result_class$result_wrap_class$this->row_limit$this->row_offset);
  1199.         return $result;
  1200.     }
  1201.  
  1202.     // }}}
  1203.  
  1204.     // }}}
  1205.     // {{{ free()
  1206.  
  1207.     /**
  1208.      * Release resources allocated for the specified prepared query.
  1209.      *
  1210.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1211.      * @access public
  1212.      */
  1213.     function free()
  1214.     {
  1215.         if (!@ibase_free_query($this->statement)) {
  1216.             return $this->db->raiseError();
  1217.         }
  1218.         return MDB2_OK;
  1219.     }
  1220. }
  1221. ?>

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