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

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