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

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