MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file oci8.php

Documentation is available at oci8.php

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

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