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.199 2007/09/23 18:27:42 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 :[a-zA-Z] and  are placeholders which can be set using
  750.      * bindParam() and the query can be send off using the execute() method.
  751.      *
  752.      * @param string $query the query to prepare
  753.      * @param mixed   $types  array that contains the types of the placeholders
  754.      * @param mixed   $result_types  array that contains the types of the columns in
  755.      *                         the result set or MDB2_PREPARE_RESULT, if set to
  756.      *                         MDB2_PREPARE_MANIP the query is handled as a manipulation query
  757.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  758.      * @return mixed resource handle for the prepared query on success, a MDB2
  759.      *         error on failure
  760.      * @access public
  761.      * @see bindParam, execute
  762.      */
  763.     function &prepare($query$types = null$result_types = null$lobs = array())
  764.     {
  765.         if ($this->options['emulate_prepared']{
  766.             $obj =parent::prepare($query$types$result_types$lobs);
  767.             return $obj;
  768.         }
  769.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  770.         $offset $this->offset;
  771.         $limit $this->limit;
  772.         $this->offset $this->limit = 0;
  773.         $result $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'pre'));
  774.         if ($result{
  775.             if (PEAR::isError($result)) {
  776.                 return $result;
  777.             }
  778.             $query $result;
  779.         }
  780.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  781.         $placeholder_type_guess $placeholder_type = null;
  782.         $question '?';
  783.         $colon ':';
  784.         $positions = array();
  785.         $position = 0;
  786.         $parameter = -1;
  787.         while ($position strlen($query)) {
  788.             $q_position strpos($query$question$position);
  789.             $c_position strpos($query$colon$position);
  790.             if ($q_position && $c_position{
  791.                 $p_position min($q_position$c_position);
  792.             elseif ($q_position{
  793.                 $p_position $q_position;
  794.             elseif ($c_position{
  795.                 $p_position $c_position;
  796.             else {
  797.                 break;
  798.             }
  799.             if (is_null($placeholder_type)) {
  800.                 $placeholder_type_guess $query[$p_position];
  801.             }
  802.             
  803.             $new_pos $this->_skipDelimitedStrings($query$position$p_position);
  804.             if (PEAR::isError($new_pos)) {
  805.                 return $new_pos;
  806.             }
  807.             if ($new_pos != $position{
  808.                 $position $new_pos;
  809.                 continue; //evaluate again starting from the new position
  810.             }
  811.  
  812.             if ($query[$position== $placeholder_type_guess{
  813.                 if (is_null($placeholder_type)) {
  814.                     $placeholder_type $query[$p_position];
  815.                     $question $colon $placeholder_type;
  816.                     if (!empty($types&& is_array($types)) {
  817.                         if ($placeholder_type == ':'{
  818.                             if (is_int(key($types))) {
  819.                                 $types_tmp $types;
  820.                                 $types = array();
  821.                                 $count = -1;
  822.                             }
  823.                         else {
  824.                             $types array_values($types);
  825.                         }
  826.                     }
  827.                 }
  828.                 if ($placeholder_type == ':'{
  829.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  830.                     if ($parameter === ''{
  831.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  832.                             'named parameter with an empty name'__FUNCTION__);
  833.                         return $err;
  834.                     }
  835.                     // use parameter name in type array
  836.                     if (isset($count&& isset($types_tmp[++$count])) {
  837.                         $types[$parameter$types_tmp[$count];
  838.                     }
  839.                     $length strlen($parameter+ 1;
  840.                 else {
  841.                     ++$parameter;
  842.                     $length strlen($parameter);
  843.                 }
  844.                 if (!in_array($parameter$positions)) {
  845.                     $positions[$parameter;
  846.                 }
  847.                 if (isset($types[$parameter])
  848.                     && ($types[$parameter== 'clob' || $types[$parameter== 'blob')
  849.                 {
  850.                     if (!isset($lobs[$parameter])) {
  851.                         $lobs[$parameter$parameter;
  852.                     }
  853.                     $value $this->quote(true$types[$parameter]);
  854.                     if (PEAR::isError($value)) {
  855.                         return $value;
  856.                     }
  857.                     $query substr_replace($query$value$p_position$length);
  858.                     $position $p_position strlen($value- 1;
  859.                 elseif ($placeholder_type == '?'{
  860.                     $query substr_replace($query':'.$parameter$p_position1);
  861.                     $position $p_position $length;
  862.                 else {
  863.                     $position $p_position + 1;
  864.                 }
  865.             else {
  866.                 $position $p_position;
  867.             }
  868.         }
  869.         if (is_array($lobs)) {
  870.             $columns $variables '';
  871.             foreach ($lobs as $parameter => $field{
  872.                 $columns.= ($columns ', ' ' RETURNING ').$field;
  873.                 $variables.= ($variables ', ' ' INTO ').':'.$parameter;
  874.             }
  875.             $query.= $columns.$variables;
  876.         }
  877.         $connection $this->getConnection();
  878.         if (PEAR::isError($connection)) {
  879.             return $connection;
  880.         }
  881.         $statement @OCIParse($connection$query);
  882.         if (!$statement{
  883.             $err =$this->raiseError(nullnullnull,
  884.                 'Could not create statement'__FUNCTION__);
  885.             return $err;
  886.         }
  887.  
  888.         $class_name 'MDB2_Statement_'.$this->phptype;
  889.         $obj = new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  890.         $this->debug($query__FUNCTION__array('is_manip' => $is_manip'when' => 'post''result' => $obj));
  891.         return $obj;
  892.     }
  893.  
  894.     // }}}
  895.     // {{{ nextID()
  896.  
  897.     /**
  898.      * Returns the next free id of a sequence
  899.      *
  900.      * @param string $seq_name name of the sequence
  901.      * @param boolean $ondemand when true the sequence is
  902.      *                            automatic created, if it
  903.      *                            not exists
  904.      * @return mixed MDB2 Error Object or id
  905.      * @access public
  906.      */
  907.     function nextID($seq_name$ondemand = true)
  908.     {
  909.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  910.         $query = "SELECT $sequence_name.nextval FROM DUAL";
  911.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  912.         $result $this->queryOne($query'integer');
  913.         $this->popExpect();
  914.         if (PEAR::isError($result)) {
  915.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  916.                 $this->loadModule('Manager'nulltrue);
  917.                 $result $this->manager->createSequence($seq_name);
  918.                 if (PEAR::isError($result)) {
  919.                     return $result;
  920.                 }
  921.                 return $this->nextId($seq_namefalse);
  922.             }
  923.         }
  924.         return $result;
  925.     }
  926.  
  927.     // }}}
  928.     // {{{ lastInsertID()
  929.  
  930.     /**
  931.      * Returns the autoincrement ID if supported or $id or fetches the current
  932.      * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  933.      *
  934.      * @param string $table name of the table into which a new row was inserted
  935.      * @param string $field name of the field into which a new row was inserted
  936.      * @return mixed MDB2 Error Object or id
  937.      * @access public
  938.      */
  939.     function lastInsertID($table = null$field = null)
  940.     {
  941.         $seq $table.(empty($field'' '_'.$field);
  942.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq)true);
  943.         return $this->queryOne("SELECT $sequence_name.currval"'integer');
  944.     }
  945.  
  946.     // }}}
  947.     // {{{ currId()
  948.  
  949.     /**
  950.      * Returns the current id of a sequence
  951.      *
  952.      * @param string $seq_name name of the sequence
  953.      * @return mixed MDB2_Error or id
  954.      * @access public
  955.      */
  956.     function currId($seq_name)
  957.     {
  958.         $sequence_name $this->getSequenceName($seq_name);
  959.         $query 'SELECT (last_number-1) FROM all_sequences';
  960.         $query.= ' WHERE sequence_name='.$this->quote($sequence_name'text');
  961.         $query.= ' OR sequence_name='.$this->quote(strtoupper($sequence_name)'text');
  962.         return $this->queryOne($query'integer');
  963.     }
  964. }
  965.  
  966. /**
  967.  * MDB2 OCI8 result driver
  968.  *
  969.  * @package MDB2
  970.  * @category Database
  971.  * @author Lukas Smith <smith@pooteeweet.org>
  972.  */
  973. class MDB2_Result_oci8 extends MDB2_Result_Common
  974. {
  975.     // }}}
  976.     // {{{ fetchRow()
  977.  
  978.     /**
  979.      * Fetch a row and insert the data into an existing array.
  980.      *
  981.      * @param int       $fetchmode  how the array data should be indexed
  982.      * @param int    $rownum    number of the row where the data can be found
  983.      * @return int data array on success, a MDB2 error on failure
  984.      * @access public
  985.      */
  986.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  987.     {
  988.         if (!is_null($rownum)) {
  989.             $seek $this->seek($rownum);
  990.             if (PEAR::isError($seek)) {
  991.                 return $seek;
  992.             }
  993.         }
  994.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  995.             $fetchmode $this->db->fetchmode;
  996.         }
  997.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  998.             @OCIFetchInto($this->result$rowOCI_ASSOC+OCI_RETURN_NULLS);
  999.             if (is_array($row)
  1000.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1001.             {
  1002.                 $row array_change_key_case($row$this->db->options['field_case']);
  1003.             }
  1004.         else {
  1005.             @OCIFetchInto($this->result$rowOCI_RETURN_NULLS);
  1006.         }
  1007.         if (!$row{
  1008.             if ($this->result === false{
  1009.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1010.                     'resultset has already been freed'__FUNCTION__);
  1011.                 return $err;
  1012.             }
  1013.             $null = null;
  1014.             return $null;
  1015.         }
  1016.         // remove additional column at the end
  1017.         if ($this->offset > 0{
  1018.             array_pop($row);
  1019.         }
  1020.         $mode = 0;
  1021.         $rtrim = false;
  1022.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1023.             if (empty($this->types)) {
  1024.                 $mode += MDB2_PORTABILITY_RTRIM;
  1025.             else {
  1026.                 $rtrim = true;
  1027.             }
  1028.         }
  1029.         if ($mode{
  1030.             $this->db->_fixResultArrayValues($row$mode);
  1031.         }
  1032.         if (!empty($this->types)) {
  1033.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1034.         }
  1035.         if (!empty($this->values)) {
  1036.             $this->_assignBindColumns($row);
  1037.         }
  1038.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1039.             $object_class $this->db->options['fetch_class'];
  1040.             if ($object_class == 'stdClass'{
  1041.                 $row = (object) $row;
  1042.             else {
  1043.                 $row &new $object_class($row);
  1044.             }
  1045.         }
  1046.         ++$this->rownum;
  1047.         return $row;
  1048.     }
  1049.  
  1050.     // }}}
  1051.     // {{{ _getColumnNames()
  1052.  
  1053.     /**
  1054.      * Retrieve the names of columns returned by the DBMS in a query result.
  1055.      *
  1056.      * @return  mixed   Array variable that holds the names of columns as keys
  1057.      *                   or an MDB2 error on failure.
  1058.      *                   Some DBMS may not return any columns when the result set
  1059.      *                   does not contain any rows.
  1060.      * @access private
  1061.      */
  1062.     function _getColumnNames()
  1063.     {
  1064.         $columns = array();
  1065.         $numcols $this->numCols();
  1066.         if (PEAR::isError($numcols)) {
  1067.             return $numcols;
  1068.         }
  1069.         for ($column = 0; $column $numcols$column++{
  1070.             $column_name @OCIColumnName($this->result$column + 1);
  1071.             $columns[$column_name$column;
  1072.         }
  1073.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1074.             $columns array_change_key_case($columns$this->db->options['field_case']);
  1075.         }
  1076.         return $columns;
  1077.     }
  1078.  
  1079.     // }}}
  1080.     // {{{ numCols()
  1081.  
  1082.     /**
  1083.      * Count the number of columns returned by the DBMS in a query result.
  1084.      *
  1085.      * @return mixed integer value with the number of columns, a MDB2 error
  1086.      *       on failure
  1087.      * @access public
  1088.      */
  1089.     function numCols()
  1090.     {
  1091.         $cols @OCINumCols($this->result);
  1092.         if (is_null($cols)) {
  1093.             if ($this->result === false{
  1094.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1095.                     'resultset has already been freed'__FUNCTION__);
  1096.             elseif (is_null($this->result)) {
  1097.                 return count($this->types);
  1098.             }
  1099.             return $this->db->raiseError(nullnullnull,
  1100.                 'Could not get column count'__FUNCTION__);
  1101.         }
  1102.         if ($this->offset > 0{
  1103.             --$cols;
  1104.         }
  1105.         return $cols;
  1106.     }
  1107.  
  1108.     // }}}
  1109.     // {{{ free()
  1110.  
  1111.     /**
  1112.      * Free the internal resources associated with $result.
  1113.      *
  1114.      * @return boolean true on success, false if $result is invalid
  1115.      * @access public
  1116.      */
  1117.     function free()
  1118.     {
  1119.         if (is_resource($this->result&& $this->db->connection{
  1120.             $free @OCIFreeCursor($this->result);
  1121.             if ($free === false{
  1122.                 return $this->db->raiseError(nullnullnull,
  1123.                     'Could not free result'__FUNCTION__);
  1124.             }
  1125.         }
  1126.         $this->result = false;
  1127.         return MDB2_OK;
  1128.  
  1129.     }
  1130. }
  1131.  
  1132. /**
  1133.  * MDB2 OCI8 buffered result driver
  1134.  *
  1135.  * @package MDB2
  1136.  * @category Database
  1137.  * @author Lukas Smith <smith@pooteeweet.org>
  1138.  */
  1139. {
  1140.     var $buffer;
  1141.     var $buffer_rownum = - 1;
  1142.  
  1143.     // {{{ _fillBuffer()
  1144.  
  1145.     /**
  1146.      * Fill the row buffer
  1147.      *
  1148.      * @param int $rownum   row number upto which the buffer should be filled
  1149.                             if the row number is null all rows are ready into the buffer
  1150.      * @return boolean true on success, false on failure
  1151.      * @access protected
  1152.      */
  1153.     function _fillBuffer($rownum = null)
  1154.     {
  1155.         if (isset($this->buffer&& is_array($this->buffer)) {
  1156.             if (is_null($rownum)) {
  1157.                 if (!end($this->buffer)) {
  1158.                     return false;
  1159.                 }
  1160.             elseif (isset($this->buffer[$rownum])) {
  1161.                 return (bool)$this->buffer[$rownum];
  1162.             }
  1163.         }
  1164.  
  1165.         $row = true;
  1166.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  1167.             && ($row @OCIFetchInto($this->result$bufferOCI_RETURN_NULLS))
  1168.         {
  1169.             ++$this->buffer_rownum;
  1170.             // remove additional column at the end
  1171.             if ($this->offset > 0{
  1172.                 array_pop($buffer);
  1173.             }
  1174.             if (empty($this->types)) {
  1175.                 foreach (array_keys($bufferas $key{
  1176.                     if (is_a($buffer[$key]'oci-lob')) {
  1177.                         $buffer[$key$buffer[$key]->load();
  1178.                     }
  1179.                 }
  1180.             }
  1181.             $this->buffer[$this->buffer_rownum$buffer;
  1182.         }
  1183.  
  1184.         if (!$row{
  1185.             ++$this->buffer_rownum;
  1186.             $this->buffer[$this->buffer_rownum= false;
  1187.             return false;
  1188.         }
  1189.         return true;
  1190.     }
  1191.  
  1192.     // }}}
  1193.     // {{{ fetchRow()
  1194.  
  1195.     /**
  1196.      * Fetch a row and insert the data into an existing array.
  1197.      *
  1198.      * @param int       $fetchmode  how the array data should be indexed
  1199.      * @param int    $rownum    number of the row where the data can be found
  1200.      * @return int data array on success, a MDB2 error on failure
  1201.      * @access public
  1202.      */
  1203.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1204.     {
  1205.         if ($this->result === false{
  1206.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1207.                 'resultset has already been freed'__FUNCTION__);
  1208.             return $err;
  1209.         elseif (is_null($this->result)) {
  1210.             return null;
  1211.         }
  1212.         if (!is_null($rownum)) {
  1213.             $seek $this->seek($rownum);
  1214.             if (PEAR::isError($seek)) {
  1215.                 return $seek;
  1216.             }
  1217.         }
  1218.         $target_rownum $this->rownum + 1;
  1219.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1220.             $fetchmode $this->db->fetchmode;
  1221.         }
  1222.         if (!$this->_fillBuffer($target_rownum)) {
  1223.             $null = null;
  1224.             return $null;
  1225.         }
  1226.         $row $this->buffer[$target_rownum];
  1227.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1228.             $column_names $this->getColumnNames();
  1229.             foreach ($column_names as $name => $i{
  1230.                 $column_names[$name$row[$i];
  1231.             }
  1232.             $row $column_names;
  1233.         }
  1234.         $mode = 0;
  1235.         $rtrim = false;
  1236.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1237.             if (empty($this->types)) {
  1238.                 $mode += MDB2_PORTABILITY_RTRIM;
  1239.             else {
  1240.                 $rtrim = true;
  1241.             }
  1242.         }
  1243.         if ($mode{
  1244.             $this->db->_fixResultArrayValues($row$mode);
  1245.         }
  1246.         if (!empty($this->types)) {
  1247.             $row $this->db->datatype->convertResultRow($this->types$row$rtrim);
  1248.         }
  1249.         if (!empty($this->values)) {
  1250.             $this->_assignBindColumns($row);
  1251.         }
  1252.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1253.             $object_class $this->db->options['fetch_class'];
  1254.             if ($object_class == 'stdClass'{
  1255.                 $row = (object) $row;
  1256.             else {
  1257.                 $row &new $object_class($row);
  1258.             }
  1259.         }
  1260.         ++$this->rownum;
  1261.         return $row;
  1262.     }
  1263.  
  1264.     // }}}
  1265.     // {{{ seek()
  1266.  
  1267.     /**
  1268.      * Seek to a specific row in a result set
  1269.      *
  1270.      * @param int    $rownum    number of the row where the data can be found
  1271.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1272.      * @access public
  1273.      */
  1274.     function seek($rownum = 0)
  1275.     {
  1276.         if ($this->result === false{
  1277.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1278.                 'resultset has already been freed'__FUNCTION__);
  1279.         }
  1280.         $this->rownum $rownum - 1;
  1281.         return MDB2_OK;
  1282.     }
  1283.  
  1284.     // }}}
  1285.     // {{{ valid()
  1286.  
  1287.     /**
  1288.      * Check if the end of the result set has been reached
  1289.      *
  1290.      * @return mixed true or false on sucess, a MDB2 error on failure
  1291.      * @access public
  1292.      */
  1293.     function valid()
  1294.     {
  1295.         if ($this->result === false{
  1296.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1297.                 'resultset has already been freed'__FUNCTION__);
  1298.         elseif (is_null($this->result)) {
  1299.             return true;
  1300.         }
  1301.         if ($this->_fillBuffer($this->rownum + 1)) {
  1302.             return true;
  1303.         }
  1304.         return false;
  1305.     }
  1306.  
  1307.     // }}}
  1308.     // {{{ numRows()
  1309.  
  1310.     /**
  1311.      * Returns the number of rows in a result object
  1312.      *
  1313.      * @return mixed MDB2 Error Object or the number of rows
  1314.      * @access public
  1315.      */
  1316.     function numRows()
  1317.     {
  1318.         if ($this->result === false{
  1319.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1320.                 'resultset has already been freed'__FUNCTION__);
  1321.         elseif (is_null($this->result)) {
  1322.             return 0;
  1323.         }
  1324.         $this->_fillBuffer();
  1325.         return $this->buffer_rownum;
  1326.     }
  1327.  
  1328.     // }}}
  1329.     // {{{ free()
  1330.  
  1331.     /**
  1332.      * Free the internal resources associated with $result.
  1333.      *
  1334.      * @return boolean true on success, false if $result is invalid
  1335.      * @access public
  1336.      */
  1337.     function free()
  1338.     {
  1339.         $this->buffer = null;
  1340.         $this->buffer_rownum = null;
  1341.         return parent::free();
  1342.     }
  1343. }
  1344.  
  1345. /**
  1346.  * MDB2 OCI8 statement driver
  1347.  *
  1348.  * @package MDB2
  1349.  * @category Database
  1350.  * @author Lukas Smith <smith@pooteeweet.org>
  1351.  */
  1352. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1353. {
  1354.     // {{{ Variables (Properties)
  1355.  
  1356.     var $type_maxlengths = array();
  1357.  
  1358.     // }}}
  1359.     // {{{ bindParam()
  1360.  
  1361.     /**
  1362.      * Bind a variable to a parameter of a prepared query.
  1363.      *
  1364.      * @param   int     $parameter  the order number of the parameter in the query
  1365.      *                                statement. The order number of the first parameter is 1.
  1366.      * @param   mixed   &$value     variable that is meant to be bound to specified
  1367.      *                                parameter. The type of the value depends on the $type argument.
  1368.      * @param   string  $type       specifies the type of the field
  1369.      * @param   int     $maxlength  specifies the maximum length of the field; if set to -1, the
  1370.      *                                current length of $value is used
  1371.      *
  1372.      * @return  mixed   MDB2_OK on success, a MDB2 error on failure
  1373.      *
  1374.      * @access  public
  1375.      */
  1376.     function bindParam($parameter&$value$type = null$maxlength = -1)
  1377.     {
  1378.         if (!is_numeric($parameter)) {
  1379.             $parameter preg_replace('/^:(.*)$/''\\1'$parameter);
  1380.         }
  1381.         if (MDB2_OK === ($ret = parent::bindParam($parameter$value$type))) {
  1382.             $this->type_maxlengths[$parameter$maxlength;
  1383.         }
  1384.  
  1385.         return $ret;
  1386.     }
  1387.  
  1388.     // }}}
  1389.     // {{{ _execute()
  1390.  
  1391.     /**
  1392.      * Execute a prepared query statement helper method.
  1393.      *
  1394.      * @param mixed $result_class string which specifies which result class to use
  1395.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1396.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1397.      * @access private
  1398.      */
  1399.     function &_execute($result_class = true$result_wrap_class = false)
  1400.     {
  1401.         if (is_null($this->statement)) {
  1402.             $result =parent::_execute($result_class$result_wrap_class);
  1403.             return $result;
  1404.         }
  1405.         $this->db->last_query = $this->query;
  1406.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'pre''parameters' => $this->values));
  1407.         if ($this->db->getOption('disable_query')) {
  1408.             $result $this->is_manip ? 0 : null;
  1409.             return $result;
  1410.         }
  1411.  
  1412.         $connection $this->db->getConnection();
  1413.         if (PEAR::isError($connection)) {
  1414.             return $connection;
  1415.         }
  1416.  
  1417.         $result = MDB2_OK;
  1418.         $lobs $quoted_values = array();
  1419.         $i = 0;
  1420.         foreach ($this->positions as $parameter{
  1421.             if (!array_key_exists($parameter$this->values)) {
  1422.                 return $this->db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  1423.                     'Unable to bind to missing placeholder: '.$parameter__FUNCTION__);
  1424.             }
  1425.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1426.             if ($type == 'clob' || $type == 'blob'{
  1427.                 $lobs[$i]['file'= false;
  1428.                 if (is_resource($this->values[$parameter])) {
  1429.                     $fp $this->values[$parameter];
  1430.                     $this->values[$parameter'';
  1431.                     while (!feof($fp)) {
  1432.                         $this->values[$parameter.= fread($fp8192);
  1433.                     }
  1434.                 elseif (is_a($this->values[$parameter]'OCI-Lob')) {
  1435.                     //do nothing
  1436.                 elseif ($this->db->getOption('lob_allow_url_include')
  1437.                           && preg_match('/^(\w+:\/\/)(.*)$/'$this->values[$parameter]$match)
  1438.                 {
  1439.                     $lobs[$i]['file'= true;
  1440.                     if ($match[1== 'file://'{
  1441.                         $this->values[$parameter$match[2];
  1442.                     }
  1443.                 }
  1444.                 $lobs[$i]['value'$this->values[$parameter];
  1445.                 $lobs[$i]['descriptor'=$this->values[$parameter];
  1446.                 // Test to see if descriptor has already been created for this
  1447.                 // variable (i.e. if it has been bound more than once):
  1448.                 if (!is_a($this->values[$parameter]'OCI-Lob')) {
  1449.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_LOB);
  1450.                     if ($this->values[$parameter=== false{
  1451.                         $result $this->db->raiseError(nullnullnull,
  1452.                             'Unable to create descriptor for LOB in parameter: '.$parameter__FUNCTION__);
  1453.                         break;
  1454.                     }
  1455.                 }
  1456.                 $lob_type ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1457.                 if (!@OCIBindByName($this->statement':'.$parameter$lobs[$i]['descriptor']-1$lob_type)) {
  1458.                     $result $this->db->raiseError($this->statementnullnull,
  1459.                         'could not bind LOB parameter'__FUNCTION__);
  1460.                     break;
  1461.                 }
  1462.             else if ($type == OCI_B_BFILE{
  1463.                 // Test to see if descriptor has already been created for this
  1464.                 // variable (i.e. if it has been bound more than once):
  1465.                 if (!is_a($this->values[$parameter]"OCI-Lob")) {
  1466.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_FILE);
  1467.                     if ($this->values[$parameter=== false{
  1468.                         $result $this->db->raiseError(nullnullnull,
  1469.                             'Unable to create descriptor for BFILE in parameter: '.$parameter__FUNCTION__);
  1470.                         break;
  1471.                     }
  1472.                 }
  1473.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1474.                     $result $this->db->raiseError($this->statementnullnull,
  1475.                         'Could not bind BFILE parameter'__FUNCTION__);
  1476.                     break;
  1477.                 }
  1478.             else if ($type == OCI_B_ROWID{
  1479.                 // Test to see if descriptor has already been created for this
  1480.                 // variable (i.e. if it has been bound more than once):
  1481.                 if (!is_a($this->values[$parameter]"OCI-Lob")) {
  1482.                     $this->values[$parameter@OCINewDescriptor($connectionOCI_D_ROWID);
  1483.                     if ($this->values[$parameter=== false{
  1484.                         $result $this->db->raiseError(nullnullnull,
  1485.                             'Unable to create descriptor for ROWID in parameter: '.$parameter__FUNCTION__);
  1486.                         break;
  1487.                     }
  1488.                 }
  1489.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1490.                     $result $this->db->raiseError($this->statementnullnull,
  1491.                         'Could not bind ROWID parameter'__FUNCTION__);
  1492.                     break;
  1493.                 }
  1494.             else if ($type == OCI_B_CURSOR{
  1495.                 // Test to see if cursor has already been allocated for this
  1496.                 // variable (i.e. if it has been bound more than once):
  1497.                 if (!is_resource($this->values[$parameter]|| !get_resource_type($this->values[$parameter]== "oci8 statement"{
  1498.                     $this->values[$parameter@OCINewCursor($connection);
  1499.                     if ($this->values[$parameter=== false{
  1500.                         $result $this->db->raiseError(nullnullnull,
  1501.                         'Unable to allocate cursor for parameter: '.$parameter__FUNCTION__);
  1502.                     break;
  1503.                     }
  1504.                 }
  1505.                 if (!@OCIBindByName($this->statement':'.$parameter$this->values[$parameter]-1$type)) {
  1506.                     $result $this->db->raiseError($this->statementnullnull,
  1507.                         'Could not bind CURSOR parameter'__FUNCTION__);
  1508.                     break;
  1509.                 }
  1510.             else {
  1511.                 $maxlength array_key_exists($parameter$this->type_maxlengths$this->type_maxlengths[$parameter: -1;
  1512.                 $this->values[$parameter$this->db->quote($this->values[$parameter]$typefalse);
  1513.                 $quoted_values[$i=$this->values[$parameter];
  1514.                 if (PEAR::isError($quoted_values[$i])) {
  1515.                     return $quoted_values[$i];
  1516.                 }
  1517.                 if (!@OCIBindByName($this->statement':'.$parameter$quoted_values[$i]$maxlength)) {
  1518.                     $result $this->db->raiseError($this->statementnullnull,
  1519.                         'could not bind non-abstract parameter'__FUNCTION__);
  1520.                     break;
  1521.                 }
  1522.             }
  1523.             ++$i;
  1524.         }
  1525.  
  1526.         $lob_keys array_keys($lobs);
  1527.         if (!PEAR::isError($result)) {
  1528.             $mode (!empty($lobs|| $this->db->in_transaction? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1529.             if (!@OCIExecute($this->statement$mode)) {
  1530.                 $err =$this->db->raiseError($this->statementnullnull,
  1531.                     'could not execute statement'__FUNCTION__);
  1532.                 return $err;
  1533.             }
  1534.  
  1535.             if (!empty($lobs)) {
  1536.                 foreach ($lob_keys as $i{
  1537.                     if (!is_null($lobs[$i]['value']&& $lobs[$i]['value'!== ''{
  1538.                         if ($lobs[$i]['file']{
  1539.                             $result $lobs[$i]['descriptor']->savefile($lobs[$i]['value']);
  1540.                         else {
  1541.                             $result $lobs[$i]['descriptor']->save($lobs[$i]['value']);
  1542.                         }
  1543.                         if (!$result{
  1544.                             $result $this->db->raiseError(nullnullnull,
  1545.                                 'Unable to save descriptor contents'__FUNCTION__);
  1546.                             break;
  1547.                         }
  1548.                     }
  1549.                 }
  1550.  
  1551.                 if (!PEAR::isError($result)) {
  1552.                     if (!$this->db->in_transaction{
  1553.                         if (!@OCICommit($connection)) {
  1554.                             $result $this->db->raiseError(nullnullnull,
  1555.                                 'Unable to commit transaction'__FUNCTION__);
  1556.                         }
  1557.                     else {
  1558.                         ++$this->db->uncommitedqueries;
  1559.                     }
  1560.                 }
  1561.             }
  1562.         }
  1563.  
  1564.         if (PEAR::isError($result)) {
  1565.             return $result;
  1566.         }
  1567.  
  1568.         if ($this->is_manip{
  1569.             $affected_rows $this->db->_affectedRows($connection$this->statement);
  1570.             return $affected_rows;
  1571.         }
  1572.  
  1573.         $result =$this->db->_wrapResult($this->statement$this->result_types,
  1574.             $result_class$result_wrap_class$this->limit$this->offset);
  1575.         $this->db->debug($this->query'execute'array('is_manip' => $this->is_manip'when' => 'post''result' => $result));
  1576.         return $result;
  1577.     }
  1578.  
  1579.     // }}}
  1580.     // {{{ free()
  1581.  
  1582.     /**
  1583.      * Release resources allocated for the specified prepared query.
  1584.      *
  1585.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1586.      * @access public
  1587.      */
  1588.     function free()
  1589.     {
  1590.         if (is_null($this->positions)) {
  1591.             return $this->db->raiseError(MDB2_ERRORnullnull,
  1592.                 'Prepared statement has already been freed'__FUNCTION__);
  1593.         }
  1594.         $result = MDB2_OK;
  1595.  
  1596.         if (!is_null($this->statement&& !@OCIFreeStatement($this->statement)) {
  1597.             $result $this->db->raiseError(nullnullnull,
  1598.                 'Could not free statement'__FUNCTION__);
  1599.         }
  1600.  
  1601.         parent::free();
  1602.         return $result;
  1603.     }
  1604. }
  1605. ?>

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