MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file fbsql.php

Documentation is available at fbsql.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-2006 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith, Frank M. Kromann                       |
  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: Lukas Smith <smith@pooteeweet.org>                           |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: fbsql.php 327310 2012-08-27 15:16:18Z danielc $
  47. //
  48.  
  49. /**
  50.  * MDB2 FrontBase driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lukas Smith <smith@pooteeweet.org>
  55.  * @author  Frank M. Kromann <frank@kromann.info>
  56.  */
  57. class MDB2_Driver_fbsql extends MDB2_Driver_Common
  58. {
  59.     // {{{ properties
  60.     var $string_quoting = array('start' => "'"'end' => "'"'escape' => "'"'escape_pattern' => false);
  61.  
  62.     var $identifier_quoting = array('start' => '"''end' => '"''escape' => '"');
  63.     // }}}
  64.     // {{{ constructor
  65.  
  66.     /**
  67.      * Constructor
  68.      */
  69.     function __construct()
  70.     {
  71.         parent::__construct();
  72.  
  73.         $this->phptype 'fbsql';
  74.         $this->dbsyntax 'fbsql';
  75.  
  76.         $this->supported['sequences''emulated';
  77.         $this->supported['indexes'= true;
  78.         $this->supported['affected_rows'= true;
  79.         $this->supported['transactions'= true;
  80.         $this->supported['savepoints'= false;
  81.         $this->supported['summary_functions'= true;
  82.         $this->supported['order_by_text'= true;
  83.         $this->supported['current_id''emulated';
  84.         $this->supported['limit_queries''emulated';
  85.         $this->supported['LOBs'= true;
  86.         $this->supported['replace'='emulated';
  87.         $this->supported['sub_selects'= true;
  88.         $this->supported['auto_increment'= false; // not implemented
  89.         $this->supported['primary_key'= true;
  90.         $this->supported['result_introspection'= true;
  91.         $this->supported['prepared_statements''emulated';
  92.         $this->supported['identifier_quoting'= true;
  93.         $this->supported['pattern_escaping'= false;
  94.         $this->supported['new_link'= false;
  95.  
  96.         $this->options['DBA_username'= false;
  97.         $this->options['DBA_password'= false;
  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.        if ($this->connection{
  113.            $native_code @fbsql_errno($this->connection);
  114.            $native_msg  @fbsql_error($this->connection);
  115.        else {
  116.            $native_code @fbsql_errno();
  117.            $native_msg  @fbsql_error();
  118.        }
  119.         if (null === $error{
  120.             static $ecode_map;
  121.             if (empty($ecode_map)) {
  122.                 $ecode_map = array(
  123.                      22 => MDB2_ERROR_SYNTAX,
  124.                      85 => MDB2_ERROR_ALREADY_EXISTS,
  125.                     108 => MDB2_ERROR_SYNTAX,
  126.                     116 => MDB2_ERROR_NOSUCHTABLE,
  127.                     124 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  128.                     215 => MDB2_ERROR_NOSUCHFIELD,
  129.                     217 => MDB2_ERROR_INVALID_NUMBER,
  130.                     226 => MDB2_ERROR_NOSUCHFIELD,
  131.                     231 => MDB2_ERROR_INVALID,
  132.                     239 => MDB2_ERROR_TRUNCATED,
  133.                     251 => MDB2_ERROR_SYNTAX,
  134.                     266 => MDB2_ERROR_NOT_FOUND,
  135.                     357 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  136.                     358 => MDB2_ERROR_CONSTRAINT,
  137.                     360 => MDB2_ERROR_CONSTRAINT,
  138.                     361 => MDB2_ERROR_CONSTRAINT,
  139.                 );
  140.             }
  141.             if (isset($ecode_map[$native_code])) {
  142.                 $error $ecode_map[$native_code];
  143.             }
  144.         }
  145.         return array($error$native_code$native_msg);
  146.     }
  147.  
  148.     // }}}
  149.     // {{{ beginTransaction()
  150.  
  151.     /**
  152.      * Start a transaction or set a savepoint.
  153.      *
  154.      * @param   string  name of a savepoint to set
  155.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  156.      *
  157.      * @access  public
  158.      */
  159.     function beginTransaction($savepoint = null)
  160.     {
  161.         $this->debug('Starting transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  162.         if (null !== $savepoint{
  163.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  164.                 'savepoints are not supported'__FUNCTION__);
  165.         }
  166.         if ($this->in_transaction{
  167.             return MDB2_OK;  //nothing to do
  168.         }
  169.         if (!$this->destructor_registered && $this->opened_persistent{
  170.             $this->destructor_registered = true;
  171.             register_shutdown_function('MDB2_closeOpenTransactions');
  172.         }
  173.         $result =$this->_doQuery('SET COMMIT FALSE;'true);
  174.         if (MDB2::isError($result)) {
  175.             return $result;
  176.         }
  177.         $this->in_transaction = true;
  178.         return MDB2_OK;
  179.     }
  180.  
  181.     // }}}
  182.     // {{{ commit()
  183.  
  184.     /**
  185.      * Commit the database changes done during a transaction that is in
  186.      * progress or release a savepoint. This function may only be called when
  187.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  188.      * transaction is implicitly started after committing the pending changes.
  189.      *
  190.      * @param   string  name of a savepoint to release
  191.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  192.      *
  193.      * @access  public
  194.      */
  195.     function commit($savepoint = null)
  196.     {
  197.         $this->debug('Committing transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  198.         if (!$this->in_transaction{
  199.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  200.                 'commit/release savepoint cannot be done changes are auto committed'__FUNCTION__);
  201.         }
  202.         if (null !== $savepoint{
  203.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  204.                 'savepoints are not supported'__FUNCTION__);
  205.         }
  206.  
  207.         $result =$this->_doQuery('COMMIT;'true);
  208.         if (MDB2::isError($result)) {
  209.             return $result;
  210.         }
  211.         $result =$this->_doQuery('SET COMMIT TRUE;'true);
  212.         if (MDB2::isError($result)) {
  213.             return $result;
  214.         }
  215.         $this->in_transaction = false;
  216.         return MDB2_OK;
  217.     }
  218.  
  219.     // }}}
  220.     // {{{
  221.  
  222.     /**
  223.      * Cancel any database changes done during a transaction or since a specific
  224.      * savepoint that is in progress. This function may only be called when
  225.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  226.      * transaction is implicitly started after canceling the pending changes.
  227.      *
  228.      * @param   string  name of a savepoint to rollback to
  229.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  230.      *
  231.      * @access  public
  232.      */
  233.     function rollback($savepoint = null)
  234.     {
  235.         $this->debug('Rolling back transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  236.         if (!$this->in_transaction{
  237.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  238.                 'rollback cannot be done changes are auto committed'__FUNCTION__);
  239.         }
  240.         if (null !== $savepoint{
  241.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  242.                 'savepoints are not supported'__FUNCTION__);
  243.         }
  244.  
  245.         $result =$this->_doQuery('ROLLBACK;'true);
  246.         if (MDB2::isError($result)) {
  247.             return $result;
  248.         }
  249.         $result =$this->_doQuery('SET COMMIT TRUE;'true);
  250.         if (MDB2::isError($result)) {
  251.             return $result;
  252.         }
  253.         $this->in_transaction = false;
  254.         return MDB2_OK;
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ _doConnect()
  259.  
  260.     /**
  261.      * do the grunt work of the connect
  262.      *
  263.      * @return connection on success or MDB2 Error Object on failure
  264.      * @access protected
  265.      */
  266.     function _doConnect($username$password$persistent = false)
  267.     {
  268.         if (!extension_loaded($this->phptype)) {
  269.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  270.                 'extension '.$this->phptype.' is not compiled into PHP'__FUNCTION__);
  271.         }
  272.  
  273.         $params = array(
  274.             $this->dsn['hostspec'$this->dsn['hostspec''localhost',
  275.             $username $username : null,
  276.             $password $password : null,
  277.         );
  278.  
  279.         $connect_function $persistent 'fbsql_pconnect' 'fbsql_connect';
  280.  
  281.         $connection @call_user_func_array($connect_function$params);
  282.         if ($connection <= 0{
  283.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  284.                 'unable to establish a connection'__FUNCTION__);
  285.         }
  286.  
  287.         if (!empty($this->dsn['charset'])) {
  288.             $result $this->setCharset($this->dsn['charset']$connection);
  289.             if (MDB2::isError($result)) {
  290.                 return $result;
  291.             }
  292.         }
  293.  
  294.         return $connection;
  295.     }
  296.  
  297.     // }}}
  298.     // {{{ connect()
  299.  
  300.     /**
  301.      * Connect to the database
  302.      *
  303.      * @return true on success, MDB2 Error Object on failure
  304.      ***/
  305.     function connect()
  306.     {
  307.         if (is_resource($this->connection)) {
  308.             //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
  309.             if (MDB2::areEquals($this->connected_dsn$this->dsn)
  310.                 && $this->opened_persistent == $this->options['persistent']
  311.             {
  312.                 return MDB2_OK;
  313.             }
  314.             $this->disconnect(false);
  315.         }
  316.  
  317.         $connection $this->_doConnect($this->dsn['username'],
  318.                                         $this->dsn['password'],
  319.                                         $this->options['persistent']);
  320.         if (MDB2::isError($connection)) {
  321.             return $connection;
  322.         }
  323.  
  324.         $this->connection $connection;
  325.         $this->connected_dsn $this->dsn;
  326.         $this->connected_database_name '';
  327.         $this->opened_persistent $this->options['persistent'];
  328.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  329.  
  330.         if ($this->database_name{
  331.             if ($this->database_name != $this->connected_database_name{
  332.                 if (!@fbsql_select_db($this->database_name$connection)) {
  333.                     $err $this->raiseError(nullnullnull,
  334.                         'Could not select the database: '.$this->database_name__FUNCTION__);
  335.                     return $err;
  336.                 }
  337.                 $this->connected_database_name $this->database_name;
  338.             }
  339.         }
  340.  
  341.         return MDB2_OK;
  342.     }
  343.  
  344.     // }}}
  345.     // {{{ databaseExists()
  346.  
  347.     /**
  348.      * check if given database name is exists?
  349.      *
  350.      * @param string $name    name of the database that should be checked
  351.      *
  352.      * @return mixed true/false on success, a MDB2 error on failure
  353.      * @access public
  354.      */
  355.     function databaseExists($name)
  356.     {
  357.         $connection $this->_doConnect($this->dsn['username'],
  358.                                         $this->dsn['password'],
  359.                                         $this->options['persistent']);
  360.         if (MDB2::isError($connection)) {
  361.             return $connection;
  362.         }
  363.  
  364.         $result @fbsql_select_db($name$connection);
  365.         @fbsql_close($connection);
  366.  
  367.         return $result;
  368.     }
  369.  
  370.     // }}}
  371.     // {{{ disconnect()
  372.  
  373.     /**
  374.      * Log out and disconnect from the database.
  375.      *
  376.      * @param  boolean $force if the disconnect should be forced even if the
  377.      *                         connection is opened persistently
  378.      * @return mixed true on success, false if not connected and error
  379.      *                 object on error
  380.      * @access public
  381.      */
  382.     function disconnect($force = true)
  383.     {
  384.         if (is_resource($this->connection)) {
  385.             if ($this->in_transaction{
  386.                 $dsn $this->dsn;
  387.                 $database_name $this->database_name;
  388.                 $persistent $this->options['persistent'];
  389.                 $this->dsn $this->connected_dsn;
  390.                 $this->database_name $this->connected_database_name;
  391.                 $this->options['persistent'$this->opened_persistent;
  392.                 $this->rollback();
  393.                 $this->dsn $dsn;
  394.                 $this->database_name $database_name;
  395.                 $this->options['persistent'$persistent;
  396.             }
  397.  
  398.             if (!$this->opened_persistent || $force{
  399.                 @fbsql_close($this->connection);
  400.             }
  401.         }
  402.         return parent::disconnect($force);
  403.     }
  404.  
  405.     // }}}
  406.     // {{{ standaloneQuery()
  407.  
  408.    /**
  409.      * execute a query as DBA
  410.      *
  411.      * @param string $query the SQL query
  412.      * @param mixed   $types  array that contains the types of the columns in
  413.      *                         the result set
  414.      * @param boolean $is_manip  if the query is a manipulation query
  415.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  416.      * @access public
  417.      */
  418.     function &standaloneQuery($query$types = null$is_manip = false)
  419.     {
  420.         $user $this->options['DBA_username']$this->options['DBA_username'$this->dsn['username'];
  421.         $pass $this->options['DBA_password']$this->options['DBA_password'$this->dsn['password'];
  422.         $connection $this->_doConnect($user$pass$this->options['persistent']);
  423.         if (MDB2::isError($connection)) {
  424.             return $connection;
  425.         }
  426.  
  427.         $offset $this->offset;
  428.         $limit $this->limit;
  429.         $this->offset $this->limit = 0;
  430.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  431.  
  432.         $result =$this->_doQuery($query$is_manip$connection$this->database_name);
  433.         if (!MDB2::isError($result)) {
  434.             $result $this->_affectedRows($connection$result);
  435.         }
  436.  
  437.         @fbsql_close($connection);
  438.         return $result;
  439.     }
  440.  
  441.     // }}}
  442.     // {{{ _doQuery()
  443.  
  444.     /**
  445.      * Execute a query
  446.      * @param string $query  query
  447.      * @param boolean $is_manip  if the query is a manipulation query
  448.      * @param resource $connection 
  449.      * @param string $database_name 
  450.      * @return result or error object
  451.      * @access protected
  452.      */
  453.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  454.     {
  455.         $this->last_query $query;
  456.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  457.         if ($result{
  458.             if (MDB2::isError($result)) {
  459.                 return $result;
  460.             }
  461.             $query $result;
  462.         }
  463.         if ($this->options['disable_query']{
  464.             $result $is_manip ? 0 : null;
  465.             return $result;
  466.         }
  467.  
  468.         if (null === $connection{
  469.             $connection $this->getConnection();
  470.             if (MDB2::isError($connection)) {
  471.                 return $connection;
  472.             }
  473.         }
  474.         if (null === $database_name{
  475.             $database_name $this->database_name;
  476.         }
  477.  
  478.         if ($database_name{
  479.             if ($database_name != $this->connected_database_name{
  480.                 if (!@fbsql_select_db($database_name$connection)) {
  481.                     $err $this->raiseError(nullnullnull,
  482.                         'Could not select the database: '.$database_name__FUNCTION__);
  483.                     return $err;
  484.                 }
  485.                 $this->connected_database_name $database_name;
  486.             }
  487.         }
  488.  
  489.         $result @fbsql_query($query$connection);
  490.         if (!$result{
  491.             $err =$this->raiseError(nullnullnull,
  492.                 'Could not execute statement'__FUNCTION__);
  493.             return $err;
  494.         }
  495.  
  496.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  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 (null === $connection{
  514.             $connection $this->getConnection();
  515.             if (MDB2::isError($connection)) {
  516.                 return $connection;
  517.             }
  518.         }
  519.         return @fbsql_affected_rows($connection);
  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.      * @param boolean $is_manip  if it is a DML query
  530.      * @param integer $limit  limit the number of rows
  531.      * @param integer $offset  start reading from given offset
  532.      * @return string modified query
  533.      * @access protected
  534.      */
  535.     function _modifyQuery($query$is_manip$limit$offset)
  536.     {
  537.         if ($limit > 0{
  538.             if ($is_manip{
  539.                 return preg_replace('/^([\s(])*SELECT(?!\s*TOP\s*\()/i',
  540.                     "\\1SELECT TOP($limit)"$query);
  541.             else {
  542.                 return preg_replace('/([\s(])*SELECT(?!\s*TOP\s*\()/i',
  543.                     "\\1SELECT TOP($offset,$limit)"$query);
  544.             }
  545.         }
  546.         // Add ; to the end of the query. This is required by FrontBase
  547.         return $query.';';
  548.     }
  549.  
  550.     // }}}
  551.     // {{{ nextID()
  552.  
  553.     /**
  554.      * Returns the next free id of a sequence
  555.      *
  556.      * @param string $seq_name name of the sequence
  557.      * @param boolean $ondemand when true the sequence is
  558.      *                           automatic created, if it
  559.      *                           not exists
  560.      *
  561.      * @return mixed MDB2 Error Object or id
  562.      * @access public
  563.      */
  564.     function nextID($seq_name$ondemand = true)
  565.     {
  566.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  567.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  568.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (NULL);";
  569.         $this->pushErrorHandling(PEAR_ERROR_RETURN);
  570.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  571.         $result =$this->_doQuery($querytrue);
  572.         $this->popExpect();
  573.         $this->popErrorHandling();
  574.         if (MDB2::isError($result)) {
  575.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  576.                 $this->loadModule('Manager'nulltrue);
  577.                 $result $this->manager->createSequence($seq_name);
  578.                 if (MDB2::isError($result)) {
  579.                     return $this->raiseError($resultnullnull,
  580.                         'on demand sequence '.$seq_name.' could not be created'__FUNCTION__);
  581.                 else {
  582.                     return $this->nextID($seq_namefalse);
  583.                 }
  584.             }
  585.             return $result;
  586.         }
  587.         $value $this->lastInsertID();
  588.         if (is_numeric($value)) {
  589.             $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value";
  590.             $result =$this->_doQuery($querytrue);
  591.             if (MDB2::isError($result)) {
  592.                 $this->warnings['nextID: could not delete previous sequence table values from '.$seq_name;
  593.             }
  594.         }
  595.         return $value;
  596.     }
  597.  
  598.     // }}}
  599.     // {{{ lastInsertID()
  600.  
  601.     /**
  602.      * Returns the autoincrement ID if supported or $id or fetches the current
  603.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  604.      *
  605.      * @param string $table name of the table into which a new row was inserted
  606.      * @param string $field name of the field into which a new row was inserted
  607.      * @return mixed MDB2 Error Object or id
  608.      * @access public
  609.      */
  610.     function lastInsertID($table = null$field = null)
  611.     {
  612.         $connection $this->getConnection();
  613.         if (MDB2::isError($connection)) {
  614.             return $connection;
  615.         }
  616.         $value @fbsql_insert_id($connection);
  617.         if (!$value{
  618.             return $this->raiseError(nullnullnull,
  619.                 'Could not get last insert ID'__FUNCTION__);
  620.         }
  621.         return $value;
  622.     }
  623.  
  624.     // }}}
  625.     // {{{ currID()
  626.  
  627.     /**
  628.      * Returns the current id of a sequence
  629.      *
  630.      * @param string $seq_name name of the sequence
  631.      * @return mixed MDB2 Error Object or id
  632.      * @access public
  633.      */
  634.     function currID($seq_name)
  635.     {
  636.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  637.         $seqcol_name $this->quoteIdentifier($this->options['seqcol_name']true);
  638.         $query = "SELECT MAX($seqcol_name) FROM $sequence_name";
  639.         return $this->queryOne($query'integer');
  640.     }
  641. }
  642.  
  643. /**
  644.  * MDB2 FrontbaseSQL result driver
  645.  *
  646.  * @package MDB2
  647.  * @category Database
  648.  * @author  Lukas Smith <smith@pooteeweet.org>
  649.  */
  650. class MDB2_Result_fbsql extends MDB2_Result_Common
  651. {
  652.     // }}}
  653.     // {{{ fetchRow()
  654.  
  655.     /**
  656.      * Fetch a row and insert the data into an existing array.
  657.      *
  658.      * @param int       $fetchmode  how the array data should be indexed
  659.      * @param int    $rownum    number of the row where the data can be found
  660.      * @return int data array on success, a MDB2 error on failure
  661.      * @access public
  662.      */
  663.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  664.     {
  665.         if (null !== $rownum{
  666.             $seek $this->seek($rownum);
  667.             if (MDB2::isError($seek)) {
  668.                 return $seek;
  669.             }
  670.         }
  671.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  672.             $fetchmode $this->db->fetchmode;
  673.         }
  674.         if (   $fetchmode == MDB2_FETCHMODE_ASSOC
  675.             || $fetchmode == MDB2_FETCHMODE_OBJECT
  676.         {
  677.             $row @fbsql_fetch_assoc($this->result);
  678.             if (is_array($row)
  679.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  680.             {
  681.                 $row array_change_key_case($row$this->db->options['field_case']);
  682.             }
  683.         else {
  684.            $row @fbsql_fetch_row($this->result);
  685.         }
  686.         if (!$row{
  687.             if (false === $this->result{
  688.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  689.                     'resultset has already been freed'__FUNCTION__);
  690.                 return $err;
  691.             }
  692.             $null = null;
  693.             return $null;
  694.         }
  695.         $mode $this->db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL;
  696.         if ($mode{
  697.             $this->db->_fixResultArrayValues($row$mode);
  698.         }
  699.         if (($fetchmode != MDB2_FETCHMODE_ASSOC&& !empty($this->types)) {
  700.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  701.         elseif (($fetchmode == MDB2_FETCHMODE_ASSOC&& !empty($this->types_assoc)) {
  702.             $row $this->db->datatype->convertResultRow($this->types_assoc$row$rtrim);
  703.         }
  704.         if (!empty($this->values)) {
  705.             $this->_assignBindColumns($row);
  706.         }
  707.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  708.             $object_class $this->db->options['fetch_class'];
  709.             if ($object_class == 'stdClass'{
  710.                 $row = (object) $row;
  711.             else {
  712.                 $row = new $object_class($row);
  713.             }
  714.         }
  715.         ++$this->rownum;
  716.         return $row;
  717.     }
  718.  
  719.     // }}}
  720.     // {{{ _getColumnNames()
  721.  
  722.     /**
  723.      * Retrieve the names of columns returned by the DBMS in a query result.
  724.      *
  725.      * @return  mixed   Array variable that holds the names of columns as keys
  726.      *                   or an MDB2 error on failure.
  727.      *                   Some DBMS may not return any columns when the result set
  728.      *                   does not contain any rows.
  729.      * @access private
  730.      */
  731.     function _getColumnNames()
  732.     {
  733.         $columns = array();
  734.         $numcols $this->numCols();
  735.         if (MDB2::isError($numcols)) {
  736.             return $numcols;
  737.         }
  738.         for ($column = 0; $column $numcols$column++{
  739.             $column_name @fbsql_field_name($this->result$column);
  740.             $columns[$column_name$column;
  741.         }
  742.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  743.             $columns array_change_key_case($columns$this->db->options['field_case']);
  744.         }
  745.         return $columns;
  746.     }
  747.  
  748.     // }}}
  749.     // {{{ numCols()
  750.  
  751.     /**
  752.      * Count the number of columns returned by the DBMS in a query result.
  753.      *
  754.      * @return mixed integer value with the number of columns, a MDB2 error
  755.      *                        on failure
  756.      * @access public
  757.      */
  758.     function numCols()
  759.     {
  760.         $cols @fbsql_num_fields($this->result);
  761.         if (null === $cols{
  762.             if (false === $this->result{
  763.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  764.                     'resultset has already been freed'__FUNCTION__);
  765.             }
  766.             if (null === $this->result{
  767.                 return count($this->types);
  768.             }
  769.             return $this->db->raiseError(nullnullnull,
  770.                 'Could not get column count'__FUNCTION__);
  771.         }
  772.         return $cols;
  773.     }
  774.  
  775.     // }}}
  776.     // {{{ nextResult()
  777.  
  778.     /**
  779.      * Move the internal result pointer to the next available result
  780.      *
  781.      * @return true on success, false if there is no more result set or an error object on failure
  782.      * @access public
  783.      */
  784.     function nextResult()
  785.     {
  786.         if (false === $this->result{
  787.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  788.                 'resultset has already been freed'__FUNCTION__);
  789.         }
  790.         if (null === $this->result{
  791.             return false;
  792.         }
  793.         return @fbsql_next_result($this->result);
  794.     }
  795.  
  796.     // }}}
  797.     // {{{ free()
  798.  
  799.     /**
  800.      * Free the internal resources associated with result.
  801.      *
  802.      * @return boolean true on success, false if result is invalid
  803.      * @access public
  804.      */
  805.     function free()
  806.     {
  807.         if (is_resource($this->result&& $this->db->connection{
  808.             $free @fbsql_free_result($this->result);
  809.             if (false === $free{
  810.                 return $this->db->raiseError(nullnullnull,
  811.                     'Could not free result'__FUNCTION__);
  812.             }
  813.         }
  814.         $this->result = false;
  815.         return MDB2_OK;
  816.     }
  817. }
  818.  
  819. /**
  820.  * MDB2 FrontbaseSQL buffered result driver
  821.  *
  822.  * @package MDB2
  823.  * @category Database
  824.  * @author  Lukas Smith <smith@pooteeweet.org>
  825.  */
  826.  
  827. {
  828.     // }}}
  829.     // {{{ seek()
  830.  
  831.     /**
  832.      * Seek to a specific row in a result set
  833.      *
  834.      * @param int    $rownum    number of the row where the data can be found
  835.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  836.      * @access public
  837.      */
  838.     function seek($rownum = 0)
  839.     {
  840.         if ($this->rownum != ($rownum - 1&& !@fbsql_data_seek($this->result$rownum)) {
  841.             if (false === $this->result{
  842.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  843.                     'resultset has already been freed'__FUNCTION__);
  844.             }
  845.             if (null === $this->result{
  846.                 return MDB2_OK;
  847.             }
  848.             return $this->db->raiseError(MDB2_ERROR_INVALIDnullnull,
  849.                 'tried to seek to an invalid row number ('.$rownum.')'__FUNCTION__);
  850.         }
  851.         $this->rownum $rownum - 1;
  852.         return MDB2_OK;
  853.     }
  854.  
  855.     // }}}
  856.     // {{{ valid()
  857.  
  858.     /**
  859.      * Check if the end of the result set has been reached
  860.      *
  861.      * @return mixed true or false on sucess, a MDB2 error on failure
  862.      * @access public
  863.      */
  864.     function valid()
  865.     {
  866.         $numrows $this->numRows();
  867.         if (MDB2::isError($numrows)) {
  868.             return $numrows;
  869.         }
  870.         return $this->rownum ($numrows - 1);
  871.     }
  872.  
  873.     // }}}
  874.     // {{{ numRows()
  875.  
  876.     /**
  877.      * Returns the number of rows in a result object
  878.      *
  879.      * @return mixed MDB2 Error Object or the number of rows
  880.      * @access public
  881.      */
  882.     function numRows()
  883.     {
  884.         $rows @fbsql_num_rows($this->result);
  885.         if (null === $rows{
  886.             if (false === $this->result{
  887.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  888.                     'resultset has already been freed'__FUNCTION__);
  889.             }
  890.             if (null === $this->result{
  891.                 return 0;
  892.             }
  893.             return $this->db->raiseError(nullnullnull,
  894.                 'Could not get row count'__FUNCTION__);
  895.         }
  896.         return $rows;
  897.     }
  898. }
  899.  
  900. /**
  901.  * MDB2 FrontbaseSQL statement driver
  902.  *
  903.  * @package MDB2
  904.  * @category Database
  905.  * @author  Lukas Smith <smith@pooteeweet.org>
  906.  */
  907.  
  908. class MDB2_Statement_fbsql extends MDB2_Statement_Common
  909. {
  910.  
  911. }
  912.  
  913. ?>

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