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

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