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

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