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

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