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.217 2008/11/16 21:45:08 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 ($this->_isNewLinkSet()) {
  371.                 $connect_function 'oci_new_connect';
  372.             else {
  373.                 $connect_function $persistent 'oci_pconnect' 'oci_connect';
  374.             }
  375.  
  376.             $charset = empty($this->dsn['charset']? null : $this->dsn['charset'];
  377.             $session_mode = empty($this->dsn['session_mode']? null : $this->dsn['session_mode'];
  378.             $connection @$connect_function($username$password$sid$charset$session_mode);
  379.             $error @OCIError();
  380.             if (isset($error['code']&& $error['code'== 12541{
  381.                 // Couldn't find TNS listener.  Try direct connection.
  382.                 $connection @$connect_function($username$passwordnull$charset);
  383.             }
  384.         else {
  385.             $connect_function $persistent 'OCIPLogon' 'OCILogon';
  386.             $connection @$connect_function($username$password$sid);
  387.  
  388.             if (!empty($this->dsn['charset'])) {
  389.                 $result $this->setCharset($this->dsn['charset']$connection);
  390.                 if (PEAR::isError($result)) {
  391.                     return $result;
  392.                 }
  393.             }
  394.         }
  395.  
  396.         if (!$connection{
  397.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILEDnullnull,
  398.                 'unable to establish a connection'__FUNCTION__);
  399.         }
  400.  
  401.        if (empty($this->dsn['disable_iso_date'])) {
  402.             $query "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
  403.             $err =$this->_doQuery($querytrue$connection);
  404.             if (PEAR::isError($err)) {
  405.                 $this->disconnect(false);
  406.                 return $err;
  407.             }
  408.        }
  409.  
  410.         $query "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
  411.         $err =$this->_doQuery($querytrue$connection);
  412.         if (PEAR::isError($err)) {
  413.             $this->disconnect(false);
  414.             return $err;
  415.         }
  416.  
  417.         return $connection;
  418.     }
  419.  
  420.     // }}}
  421.     // {{{ connect()
  422.  
  423.     /**
  424.      * Connect to the database
  425.      *
  426.      * @return MDB2_OK on success, MDB2 Error Object on failure
  427.      * @access public
  428.      */
  429.     function connect()
  430.     {
  431.         if (is_resource($this->connection)) {
  432.             //if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
  433.             if (MDB2::areEquals($this->connected_dsn$this->dsn)
  434.                 && $this->opened_persistent == $this->options['persistent']
  435.             {
  436.                 return MDB2_OK;
  437.             }
  438.             $this->disconnect(false);
  439.         }
  440.  
  441.         if ($this->database_name && $this->options['emulate_database']{
  442.              $this->dsn['username'$this->options['database_name_prefix'].$this->database_name;
  443.         }
  444.  
  445.         $connection $this->_doConnect($this->dsn['username'],
  446.                                         $this->dsn['password'],
  447.                                         $this->options['persistent']);
  448.         if (PEAR::isError($connection)) {
  449.             return $connection;
  450.         }
  451.         $this->connection $connection;
  452.         $this->connected_dsn $this->dsn;
  453.         $this->connected_database_name '';
  454.         $this->opened_persistent $this->options['persistent'];
  455.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  456.  
  457.         if ($this->database_name{
  458.             if ($this->database_name != $this->connected_database_name{
  459.                 $query 'ALTER SESSION SET CURRENT_SCHEMA = "' .strtoupper($this->database_name.'"';
  460.                 $result =$this->_doQuery($query);
  461.                 if (PEAR::isError($result)) {
  462.                     $err $this->raiseError($resultnullnull,
  463.                         'Could not select the database: '.$this->database_name__FUNCTION__);
  464.                     return $err;
  465.                 }
  466.                 $this->connected_database_name $this->database_name;
  467.             }
  468.         }
  469.  
  470.         $this->as_keyword ' ';
  471.         $server_info $this->getServerVersion();
  472.         if (is_array($server_info)) {
  473.             if ($server_info['major'>= '10'{
  474.                 $this->as_keyword ' AS ';
  475.             }
  476.         }
  477.         return MDB2_OK;
  478.     }
  479.  
  480.     // }}}
  481.     // {{{ databaseExists()
  482.  
  483.     /**
  484.      * check if given database name is exists?
  485.      *
  486.      * @param string $name    name of the database that should be checked
  487.      *
  488.      * @return mixed true/false on success, a MDB2 error on failure
  489.      * @access public
  490.      */
  491.     function databaseExists($name)
  492.     {
  493.         $connection $this->_doConnect($this->dsn['username'],
  494.                                         $this->dsn['password'],
  495.                                         $this->options['persistent']);
  496.         if (PEAR::isError($connection)) {
  497.             return $connection;
  498.         }
  499.  
  500.         $query 'ALTER SESSION SET CURRENT_SCHEMA = "' .strtoupper($name.'"';
  501.         $result =$this->_doQuery($querytrue$connectionfalse);
  502.         if (PEAR::isError($result)) {
  503.             if (!MDB2::isError($resultMDB2_ERROR_NOT_FOUND)) {
  504.                 return $result;
  505.             }
  506.             return false;
  507.         }
  508.         return true;
  509.     }
  510.  
  511.     // }}}
  512.     // {{{ disconnect()
  513.  
  514.     /**
  515.      * Log out and disconnect from the database.
  516.      *
  517.      * @param  boolean $force if the disconnect should be forced even if the
  518.      *                         connection is opened persistently
  519.      * @return mixed true on success, false if not connected and error
  520.      *                 object on error
  521.      * @access public
  522.      */
  523.     function disconnect($force = true)
  524.     {
  525.         if (is_resource($this->connection)) {
  526.             if ($this->in_transaction{
  527.                 $dsn $this->dsn;
  528.                 $database_name $this->database_name;
  529.                 $persistent $this->options['persistent'];
  530.                 $this->dsn $this->connected_dsn;
  531.                 $this->database_name $this->connected_database_name;
  532.                 $this->options['persistent'$this->opened_persistent;
  533.                 $this->rollback();
  534.                 $this->dsn $dsn;
  535.                 $this->database_name $database_name;
  536.                 $this->options['persistent'$persistent;
  537.             }
  538.  
  539.             if (!$this->opened_persistent || $force{
  540.                 $ok = false;
  541.                 if (function_exists('oci_close')) {
  542.                     $ok @oci_close($this->connection);
  543.                 else {
  544.                     $ok @OCILogOff($this->connection);
  545.                 }
  546.                 if (!$ok{
  547.                     return $this->raiseError(MDB2_ERROR_DISCONNECT_FAILED,
  548.                            nullnullnull__FUNCTION__);
  549.                 }
  550.             }
  551.             $this->uncommitedqueries = 0;
  552.         else {
  553.             return false;
  554.         }
  555.         return parent::disconnect($force);
  556.     }
  557.  
  558.     // }}}
  559.     // {{{ standaloneQuery()
  560.  
  561.     /**
  562.      * execute a query as DBA
  563.      *
  564.      * @param string $query     the SQL query
  565.      * @param mixed  $types     array containing the types of the columns in
  566.      *                           the result set
  567.      * @param boolean $is_manip if the query is a manipulation query
  568.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  569.      * @access public
  570.      */
  571.     function &standaloneQuery($query$types = null$is_manip = false)
  572.     {
  573.         $user $this->options['DBA_username']$this->options['DBA_username'$this->dsn['username'];
  574.         $pass $this->options['DBA_password']$this->options['DBA_password'$this->dsn['password'];
  575.         $connection $this->_doConnect($user$pass$this->options['persistent']);
  576.         if (PEAR::isError($connection)) {
  577.             return $connection;
  578.         }
  579.  
  580.         $offset $this->offset;
  581.         $limit $this->limit;
  582.         $this->offset $this->limit = 0;
  583.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  584.  
  585.         $result =$this->_doQuery($query$is_manip$connectionfalse);
  586.         if (!PEAR::isError($result)) {
  587.             if ($is_manip{
  588.                 $result $this->_affectedRows($connection$result);
  589.             else {
  590.                 $result =$this->_wrapResult($result$typestruefalse$limit$offset);
  591.             }
  592.         }
  593.  
  594.         @OCILogOff($connection);
  595.         return $result;
  596.     }
  597.  
  598.     // }}}
  599.     // {{{ _modifyQuery()
  600.  
  601.     /**
  602.      * Changes a query string for various DBMS specific reasons
  603.      *
  604.      * @param string $query  query to modify
  605.      * @param boolean $is_manip  if it is a DML query
  606.      * @param integer $limit  limit the number of rows
  607.      * @param integer $offset  start reading from given offset
  608.      * @return string modified query
  609.      * @access protected
  610.      */
  611.     function _modifyQuery($query$is_manip$limit$offset)
  612.     {
  613.         if (preg_match('/^\s*SELECT/i'$query)) {
  614.             if (!preg_match('/\sFROM\s/i'$query)) {
  615.                 $query.= " FROM dual";
  616.             }
  617.             if ($limit > 0{
  618.                 // taken from http://svn.ez.no/svn/ezcomponents/packages/Database
  619.                 $max $offset $limit;
  620.                 if ($offset > 0{
  621.                     $min $offset + 1;
  622.                     $query = "SELECT * FROM (SELECT a.*, ROWNUM mdb2rn FROM ($query) a WHERE ROWNUM <= $max) WHERE mdb2rn >= $min";
  623.                 else {
  624.                     $query = "SELECT a.* FROM ($query) a WHERE ROWNUM <= $max";
  625.                 }
  626.             }
  627.         }
  628.         return $query;
  629.     }
  630.  
  631.     // }}}
  632.     // {{{ _doQuery()
  633.  
  634.     /**
  635.      * Execute a query
  636.      * @param string $query  query
  637.      * @param boolean $is_manip  if the query is a manipulation query
  638.      * @param resource $connection 
  639.      * @param string $database_name 
  640.      * @return result or error object
  641.      * @access protected
  642.      */
  643.     function &_doQuery($query$is_manip = false$connection = null$database_name = null)
  644.     {
  645.         $this->last_query $query;
  646.         $result $this->debug($query'query'array('is_manip' => $is_manip'when' => 'pre'));
  647.         if ($result{
  648.             if (PEAR::isError($result)) {
  649.                 return $result;
  650.             }
  651.             $query $result;
  652.         }
  653.         if ($this->getOption('disable_query')) {
  654.             if ($is_manip{
  655.                 return 0;
  656.             }
  657.             return null;
  658.         }
  659.  
  660.         if (is_null($connection)) {
  661.             $connection $this->getConnection();
  662.             if (PEAR::isError($connection)) {
  663.                 return $connection;
  664.             }
  665.         }
  666.  
  667.         $query str_replace("\r\n""\n"$query)//for fixing end-of-line character in the PL/SQL in windows
  668.         $result @OCIParse($connection$query);
  669.         if (!$result{
  670.             $err $this->raiseError(nullnullnull,
  671.                 'Could not create statement'__FUNCTION__);
  672.             return $err;
  673.         }
  674.  
  675.         $mode $this->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  676.         if (!@OCIExecute($result$mode)) {
  677.             $err =$this->raiseError($resultnullnull,
  678.                 'Could not execute statement'__FUNCTION__);
  679.             return $err;
  680.         }
  681.  
  682.         if (is_numeric($this->options['result_prefetching'])) {
  683.             @ocisetprefetch($result$this->options['result_prefetching']);
  684.         }
  685.  
  686.         $this->debug($query'query'array('is_manip' => $is_manip'when' => 'post''result' => $result));
  687.         return $result;
  688.     }
  689.  
  690.     // }}}
  691.     // {{{ _affectedRows()
  692.  
  693.     /**
  694.      * Returns the number of rows affected
  695.      *
  696.      * @param resource $result 
  697.      * @param resource $connection 
  698.      * @return mixed MDB2 Error Object or the number of rows affected
  699.      * @access private
  700.      */
  701.     function _affectedRows($connection$result = null)
  702.     {
  703.         if (is_null($connection)) {
  704.             $connection $this->getConnection();
  705.             if (PEAR::isError($connection)) {
  706.                 return $connection;
  707.             }
  708.         }
  709.         return @OCIRowCount($result);
  710.     }
  711.  
  712.     // }}}
  713.     // {{{ getServerVersion()
  714.  
  715.     /**
  716.      * return version information about the server
  717.      *
  718.      * @param bool   $native  determines if the raw version string should be returned
  719.      * @return mixed array/string with version information or MDB2 error object
  720.      * @access public
  721.      */
  722.     function getServerVersion($native = false)
  723.     {
  724.         $connection $this->getConnection();
  725.         if (PEAR::isError($connection)) {
  726.             return $connection;
  727.         }
  728.         if ($this->connected_server_info{
  729.             $server_info $this->connected_server_info;
  730.         else {
  731.             $server_info @ociserverversion($connection);
  732.         }
  733.         if (!$server_info{
  734.             return $this->raiseError(nullnullnull,
  735.                 'Could not get server information'__FUNCTION__);
  736.         }
  737.         // cache server_info
  738.         $this->connected_server_info $server_info;
  739.         if (!$native{
  740.             if (!preg_match('/ (\d+)\.(\d+)\.(\d+)\.([\d\.]+) /'$server_info$tmp)) {
  741.                 return $this->raiseError(MDB2_ERROR_INVALIDnullnull,
  742.                     'Could not parse version information:'.$server_info__FUNCTION__);
  743.             }
  744.             $server_info = array(
  745.                 'major' => $tmp[1],
  746.                 'minor' => $tmp[2],
  747.                 'patch' => $tmp[3],
  748.                 'extra' => $tmp[4],
  749.                 'native' => $server_info,
  750.             );
  751.         }
  752.         return $server_info;
  753.     }
  754.  
  755.     // }}}
  756.     // {{{ prepare()
  757.  
  758.     /**
  759.      * Prepares a query for multiple execution with execute().
  760.      * With some database backends, this is emulated.
  761.      * prepare() requires a generic query as string like
  762.      * 'INSERT INTO numbers VALUES(?,?)' or
  763.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  764.      * The ? and :name and are placeholders which can be set using
  765.      * bindParam() and the query can be sent off using the execute() method.
  766.      * The allowed format for :name can be set with the 'bindname_format' option.
  767.      *
  768.      * @param string $query the query to prepare
  769.      * @param mixed   $types  array that contains the types of the placeholders
  770.      * @param mixed   $result_types  array that contains the types of the columns in
  771.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  772.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  773.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  774.      * @return mixed resource handle for the prepared query on success, a MDB2
  775.      *         error on failure
  776.      * @access public
  777.      * @see bindParam, execute
  778.      */
  779.     function &prepare($query$types = null$result_types = null$lobs = array())
  780.     {
  781.         if ($this->options['emulate_prepared']{
  782.             $obj =parent::prepare($query$types$result_types$lobs);
  783.             return $obj;
  784.         }
  785.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  786.         $offset $this->offset;
  787.         $limit $this->limit;
  788.         $this->offset $this->limit = 0;
  789.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  790.         if ($result{
  791.             if (PEAR::isError($result)) {
  792.                 return $result;
  793.             }
  794.             $query $result;
  795.         }
  796.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  797.         $placeholder_type_guess $placeholder_type = null;
  798.         $question '?';
  799.         $colon ':';
  800.         $positions = array();
  801.         $position = 0;
  802.         $parameter = -1;
  803.         while ($position strlen($query)) {
  804.             $q_position strpos($query$question$position);
  805.             $c_position strpos($query$colon$position);
  806.             if ($q_position && $c_position{
  807.                 $p_position min($q_position$c_position);
  808.             elseif ($q_position{
  809.                 $p_position $q_position;
  810.             elseif ($c_position{
  811.                 $p_position $c_position;
  812.             else {
  813.                 break;
  814.             }
  815.             if (is_null($placeholder_type)) {
  816.                 $placeholder_type_guess $query[$p_position];
  817.             }
  818.             
  819.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  820.             if (PEAR::isError($new_pos)) {
  821.                 return $new_pos;
  822.             }
  823.             if ($new_pos != $position{
  824.                 $position $new_pos;
  825.                 continue; //evaluate again starting from the new position
  826.             }
  827.  
  828.             if ($query[$position== $placeholder_type_guess{
  829.                 if (is_null($placeholder_type)) {
  830.                     $placeholder_type $query[$p_position];
  831.                     $question $colon $placeholder_type;
  832.                     if (!empty($types&& is_array($types)) {
  833.                         if ($placeholder_type == ':'{
  834.                             if (is_int(key($types))) {
  835.                                 $types_tmp $types;
  836.                                 $types = array();
  837.                                 $count = -1;
  838.                             }
  839.                         else {
  840.                             $types array_values($types);
  841.                         }
  842.                     }
  843.                 }
  844.                 if ($placeholder_type == ':'{
  845.                     $regexp '/^.{'.($position+1).'}('.$this->options['bindname_format'].').*$/s';
  846.                     $parameter preg_replace($regexp'\\1'$query);
  847.                     if ($parameter === ''{
  848.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  849.                             'named parameter name must match "bindname_format" option'__FUNCTION__);
  850.                         return $err;
  851.                     }
  852.                     // use parameter name in type array
  853.                     if (isset($count&& isset($types_tmp[++$count])) {
  854.                         $types[$parameter$types_tmp[$count];
  855.                     }
  856.                     $length strlen($parameter+ 1;
  857.                 else {
  858.                     ++$parameter;
  859.                     //$length = strlen($parameter);
  860.                     $length = 1; // strlen('?')
  861.                 }
  862.                 if (!in_array($parameter$positions)) {
  863.                     $positions[$parameter;
  864.                 }
  865.                 if (isset($types[$parameter])
  866.                     && ($types[$parameter== 'clob' || $types[$parameter== 'blob')
  867.                 {
  868.                     if (!isset($lobs[$parameter])) {
  869.                         $lobs[$parameter$parameter;
  870.                     }
  871.                     $value $this->quote(true$types[$parameter]);
  872.                     if (PEAR::isError($value)) {
  873.                         return $value;
  874.                     }
  875.                     $query substr_replace($query$value$p_position$length);
  876.                     $position $p_position strlen($value- 1;
  877.                 elseif ($placeholder_type == '?'{
  878.                     $query substr_replace($query':'.$parameter$p_position1);
  879.                     $position $p_position $length;
  880.                 else {
  881.                     $position $p_position + 1;
  882.                 }
  883.             else {
  884.                 $position $p_position;
  885.             }
  886.         }
  887.         if (is_array($lobs)) {
  888.             $columns $variables '';
  889.             foreach ($lobs as $parameter => $field{
  890.                 $columns.= ($columns ', ' ' RETURNING ').$field;
  891.                 $variables.= ($variables ', ' ' INTO ').':'.$parameter;
  892.             }
  893.             $query.= $columns.$variables;
  894.         }
  895.         $connection $this->getConnection();
  896.         if (PEAR::isError($connection)) {
  897.             return $connection;
  898.         }
  899.         $statement @OCIParse($connection$query);
  900.         if (!$statement{
  901.             $err =$this->raiseError(nullnullnull,
  902.                 'Could not create statement'__FUNCTION__);
  903.             return $err;
  904.         }
  905.  
  906.         $class_name 'MDB2_Statement_'.$this->phptype;
  907.         $obj = new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  908.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  909.         return $obj;
  910.     }
  911.  
  912.     // }}}
  913.     // {{{ nextID()
  914.  
  915.     /**
  916.      * Returns the next free id of a sequence
  917.      *
  918.      * @param string $seq_name name of the sequence
  919.      * @param boolean $ondemand when true the sequence is
  920.      *                            automatic created, if it
  921.      *                            not exists
  922.      * @return mixed MDB2 Error Object or id
  923.      * @access public
  924.      */
  925.     function nextID($seq_name$ondemand = true)
  926.     {
  927.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  928.         $query = "SELECT $sequence_name.nextval FROM DUAL";
  929.         $this->pushErrorHandling(PEAR_ERROR_RETURN);
  930.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  931.         $result $this->queryOne($query'integer');
  932.         $this->popExpect();
  933.         $this->popErrorHandling();
  934.         if (PEAR::isError($result)) {
  935.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  936.                 $this->loadModule('Manager'nulltrue);
  937.                 $result $this->manager->createSequence($seq_name);
  938.                 if (PEAR::isError($result)) {
  939.                     return $result;
  940.                 }
  941.                 return $this->nextId($seq_namefalse);
  942.             }
  943.         }
  944.         return $result;
  945.     }
  946.  
  947.     // }}}
  948.     // {{{ lastInsertID()
  949.  
  950.     /**
  951.      * Returns the autoincrement ID if supported or $id or fetches the current
  952.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  953.      *
  954.      * @param string $table name of the table into which a new row was inserted
  955.      * @param string $field name of the field into which a new row was inserted
  956.      * @return mixed MDB2 Error Object or id
  957.      * @access public
  958.      */
  959.     function lastInsertID($table = null$field = null)
  960.     {
  961.         $seq $table.(empty($field'' '_'.$field);
  962.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq)true);
  963.         return $this->queryOne("SELECT $sequence_name.currval"'integer');
  964.     }
  965.  
  966.     // }}}
  967.     // {{{ currId()
  968.  
  969.     /**
  970.      * Returns the current id of a sequence
  971.      *
  972.      * @param string $seq_name name of the sequence
  973.      * @return mixed MDB2_Error or id
  974.      * @access public
  975.      */
  976.     function currId($seq_name)
  977.     {
  978.         $sequence_name $this->getSequenceName($seq_name);
  979.         $query 'SELECT (last_number-1) FROM all_sequences';
  980.         $query.= ' WHERE sequence_name='.$this->quote($sequence_name'text');
  981.         $query.= ' OR sequence_name='.$this->quote(strtoupper($sequence_name)'text');
  982.         return $this->queryOne($query'integer');
  983.     }
  984. }
  985.  
  986. /**
  987.  * MDB2 OCI8 result driver
  988.  *
  989.  * @package MDB2
  990.  * @category Database
  991.  * @author Lukas Smith <smith@pooteeweet.org>
  992.  */
  993. class MDB2_Result_oci8 extends MDB2_Result_Common
  994. {
  995.     // }}}
  996.     // {{{ fetchRow()
  997.  
  998.     /**
  999.      * Fetch a row and insert the data into an existing array.
  1000.      *
  1001.      * @param int       $fetchmode  how the array data should be indexed
  1002.      * @param int    $rownum    number of the row where the data can be found
  1003.      * @return int data array on success, a MDB2 error on failure
  1004.      * @access public
  1005.      */
  1006.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1007.     {
  1008.         if (!is_null($rownum)) {
  1009.             $seek $this->seek($rownum);
  1010.             if (PEAR::isError($seek)) {
  1011.                 return $seek;
  1012.             }
  1013.         }
  1014.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1015.             $fetchmode $this->db->fetchmode;
  1016.         }
  1017.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1018.             @OCIFetchInto($this->result$rowOCI_ASSOC+OCI_RETURN_NULLS);
  1019.             if (is_array($row)
  1020.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1021.             {
  1022.                 $row array_change_key_case($row$this->db->options['field_case']);
  1023.             }
  1024.         else {
  1025.             @OCIFetchInto($this->result$rowOCI_RETURN_NULLS);
  1026.         }
  1027.         if (!$row{
  1028.             if ($this->result === false{
  1029.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1030.                     'resultset has already been freed'__FUNCTION__);
  1031.                 return $err;
  1032.             }
  1033.             $null = null;
  1034.             return $null;
  1035.         }
  1036.         // remove additional column at the end
  1037.         if ($this->offset > 0{
  1038.             array_pop($row);
  1039.         }
  1040.         $mode = 0;
  1041.         $rtrim = false;
  1042.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1043.             if (empty($this->types)) {
  1044.                 $mode += MDB2_PORTABILITY_RTRIM;
  1045.             else {
  1046.                 $rtrim = true;
  1047.             }
  1048.         }
  1049.         if ($mode{
  1050.             $this->db->_fixResultArrayValues($row$mode);
  1051.         }
  1052.         if (!empty($this->types)) {
  1053.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1054.         }
  1055.         if (!empty($this->values)) {
  1056.             $this->_assignBindColumns($row);
  1057.         }
  1058.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1059.             $object_class $this->db->options['fetch_class'];
  1060.             if ($object_class == 'stdClass'{
  1061.                 $row = (object) $row;
  1062.             else {
  1063.                 $row &new $object_class($row);
  1064.             }
  1065.         }
  1066.         ++$this->rownum;
  1067.         return $row;
  1068.     }
  1069.  
  1070.     // }}}
  1071.     // {{{ _getColumnNames()
  1072.  
  1073.     /**
  1074.      * Retrieve the names of columns returned by the DBMS in a query result.
  1075.      *
  1076.      * @return  mixed   Array variable that holds the names of columns as keys
  1077.      *                   or an MDB2 error on failure.
  1078.      *                   Some DBMS may not return any columns when the result set
  1079.      *                   does not contain any rows.
  1080.      * @access private
  1081.      */
  1082.     function _getColumnNames()
  1083.     {
  1084.         $columns = array();
  1085.         $numcols $this->numCols();
  1086.         if (PEAR::isError($numcols)) {
  1087.             return $numcols;
  1088.         }
  1089.         for ($column = 0; $column $numcols$column++{
  1090.             $column_name @OCIColumnName($this->result$column + 1);
  1091.             $columns[$column_name$column;
  1092.         }
  1093.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1094.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1095.         }
  1096.         return $columns;
  1097.     }
  1098.  
  1099.     // }}}
  1100.     // {{{ numCols()
  1101.  
  1102.     /**
  1103.      * Count the number of columns returned by the DBMS in a query result.
  1104.      *
  1105.      * @return mixed integer value with the number of columns, a MDB2 error
  1106.      *       on failure
  1107.      * @access public
  1108.      */
  1109.     function numCols()
  1110.     {
  1111.         $cols @OCINumCols($this->result);
  1112.         if (is_null($cols)) {
  1113.             if ($this->result === false{
  1114.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1115.                     'resultset has already been freed'__FUNCTION__);
  1116.             elseif (is_null($this->result)) {
  1117.                 return count($this->types);
  1118.             }
  1119.             return $this->db->raiseError(nullnullnull,
  1120.                 'Could not get column count'__FUNCTION__);
  1121.         }
  1122.         if ($this->offset > 0{
  1123.             --$cols;
  1124.         }
  1125.         return $cols;
  1126.     }
  1127.  
  1128.     // }}}
  1129.     // {{{ free()
  1130.  
  1131.     /**
  1132.      * Free the internal resources associated with $result.
  1133.      *
  1134.      * @return boolean true on success, false if $result is invalid
  1135.      * @access public
  1136.      */
  1137.     function free()
  1138.     {
  1139.         if (is_resource($this->result&& $this->db->connection{
  1140.             $free @OCIFreeCursor($this->result);
  1141.             if ($free === false{
  1142.                 return $this->db->raiseError(nullnullnull,
  1143.                     'Could not free result'__FUNCTION__);
  1144.             }
  1145.         }
  1146.         $this->result = false;
  1147.         return MDB2_OK;
  1148.  
  1149.     }
  1150. }
  1151.  
  1152. /**
  1153.  * MDB2 OCI8 buffered result driver
  1154.  *
  1155.  * @package MDB2
  1156.  * @category Database
  1157.  * @author Lukas Smith <smith@pooteeweet.org>
  1158.  */
  1159. {
  1160.     var $buffer;
  1161.     var $buffer_rownum = - 1;
  1162.  
  1163.     // {{{ _fillBuffer()
  1164.  
  1165.     /**
  1166.      * Fill the row buffer
  1167.      *
  1168.      * @param int $rownum   row number upto which the buffer should be filled
  1169.                             if the row number is null all rows are ready into the buffer
  1170.      * @return boolean true on success, false on failure
  1171.      * @access protected
  1172.      */
  1173.     function _fillBuffer($rownum = null)
  1174.     {
  1175.         if (isset($this->buffer&& is_array($this->buffer)) {
  1176.             if (is_null($rownum)) {
  1177.                 if (!end($this->buffer)) {
  1178.                     return false;
  1179.                 }
  1180.             elseif (isset($this->buffer[$rownum])) {
  1181.                 return (bool)$this->buffer[$rownum];
  1182.             }
  1183.         }
  1184.  
  1185.         $row = true;
  1186.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  1187.             && ($row @OCIFetchInto($this->result$bufferOCI_RETURN_NULLS))
  1188.         {
  1189.             ++$this->buffer_rownum;
  1190.             // remove additional column at the end
  1191.             if ($this->offset > 0{
  1192.                 array_pop($buffer);
  1193.             }
  1194.             if (empty($this->types)) {
  1195.                 foreach (array_keys($bufferas $key{
  1196.                     if (is_a($buffer[$key]'oci-lob')) {
  1197.                         $buffer[$key$buffer[$key]->load();
  1198.                     }
  1199.                 }
  1200.             }
  1201.             $this->buffer[$this->buffer_rownum$buffer;
  1202.         }
  1203.  
  1204.         if (!$row{
  1205.             ++$this->buffer_rownum;
  1206.             $this->buffer[$this->buffer_rownum= false;
  1207.             return false;
  1208.         }
  1209.         return true;
  1210.     }
  1211.  
  1212.     // }}}
  1213.     // {{{ fetchRow()
  1214.  
  1215.     /**
  1216.      * Fetch a row and insert the data into an existing array.
  1217.      *
  1218.      * @param int       $fetchmode  how the array data should be indexed
  1219.      * @param int    $rownum    number of the row where the data can be found
  1220.      * @return int data array on success, a MDB2 error on failure
  1221.      * @access public
  1222.      */
  1223.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1224.     {
  1225.         if ($this->result === false{
  1226.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1227.                 'resultset has already been freed'__FUNCTION__);
  1228.             return $err;
  1229.         elseif (is_null($this->result)) {
  1230.             return null;
  1231.         }
  1232.         if (!is_null($rownum)) {
  1233.             $seek $this->seek($rownum);
  1234.             if (PEAR::isError($seek)) {
  1235.                 return $seek;
  1236.             }
  1237.         }
  1238.         $target_rownum $this->rownum + 1;
  1239.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1240.             $fetchmode $this->db->fetchmode;
  1241.         }
  1242.         if (!$this->_fillBuffer($target_rownum)) {
  1243.             $null = null;
  1244.             return $null;
  1245.         }
  1246.         $row $this->buffer[$target_rownum];
  1247.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1248.             $column_names $this->getColumnNames();
  1249.             foreach ($column_names as $name => $i{
  1250.                 $column_names[$name$row[$i];
  1251.             }
  1252.             $row $column_names;
  1253.         }
  1254.         $mode = 0;
  1255.         $rtrim = false;
  1256.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1257.             if (empty($this->types)) {
  1258.                 $mode += MDB2_PORTABILITY_RTRIM;
  1259.             else {
  1260.                 $rtrim = true;
  1261.             }
  1262.         }
  1263.         if ($mode{
  1264.             $this->db->_fixResultArrayValues($row$mode);
  1265.         }
  1266.         if (!empty($this->types)) {
  1267.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1268.         }
  1269.         if (!empty($this->values)) {
  1270.             $this->_assignBindColumns($row);
  1271.         }
  1272.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1273.             $object_class $this->db->options['fetch_class'];
  1274.             if ($object_class == 'stdClass'{
  1275.                 $row = (object) $row;
  1276.             else {
  1277.                 $row &new $object_class($row);
  1278.             }
  1279.         }
  1280.         ++$this->rownum;
  1281.         return $row;
  1282.     }
  1283.  
  1284.     // }}}
  1285.     // {{{ seek()
  1286.  
  1287.     /**
  1288.      * Seek to a specific row in a result set
  1289.      *
  1290.      * @param int    $rownum    number of the row where the data can be found
  1291.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1292.      * @access public
  1293.      */
  1294.     function seek($rownum = 0)
  1295.     {
  1296.         if ($this->result === false{
  1297.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1298.                 'resultset has already been freed'__FUNCTION__);
  1299.         }
  1300.         $this->rownum $rownum - 1;
  1301.         return MDB2_OK;
  1302.     }
  1303.  
  1304.     // }}}
  1305.     // {{{ valid()
  1306.  
  1307.     /**
  1308.      * Check if the end of the result set has been reached
  1309.      *
  1310.      * @return mixed true or false on sucess, a MDB2 error on failure
  1311.      * @access public
  1312.      */
  1313.     function valid()
  1314.     {
  1315.         if ($this->result === false{
  1316.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1317.                 'resultset has already been freed'__FUNCTION__);
  1318.         elseif (is_null($this->result)) {
  1319.             return true;
  1320.         }
  1321.         if ($this->_fillBuffer($this->rownum + 1)) {
  1322.             return true;
  1323.         }
  1324.         return false;
  1325.     }
  1326.  
  1327.     // }}}
  1328.     // {{{ numRows()
  1329.  
  1330.     /**
  1331.      * Returns the number of rows in a result object
  1332.      *
  1333.      * @return mixed MDB2 Error Object or the number of rows
  1334.      * @access public
  1335.      */
  1336.     function numRows()
  1337.     {
  1338.         if ($this->result === false{
  1339.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1340.                 'resultset has already been freed'__FUNCTION__);
  1341.         elseif (is_null($this->result)) {
  1342.             return 0;
  1343.         }
  1344.         $this->_fillBuffer();
  1345.         return $this->buffer_rownum;
  1346.     }
  1347.  
  1348.     // }}}
  1349.     // {{{ free()
  1350.  
  1351.     /**
  1352.      * Free the internal resources associated with $result.
  1353.      *
  1354.      * @return boolean true on success, false if $result is invalid
  1355.      * @access public
  1356.      */
  1357.     function free()
  1358.     {
  1359.         $this->buffer = null;
  1360.         $this->buffer_rownum = null;
  1361.         return parent::free();
  1362.     }
  1363. }
  1364.  
  1365. /**
  1366.  * MDB2 OCI8 statement driver
  1367.  *
  1368.  * @package MDB2
  1369.  * @category Database
  1370.  * @author Lukas Smith <smith@pooteeweet.org>
  1371.  */
  1372. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1373. {
  1374.     // {{{ Variables (Properties)
  1375.  
  1376.     var $type_maxlengths = array();
  1377.  
  1378.     // }}}
  1379.     // {{{ bindParam()
  1380.  
  1381.     /**
  1382.      * Bind a variable to a parameter of a prepared query.
  1383.      *
  1384.      * @param   int     $parameter  the order number of the parameter in the query
  1385.      *                                statement. The order number of the first parameter is 1.
  1386.      * @param   mixed   &$value     variable that is meant to be bound to specified
  1387.      *                                parameter. The type of the value depends on the $type argument.
  1388.      * @param   string  $type       specifies the type of the field
  1389.      * @param   int     $maxlength  specifies the maximum length of the field; if set to -1, the
  1390.      *                                current length of $value is used
  1391.      *
  1392.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  1393.      *
  1394.      * @access  public
  1395.      */
  1396.     function bindParam($parameter&$value$type = null$maxlength = -1)
  1397.     {
  1398.         if (!is_numeric($parameter)) {
  1399.             $parameter preg_replace('/^:(.*)$/''\\1'$parameter);
  1400.         }
  1401.         if (MDB2_OK === ($ret = parent::bindParam($parameter$value$type))) {
  1402.             $this->type_maxlengths[$parameter$maxlength;
  1403.         }
  1404.  
  1405.         return $ret;
  1406.     }
  1407.  
  1408.     // }}}
  1409.     // {{{ _execute()
  1410.  
  1411.     /**
  1412.      * Execute a prepared query statement helper method.
  1413.      *
  1414.      * @param mixed $result_class string which specifies which result class to use
  1415.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1416.      *
  1417.      * @return mixed MDB2_Result or integer (affected rows) on success,
  1418.      *                a MDB2 error on failure
  1419.      * @access private
  1420.      */
  1421.     function &_execute($result_class = true$result_wrap_class = false)
  1422.     {
  1423.         if (is_null($this->statement)) {
  1424.             $result =parent::_execute($result_class$result_wrap_class);
  1425.             return $result;
  1426.         }
  1427.         $this->db->last_query = $this->query;
  1428.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1429.         if ($this->db->getOption('disable_query')) {
  1430.             $result $this->is_manip ? 0 : null;
  1431.             return $result;
  1432.         }
  1433.  
  1434.         $connection $this->db->getConnection();
  1435.         if (PEAR::isError($connection)) {
  1436.             return $connection;
  1437.         }
  1438.  
  1439.         $result = MDB2_OK;
  1440.         $lobs $quoted_values = array();
  1441.         $i = 0;
  1442.         foreach ($this->positions as $parameter{
  1443.             if (!array_key_exists($parameter$this->values)) {
  1444.                 return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1445.                     'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1446.             }
  1447.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1448.             if ($type == 'clob' || $type == 'blob'{
  1449.                 $lobs[$i]['file'= false;
  1450.                 if (is_resource($this->values[$parameter])) {
  1451.                     $fp $this->values[$parameter];
  1452.                     $this->values[$parameter'';
  1453.                     while (!feof($fp)) {
  1454.                         $this->values[$parameter.= fread($fp8192);
  1455.                     }
  1456.                 elseif (is_a($this->values[$parameter]'OCI-Lob')) {
  1457.                     //do nothing
  1458.                 elseif ($this->db->getOption('lob_allow_url_include')
  1459.                           && preg_match('/^(\w+:\/\/)(.*)$/'$this->values[$parameter]$match)
  1460.                 {
  1461.                     $lobs[$i]['file'= true;
  1462.                     if ($match[1== 'file://'{
  1463.                         $this->values[$parameter$match[2];
  1464.                     }
  1465.                 }
  1466.                 $lobs[$i]['value'$this->values[$parameter];
  1467.                 $lobs[$i]['descriptor'=$this->values[$parameter];
  1468.                 // Test to see if descriptor has already been created for this
  1469.                 // variable (i.e. if it has been bound more than once):
  1470.                 if (!is_a($this->values[$parameter]'OCI-Lob')) {
  1471.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_LOB);
  1472.                     if ($this->values[$parameter=== false{
  1473.                         $result $this->db->raiseError(nullnullnull,
  1474.                             'Unable to create descriptor for LOB in parameter: '.$parameter__FUNCTION__);
  1475.                         break;
  1476.                     }
  1477.                 }
  1478.                 $lob_type ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1479.                 if (!@OCIBindByName($this->statement':'.$parameter$lobs[$i]['descriptor']-1$lob_type)) {
  1480.                     $result $this->db->raiseError($this->statementnullnull,
  1481.                         'could not bind LOB parameter'__FUNCTION__);
  1482.                     break;
  1483.                 }
  1484.             else if ($type == OCI_B_BFILE{
  1485.                 // Test to see if descriptor has already been created for this
  1486.                 // variable (i.e. if it has been bound more than once):
  1487.                 if (!is_a($this->values[$parameter]"OCI-Lob")) {
  1488.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_FILE);
  1489.                     if ($this->values[$parameter=== false{
  1490.                         $result $this->db->raiseError(nullnullnull,
  1491.                             'Unable to create descriptor for BFILE in parameter: '.$parameter__FUNCTION__);
  1492.                         break;
  1493.                     }
  1494.                 }
  1495.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1496.                     $result $this->db->raiseError($this->statementnullnull,
  1497.                         'Could not bind BFILE parameter'__FUNCTION__);
  1498.                     break;
  1499.                 }
  1500.             else if ($type == OCI_B_ROWID{
  1501.                 // Test to see if descriptor has already been created for this
  1502.                 // variable (i.e. if it has been bound more than once):
  1503.                 if (!is_a($this->values[$parameter]"OCI-Lob")) {
  1504.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_ROWID);
  1505.                     if ($this->values[$parameter=== false{
  1506.                         $result $this->db->raiseError(nullnullnull,
  1507.                             'Unable to create descriptor for ROWID in parameter: '.$parameter__FUNCTION__);
  1508.                         break;
  1509.                     }
  1510.                 }
  1511.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1512.                     $result $this->db->raiseError($this->statementnullnull,
  1513.                         'Could not bind ROWID parameter'__FUNCTION__);
  1514.                     break;
  1515.                 }
  1516.             else if ($type == OCI_B_CURSOR{
  1517.                 // Test to see if cursor has already been allocated for this
  1518.                 // variable (i.e. if it has been bound more than once):
  1519.                 if (!is_resource($this->values[$parameter]|| !get_resource_type($this->values[$parameter]== "oci8 statement"{
  1520.                     $this->values[$parameter@OCINewCursor($connection);
  1521.                     if ($this->values[$parameter=== false{
  1522.                         $result $this->db->raiseError(nullnullnull,
  1523.                         'Unable to allocate cursor for parameter: '.$parameter__FUNCTION__);
  1524.                     break;
  1525.                     }
  1526.                 }
  1527.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1528.                     $result $this->db->raiseError($this->statementnullnull,
  1529.                         'Could not bind CURSOR parameter'__FUNCTION__);
  1530.                     break;
  1531.                 }
  1532.             else {
  1533.                 $maxlength array_key_exists($parameter$this->type_maxlengths$this->type_maxlengths[$parameter: -1;
  1534.                 $this->values[$parameter$this->db->quote($this->values[$parameter]$typefalse);
  1535.                 $quoted_values[$i=$this->values[$parameter];
  1536.                 if (PEAR::isError($quoted_values[$i])) {
  1537.                     return $quoted_values[$i];
  1538.                 }
  1539.                 if (!@OCIBindByName($this->statement':'.$parameter$quoted_values[$i]$maxlength)) {
  1540.                     $result $this->db->raiseError($this->statementnullnull,
  1541.                         'could not bind non-abstract parameter'__FUNCTION__);
  1542.                     break;
  1543.                 }
  1544.             }
  1545.             ++$i;
  1546.         }
  1547.  
  1548.         $lob_keys array_keys($lobs);
  1549.         if (!PEAR::isError($result)) {
  1550.             $mode (!empty($lobs|| $this->db->in_transaction? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1551.             if (!@OCIExecute($this->statement$mode)) {
  1552.                 $err =$this->db->raiseError($this->statementnullnull,
  1553.                     'could not execute statement'__FUNCTION__);
  1554.                 return $err;
  1555.             }
  1556.  
  1557.             if (!empty($lobs)) {
  1558.                 foreach ($lob_keys as $i{
  1559.                     if (!is_null($lobs[$i]['value']&& $lobs[$i]['value'!== ''{
  1560.                         if (is_object($lobs[$i]['value'])) {
  1561.                             // Probably a NULL LOB
  1562.                             // @see http://bugs.php.net/bug.php?id=27485
  1563.                             continue;
  1564.                         }
  1565.                         if ($lobs[$i]['file']{
  1566.                             $result $lobs[$i]['descriptor']->savefile($lobs[$i]['value']);
  1567.                         else {
  1568.                             $result $lobs[$i]['descriptor']->save($lobs[$i]['value']);
  1569.                         }
  1570.                         if (!$result{
  1571.                             $result $this->db->raiseError(nullnullnull,
  1572.                                 'Unable to save descriptor contents'__FUNCTION__);
  1573.                             break;
  1574.                         }
  1575.                     }
  1576.                 }
  1577.  
  1578.                 if (!PEAR::isError($result)) {
  1579.                     if (!$this->db->in_transaction{
  1580.                         if (!@OCICommit($connection)) {
  1581.                             $result $this->db->raiseError(nullnullnull,
  1582.                                 'Unable to commit transaction'__FUNCTION__);
  1583.                         }
  1584.                     else {
  1585.                         ++$this->db->uncommitedqueries;
  1586.                     }
  1587.                 }
  1588.             }
  1589.         }
  1590.  
  1591.         if (PEAR::isError($result)) {
  1592.             return $result;
  1593.         }
  1594.  
  1595.         if ($this->is_manip{
  1596.             $affected_rows $this->db->_affectedRows($connection$this->statement);
  1597.             return $affected_rows;
  1598.         }
  1599.  
  1600.         $result =$this->db->_wrapResult($this->statement$this->result_types,
  1601.             $result_class$result_wrap_class$this->limit$this->offset);
  1602.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1603.         return $result;
  1604.     }
  1605.  
  1606.     // }}}
  1607.     // {{{ free()
  1608.  
  1609.     /**
  1610.      * Release resources allocated for the specified prepared query.
  1611.      *
  1612.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1613.      * @access public
  1614.      */
  1615.     function free()
  1616.     {
  1617.         if (is_null($this->positions)) {
  1618.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1619.                 'Prepared statement has already been freed'__FUNCTION__);
  1620.         }
  1621.         $result = MDB2_OK;
  1622.  
  1623.         if (!is_null($this->statement&& !@OCIFreeStatement($this->statement)) {
  1624.             $result $this->db->raiseError(nullnullnull,
  1625.                 'Could not free statement'__FUNCTION__);
  1626.         }
  1627.  
  1628.         parent::free();
  1629.         return $result;
  1630.     }
  1631. }
  1632. ?>

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