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

Source for file oci8.php

Documentation is available at oci8.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5                                                 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith                                         |
  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: oci8.php,v 1.213 2008/03/08 14:18:39 quipo Exp $
  47.  
  48. /**
  49.  * MDB2 OCI8 driver
  50.  *
  51.  * @package MDB2
  52.  * @category Database
  53.  * @author Lukas Smith <smith@pooteeweet.org>
  54.  */
  55. class MDB2_Driver_oci8 extends MDB2_Driver_Common
  56. {
  57.     // {{{ properties
  58.     var $string_quoting = array('start' => "'"'end' => "'"'escape' => "'"'escape_pattern' => '@');
  59.  
  60.     var $identifier_quoting = array('start' => '"''end' => '"''escape' => '"');
  61.  
  62.     var $uncommitedqueries = 0;
  63.     // }}}
  64.     // {{{ constructor
  65.  
  66.     /**
  67.      * Constructor
  68.      */
  69.     function __construct()
  70.     {
  71.         parent::__construct();
  72.  
  73.         $this->phptype 'oci8';
  74.         $this->dbsyntax 'oci8';
  75.  
  76.         $this->supported['sequences'= true;
  77.         $this->supported['indexes'= true;
  78.         $this->supported['summary_functions'= true;
  79.         $this->supported['order_by_text'= true;
  80.         $this->supported['current_id'= true;
  81.         $this->supported['affected_rows'= true;
  82.         $this->supported['transactions'= true;
  83.         $this->supported['savepoints'= true;
  84.         $this->supported['limit_queries'= true;
  85.         $this->supported['LOBs'= true;
  86.         $this->supported['replace''emulated';
  87.         $this->supported['sub_selects'= true;
  88.         $this->supported['triggers'= true;
  89.         $this->supported['auto_increment'= false; // implementation is broken
  90.         $this->supported['primary_key'= true;
  91.         $this->supported['result_introspection'= true;
  92.         $this->supported['prepared_statements'= true;
  93.         $this->supported['identifier_quoting'= true;
  94.         $this->supported['pattern_escaping'= true;
  95.         $this->supported['new_link'= true;
  96.  
  97.         $this->options['DBA_username'= false;
  98.         $this->options['DBA_password'= false;
  99.         $this->options['database_name_prefix'= false;
  100.         $this->options['emulate_database'= true;
  101.         $this->options['default_tablespace'= false;
  102.         $this->options['default_text_field_length'= 2000;
  103.         $this->options['lob_allow_url_include'= false;
  104.         $this->options['result_prefetching'= false;
  105.         $this->options['max_identifiers_length'= 30;
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ errorInfo()
  110.  
  111.     /**
  112.      * This method is used to collect information about an error
  113.      *
  114.      * @param integer $error 
  115.      * @return array 
  116.      * @access public
  117.      */
  118.     function errorInfo($error = null)
  119.     {
  120.         if (is_resource($error)) {
  121.             $error_data @OCIError($error);
  122.             $error = null;
  123.         elseif ($this->connection{
  124.             $error_data @OCIError($this->connection);
  125.         else {
  126.             $error_data @OCIError();
  127.         }
  128.         $native_code $error_data['code'];
  129.         $native_msg  $error_data['message'];
  130.         if (is_null($error)) {
  131.             static $ecode_map;
  132.             if (empty($ecode_map)) {
  133.                 $ecode_map = array(
  134.                     1    => MDB2_ERROR_CONSTRAINT,
  135.                     900  => MDB2_ERROR_SYNTAX,
  136.                     904  => MDB2_ERROR_NOSUCHFIELD,
  137.                     911  => MDB2_ERROR_SYNTAX//invalid character
  138.                     913  => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  139.                     921  => MDB2_ERROR_SYNTAX,
  140.                     923  => MDB2_ERROR_SYNTAX,
  141.                     942  => MDB2_ERROR_NOSUCHTABLE,
  142.                     955  => MDB2_ERROR_ALREADY_EXISTS,
  143.                     1400 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  144.                     1401 => MDB2_ERROR_INVALID,
  145.                     1407 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  146.                     1418 => MDB2_ERROR_NOT_FOUND,
  147.                     1435 => MDB2_ERROR_NOT_FOUND,
  148.                     1476 => MDB2_ERROR_DIVZERO,
  149.                     1722 => MDB2_ERROR_INVALID_NUMBER,
  150.                     2289 => MDB2_ERROR_NOSUCHTABLE,
  151.                     2291 => MDB2_ERROR_CONSTRAINT,
  152.                     2292 => MDB2_ERROR_CONSTRAINT,
  153.                     2449 => MDB2_ERROR_CONSTRAINT,
  154.                     24344 => MDB2_ERROR_SYNTAX//success with compilation error
  155.                 );
  156.             }
  157.             if (isset($ecode_map[$native_code])) {
  158.                 $error $ecode_map[$native_code];
  159.             }
  160.         }
  161.         return array($error$native_code$native_msg);
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ beginTransaction()
  166.  
  167.     /**
  168.      * Start a transaction or set a savepoint.
  169.      *
  170.      * @param   string  name of a savepoint to set
  171.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  172.      *
  173.      * @access  public
  174.      */
  175.     function beginTransaction($savepoint = null)
  176.     {
  177.         $this->debug('Starting transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  178.         if (!is_null($savepoint)) {
  179.             if (!$this->in_transaction{
  180.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  181.                     'savepoint cannot be released when changes are auto committed'__FUNCTION__);
  182.             }
  183.             $query 'SAVEPOINT '.$savepoint;
  184.             return $this->_doQuery($querytrue);
  185.         elseif ($this->in_transaction{
  186.             return MDB2_OK;  //nothing to do
  187.         }
  188.         if (!$this->destructor_registered && $this->opened_persistent{
  189.             $this->destructor_registered = true;
  190.             register_shutdown_function('MDB2_closeOpenTransactions');
  191.         }
  192.         $this->in_transaction = true;
  193.         ++$this->uncommitedqueries;
  194.         return MDB2_OK;
  195.     }
  196.  
  197.     // }}}
  198.     // {{{ commit()
  199.  
  200.     /**
  201.      * Commit the database changes done during a transaction that is in
  202.      * progress or release a savepoint. This function may only be called when
  203.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  204.      * transaction is implicitly started after committing the pending changes.
  205.      *
  206.      * @param   string  name of a savepoint to release
  207.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  208.      *
  209.      * @access  public
  210.      */
  211.     function commit($savepoint = null)
  212.     {
  213.         $this->debug('Committing transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  214.         if (!$this->in_transaction{
  215.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  216.                 'commit/release savepoint cannot be done changes are auto committed'__FUNCTION__);
  217.         }
  218.         if (!is_null($savepoint)) {
  219.             return MDB2_OK;
  220.         }
  221.  
  222.         if ($this->uncommitedqueries{
  223.             $connection $this->getConnection();
  224.             if (PEAR::isError($connection)) {
  225.                 return $connection;
  226.             }
  227.             if (!@OCICommit($connection)) {
  228.                 return $this->raiseError(nullnullnull,
  229.                 'Unable to commit transaction'__FUNCTION__);
  230.             }
  231.             $this->uncommitedqueries = 0;
  232.         }
  233.         $this->in_transaction = false;
  234.         return MDB2_OK;
  235.     }
  236.  
  237.     // }}}
  238.     // {{{ rollback()
  239.  
  240.     /**
  241.      * Cancel any database changes done during a transaction or since a specific
  242.      * savepoint that is in progress. This function may only be called when
  243.      * auto-committing is disabled, otherwise it will fail. Therefore, a new
  244.      * transaction is implicitly started after canceling the pending changes.
  245.      *
  246.      * @param   string  name of a savepoint to rollback to
  247.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  248.      *
  249.      * @access  public
  250.      */
  251.     function rollback($savepoint = null)
  252.     {
  253.         $this->debug('Rolling back transaction/savepoint'__FUNCTION__array('is_manip' => true'savepoint' => $savepoint));
  254.         if (!$this->in_transaction{
  255.             return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  256.                 'rollback cannot be done changes are auto committed'__FUNCTION__);
  257.         }
  258.         if (!is_null($savepoint)) {
  259.             $query 'ROLLBACK TO SAVEPOINT '.$savepoint;
  260.             return $this->_doQuery($querytrue);
  261.         }
  262.  
  263.         if ($this->uncommitedqueries{
  264.             $connection $this->getConnection();
  265.             if (PEAR::isError($connection)) {
  266.                 return $connection;
  267.             }
  268.             if (!@OCIRollback($connection)) {
  269.                 return $this->raiseError(nullnullnull,
  270.                 'Unable to rollback transaction'__FUNCTION__);
  271.             }
  272.             $this->uncommitedqueries = 0;
  273.         }
  274.         $this->in_transaction = false;
  275.         return MDB2_OK;
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ function setTransactionIsolation()
  280.  
  281.     /**
  282.      * Set the transacton isolation level.
  283.      *
  284.      * @param   string  standard isolation level
  285.      *                   READ UNCOMMITTED (allows dirty reads)
  286.      *                   READ COMMITTED (prevents dirty reads)
  287.      *                   REPEATABLE READ (prevents nonrepeatable reads)
  288.      *                   SERIALIZABLE (prevents phantom reads)
  289.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  290.      *
  291.      * @access  public
  292.      * @since   2.1.1
  293.      */
  294.     function setTransactionIsolation($isolation)
  295.     {
  296.         $this->debug('Setting transaction isolation level'__FUNCTION__array('is_manip' => true));
  297.         switch ($isolation{
  298.         case 'READ UNCOMMITTED':
  299.             $isolation 'READ COMMITTED';
  300.         case 'READ COMMITTED':
  301.         case 'REPEATABLE READ':
  302.             $isolation 'SERIALIZABLE';
  303.         case 'SERIALIZABLE':
  304.             break;
  305.         default:
  306.             return $this->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  307.                 'isolation level is not supported: '.$isolation__FUNCTION__);
  308.         }
  309.  
  310.         $query = "ALTER SESSION ISOLATION LEVEL $isolation";
  311.         return $this->_doQuery($querytrue);
  312.     }
  313.  
  314.     // }}}
  315.     // {{{ _doConnect()
  316.  
  317.     /**
  318.      * do the grunt work of the connect
  319.      *
  320.      * @return connection on success or MDB2 Error Object on failure
  321.      * @access protected
  322.      */
  323.     function _doConnect($username$password$persistent = false)
  324.     {
  325.         if (!PEAR::loadExtension($this->phptype)) {
  326.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  327.                 'extension '.$this->phptype.' is not compiled into PHP'__FUNCTION__);
  328.         }
  329.  
  330.         $sid '';
  331.  
  332.         if (!empty($this->dsn['service']&& $this->dsn['hostspec']{
  333.             //oci8://username:password@foo.example.com[:port]/?service=service
  334.             // service name is given, it is assumed that hostspec is really a
  335.             // hostname, we try to construct an oracle connection string from this
  336.             $port $this->dsn['port'$this->dsn['port': 1521;
  337.             $sid sprintf("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
  338.                             (HOST=%s) (PORT=%s)))
  339.                             (CONNECT_DATA=(SERVICE_NAME=%s)))",
  340.                 $this->dsn['hostspec'],
  341.                 $port,
  342.                 $this->dsn['service']
  343.             );
  344.         elseif ($this->dsn['hostspec']{
  345.             // we are given something like 'oci8://username:password@foo/'
  346.             // we have hostspec but not a service name, now we assume that
  347.             // hostspec is a tnsname defined in tnsnames.ora
  348.             $sid $this->dsn['hostspec'];
  349.             if (isset($this->dsn['port']&& $this->dsn['port']{
  350.                 $sid $sid.':'.$this->dsn['port'];
  351.             }
  352.         else {
  353.             // oci://username:password@
  354.             // if everything fails, we have to rely on environment variables
  355.             // not before a check to 'emulate_database'
  356.             if (!$this->options['emulate_database'&& $this->database_name{
  357.                 $sid $this->database_name;
  358.             elseif (getenv('ORACLE_SID')) {
  359.                 $sid getenv('ORACLE_SID');
  360.             elseif ($sid getenv('TWO_TASK')) {
  361.                 $sid getenv('TWO_TASK');
  362.             else {
  363.                 return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  364.                     'not a valid connection string or environment variable [ORACLE_SID|TWO_TASK] not set',
  365.                     __FUNCTION__);
  366.             }
  367.         }
  368.  
  369.         if (function_exists('oci_connect')) {
  370.             if (isset($this->dsn['new_link'])
  371.                 && ($this->dsn['new_link'== 'true' || $this->dsn['new_link'=== true)
  372.             {
  373.                 $connect_function 'oci_new_connect';
  374.             else {
  375.                 $connect_function $persistent 'oci_pconnect' 'oci_connect';
  376.             }
  377.  
  378.             $charset = empty($this->dsn['charset']? null : $this->dsn['charset'];
  379.             $session_mode = empty($this->dsn['session_mode']? null : $this->dsn['session_mode'];
  380.             $connection @$connect_function($username$password$sid$charset$session_mode);
  381.             $error @OCIError();
  382.             if (isset($error['code']&& $error['code'== 12541{
  383.                 // Couldn't find TNS listener.  Try direct connection.
  384.                 $connection @$connect_function($username$passwordnull$charset);
  385.             }
  386.         else {
  387.             $connect_function $persistent 'OCIPLogon' 'OCILogon';
  388.             $connection @$connect_function($username$password$sid);
  389.  
  390.             if (!empty($this->dsn['charset'])) {
  391.                 $result $this->setCharset($this->dsn['charset']$connection);
  392.                 if (PEAR::isError($result)) {
  393.                     return $result;
  394.                 }
  395.             }
  396.         }
  397.  
  398.         if (!$connection{
  399.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  400.                 'unable to establish a connection'__FUNCTION__);
  401.         }
  402.  
  403.        if (empty($this->dsn['disable_iso_date'])) {
  404.             $query "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
  405.             $err =$this->_doQuery($querytrue$connection);
  406.             if (PEAR::isError($err)) {
  407.                 $this->disconnect(false);
  408.                 return $err;
  409.             }
  410.        }
  411.  
  412.         $query "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
  413.         $err =$this->_doQuery($querytrue$connection);
  414.         if (PEAR::isError($err)) {
  415.             $this->disconnect(false);
  416.             return $err;
  417.         }
  418.  
  419.         return $connection;
  420.     }
  421.  
  422.     // }}}
  423.     // {{{ connect()
  424.  
  425.     /**
  426.      * Connect to the database
  427.      *
  428.      * @return MDB2_OK on success, MDB2 Error Object on failure
  429.      * @access public
  430.      */
  431.     function connect()
  432.     {
  433.         if (is_resource($this->connection)) {
  434.             //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
  435.             if (MDB2::areEquals($this->connected_dsn$this->dsn)
  436.                 && $this->opened_persistent == $this->options['persistent']
  437.             {
  438.                 return MDB2_OK;
  439.             }
  440.             $this->disconnect(false);
  441.         }
  442.  
  443.         if ($this->database_name && $this->options['emulate_database']{
  444.              $this->dsn['username'$this->options['database_name_prefix'].$this->database_name;
  445.         }
  446.  
  447.         $connection $this->_doConnect($this->dsn['username'],
  448.                                         $this->dsn['password'],
  449.                                         $this->options['persistent']);
  450.         if (PEAR::isError($connection)) {
  451.             return $connection;
  452.         }
  453.         $this->connection $connection;
  454.         $this->connected_dsn $this->dsn;
  455.         $this->connected_database_name '';
  456.         $this->opened_persistent $this->options['persistent'];
  457.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  458.  
  459.         if ($this->database_name{
  460.             if ($this->database_name != $this->connected_database_name{
  461.                 $query 'ALTER SESSION SET CURRENT_SCHEMA = "' .strtoupper($this->database_name.'"';
  462.                 $result =$this->_doQuery($query);
  463.                 if (PEAR::isError($result)) {
  464.                     $err $this->raiseError($resultnullnull,
  465.                         'Could not select the database: '.$this->database_name__FUNCTION__);
  466.                     return $err;
  467.                 }
  468.                 $this->connected_database_name $this->database_name;
  469.             }
  470.         }
  471.  
  472.         $this->as_keyword ' ';
  473.         $server_info $this->getServerVersion();
  474.         if (is_array($server_info)) {
  475.             if ($server_info['major'>= '10'{
  476.                 $this->as_keyword ' AS ';
  477.             }
  478.         }
  479.         return MDB2_OK;
  480.     }
  481.  
  482.     // }}}
  483.     // {{{ databaseExists()
  484.  
  485.     /**
  486.      * check if given database name is exists?
  487.      *
  488.      * @param string $name    name of the database that should be checked
  489.      *
  490.      * @return mixed true/false on success, a MDB2 error on failure
  491.      * @access public
  492.      */
  493.     function databaseExists($name)
  494.     {
  495.         $connection $this->_doConnect($this->dsn['username'],
  496.                                         $this->dsn['password'],
  497.                                         $this->options['persistent']);
  498.         if (PEAR::isError($connection)) {
  499.             return $connection;
  500.         }
  501.  
  502.         $query 'ALTER SESSION SET CURRENT_SCHEMA = "' .strtoupper($name.'"';
  503.         $result =$this->_doQuery($querytrue$connectionfalse);
  504.         if (PEAR::isError($result)) {
  505.             if (!MDB2::isError($resultMDB2_ERROR_NOT_FOUND)) {
  506.                 return $result;
  507.             }
  508.             return false;
  509.         }
  510.         return true;
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ disconnect()
  515.  
  516.     /**
  517.      * Log out and disconnect from the database.
  518.      *
  519.      * @param  boolean $force if the disconnect should be forced even if the
  520.      *                         connection is opened persistently
  521.      * @return mixed true on success, false if not connected and error
  522.      *                 object on error
  523.      * @access public
  524.      */
  525.     function disconnect($force = true)
  526.     {
  527.         if (is_resource($this->connection)) {
  528.             if ($this->in_transaction{
  529.                 $dsn $this->dsn;
  530.                 $database_name $this->database_name;
  531.                 $persistent $this->options['persistent'];
  532.                 $this->dsn $this->connected_dsn;
  533.                 $this->database_name $this->connected_database_name;
  534.                 $this->options['persistent'$this->opened_persistent;
  535.                 $this->rollback();
  536.                 $this->dsn $dsn;
  537.                 $this->database_name $database_name;
  538.                 $this->options['persistent'$persistent;
  539.             }
  540.  
  541.             if (!$this->opened_persistent || $force{
  542.                 if (function_exists('oci_close')) {
  543.                     @oci_close($this->connection);
  544.                 else {
  545.                     @OCILogOff($this->connection);
  546.                 }
  547.             }
  548.             $this->uncommitedqueries = 0;
  549.         }
  550.         return parent::disconnect($force);
  551.     }
  552.  
  553.     // }}}
  554.     // {{{ standaloneQuery()
  555.  
  556.     /**
  557.      * execute a query as DBA
  558.      *
  559.      * @param string $query     the SQL query
  560.      * @param mixed  $types     array containing the types of the columns in
  561.      *                           the result set
  562.      * @param boolean $is_manip if the query is a manipulation query
  563.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  564.      * @access public
  565.      */
  566.     function &standaloneQuery($query$types = null$is_manip = false)
  567.     {
  568.         $user $this->options['DBA_username']$this->options['DBA_username'$this->dsn['username'];
  569.         $pass $this->options['DBA_password']$this->options['DBA_password'$this->dsn['password'];
  570.         $connection $this->_doConnect($user$pass$this->options['persistent']);
  571.         if (PEAR::isError($connection)) {
  572.             return $connection;
  573.         }
  574.  
  575.         $offset $this->offset;
  576.         $limit $this->limit;
  577.         $this->offset $this->limit = 0;
  578.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  579.  
  580.         $result =$this->_doQuery($query$is_manip$connectionfalse);
  581.         if (!PEAR::isError($result)) {
  582.             if ($is_manip{
  583.                 $result $this->_affectedRows($connection$result);
  584.             else {
  585.                 $result =$this->_wrapResult($result$typestruefalse$limit$offset);
  586.             }
  587.         }
  588.  
  589.         @OCILogOff($connection);
  590.         return $result;
  591.     }
  592.  
  593.     // }}}
  594.     // {{{ _modifyQuery()
  595.  
  596.     /**
  597.      * Changes a query string for various DBMS specific reasons
  598.      *
  599.      * @param string $query  query to modify
  600.      * @param boolean $is_manip  if it is a DML query
  601.      * @param integer $limit  limit the number of rows
  602.      * @param integer $offset  start reading from given offset
  603.      * @return string modified query
  604.      * @access protected
  605.      */
  606.     function _modifyQuery($query$is_manip$limit$offset)
  607.     {
  608.         if (preg_match('/^\s*SELECT/i'$query)) {
  609.             if (!preg_match('/\sFROM\s/i'$query)) {
  610.                 $query.= " FROM dual";
  611.             }
  612.             if ($limit > 0{
  613.                 // taken from http://svn.ez.no/svn/ezcomponents/packages/Database
  614.                 $max $offset $limit;
  615.                 if ($offset > 0{
  616.                     $min $offset + 1;
  617.                     $query = "SELECT * FROM (SELECT a.*, ROWNUM mdb2rn FROM ($query) a WHERE ROWNUM <= $max) WHERE mdb2rn >= $min";
  618.                 else {
  619.                     $query = "SELECT a.* FROM ($query) a WHERE ROWNUM <= $max";
  620.                 }
  621.             }
  622.         }
  623.         return $query;
  624.     }
  625.  
  626.     // }}}
  627.     // {{{ _doQuery()
  628.  
  629.     /**
  630.      * Execute a query
  631.      * @param string $query  query
  632.      * @param boolean $is_manip  if the query is a manipulation query
  633.      * @param resource $connection 
  634.      * @param string $database_name 
  635.      * @return result or error object
  636.      * @access protected
  637.      */
  638.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  639.     {
  640.         $this->last_query $query;
  641.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  642.         if ($result{
  643.             if (PEAR::isError($result)) {
  644.                 return $result;
  645.             }
  646.             $query $result;
  647.         }
  648.         if ($this->getOption('disable_query')) {
  649.             if ($is_manip{
  650.                 return 0;
  651.             }
  652.             return null;
  653.         }
  654.  
  655.         if (is_null($connection)) {
  656.             $connection $this->getConnection();
  657.             if (PEAR::isError($connection)) {
  658.                 return $connection;
  659.             }
  660.         }
  661.  
  662.         $query str_replace("\r\n""\n"$query)//for fixing end-of-line character in the PL/SQL in windows
  663.         $result @OCIParse($connection$query);
  664.         if (!$result{
  665.             $err $this->raiseError(nullnullnull,
  666.                 'Could not create statement'__FUNCTION__);
  667.             return $err;
  668.         }
  669.  
  670.         $mode $this->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  671.         if (!@OCIExecute($result$mode)) {
  672.             $err =$this->raiseError($resultnullnull,
  673.                 'Could not execute statement'__FUNCTION__);
  674.             return $err;
  675.         }
  676.  
  677.         if (is_numeric($this->options['result_prefetching'])) {
  678.             @ocisetprefetch($result$this->options['result_prefetching']);
  679.         }
  680.  
  681.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  682.         return $result;
  683.     }
  684.  
  685.     // }}}
  686.     // {{{ _affectedRows()
  687.  
  688.     /**
  689.      * Returns the number of rows affected
  690.      *
  691.      * @param resource $result 
  692.      * @param resource $connection 
  693.      * @return mixed MDB2 Error Object or the number of rows affected
  694.      * @access private
  695.      */
  696.     function _affectedRows($connection$result = null)
  697.     {
  698.         if (is_null($connection)) {
  699.             $connection $this->getConnection();
  700.             if (PEAR::isError($connection)) {
  701.                 return $connection;
  702.             }
  703.         }
  704.         return @OCIRowCount($result);
  705.     }
  706.  
  707.     // }}}
  708.     // {{{ getServerVersion()
  709.  
  710.     /**
  711.      * return version information about the server
  712.      *
  713.      * @param bool   $native  determines if the raw version string should be returned
  714.      * @return mixed array/string with version information or MDB2 error object
  715.      * @access public
  716.      */
  717.     function getServerVersion($native = false)
  718.     {
  719.         $connection $this->getConnection();
  720.         if (PEAR::isError($connection)) {
  721.             return $connection;
  722.         }
  723.         if ($this->connected_server_info{
  724.             $server_info $this->connected_server_info;
  725.         else {
  726.             $server_info @ociserverversion($connection);
  727.         }
  728.         if (!$server_info{
  729.             return $this->raiseError(nullnullnull,
  730.                 'Could not get server information'__FUNCTION__);
  731.         }
  732.         // cache server_info
  733.         $this->connected_server_info $server_info;
  734.         if (!$native{
  735.             if (!preg_match('/ (\d+)\.(\d+)\.(\d+)\.([\d\.]+) /'$server_info$tmp)) {
  736.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  737.                     'Could not parse version information:'.$server_info__FUNCTION__);
  738.             }
  739.             $server_info = array(
  740.                 'major' => $tmp[1],
  741.                 'minor' => $tmp[2],
  742.                 'patch' => $tmp[3],
  743.                 'extra' => $tmp[4],
  744.                 'native' => $server_info,
  745.             );
  746.         }
  747.         return $server_info;
  748.     }
  749.  
  750.     // }}}
  751.     // {{{ prepare()
  752.  
  753.     /**
  754.      * Prepares a query for multiple execution with execute().
  755.      * With some database backends, this is emulated.
  756.      * prepare() requires a generic query as string like
  757.      * 'INSERT INTO numbers VALUES(?,?)' or
  758.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  759.      * The ? and :name and are placeholders which can be set using
  760.      * bindParam() and the query can be sent off using the execute() method.
  761.      * The allowed format for :name can be set with the 'bindname_format' option.
  762.      *
  763.      * @param string $query the query to prepare
  764.      * @param mixed   $types  array that contains the types of the placeholders
  765.      * @param mixed   $result_types  array that contains the types of the columns in
  766.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  767.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  768.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  769.      * @return mixed resource handle for the prepared query on success, a MDB2
  770.      *         error on failure
  771.      * @access public
  772.      * @see bindParam, execute
  773.      */
  774.     function &prepare($query$types = null$result_types = null$lobs = array())
  775.     {
  776.         if ($this->options['emulate_prepared']{
  777.             $obj =parent::prepare($query$types$result_types$lobs);
  778.             return $obj;
  779.         }
  780.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  781.         $offset $this->offset;
  782.         $limit $this->limit;
  783.         $this->offset $this->limit = 0;
  784.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  785.         if ($result{
  786.             if (PEAR::isError($result)) {
  787.                 return $result;
  788.             }
  789.             $query $result;
  790.         }
  791.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  792.         $placeholder_type_guess $placeholder_type = null;
  793.         $question '?';
  794.         $colon ':';
  795.         $positions = array();
  796.         $position = 0;
  797.         $parameter = -1;
  798.         while ($position strlen($query)) {
  799.             $q_position strpos($query$question$position);
  800.             $c_position strpos($query$colon$position);
  801.             if ($q_position && $c_position{
  802.                 $p_position min($q_position$c_position);
  803.             elseif ($q_position{
  804.                 $p_position $q_position;
  805.             elseif ($c_position{
  806.                 $p_position $c_position;
  807.             else {
  808.                 break;
  809.             }
  810.             if (is_null($placeholder_type)) {
  811.                 $placeholder_type_guess $query[$p_position];
  812.             }
  813.             
  814.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  815.             if (PEAR::isError($new_pos)) {
  816.                 return $new_pos;
  817.             }
  818.             if ($new_pos != $position{
  819.                 $position $new_pos;
  820.                 continue; //evaluate again starting from the new position
  821.             }
  822.  
  823.             if ($query[$position== $placeholder_type_guess{
  824.                 if (is_null($placeholder_type)) {
  825.                     $placeholder_type $query[$p_position];
  826.                     $question $colon $placeholder_type;
  827.                     if (!empty($types&& is_array($types)) {
  828.                         if ($placeholder_type == ':'{
  829.                             if (is_int(key($types))) {
  830.                                 $types_tmp $types;
  831.                                 $types = array();
  832.                                 $count = -1;
  833.                             }
  834.                         else {
  835.                             $types array_values($types);
  836.                         }
  837.                     }
  838.                 }
  839.                 if ($placeholder_type == ':'{
  840.                     $regexp '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
  841.                     $parameter preg_replace($regexp'\\1'$query);
  842.                     if ($parameter === ''{
  843.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  844.                             'named parameter name must match "bindname_format" option'__FUNCTION__);
  845.                         return $err;
  846.                     }
  847.                     // use parameter name in type array
  848.                     if (isset($count&& isset($types_tmp[++$count])) {
  849.                         $types[$parameter$types_tmp[$count];
  850.                     }
  851.                     $length strlen($parameter+ 1;
  852.                 else {
  853.                     ++$parameter;
  854.                     $length strlen($parameter);
  855.                 }
  856.                 if (!in_array($parameter$positions)) {
  857.                     $positions[$parameter;
  858.                 }
  859.                 if (isset($types[$parameter])
  860.                     && ($types[$parameter== 'clob' || $types[$parameter== 'blob')
  861.                 {
  862.                     if (!isset($lobs[$parameter])) {
  863.                         $lobs[$parameter$parameter;
  864.                     }
  865.                     $value $this->quote(true$types[$parameter]);
  866.                     if (PEAR::isError($value)) {
  867.                         return $value;
  868.                     }
  869.                     $query substr_replace($query$value$p_position$length);
  870.                     $position $p_position strlen($value- 1;
  871.                 elseif ($placeholder_type == '?'{
  872.                     $query substr_replace($query':'.$parameter$p_position1);
  873.                     $position $p_position $length;
  874.                 else {
  875.                     $position $p_position + 1;
  876.                 }
  877.             else {
  878.                 $position $p_position;
  879.             }
  880.         }
  881.         if (is_array($lobs)) {
  882.             $columns $variables '';
  883.             foreach ($lobs as $parameter => $field{
  884.                 $columns.= ($columns ', ' ' RETURNING ').$field;
  885.                 $variables.= ($variables ', ' ' INTO ').':'.$parameter;
  886.             }
  887.             $query.= $columns.$variables;
  888.         }
  889.         $connection $this->getConnection();
  890.         if (PEAR::isError($connection)) {
  891.             return $connection;
  892.         }
  893.         $statement @OCIParse($connection$query);
  894.         if (!$statement{
  895.             $err =$this->raiseError(nullnullnull,
  896.                 'Could not create statement'__FUNCTION__);
  897.             return $err;
  898.         }
  899.  
  900.         $class_name 'MDB2_Statement_'.$this->phptype;
  901.         $obj = new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  902.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  903.         return $obj;
  904.     }
  905.  
  906.     // }}}
  907.     // {{{ nextID()
  908.  
  909.     /**
  910.      * Returns the next free id of a sequence
  911.      *
  912.      * @param string $seq_name name of the sequence
  913.      * @param boolean $ondemand when true the sequence is
  914.      *                            automatic created, if it
  915.      *                            not exists
  916.      * @return mixed MDB2 Error Object or id
  917.      * @access public
  918.      */
  919.     function nextID($seq_name$ondemand = true)
  920.     {
  921.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  922.         $query = "SELECT $sequence_name.nextval FROM DUAL";
  923.         $this->pushErrorHandling(PEAR_ERROR_RETURN);
  924.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  925.         $result $this->queryOne($query'integer');
  926.         $this->popExpect();
  927.         $this->popErrorHandling();
  928.         if (PEAR::isError($result)) {
  929.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  930.                 $this->loadModule('Manager'nulltrue);
  931.                 $result $this->manager->createSequence($seq_name);
  932.                 if (PEAR::isError($result)) {
  933.                     return $result;
  934.                 }
  935.                 return $this->nextId($seq_namefalse);
  936.             }
  937.         }
  938.         return $result;
  939.     }
  940.  
  941.     // }}}
  942.     // {{{ lastInsertID()
  943.  
  944.     /**
  945.      * Returns the autoincrement ID if supported or $id or fetches the current
  946.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  947.      *
  948.      * @param string $table name of the table into which a new row was inserted
  949.      * @param string $field name of the field into which a new row was inserted
  950.      * @return mixed MDB2 Error Object or id
  951.      * @access public
  952.      */
  953.     function lastInsertID($table = null$field = null)
  954.     {
  955.         $seq $table.(empty($field'' '_'.$field);
  956.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq)true);
  957.         return $this->queryOne("SELECT $sequence_name.currval"'integer');
  958.     }
  959.  
  960.     // }}}
  961.     // {{{ currId()
  962.  
  963.     /**
  964.      * Returns the current id of a sequence
  965.      *
  966.      * @param string $seq_name name of the sequence
  967.      * @return mixed MDB2_Error or id
  968.      * @access public
  969.      */
  970.     function currId($seq_name)
  971.     {
  972.         $sequence_name $this->getSequenceName($seq_name);
  973.         $query 'SELECT (last_number-1) FROM all_sequences';
  974.         $query.= ' WHERE sequence_name='.$this->quote($sequence_name'text');
  975.         $query.= ' OR sequence_name='.$this->quote(strtoupper($sequence_name)'text');
  976.         return $this->queryOne($query'integer');
  977.     }
  978. }
  979.  
  980. /**
  981.  * MDB2 OCI8 result driver
  982.  *
  983.  * @package MDB2
  984.  * @category Database
  985.  * @author Lukas Smith <smith@pooteeweet.org>
  986.  */
  987. class MDB2_Result_oci8 extends MDB2_Result_Common
  988. {
  989.     // }}}
  990.     // {{{ fetchRow()
  991.  
  992.     /**
  993.      * Fetch a row and insert the data into an existing array.
  994.      *
  995.      * @param int       $fetchmode  how the array data should be indexed
  996.      * @param int    $rownum    number of the row where the data can be found
  997.      * @return int data array on success, a MDB2 error on failure
  998.      * @access public
  999.      */
  1000.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1001.     {
  1002.         if (!is_null($rownum)) {
  1003.             $seek $this->seek($rownum);
  1004.             if (PEAR::isError($seek)) {
  1005.                 return $seek;
  1006.             }
  1007.         }
  1008.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1009.             $fetchmode $this->db->fetchmode;
  1010.         }
  1011.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1012.             @OCIFetchInto($this->result$rowOCI_ASSOC+OCI_RETURN_NULLS);
  1013.             if (is_array($row)
  1014.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1015.             {
  1016.                 $row array_change_key_case($row$this->db->options['field_case']);
  1017.             }
  1018.         else {
  1019.             @OCIFetchInto($this->result$rowOCI_RETURN_NULLS);
  1020.         }
  1021.         if (!$row{
  1022.             if ($this->result === false{
  1023.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1024.                     'resultset has already been freed'__FUNCTION__);
  1025.                 return $err;
  1026.             }
  1027.             $null = null;
  1028.             return $null;
  1029.         }
  1030.         // remove additional column at the end
  1031.         if ($this->offset > 0{
  1032.             array_pop($row);
  1033.         }
  1034.         $mode = 0;
  1035.         $rtrim = false;
  1036.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1037.             if (empty($this->types)) {
  1038.                 $mode += MDB2_PORTABILITY_RTRIM;
  1039.             else {
  1040.                 $rtrim = true;
  1041.             }
  1042.         }
  1043.         if ($mode{
  1044.             $this->db->_fixResultArrayValues($row$mode);
  1045.         }
  1046.         if (!empty($this->types)) {
  1047.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1048.         }
  1049.         if (!empty($this->values)) {
  1050.             $this->_assignBindColumns($row);
  1051.         }
  1052.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1053.             $object_class $this->db->options['fetch_class'];
  1054.             if ($object_class == 'stdClass'{
  1055.                 $row = (object) $row;
  1056.             else {
  1057.                 $row &new $object_class($row);
  1058.             }
  1059.         }
  1060.         ++$this->rownum;
  1061.         return $row;
  1062.     }
  1063.  
  1064.     // }}}
  1065.     // {{{ _getColumnNames()
  1066.  
  1067.     /**
  1068.      * Retrieve the names of columns returned by the DBMS in a query result.
  1069.      *
  1070.      * @return  mixed   Array variable that holds the names of columns as keys
  1071.      *                   or an MDB2 error on failure.
  1072.      *                   Some DBMS may not return any columns when the result set
  1073.      *                   does not contain any rows.
  1074.      * @access private
  1075.      */
  1076.     function _getColumnNames()
  1077.     {
  1078.         $columns = array();
  1079.         $numcols $this->numCols();
  1080.         if (PEAR::isError($numcols)) {
  1081.             return $numcols;
  1082.         }
  1083.         for ($column = 0; $column $numcols$column++{
  1084.             $column_name @OCIColumnName($this->result$column + 1);
  1085.             $columns[$column_name$column;
  1086.         }
  1087.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1088.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1089.         }
  1090.         return $columns;
  1091.     }
  1092.  
  1093.     // }}}
  1094.     // {{{ numCols()
  1095.  
  1096.     /**
  1097.      * Count the number of columns returned by the DBMS in a query result.
  1098.      *
  1099.      * @return mixed integer value with the number of columns, a MDB2 error
  1100.      *       on failure
  1101.      * @access public
  1102.      */
  1103.     function numCols()
  1104.     {
  1105.         $cols @OCINumCols($this->result);
  1106.         if (is_null($cols)) {
  1107.             if ($this->result === false{
  1108.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1109.                     'resultset has already been freed'__FUNCTION__);
  1110.             elseif (is_null($this->result)) {
  1111.                 return count($this->types);
  1112.             }
  1113.             return $this->db->raiseError(nullnullnull,
  1114.                 'Could not get column count'__FUNCTION__);
  1115.         }
  1116.         if ($this->offset > 0{
  1117.             --$cols;
  1118.         }
  1119.         return $cols;
  1120.     }
  1121.  
  1122.     // }}}
  1123.     // {{{ free()
  1124.  
  1125.     /**
  1126.      * Free the internal resources associated with $result.
  1127.      *
  1128.      * @return boolean true on success, false if $result is invalid
  1129.      * @access public
  1130.      */
  1131.     function free()
  1132.     {
  1133.         if (is_resource($this->result&& $this->db->connection{
  1134.             $free @OCIFreeCursor($this->result);
  1135.             if ($free === false{
  1136.                 return $this->db->raiseError(nullnullnull,
  1137.                     'Could not free result'__FUNCTION__);
  1138.             }
  1139.         }
  1140.         $this->result = false;
  1141.         return MDB2_OK;
  1142.  
  1143.     }
  1144. }
  1145.  
  1146. /**
  1147.  * MDB2 OCI8 buffered result driver
  1148.  *
  1149.  * @package MDB2
  1150.  * @category Database
  1151.  * @author Lukas Smith <smith@pooteeweet.org>
  1152.  */
  1153. {
  1154.     var $buffer;
  1155.     var $buffer_rownum = - 1;
  1156.  
  1157.     // {{{ _fillBuffer()
  1158.  
  1159.     /**
  1160.      * Fill the row buffer
  1161.      *
  1162.      * @param int $rownum   row number upto which the buffer should be filled
  1163.                             if the row number is null all rows are ready into the buffer
  1164.      * @return boolean true on success, false on failure
  1165.      * @access protected
  1166.      */
  1167.     function _fillBuffer($rownum = null)
  1168.     {
  1169.         if (isset($this->buffer&& is_array($this->buffer)) {
  1170.             if (is_null($rownum)) {
  1171.                 if (!end($this->buffer)) {
  1172.                     return false;
  1173.                 }
  1174.             elseif (isset($this->buffer[$rownum])) {
  1175.                 return (bool)$this->buffer[$rownum];
  1176.             }
  1177.         }
  1178.  
  1179.         $row = true;
  1180.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  1181.             && ($row @OCIFetchInto($this->result$bufferOCI_RETURN_NULLS))
  1182.         {
  1183.             ++$this->buffer_rownum;
  1184.             // remove additional column at the end
  1185.             if ($this->offset > 0{
  1186.                 array_pop($buffer);
  1187.             }
  1188.             if (empty($this->types)) {
  1189.                 foreach (array_keys($bufferas $key{
  1190.                     if (is_a($buffer[$key]'oci-lob')) {
  1191.                         $buffer[$key$buffer[$key]->load();
  1192.                     }
  1193.                 }
  1194.             }
  1195.             $this->buffer[$this->buffer_rownum$buffer;
  1196.         }
  1197.  
  1198.         if (!$row{
  1199.             ++$this->buffer_rownum;
  1200.             $this->buffer[$this->buffer_rownum= false;
  1201.             return false;
  1202.         }
  1203.         return true;
  1204.     }
  1205.  
  1206.     // }}}
  1207.     // {{{ fetchRow()
  1208.  
  1209.     /**
  1210.      * Fetch a row and insert the data into an existing array.
  1211.      *
  1212.      * @param int       $fetchmode  how the array data should be indexed
  1213.      * @param int    $rownum    number of the row where the data can be found
  1214.      * @return int data array on success, a MDB2 error on failure
  1215.      * @access public
  1216.      */
  1217.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1218.     {
  1219.         if ($this->result === false{
  1220.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1221.                 'resultset has already been freed'__FUNCTION__);
  1222.             return $err;
  1223.         elseif (is_null($this->result)) {
  1224.             return null;
  1225.         }
  1226.         if (!is_null($rownum)) {
  1227.             $seek $this->seek($rownum);
  1228.             if (PEAR::isError($seek)) {
  1229.                 return $seek;
  1230.             }
  1231.         }
  1232.         $target_rownum $this->rownum + 1;
  1233.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1234.             $fetchmode $this->db->fetchmode;
  1235.         }
  1236.         if (!$this->_fillBuffer($target_rownum)) {
  1237.             $null = null;
  1238.             return $null;
  1239.         }
  1240.         $row $this->buffer[$target_rownum];
  1241.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1242.             $column_names $this->getColumnNames();
  1243.             foreach ($column_names as $name => $i{
  1244.                 $column_names[$name$row[$i];
  1245.             }
  1246.             $row $column_names;
  1247.         }
  1248.         $mode = 0;
  1249.         $rtrim = false;
  1250.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1251.             if (empty($this->types)) {
  1252.                 $mode += MDB2_PORTABILITY_RTRIM;
  1253.             else {
  1254.                 $rtrim = true;
  1255.             }
  1256.         }
  1257.         if ($mode{
  1258.             $this->db->_fixResultArrayValues($row$mode);
  1259.         }
  1260.         if (!empty($this->types)) {
  1261.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1262.         }
  1263.         if (!empty($this->values)) {
  1264.             $this->_assignBindColumns($row);
  1265.         }
  1266.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1267.             $object_class $this->db->options['fetch_class'];
  1268.             if ($object_class == 'stdClass'{
  1269.                 $row = (object) $row;
  1270.             else {
  1271.                 $row &new $object_class($row);
  1272.             }
  1273.         }
  1274.         ++$this->rownum;
  1275.         return $row;
  1276.     }
  1277.  
  1278.     // }}}
  1279.     // {{{ seek()
  1280.  
  1281.     /**
  1282.      * Seek to a specific row in a result set
  1283.      *
  1284.      * @param int    $rownum    number of the row where the data can be found
  1285.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1286.      * @access public
  1287.      */
  1288.     function seek($rownum = 0)
  1289.     {
  1290.         if ($this->result === false{
  1291.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1292.                 'resultset has already been freed'__FUNCTION__);
  1293.         }
  1294.         $this->rownum $rownum - 1;
  1295.         return MDB2_OK;
  1296.     }
  1297.  
  1298.     // }}}
  1299.     // {{{ valid()
  1300.  
  1301.     /**
  1302.      * Check if the end of the result set has been reached
  1303.      *
  1304.      * @return mixed true or false on sucess, a MDB2 error on failure
  1305.      * @access public
  1306.      */
  1307.     function valid()
  1308.     {
  1309.         if ($this->result === false{
  1310.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1311.                 'resultset has already been freed'__FUNCTION__);
  1312.         elseif (is_null($this->result)) {
  1313.             return true;
  1314.         }
  1315.         if ($this->_fillBuffer($this->rownum + 1)) {
  1316.             return true;
  1317.         }
  1318.         return false;
  1319.     }
  1320.  
  1321.     // }}}
  1322.     // {{{ numRows()
  1323.  
  1324.     /**
  1325.      * Returns the number of rows in a result object
  1326.      *
  1327.      * @return mixed MDB2 Error Object or the number of rows
  1328.      * @access public
  1329.      */
  1330.     function numRows()
  1331.     {
  1332.         if ($this->result === false{
  1333.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1334.                 'resultset has already been freed'__FUNCTION__);
  1335.         elseif (is_null($this->result)) {
  1336.             return 0;
  1337.         }
  1338.         $this->_fillBuffer();
  1339.         return $this->buffer_rownum;
  1340.     }
  1341.  
  1342.     // }}}
  1343.     // {{{ free()
  1344.  
  1345.     /**
  1346.      * Free the internal resources associated with $result.
  1347.      *
  1348.      * @return boolean true on success, false if $result is invalid
  1349.      * @access public
  1350.      */
  1351.     function free()
  1352.     {
  1353.         $this->buffer = null;
  1354.         $this->buffer_rownum = null;
  1355.         return parent::free();
  1356.     }
  1357. }
  1358.  
  1359. /**
  1360.  * MDB2 OCI8 statement driver
  1361.  *
  1362.  * @package MDB2
  1363.  * @category Database
  1364.  * @author Lukas Smith <smith@pooteeweet.org>
  1365.  */
  1366. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1367. {
  1368.     // {{{ Variables (Properties)
  1369.  
  1370.     var $type_maxlengths = array();
  1371.  
  1372.     // }}}
  1373.     // {{{ bindParam()
  1374.  
  1375.     /**
  1376.      * Bind a variable to a parameter of a prepared query.
  1377.      *
  1378.      * @param   int     $parameter  the order number of the parameter in the query
  1379.      *                                statement. The order number of the first parameter is 1.
  1380.      * @param   mixed   &$value     variable that is meant to be bound to specified
  1381.      *                                parameter. The type of the value depends on the $type argument.
  1382.      * @param   string  $type       specifies the type of the field
  1383.      * @param   int     $maxlength  specifies the maximum length of the field; if set to -1, the
  1384.      *                                current length of $value is used
  1385.      *
  1386.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  1387.      *
  1388.      * @access  public
  1389.      */
  1390.     function bindParam($parameter&$value$type = null$maxlength = -1)
  1391.     {
  1392.         if (!is_numeric($parameter)) {
  1393.             $parameter preg_replace('/^:(.*)$/''\\1'$parameter);
  1394.         }
  1395.         if (MDB2_OK === ($ret = parent::bindParam($parameter$value$type))) {
  1396.             $this->type_maxlengths[$parameter$maxlength;
  1397.         }
  1398.  
  1399.         return $ret;
  1400.     }
  1401.  
  1402.     // }}}
  1403.     // {{{ _execute()
  1404.  
  1405.     /**
  1406.      * Execute a prepared query statement helper method.
  1407.      *
  1408.      * @param mixed $result_class string which specifies which result class to use
  1409.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1410.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1411.      * @access private
  1412.      */
  1413.     function &_execute($result_class = true$result_wrap_class = false)
  1414.     {
  1415.         if (is_null($this->statement)) {
  1416.             $result =parent::_execute($result_class$result_wrap_class);
  1417.             return $result;
  1418.         }
  1419.         $this->db->last_query = $this->query;
  1420.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1421.         if ($this->db->getOption('disable_query')) {
  1422.             $result $this->is_manip ? 0 : null;
  1423.             return $result;
  1424.         }
  1425.  
  1426.         $connection $this->db->getConnection();
  1427.         if (PEAR::isError($connection)) {
  1428.             return $connection;
  1429.         }
  1430.  
  1431.         $result = MDB2_OK;
  1432.         $lobs $quoted_values = array();
  1433.         $i = 0;
  1434.         foreach ($this->positions as $parameter{
  1435.             if (!array_key_exists($parameter$this->values)) {
  1436.                 return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1437.                     'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1438.             }
  1439.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1440.             if ($type == 'clob' || $type == 'blob'{
  1441.                 $lobs[$i]['file'= false;
  1442.                 if (is_resource($this->values[$parameter])) {
  1443.                     $fp $this->values[$parameter];
  1444.                     $this->values[$parameter'';
  1445.                     while (!feof($fp)) {
  1446.                         $this->values[$parameter.= fread($fp8192);
  1447.                     }
  1448.                 elseif (is_a($this->values[$parameter]'OCI-Lob')) {
  1449.                     //do nothing
  1450.                 elseif ($this->db->getOption('lob_allow_url_include')
  1451.                           && preg_match('/^(\w+:\/\/)(.*)$/'$this->values[$parameter]$match)
  1452.                 {
  1453.                     $lobs[$i]['file'= true;
  1454.                     if ($match[1== 'file://'{
  1455.                         $this->values[$parameter$match[2];
  1456.                     }
  1457.                 }
  1458.                 $lobs[$i]['value'$this->values[$parameter];
  1459.                 $lobs[$i]['descriptor'=$this->values[$parameter];
  1460.                 // Test to see if descriptor has already been created for this
  1461.                 // variable (i.e. if it has been bound more than once):
  1462.                 if (!is_a($this->values[$parameter]'OCI-Lob')) {
  1463.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_LOB);
  1464.                     if ($this->values[$parameter=== false{
  1465.                         $result $this->db->raiseError(nullnullnull,
  1466.                             'Unable to create descriptor for LOB in parameter: '.$parameter__FUNCTION__);
  1467.                         break;
  1468.                     }
  1469.                 }
  1470.                 $lob_type ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1471.                 if (!@OCIBindByName($this->statement':'.$parameter$lobs[$i]['descriptor']-1$lob_type)) {
  1472.                     $result $this->db->raiseError($this->statementnullnull,
  1473.                         'could not bind LOB parameter'__FUNCTION__);
  1474.                     break;
  1475.                 }
  1476.             else if ($type == OCI_B_BFILE{
  1477.                 // Test to see if descriptor has already been created for this
  1478.                 // variable (i.e. if it has been bound more than once):
  1479.                 if (!is_a($this->values[$parameter]"OCI-Lob")) {
  1480.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_FILE);
  1481.                     if ($this->values[$parameter=== false{
  1482.                         $result $this->db->raiseError(nullnullnull,
  1483.                             'Unable to create descriptor for BFILE in parameter: '.$parameter__FUNCTION__);
  1484.                         break;
  1485.                     }
  1486.                 }
  1487.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1488.                     $result $this->db->raiseError($this->statementnullnull,
  1489.                         'Could not bind BFILE parameter'__FUNCTION__);
  1490.                     break;
  1491.                 }
  1492.             else if ($type == OCI_B_ROWID{
  1493.                 // Test to see if descriptor has already been created for this
  1494.                 // variable (i.e. if it has been bound more than once):
  1495.                 if (!is_a($this->values[$parameter]"OCI-Lob")) {
  1496.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_ROWID);
  1497.                     if ($this->values[$parameter=== false{
  1498.                         $result $this->db->raiseError(nullnullnull,
  1499.                             'Unable to create descriptor for ROWID in parameter: '.$parameter__FUNCTION__);
  1500.                         break;
  1501.                     }
  1502.                 }
  1503.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1504.                     $result $this->db->raiseError($this->statementnullnull,
  1505.                         'Could not bind ROWID parameter'__FUNCTION__);
  1506.                     break;
  1507.                 }
  1508.             else if ($type == OCI_B_CURSOR{
  1509.                 // Test to see if cursor has already been allocated for this
  1510.                 // variable (i.e. if it has been bound more than once):
  1511.                 if (!is_resource($this->values[$parameter]|| !get_resource_type($this->values[$parameter]== "oci8 statement"{
  1512.                     $this->values[$parameter@OCINewCursor($connection);
  1513.                     if ($this->values[$parameter=== false{
  1514.                         $result $this->db->raiseError(nullnullnull,
  1515.                         'Unable to allocate cursor for parameter: '.$parameter__FUNCTION__);
  1516.                     break;
  1517.                     }
  1518.                 }
  1519.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1520.                     $result $this->db->raiseError($this->statementnullnull,
  1521.                         'Could not bind CURSOR parameter'__FUNCTION__);
  1522.                     break;
  1523.                 }
  1524.             else {
  1525.                 $maxlength array_key_exists($parameter$this->type_maxlengths$this->type_maxlengths[$parameter: -1;
  1526.                 $this->values[$parameter$this->db->quote($this->values[$parameter]$typefalse);
  1527.                 $quoted_values[$i=$this->values[$parameter];
  1528.                 if (PEAR::isError($quoted_values[$i])) {
  1529.                     return $quoted_values[$i];
  1530.                 }
  1531.                 if (!@OCIBindByName($this->statement':'.$parameter$quoted_values[$i]$maxlength)) {
  1532.                     $result $this->db->raiseError($this->statementnullnull,
  1533.                         'could not bind non-abstract parameter'__FUNCTION__);
  1534.                     break;
  1535.                 }
  1536.             }
  1537.             ++$i;
  1538.         }
  1539.  
  1540.         $lob_keys array_keys($lobs);
  1541.         if (!PEAR::isError($result)) {
  1542.             $mode (!empty($lobs|| $this->db->in_transaction? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1543.             if (!@OCIExecute($this->statement$mode)) {
  1544.                 $err =$this->db->raiseError($this->statementnullnull,
  1545.                     'could not execute statement'__FUNCTION__);
  1546.                 return $err;
  1547.             }
  1548.  
  1549.             if (!empty($lobs)) {
  1550.                 foreach ($lob_keys as $i{
  1551.                     if (!is_null($lobs[$i]['value']&& $lobs[$i]['value'!== ''{
  1552.                         if (is_object($lobs[$i]['value'])) {
  1553.                             // Probably a NULL LOB
  1554.                             // @see http://bugs.php.net/bug.php?id=27485
  1555.                             continue;
  1556.                         }
  1557.                         if ($lobs[$i]['file']{
  1558.                             $result $lobs[$i]['descriptor']->savefile($lobs[$i]['value']);
  1559.                         else {
  1560.                             $result $lobs[$i]['descriptor']->save($lobs[$i]['value']);
  1561.                         }
  1562.                         if (!$result{
  1563.                             $result $this->db->raiseError(nullnullnull,
  1564.                                 'Unable to save descriptor contents'__FUNCTION__);
  1565.                             break;
  1566.                         }
  1567.                     }
  1568.                 }
  1569.  
  1570.                 if (!PEAR::isError($result)) {
  1571.                     if (!$this->db->in_transaction{
  1572.                         if (!@OCICommit($connection)) {
  1573.                             $result $this->db->raiseError(nullnullnull,
  1574.                                 'Unable to commit transaction'__FUNCTION__);
  1575.                         }
  1576.                     else {
  1577.                         ++$this->db->uncommitedqueries;
  1578.                     }
  1579.                 }
  1580.             }
  1581.         }
  1582.  
  1583.         if (PEAR::isError($result)) {
  1584.             return $result;
  1585.         }
  1586.  
  1587.         if ($this->is_manip{
  1588.             $affected_rows $this->db->_affectedRows($connection$this->statement);
  1589.             return $affected_rows;
  1590.         }
  1591.  
  1592.         $result =$this->db->_wrapResult($this->statement$this->result_types,
  1593.             $result_class$result_wrap_class$this->limit$this->offset);
  1594.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1595.         return $result;
  1596.     }
  1597.  
  1598.     // }}}
  1599.     // {{{ free()
  1600.  
  1601.     /**
  1602.      * Release resources allocated for the specified prepared query.
  1603.      *
  1604.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1605.      * @access public
  1606.      */
  1607.     function free()
  1608.     {
  1609.         if (is_null($this->positions)) {
  1610.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1611.                 'Prepared statement has already been freed'__FUNCTION__);
  1612.         }
  1613.         $result = MDB2_OK;
  1614.  
  1615.         if (!is_null($this->statement&& !@OCIFreeStatement($this->statement)) {
  1616.             $result $this->db->raiseError(nullnullnull,
  1617.                 'Could not free statement'__FUNCTION__);
  1618.         }
  1619.  
  1620.         parent::free();
  1621.         return $result;
  1622.     }
  1623. }
  1624. ?>

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