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

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