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-2004 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@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45.  
  46. // $Id: oci8.php,v 1.80 2005/07/28 09:27:07 lsmith Exp $
  47.  
  48. /**
  49.  * MDB2 OCI8 driver
  50.  *
  51.  * @package MDB2
  52.  * @category Database
  53.  * @author Lukas Smith <smith@backendmedia.com>
  54.  */
  55. class MDB2_Driver_oci8 extends MDB2_Driver_Common
  56. {
  57.     // {{{ properties
  58.     var $escape_quotes = "'";
  59.  
  60.     var $uncommitedqueries = 0;
  61.  
  62.     // }}}
  63.     // {{{ constructor
  64.  
  65.     /**
  66.      * Constructor
  67.      */
  68.     function __construct()
  69.     {
  70.         parent::__construct();
  71.  
  72.         $this->phptype 'oci8';
  73.         $this->dbsyntax 'oci8';
  74.  
  75.         $this->supported['sequences'= true;
  76.         $this->supported['indexes'= true;
  77.         $this->supported['summary_functions'= true;
  78.         $this->supported['order_by_text'= true;
  79.         $this->supported['current_id'= true;
  80.         $this->supported['affected_rows'= true;
  81.         $this->supported['transactions'= true;
  82.         $this->supported['limit_queries'= true;
  83.         $this->supported['LOBs'= true;
  84.         $this->supported['replace'= true;
  85.         $this->supported['sub_selects'= true;
  86.         $this->supported['auto_increment'= false;
  87.  
  88.         $this->options['DBA_username'= false;
  89.         $this->options['DBA_password'= false;
  90.         $this->options['database_name_prefix'= false;
  91.         $this->options['emulate_database'= true;
  92.         $this->options['default_tablespace'= false;
  93.         $this->options['default_text_field_length'= 4000;
  94.     }
  95.  
  96.     // }}}
  97.     // {{{ errorInfo()
  98.  
  99.     /**
  100.      * This method is used to collect information about an error
  101.      *
  102.      * @param integer $error 
  103.      * @return array 
  104.      * @access public
  105.      */
  106.     function errorInfo($error = null)
  107.     {
  108.         if (is_resource($error)) {
  109.             $error_data @OCIError($error);
  110.             $error = null;
  111.         elseif ($this->connection{
  112.             $error_data @OCIError($this->connection);
  113.         else {
  114.             $error_data @OCIError();
  115.         }
  116.         $native_code $error_data['code'];
  117.         $native_msg  $error_data['message'];
  118.         if (is_null($error)) {
  119.             static $ecode_map;
  120.             if (empty($ecode_map)) {
  121.                 $ecode_map = array(
  122.                     1    => MDB2_ERROR_CONSTRAINT,
  123.                     900  => MDB2_ERROR_SYNTAX,
  124.                     904  => MDB2_ERROR_NOSUCHFIELD,
  125.                     913  => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  126.                     921  => MDB2_ERROR_SYNTAX,
  127.                     923  => MDB2_ERROR_SYNTAX,
  128.                     942  => MDB2_ERROR_NOSUCHTABLE,
  129.                     955  => MDB2_ERROR_ALREADY_EXISTS,
  130.                     1400 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  131.                     1401 => MDB2_ERROR_INVALID,
  132.                     1407 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  133.                     1418 => MDB2_ERROR_NOT_FOUND,
  134.                     1476 => MDB2_ERROR_DIVZERO,
  135.                     1722 => MDB2_ERROR_INVALID_NUMBER,
  136.                     2289 => MDB2_ERROR_NOSUCHTABLE,
  137.                     2291 => MDB2_ERROR_CONSTRAINT,
  138.                     2292 => MDB2_ERROR_CONSTRAINT,
  139.                     2449 => MDB2_ERROR_CONSTRAINT,
  140.                 );
  141.             }
  142.             if (isset($ecode_map[$native_code])) {
  143.                 $error $ecode_map[$native_code];
  144.             }
  145.         }
  146.         return array($error$native_code$native_msg);
  147.     }
  148.  
  149.     // }}}
  150.     // {{{ beginTransaction()
  151.  
  152.     /**
  153.      * Start a transaction.
  154.      *
  155.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  156.      * @access public
  157.      */
  158.     function beginTransaction()
  159.     {
  160.         $this->debug('starting transaction''beginTransaction');
  161.         if ($this->in_transaction{
  162.             return MDB2_OK;  //nothing to do
  163.         }
  164.         if (!$this->destructor_registered && $this->opened_persistent{
  165.             $this->destructor_registered = true;
  166.             register_shutdown_function('MDB2_closeOpenTransactions');
  167.         }
  168.         $this->in_transaction = true;
  169.         ++$this->uncommitedqueries;
  170.         return MDB2_OK;
  171.     }
  172.  
  173.     // }}}
  174.     // {{{ commit()
  175.  
  176.     /**
  177.      * Commit the database changes done during a transaction that is in
  178.      * progress.
  179.      *
  180.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  181.      * @access public
  182.      */
  183.     function commit()
  184.     {
  185.         $this->debug('commit transaction''commit');
  186.         if (!$this->in_transaction{
  187.             return $this->raiseError(MDB2_ERRORnullnull,
  188.                 'commit: transaction changes are being auto committed');
  189.         }
  190.         if ($this->uncommitedqueries{
  191.             if (!@OCICommit($this->connection)) {
  192.                 return $this->raiseError();
  193.             }
  194.             $this->uncommitedqueries = 0;
  195.         }
  196.         $this->in_transaction = false;
  197.         return MDB2_OK;
  198.     }
  199.  
  200.     // }}}
  201.     // {{{ rollback()
  202.  
  203.     /**
  204.      * Cancel any database changes done during a transaction that is in
  205.      * progress.
  206.      *
  207.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  208.      * @access public
  209.      */
  210.     function rollback()
  211.     {
  212.         $this->debug('rolling back transaction''rollback');
  213.         if (!$this->in_transaction{
  214.             return $this->raiseError(MDB2_ERRORnullnull,
  215.                 'rollback: transactions can not be rolled back when changes are auto committed');
  216.         }
  217.         if ($this->uncommitedqueries{
  218.             if (!@OCIRollback($this->connection)) {
  219.                 return $this->raiseError();
  220.             }
  221.             $this->uncommitedqueries = 0;
  222.         }
  223.         $this->in_transaction = false;
  224.         return MDB2_OK;
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ _doConnect()
  229.  
  230.     /**
  231.      * do the grunt work of the connect
  232.      *
  233.      * @return connection on success or MDB2 Error Object on failure
  234.      * @access protected
  235.      */
  236.     function _doConnect($username$password$persistent = false)
  237.     {
  238.         if (!PEAR::loadExtension($this->phptype)) {
  239.             return $this->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  240.                 'extension '.$this->phptype.' is not compiled into PHP');
  241.         }
  242.  
  243.         if (isset($this->dsn['hostspec'])) {
  244.             $sid $this->dsn['hostspec'];
  245.         else {
  246.             $sid getenv('ORACLE_SID');
  247.         }
  248.         if (empty($sid)) {
  249.             return $this->raiseError(MDB2_ERRORnullnull,
  250.                 'it was not specified a valid Oracle Service Identifier (SID)');
  251.         }
  252.  
  253.         if (function_exists('oci_connect')) {
  254.             if (isset($this->dsn['new_link'])
  255.                 && ($this->dsn['new_link'== 'true' || $this->dsn['new_link'=== true)
  256.             {
  257.                 $connect_function 'oci_new_connect';
  258.             else {
  259.                 $connect_function $persistent 'oci_pconnect' 'oci_connect';
  260.             }
  261.  
  262.             $charset = empty($this->dsn['charset']? null : $this->dsn['charset'];
  263.             $connection @$connect_function($username$password$sid$charset);
  264.             $error @OCIError();
  265.             if (isset($error['code']&& $error['code'== 12541{
  266.                 // Couldn't find TNS listener.  Try direct connection.
  267.                 $connection @$connect_function($username$passwordnull$charset);
  268.             }
  269.         else {
  270.             $connect_function $persistent 'OCIPLogon' 'OCILogon';
  271.             $connection @$connect_function($username$password$sid);
  272.         }
  273.  
  274.         if (!$connection{
  275.             return $this->raiseError(MDB2_ERROR_CONNECT_FAILED);
  276.         }
  277.  
  278.         return $connection;
  279.     }
  280.  
  281.     // }}}
  282.     // {{{ connect()
  283.  
  284.     /**
  285.      * Connect to the database
  286.      *
  287.      * @return MDB2_OK on success, MDB2 Error Object on failure
  288.      * @access public
  289.      */
  290.     function connect()
  291.     {
  292.         if ($this->database_name && $this->options['emulate_database']{
  293.              $this->dsn['username'$this->options['database_name_prefix'].$this->database_name;
  294.         }
  295.         if (is_resource($this->connection)) {
  296.             if (count(array_diff($this->connected_dsn$this->dsn)) == 0
  297.                 && $this->opened_persistent == $this->options['persistent']
  298.             {
  299.                 return MDB2_OK;
  300.             }
  301.             $this->disconnect(false);
  302.         }
  303.  
  304.         $connection $this->_doConnect(
  305.             $this->dsn['username'],
  306.             $this->dsn['password'],
  307.             $this->options['persistent']
  308.         );
  309.         if (PEAR::isError($connection)) {
  310.             return $connection;
  311.         }
  312.         $this->connection $connection;
  313.         $this->connected_dsn $this->dsn;
  314.         $this->opened_persistent $this->options['persistent'];
  315.         $this->dbsyntax $this->dsn['dbsyntax'$this->dsn['dbsyntax'$this->phptype;
  316.  
  317.         $query "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
  318.         $error $this->_doQuery($querytrue);
  319.         if (PEAR::isError($error)) {
  320.             $this->disconnect(false);
  321.             return $error;
  322.         }
  323.  
  324.         $query "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
  325.         $error $this->_doQuery($querytrue);
  326.         if (PEAR::isError($error)) {
  327.             $this->disconnect(false);
  328.             return $error;
  329.         }
  330.  
  331.         return MDB2_OK;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ disconnect()
  336.  
  337.     /**
  338.      * Log out and disconnect from the database.
  339.      *
  340.      * @return mixed true on success, false if not connected and error
  341.      *                 object on error
  342.      * @access public
  343.      */
  344.     function disconnect($force = true)
  345.     {
  346.         if (is_resource($this->connection)) {
  347.             if (!$this->opened_persistent || $force{
  348.                 if (function_exists('oci_close')) {
  349.                     @oci_close($this->connection);
  350.                 else {
  351.                     @OCILogOff($this->connection);
  352.                 }
  353.             }
  354.             $this->connection = 0;
  355.             $this->uncommitedqueries = 0;
  356.         }
  357.         return MDB2_OK;
  358.     }
  359.  
  360.     // }}}
  361.     // {{{ standaloneQuery()
  362.  
  363.    /**
  364.      * execute a query as DBA
  365.      *
  366.      * @param string $query the SQL query
  367.      * @param mixed   $types  array that contains the types of the columns in
  368.      *                         the result set
  369.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  370.      * @access public
  371.      */
  372.     function &standaloneQuery($query$types = null)
  373.     {
  374.         $connection $this->_doConnect(
  375.             $this->options['DBA_username'],
  376.             $this->options['DBA_password'],
  377.             $this->options['persistent']
  378.         );
  379.         if (PEAR::isError($connection)) {
  380.             return $connection;
  381.         }
  382.  
  383.         $isManip = MDB2::isManip($query);
  384.         $offset $this->row_offset;
  385.         $limit $this->row_limit;
  386.         $this->row_offset $this->row_limit = 0;
  387.         $query $this->_modifyQuery($query$isManip$limit$offset);
  388.  
  389.         $result $this->_doQuery($query$isManip$connectionfalse);
  390.  
  391.         @OCILogOff($connection);
  392.         if (PEAR::isError($result)) {
  393.             return $result;
  394.         }
  395.  
  396.         if ($isManip{
  397.             return $result;
  398.         }
  399.         return $this->_wrapResult($result$typestruefalse$limit$offset);
  400.     }
  401.  
  402.     // }}}
  403.     // {{{ _modifyQuery()
  404.  
  405.     /**
  406.      * Changes a query string for various DBMS specific reasons
  407.      *
  408.      * @param string $query  query to modify
  409.      * @return the new (modified) query
  410.      * @access protected
  411.      */
  412.     function _modifyQuery($query)
  413.     {
  414.         // "SELECT 2+2" must be "SELECT 2+2 FROM dual" in Oracle
  415.         if (preg_match('/^\s*SELECT/i'$query)
  416.             && !preg_match('/\sFROM\s/i'$query)
  417.         {
  418.             $query .= " FROM dual";
  419.         }
  420.         return $query;
  421.     }
  422.  
  423.     // }}}
  424.     // {{{ _doQuery()
  425.  
  426.     /**
  427.      * Execute a query
  428.      * @param string $query  query
  429.      * @param boolean $isManip  if the query is a manipulation query
  430.      * @param resource $connection 
  431.      * @param string $database_name 
  432.      * @return result or error object
  433.      * @access protected
  434.      */
  435.     function _doQuery($query$isManip = false$connection = null$database_name = null)
  436.     {
  437.         $this->last_query $query;
  438.         $this->debug($query'query');
  439.         if ($this->getOption('disable_query')) {
  440.             if ($isManip{
  441.                 return MDB2_OK;
  442.             }
  443.             return null;
  444.         }
  445.  
  446.         if (is_null($connection)) {
  447.             $error $this->connect();
  448.             if (PEAR::isError($error)) {
  449.                 return $error;
  450.             }
  451.             $connection $this->connection;
  452.         }
  453.  
  454.         $result @OCIParse($connection$query);
  455.         if (!$result{
  456.             return $this->raiseError(MDB2_ERRORnullnull,
  457.                 'Could not create statement');
  458.         }
  459.  
  460.         $mode $this->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  461.         if (!@OCIExecute($result$mode)) {
  462.             return $this->raiseError($result);
  463.         }
  464.  
  465.         if ($isManip{
  466.             return @OCIRowCount($result);
  467.         }
  468.  
  469.         if (is_numeric($this->options['result_buffering'])) {
  470.             @ocisetprefetch($result$this->options['result_buffering']);
  471.         }
  472.         return $result;
  473.     }
  474.  
  475.     // }}}
  476.     // {{{ prepare()
  477.  
  478.     /**
  479.      * Prepares a query for multiple execution with execute().
  480.      * With some database backends, this is emulated.
  481.      * prepare() requires a generic query as string like
  482.      * 'INSERT INTO numbers VALUES(?,?)' or
  483.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  484.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  485.      * bindParam() and the query can be send off using the execute() method.
  486.      *
  487.      * @param string $query the query to prepare
  488.      * @param mixed   $types  array that contains the types of the placeholders
  489.      * @param mixed   $result_types  array that contains the types of the columns in
  490.      *                         the result set
  491.      * @return mixed resource handle for the prepared query on success, a MDB2
  492.      *         error on failure
  493.      * @access public
  494.      * @see bindParam, execute
  495.      */
  496.     function &prepare($query$types = null$result_types = null)
  497.     {
  498.         $this->debug($query'prepare');
  499.         $query $this->_modifyQuery($query);
  500.         $contains_lobs = false;
  501.         if (is_array($types)) {
  502.             $columns $variables '';
  503.             foreach ($types as $parameter => $type{
  504.                 if ($type == 'clob' || $type == 'blob'{
  505.                     $contains_lobs = true;
  506.                     $columns.= ($columns ', ' ' RETURNING ').$parameter;
  507.                     $variables.= ($variables ', ' ' INTO ').':'.$parameter;
  508.                 }
  509.             }
  510.         }
  511.         $placeholder_type_guess $placeholder_type = null;
  512.         $question '?';
  513.         $colon ':';
  514.         $position = 0;
  515.         $parameter = -1;
  516.         while ($position strlen($query)) {
  517.             $q_position strpos($query$question$position);
  518.             $c_position strpos($query$colon$position);
  519.             if ($q_position && $c_position{
  520.                 $p_position min($q_position$c_position);
  521.             elseif ($q_position{
  522.                 $p_position $q_position;
  523.             elseif ($c_position{
  524.                 $p_position $c_position;
  525.             else {
  526.                 break;
  527.             }
  528.             if (is_null($placeholder_type)) {
  529.                 $placeholder_type_guess $query[$p_position];
  530.             }
  531.             if (is_int($quote strpos($query"'"$position)) && $quote $p_position{
  532.                 if (!is_int($end_quote strpos($query"'"$quote + 1))) {
  533.                     return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  534.                         'prepare: query with an unterminated text string specified');
  535.                 }
  536.                 switch ($this->escape_quotes{
  537.                 case '':
  538.                 case "'":
  539.                     $position $end_quote + 1;
  540.                     break;
  541.                 default:
  542.                     if ($end_quote == $quote + 1{
  543.                         $position $end_quote + 1;
  544.                     else {
  545.                         if ($query[$end_quote-1== $this->escape_quotes{
  546.                             $position $end_quote;
  547.                         else {
  548.                             $position $end_quote + 1;
  549.                         }
  550.                     }
  551.                     break;
  552.                 }
  553.             elseif ($query[$position== $placeholder_type_guess{
  554.                 if ($placeholder_type_guess == ':' && !$contains_lobs{
  555.                     break;
  556.                 }
  557.                 if (is_null($placeholder_type)) {
  558.                     $placeholder_type $query[$p_position];
  559.                     $question $colon $placeholder_type;
  560.                 }
  561.                 if ($contains_lobs{
  562.                     if ($placeholder_type == ':'{
  563.                         $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/i''\\1'$query);
  564.                         if ($parameter === ''{
  565.                             return $this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  566.                                 'prepare: named parameter with an empty name');
  567.                         }
  568.                         $length strlen($parameter)+1;
  569.                     else {
  570.                         ++$parameter;
  571.                         $length strlen($parameter);
  572.                     }
  573.                     if (isset($types[$parameter])
  574.                         && ($types[$parameter== 'clob' || $types[$parameter== 'blob')
  575.                     {
  576.                         $value $this->quote(true$types[$parameter]);
  577.                         $query substr_replace($query$value$p_position$length);
  578.                         $position $p_position strlen($value- 1;
  579.                     elseif ($placeholder_type == '?'{
  580.                         $query substr_replace($query':'.$parameter$p_position1);
  581.                         $position $p_position strlen($parameter);
  582.                     }
  583.                 else {
  584.                     $query substr_replace($query':'.++$parameter$p_position1);
  585.                     $position $p_position strlen($parameter);
  586.                 }
  587.             else {
  588.                 $position $p_position;
  589.             }
  590.         }
  591.         if (is_array($types)) {
  592.             $query.= $columns.$variables;
  593.         }
  594.         $statement @OCIParse($this->connection$query);
  595.         if (!$statement{
  596.             return $this->raiseError(MDB2_ERRORnullnull,
  597.                 'Could not create statement');
  598.         }
  599.  
  600.         $class_name 'MDB2_Statement_'.$this->phptype;
  601.         $obj =new $class_name($this$statement$query$types$result_types$this->row_limit$this->row_offset);
  602.         return $obj;
  603.     }
  604.  
  605.     // }}}
  606.     // {{{ nextID()
  607.  
  608.     /**
  609.      * returns the next free id of a sequence
  610.      *
  611.      * @param string $seq_name name of the sequence
  612.      * @param boolean $ondemand when true the seqence is
  613.      *                            automatic created, if it
  614.      *                            not exists
  615.      * @return mixed MDB2 Error Object or id
  616.      * @access public
  617.      */
  618.     function nextID($seq_name$ondemand = true)
  619.     {
  620.         $sequence_name $this->getSequenceName($seq_name);
  621.         $query = "SELECT $sequence_name.nextval FROM DUAL";
  622.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  623.         $result $this->queryOne($query'integer');
  624.         $this->popExpect();
  625.         if (PEAR::isError($result)) {
  626.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  627.                 $this->loadModule('Manager');
  628.                 $result $this->manager->createSequence($seq_name1);
  629.                 if (PEAR::isError($result)) {
  630.                     return $result;
  631.                 }
  632.                 return $this->nextId($seq_namefalse);
  633.             }
  634.         }
  635.         return $result;
  636.     }
  637.  
  638.     // }}}
  639.     // {{{ currId()
  640.  
  641.     /**
  642.      * returns the current id of a sequence
  643.      *
  644.      * @param string $seq_name name of the sequence
  645.      * @return mixed MDB2_Error or id
  646.      * @access public
  647.      */
  648.     function currId($seq_name)
  649.     {
  650.         $sequence_name $this->getSequenceName($seq_name);
  651.         return $this->queryOne("SELECT $sequence_name.currval FROM DUAL");
  652.     }
  653. }
  654.  
  655. class MDB2_Result_oci8 extends MDB2_Result_Common
  656. {
  657.     // {{{ _skipLimitOffset()
  658.  
  659.     /**
  660.      * Skip the first row of a result set.
  661.      *
  662.      * @param resource $result 
  663.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  664.      * @access protected
  665.      */
  666.     function _skipLimitOffset()
  667.     {
  668.         if ($this->limit{
  669.             if ($this->rownum $this->limit{
  670.                 return false;
  671.             }
  672.         }
  673.         if ($this->offset{
  674.             while ($this->offset_count $this->offset{
  675.                 ++$this->offset_count;
  676.                 if (!@OCIFetchInto($this->result$rowOCI_RETURN_NULLS)) {
  677.                     $this->offset_count $this->offset;
  678.                     return false;
  679.                 }
  680.             }
  681.         }
  682.         return true;
  683.     }
  684.  
  685.     // }}}
  686.     // {{{ fetchRow()
  687.  
  688.     /**
  689.      * Fetch a row and insert the data into an existing array.
  690.      *
  691.      * @param int       $fetchmode  how the array data should be indexed
  692.      * @param int    $rownum    number of the row where the data can be found
  693.      * @return int data array on success, a MDB2 error on failure
  694.      * @access public
  695.      */
  696.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  697.     {
  698.         if (!$this->_skipLimitOffset()) {
  699.             return null;
  700.         }
  701.         if (!is_null($rownum)) {
  702.             $seek $this->seek($rownum);
  703.             if (PEAR::isError($seek)) {
  704.                 return $seek;
  705.             }
  706.         }
  707.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  708.             $fetchmode $this->db->fetchmode;
  709.         }
  710.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  711.             @OCIFetchInto($this->result$rowOCI_ASSOC+OCI_RETURN_NULLS);
  712.             if (is_array($row)
  713.                 && $this->db->options['portability'MDB2_PORTABILITY_LOWERCASE
  714.             {
  715.                 $row array_change_key_case($rowCASE_LOWER);
  716.             }
  717.         else {
  718.             @OCIFetchInto($this->result$rowOCI_RETURN_NULLS);
  719.         }
  720.         if (!$row{
  721.             if (is_null($this->result)) {
  722.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  723.                     'fetchRow: resultset has already been freed');
  724.             }
  725.             return null;
  726.         }
  727.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  728.             $this->db->_rtrimArrayValues($row);
  729.         }
  730.         if (isset($this->values)) {
  731.             $this->_assignBindColumns($row);
  732.         }
  733.         if (isset($this->types)) {
  734.             $row $this->db->datatype->convertResultRow($this->types$row);
  735.         }
  736.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  737.             $object_class $this->db->options['fetch_class'];
  738.             if ($object_class == 'stdClass'{
  739.                 $row = (object) $row;
  740.             else {
  741.                 $row &new $object_class($row);
  742.             }
  743.         }
  744.         ++$this->rownum;
  745.         return $row;
  746.     }
  747.  
  748.     // }}}
  749.     // {{{ _getColumnNames()
  750.  
  751.     /**
  752.      * Retrieve the names of columns returned by the DBMS in a query result.
  753.      *
  754.      * @return mixed associative array variable
  755.      *       that holds the names of columns. The indexes of the array are
  756.      *       the column names mapped to lower case and the values are the
  757.      *       respective numbers of the columns starting from 0. Some DBMS may
  758.      *       not return any columns when the result set does not contain any
  759.      *       rows.
  760.      * @access private
  761.      */
  762.     function _getColumnNames()
  763.     {
  764.         $columns = array();
  765.         $numcols $this->numCols();
  766.         if (PEAR::isError($numcols)) {
  767.             return $numcols;
  768.         }
  769.         for ($column = 0; $column $numcols$column++{
  770.             $column_name @OCIColumnName($this->result$column + 1);
  771.             $columns[$column_name$column;
  772.         }
  773.         if ($this->db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  774.             $columns array_change_key_case($columnsCASE_LOWER);
  775.         }
  776.         return $columns;
  777.     }
  778.  
  779.     // }}}
  780.     // {{{ numCols()
  781.  
  782.     /**
  783.      * Count the number of columns returned by the DBMS in a query result.
  784.      *
  785.      * @return mixed integer value with the number of columns, a MDB2 error
  786.      *       on failure
  787.      * @access public
  788.      */
  789.     function numCols()
  790.     {
  791.         $cols @OCINumCols($this->result);
  792.         if (is_null($cols)) {
  793.             if (is_null($this->result)) {
  794.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  795.                     'numCols: resultset has already been freed');
  796.             }
  797.             return $this->db->raiseError();
  798.         }
  799.         return $cols;
  800.     }
  801.  
  802.     // }}}
  803.     // {{{ free()
  804.  
  805.     /**
  806.      * Free the internal resources associated with $result.
  807.      *
  808.      * @return boolean true on success, false if $result is invalid
  809.      * @access public
  810.      */
  811.     function free()
  812.     {
  813.         $free @OCIFreeCursor($this->result);
  814.         if (!$free{
  815.             if (is_null($this->result)) {
  816.                 return MDB2_OK;
  817.             }
  818.             return $this->db->raiseError();
  819.         }
  820.         $this->result = null;
  821.         return MDB2_OK;
  822.     }
  823. }
  824.  
  825. {
  826.     var $buffer;
  827.     var $buffer_rownum = - 1;
  828.  
  829.     // {{{ _fillBuffer()
  830.  
  831.     /**
  832.      * Fill the row buffer
  833.      *
  834.      * @param int $rownum   row number upto which the buffer should be filled
  835.                             if the row number is null all rows are ready into the buffer
  836.      * @return boolean true on success, false on failure
  837.      * @access protected
  838.      */
  839.     function _fillBuffer($rownum = null)
  840.     {
  841.         if (isset($this->buffer&& is_array($this->buffer)) {
  842.             if (is_null($rownum)) {
  843.                 if (!end($this->buffer)) {
  844.                     return false;
  845.                 }
  846.             elseif (isset($this->buffer[$rownum])) {
  847.                 return (bool)$this->buffer[$rownum];
  848.             }
  849.         }
  850.  
  851.         if (!$this->_skipLimitOffset()) {
  852.             return false;
  853.         }
  854.  
  855.         $row = true;
  856.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  857.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  858.             && ($row @OCIFetchInto($this->result$bufferOCI_RETURN_NULLS))
  859.         {
  860.             ++$this->buffer_rownum;
  861.             $this->buffer[$this->buffer_rownum$buffer;
  862.         }
  863.  
  864.         if (!$row{
  865.             ++$this->buffer_rownum;
  866.             $this->buffer[$this->buffer_rownum= false;
  867.             return false;
  868.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  869.             ++$this->buffer_rownum;
  870.             $this->buffer[$this->buffer_rownum= false;
  871.         }
  872.         return true;
  873.     }
  874.  
  875.     // }}}
  876.     // {{{ fetchRow()
  877.  
  878.     /**
  879.      * Fetch a row and insert the data into an existing array.
  880.      *
  881.      * @param int       $fetchmode  how the array data should be indexed
  882.      * @param int    $rownum    number of the row where the data can be found
  883.      * @return int data array on success, a MDB2 error on failure
  884.      * @access public
  885.      */
  886.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  887.     {
  888.         if (is_null($this->result)) {
  889.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  890.                 'fetchRow: resultset has already been freed');
  891.         }
  892.         if (!is_null($rownum)) {
  893.             $seek $this->seek($rownum);
  894.             if (PEAR::isError($seek)) {
  895.                 return $seek;
  896.             }
  897.         }
  898.         $target_rownum $this->rownum + 1;
  899.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  900.             $fetchmode $this->db->fetchmode;
  901.         }
  902.         if (!$this->_fillBuffer($target_rownum)) {
  903.             return null;
  904.         }
  905.         $row $this->buffer[$target_rownum];
  906.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  907.             $column_names $this->getColumnNames();
  908.             foreach ($column_names as $name => $i{
  909.                 $column_names[$name$row[$i];
  910.             }
  911.             $row $column_names;
  912.         }
  913.         if (isset($this->values)) {
  914.             $this->_assignBindColumns($row);
  915.         }
  916.         if (isset($this->types)) {
  917.             $row $this->db->datatype->convertResultRow($this->types$row);
  918.         }
  919.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  920.             $this->db->_rtrimArrayValues($row);
  921.         }
  922.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  923.             $object_class $this->db->options['fetch_class'];
  924.             if ($object_class == 'stdClass'{
  925.                 $row = (object) $row;
  926.             else {
  927.                 $row &new $object_class($row);
  928.             }
  929.         }
  930.         ++$this->rownum;
  931.         return $row;
  932.     }
  933.  
  934.     // }}}
  935.     // {{{ seek()
  936.  
  937.     /**
  938.     * seek to a specific row in a result set
  939.     *
  940.     * @param int    $rownum    number of the row where the data can be found
  941.     * @return mixed MDB2_OK on success, a MDB2 error on failure
  942.     * @access public
  943.     */
  944.     function seek($rownum = 0)
  945.     {
  946.         if (is_null($this->result)) {
  947.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  948.                 'seek: resultset has already been freed');
  949.         }
  950.         $this->rownum $rownum - 1;
  951.         return MDB2_OK;
  952.     }
  953.  
  954.     // }}}
  955.     // {{{ valid()
  956.  
  957.     /**
  958.      * check if the end of the result set has been reached
  959.      *
  960.      * @return mixed true or false on sucess, a MDB2 error on failure
  961.      * @access public
  962.      */
  963.     function valid()
  964.     {
  965.         if (is_null($this->result)) {
  966.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  967.                 'valid: resultset has already been freed');
  968.         }
  969.         if ($this->_fillBuffer($this->rownum + 1)) {
  970.             return true;
  971.         }
  972.         return false;
  973.     }
  974.  
  975.     // }}}
  976.     // {{{ numRows()
  977.  
  978.     /**
  979.      * returns the number of rows in a result object
  980.      *
  981.      * @return mixed MDB2 Error Object or the number of rows
  982.      * @access public
  983.      */
  984.     function numRows()
  985.     {
  986.         if (is_null($this->result)) {
  987.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  988.                 'seek: resultset has already been freed');
  989.         }
  990.         $this->_fillBuffer();
  991.         return $this->buffer_rownum;
  992.     }
  993.  
  994.     // }}}
  995.     // {{{ free()
  996.  
  997.     /**
  998.      * Free the internal resources associated with $result.
  999.      *
  1000.      * @return boolean true on success, false if $result is invalid
  1001.      * @access public
  1002.      */
  1003.     function free()
  1004.     {
  1005.         $this->buffer = null;
  1006.         $this->buffer_rownum = null;
  1007.         $free = parent::free();
  1008.     }
  1009. }
  1010.  
  1011. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1012. {
  1013.     // }}}
  1014.     // {{{ _execute()
  1015.  
  1016.     /**
  1017.      * Execute a prepared query statement helper method.
  1018.      *
  1019.      * @param mixed $result_class string which specifies which result class to use
  1020.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1021.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1022.      * @access private
  1023.      */
  1024.     function &_execute($result_class = true$result_wrap_class = false)
  1025.     {
  1026.         $isManip = MDB2::isManip($this->query);
  1027.         $this->db->last_query = $this->query;
  1028.         $this->db->debug($this->query'execute');
  1029.         if ($this->db->getOption('disable_query')) {
  1030.             if ($isManip{
  1031.                 return MDB2_OK;
  1032.             }
  1033.             return null;
  1034.         }
  1035.  
  1036.         $connected $this->db->connect();
  1037.         if (PEAR::isError($connected)) {
  1038.             return $connected;
  1039.         }
  1040.  
  1041.         $result = MDB2_OK;
  1042.         $lobs $quoted_values = array();
  1043.         $i = 0;
  1044.         $types_numeric is_numeric(key($this->types));
  1045.         foreach ($this->values as $parameter => $value{
  1046.             $type_key $types_numeric $i $parameter;
  1047.             $type = isset($this->types[$type_key]$this->types[$type_key: null;
  1048.             if ($type == 'clob' || $type == 'blob'{
  1049.                 $lobs[$i]['file'= false;
  1050.                 if (is_resource($value)) {
  1051.                     $fp $value;
  1052.                     $value '';
  1053.                     while (!feof($fp)) {
  1054.                         $value.= fread($fp8192);
  1055.                     }
  1056.                 elseif (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1057.                     $lobs[$i]['file'= true;
  1058.                     if ($match[1== 'file://'{
  1059.                         $value $match[2];
  1060.                     }
  1061.                 }
  1062.                 $lobs[$i]['value'$value;
  1063.                 $lobs[$i]['descriptor'@OCINewDescriptor($this->db->connectionOCI_D_LOB);
  1064.                 if (!is_object($lobs[$i]['descriptor'])) {
  1065.                     $result $this->db->raiseError();
  1066.                     break;
  1067.                 }
  1068.                 $lob_type ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1069.                 if (!@OCIBindByName($this->statement':'.$parameter$lobs[$i]['descriptor']-1$lob_type)) {
  1070.                     $result $this->db->raiseError($this->statement);
  1071.                     break;
  1072.                 }
  1073.             else {
  1074.                 $quoted_values[$i$this->db->quote($value$typefalse);
  1075.                 if (PEAR::isError($quoted_values[$i])) {
  1076.                     return $quoted_values[$i];
  1077.                 }
  1078.                 if (!@OCIBindByName($this->statement':'.$parameter$quoted_values[$i])) {
  1079.                     $result $this->db->raiseError($this->statement);
  1080.                     break;
  1081.                 }
  1082.             }
  1083.             ++$i;
  1084.         }
  1085.  
  1086.         if (!PEAR::isError($result)) {
  1087.             $mode (!empty($lobs|| $this->db->in_transaction? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1088.             if (!@OCIExecute($this->statement$mode)) {
  1089.                 return $this->db->raiseError($this->statement);
  1090.             }
  1091.  
  1092.             if (!empty($lobs)) {
  1093.                 foreach ($lobs as $i => $stream{
  1094.                     if (!is_null($stream['value']&& $stream['value'!== ''{
  1095.                         if ($stream['file']{
  1096.                             $result $lobs[$i]['descriptor']->savefile($stream['value']);
  1097.                         else {
  1098.                             $result $lobs[$i]['descriptor']->save($stream['value']);
  1099.                         }
  1100.                         if (!$result{
  1101.                             $result $this->db->raiseError();
  1102.                             break;
  1103.                         }
  1104.                     }
  1105.                 }
  1106.  
  1107.                 if (!PEAR::isError($result)) {
  1108.                     if (!$this->db->in_transaction{
  1109.                         if (!@OCICommit($this->db->connection)) {
  1110.                             $result $this->db->raiseError();
  1111.                         }
  1112.                     else {
  1113.                         ++$this->db->uncommitedqueries;
  1114.                     }
  1115.                 }
  1116.             }
  1117.         }
  1118.  
  1119.         foreach ($lobs as $i => $foo{
  1120.             $lobs[$i]['descriptor']->close();
  1121.             $lobs[$i]['descriptor']->free();
  1122.         }
  1123.  
  1124.         if (PEAR::isError($result)) {
  1125.             return $result;
  1126.         }
  1127.  
  1128.         if ($isManip{
  1129.             return @OCIRowCount($this->statement);
  1130.         }
  1131.  
  1132.         return $this->db->_wrapResult($this->statement$isManip$this->types,
  1133.             $result_class$result_wrap_class$this->row_offset$this->row_limit);
  1134.     }
  1135.  
  1136.     // }}}
  1137.     // {{{ free()
  1138.  
  1139.     /**
  1140.      * Release resources allocated for the specified prepared query.
  1141.      *
  1142.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1143.      * @access public
  1144.      */
  1145.     function free()
  1146.     {
  1147.         if (!@OCIFreeStatement($this->statement)) {
  1148.             return $this->db->raiseError();
  1149.         }
  1150.         return MDB2_OK;
  1151.     }
  1152. }
  1153. ?>

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