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.112 2005/12/29 18:25:07 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.         else {
  609.             $types = array();
  610.         }
  611.         $placeholder_type_guess $placeholder_type = null;
  612.         $question '?';
  613.         $colon ':';
  614.         $position = 0;
  615.         $parameter = -1;
  616.         while ($position strlen($query)) {
  617.             $q_position strpos($query$question$position);
  618.             $c_position strpos($query$colon$position);
  619.             if ($q_position && $c_position{
  620.                 $p_position min($q_position$c_position);
  621.             elseif ($q_position{
  622.                 $p_position $q_position;
  623.             elseif ($c_position{
  624.                 $p_position $c_position;
  625.             else {
  626.                 break;
  627.             }
  628.             if (is_null($placeholder_type)) {
  629.                 $placeholder_type_guess $query[$p_position];
  630.             }
  631.             if (is_int($quote strpos($query"'"$position)) && $quote $p_position{
  632.                 if (!is_int($end_quote strpos($query"'"$quote + 1))) {
  633.                     $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  634.                         'prepare: query with an unterminated text string specified');
  635.                     return $err;
  636.                 }
  637.                 switch ($this->escape_quotes{
  638.                 case '':
  639.                 case "'":
  640.                     $position $end_quote + 1;
  641.                     break;
  642.                 default:
  643.                     if ($end_quote == $quote + 1{
  644.                         $position $end_quote + 1;
  645.                     else {
  646.                         if ($query[$end_quote-1== $this->escape_quotes{
  647.                             $position $end_quote;
  648.                         else {
  649.                             $position $end_quote + 1;
  650.                         }
  651.                     }
  652.                     break;
  653.                 }
  654.             elseif ($query[$position== $placeholder_type_guess{
  655.                 if ($placeholder_type_guess == ':' && !$contains_lobs{
  656.                     break;
  657.                 }
  658.                 if (is_null($placeholder_type)) {
  659.                     $placeholder_type $query[$p_position];
  660.                     $question $colon $placeholder_type;
  661.                 }
  662.                 if ($contains_lobs{
  663.                     $type next($types);
  664.                     if ($placeholder_type == ':'{
  665.                         $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  666.                         if ($parameter === ''{
  667.                             $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  668.                                 'prepare: named parameter with an empty name');
  669.                             return $err;
  670.                         }
  671.                         $length strlen($parameter)+1;
  672.                         if (!$type && isset($types[$parameter])) {
  673.                             $type $types[$parameter];
  674.                         }
  675.                     else {
  676.                         ++$parameter;
  677.                         $length strlen($parameter);
  678.                     }
  679.                     if ($type == 'clob' || $type == 'blob'{
  680.                         $value $this->quote(true$type);
  681.                         $query substr_replace($query$value$p_position$length);
  682.                         $position $p_position strlen($value- 1;
  683.                     elseif ($placeholder_type == '?'{
  684.                         $query substr_replace($query':'.$parameter$p_position1);
  685.                         $position $p_position strlen($parameter);
  686.                     }
  687.                 else {
  688.                     $query substr_replace($query':'.++$parameter$p_position1);
  689.                     $position $p_position strlen($parameter);
  690.                 }
  691.             else {
  692.                 $position $p_position;
  693.             }
  694.         }
  695.         if (is_array($types)) {
  696.             $query.= $columns.$variables;
  697.         }
  698.         $connection $this->getConnection();
  699.         if (PEAR::isError($connection)) {
  700.             return $connection;
  701.         }
  702.         $statement @OCIParse($connection$query);
  703.         if (!$statement{
  704.             $err =$this->raiseError(MDB2_ERRORnullnull,
  705.                 'Could not create statement');
  706.             return $err;
  707.         }
  708.  
  709.         $class_name 'MDB2_Statement_'.$this->phptype;
  710.         $obj =new $class_name($this$statement$query$types$result_types$is_manip$this->row_limit$this->row_offset);
  711.         return $obj;
  712.     }
  713.  
  714.     // }}}
  715.     // {{{ nextID()
  716.  
  717.     /**
  718.      * returns the next free id of a sequence
  719.      *
  720.      * @param string $seq_name name of the sequence
  721.      * @param boolean $ondemand when true the seqence is
  722.      *                            automatic created, if it
  723.      *                            not exists
  724.      * @return mixed MDB2 Error Object or id
  725.      * @access public
  726.      */
  727.     function nextID($seq_name$ondemand = true)
  728.     {
  729.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  730.         $query = "SELECT $sequence_name.nextval FROM DUAL";
  731.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  732.         $result $this->queryOne($query'integer');
  733.         $this->popExpect();
  734.         if (PEAR::isError($result)) {
  735.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  736.                 $this->loadModule('Manager');
  737.                 $result $this->manager->createSequence($seq_name1);
  738.                 if (PEAR::isError($result)) {
  739.                     return $result;
  740.                 }
  741.                 return $this->nextId($seq_namefalse);
  742.             }
  743.         }
  744.         return $result;
  745.     }
  746.  
  747.     // }}}
  748.     // {{{ currId()
  749.  
  750.     /**
  751.      * returns the current id of a sequence
  752.      *
  753.      * @param string $seq_name name of the sequence
  754.      * @return mixed MDB2_Error or id
  755.      * @access public
  756.      */
  757.     function currId($seq_name)
  758.     {
  759.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  760.         return $this->queryOne("SELECT $sequence_name.currval FROM DUAL");
  761.     }
  762. }
  763.  
  764. class MDB2_Result_oci8 extends MDB2_Result_Common
  765. {
  766.     // {{{ _skipLimitOffset()
  767.  
  768.     /**
  769.      * Skip the first row of a result set.
  770.      *
  771.      * @param resource $result 
  772.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  773.      * @access protected
  774.      */
  775.     function _skipLimitOffset()
  776.     {
  777.         if ($this->limit{
  778.             if ($this->rownum $this->limit{
  779.                 return false;
  780.             }
  781.         }
  782.         if ($this->offset{
  783.             while ($this->offset_count $this->offset{
  784.                 ++$this->offset_count;
  785.                 if (!@OCIFetchInto($this->result$rowOCI_RETURN_NULLS)) {
  786.                     $this->offset_count $this->offset;
  787.                     return false;
  788.                 }
  789.             }
  790.         }
  791.         return true;
  792.     }
  793.  
  794.     // }}}
  795.     // {{{ fetchRow()
  796.  
  797.     /**
  798.      * Fetch a row and insert the data into an existing array.
  799.      *
  800.      * @param int       $fetchmode  how the array data should be indexed
  801.      * @param int    $rownum    number of the row where the data can be found
  802.      * @return int data array on success, a MDB2 error on failure
  803.      * @access public
  804.      */
  805.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  806.     {
  807.         if (!$this->_skipLimitOffset()) {
  808.             $null = null;
  809.             return $null;
  810.         }
  811.         if (!is_null($rownum)) {
  812.             $seek $this->seek($rownum);
  813.             if (PEAR::isError($seek)) {
  814.                 return $seek;
  815.             }
  816.         }
  817.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  818.             $fetchmode $this->db->fetchmode;
  819.         }
  820.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  821.             @OCIFetchInto($this->result$rowOCI_ASSOC+OCI_RETURN_NULLS);
  822.             if (is_array($row)
  823.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  824.             {
  825.                 $row array_change_key_case($row$this->db->options['field_case']);
  826.             }
  827.         else {
  828.             @OCIFetchInto($this->result$rowOCI_RETURN_NULLS);
  829.         }
  830.         if (!$row{
  831.             if (is_null($this->result)) {
  832.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  833.                     'fetchRow: resultset has already been freed');
  834.                 return $err;
  835.             }
  836.             $null = null;
  837.             return $null;
  838.  
  839.         }
  840.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  841.             $this->db->_fixResultArrayValues($rowMDB2_PORTABILITY_RTRIM);
  842.         }
  843.         if (!empty($this->values)) {
  844.             $this->_assignBindColumns($row);
  845.         }
  846.         if (!empty($this->types)) {
  847.             $row $this->db->datatype->convertResultRow($this->types$row);
  848.         }
  849.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  850.             $object_class $this->db->options['fetch_class'];
  851.             if ($object_class == 'stdClass'{
  852.                 $row = (object) $row;
  853.             else {
  854.                 $row &new $object_class($row);
  855.             }
  856.         }
  857.         ++$this->rownum;
  858.         return $row;
  859.     }
  860.  
  861.     // }}}
  862.     // {{{ _getColumnNames()
  863.  
  864.     /**
  865.      * Retrieve the names of columns returned by the DBMS in a query result.
  866.      *
  867.      * @return mixed associative array variable
  868.      *       that holds the names of columns. The indexes of the array are
  869.      *       the column names mapped to lower case and the values are the
  870.      *       respective numbers of the columns starting from 0. Some DBMS may
  871.      *       not return any columns when the result set does not contain any
  872.      *       rows.
  873.      * @access private
  874.      */
  875.     function _getColumnNames()
  876.     {
  877.         $columns = array();
  878.         $numcols $this->numCols();
  879.         if (PEAR::isError($numcols)) {
  880.             return $numcols;
  881.         }
  882.         for ($column = 0; $column $numcols$column++{
  883.             $column_name @OCIColumnName($this->result$column + 1);
  884.             $columns[$column_name$column;
  885.         }
  886.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  887.             $columns array_change_key_case($columns$this->db->options['field_case']);
  888.         }
  889.         return $columns;
  890.     }
  891.  
  892.     // }}}
  893.     // {{{ numCols()
  894.  
  895.     /**
  896.      * Count the number of columns returned by the DBMS in a query result.
  897.      *
  898.      * @return mixed integer value with the number of columns, a MDB2 error
  899.      *       on failure
  900.      * @access public
  901.      */
  902.     function numCols()
  903.     {
  904.         $cols @OCINumCols($this->result);
  905.         if (is_null($cols)) {
  906.             if (is_null($this->result)) {
  907.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  908.                     'numCols: resultset has already been freed');
  909.             }
  910.             return $this->db->raiseError();
  911.         }
  912.         return $cols;
  913.     }
  914.  
  915.     // }}}
  916.     // {{{ free()
  917.  
  918.     /**
  919.      * Free the internal resources associated with $result.
  920.      *
  921.      * @return boolean true on success, false if $result is invalid
  922.      * @access public
  923.      */
  924.     function free()
  925.     {
  926.         $free @OCIFreeCursor($this->result);
  927.         if (!$free{
  928.             if (is_null($this->result)) {
  929.                 return MDB2_OK;
  930.             }
  931.             return $this->db->raiseError();
  932.         }
  933.         $this->result = null;
  934.         return MDB2_OK;
  935.     }
  936. }
  937.  
  938. {
  939.     var $buffer;
  940.     var $buffer_rownum = - 1;
  941.  
  942.     // {{{ _fillBuffer()
  943.  
  944.     /**
  945.      * Fill the row buffer
  946.      *
  947.      * @param int $rownum   row number upto which the buffer should be filled
  948.                             if the row number is null all rows are ready into the buffer
  949.      * @return boolean true on success, false on failure
  950.      * @access protected
  951.      */
  952.     function _fillBuffer($rownum = null)
  953.     {
  954.         if (isset($this->buffer&& is_array($this->buffer)) {
  955.             if (is_null($rownum)) {
  956.                 if (!end($this->buffer)) {
  957.                     return false;
  958.                 }
  959.             elseif (isset($this->buffer[$rownum])) {
  960.                 return (bool)$this->buffer[$rownum];
  961.             }
  962.         }
  963.  
  964.         if (!$this->_skipLimitOffset()) {
  965.             return false;
  966.         }
  967.  
  968.         $row = true;
  969.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  970.             && (!$this->limit || $this->buffer_rownum < $this->limit)
  971.             && ($row @OCIFetchInto($this->result$bufferOCI_RETURN_NULLS))
  972.         {
  973.             ++$this->buffer_rownum;
  974.             $this->buffer[$this->buffer_rownum$buffer;
  975.         }
  976.  
  977.         if (!$row{
  978.             ++$this->buffer_rownum;
  979.             $this->buffer[$this->buffer_rownum= false;
  980.             return false;
  981.         elseif ($this->limit && $this->buffer_rownum >= $this->limit{
  982.             ++$this->buffer_rownum;
  983.             $this->buffer[$this->buffer_rownum= false;
  984.         }
  985.         return true;
  986.     }
  987.  
  988.     // }}}
  989.     // {{{ fetchRow()
  990.  
  991.     /**
  992.      * Fetch a row and insert the data into an existing array.
  993.      *
  994.      * @param int       $fetchmode  how the array data should be indexed
  995.      * @param int    $rownum    number of the row where the data can be found
  996.      * @return int data array on success, a MDB2 error on failure
  997.      * @access public
  998.      */
  999.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  1000.     {
  1001.         if (is_null($this->result)) {
  1002.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1003.                 'fetchRow: resultset has already been freed');
  1004.             return $err;
  1005.         }
  1006.         if (!is_null($rownum)) {
  1007.             $seek $this->seek($rownum);
  1008.             if (PEAR::isError($seek)) {
  1009.                 return $seek;
  1010.             }
  1011.         }
  1012.         $target_rownum $this->rownum + 1;
  1013.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  1014.             $fetchmode $this->db->fetchmode;
  1015.         }
  1016.         if (!$this->_fillBuffer($target_rownum)) {
  1017.             $null = null;
  1018.             return $null;
  1019.         }
  1020.         $row $this->buffer[$target_rownum];
  1021.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  1022.             $column_names $this->getColumnNames();
  1023.             foreach ($column_names as $name => $i{
  1024.                 $column_names[$name$row[$i];
  1025.             }
  1026.             $row $column_names;
  1027.         }
  1028.         if (!empty($this->values)) {
  1029.             $this->_assignBindColumns($row);
  1030.         }
  1031.         if (!empty($this->types)) {
  1032.             $row $this->db->datatype->convertResultRow($this->types$row);
  1033.         }
  1034.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1035.             $this->db->_fixResultArrayValues($rowMDB2_PORTABILITY_RTRIM);
  1036.         }
  1037.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1038.             $object_class $this->db->options['fetch_class'];
  1039.             if ($object_class == 'stdClass'{
  1040.                 $row = (object) $row;
  1041.             else {
  1042.                 $row &new $object_class($row);
  1043.             }
  1044.         }
  1045.         ++$this->rownum;
  1046.         return $row;
  1047.     }
  1048.  
  1049.     // }}}
  1050.     // {{{ seek()
  1051.  
  1052.     /**
  1053.      * seek to a specific row in a result set
  1054.      *
  1055.      * @param int    $rownum    number of the row where the data can be found
  1056.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1057.      * @access public
  1058.      */
  1059.     function seek($rownum = 0)
  1060.     {
  1061.         if (is_null($this->result)) {
  1062.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1063.                 'seek: resultset has already been freed');
  1064.         }
  1065.         $this->rownum $rownum - 1;
  1066.         return MDB2_OK;
  1067.     }
  1068.  
  1069.     // }}}
  1070.     // {{{ valid()
  1071.  
  1072.     /**
  1073.      * check if the end of the result set has been reached
  1074.      *
  1075.      * @return mixed true or false on sucess, a MDB2 error on failure
  1076.      * @access public
  1077.      */
  1078.     function valid()
  1079.     {
  1080.         if (is_null($this->result)) {
  1081.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1082.                 'valid: resultset has already been freed');
  1083.         }
  1084.         if ($this->_fillBuffer($this->rownum + 1)) {
  1085.             return true;
  1086.         }
  1087.         return false;
  1088.     }
  1089.  
  1090.     // }}}
  1091.     // {{{ numRows()
  1092.  
  1093.     /**
  1094.      * returns the number of rows in a result object
  1095.      *
  1096.      * @return mixed MDB2 Error Object or the number of rows
  1097.      * @access public
  1098.      */
  1099.     function numRows()
  1100.     {
  1101.         if (is_null($this->result)) {
  1102.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1103.                 'seek: resultset has already been freed');
  1104.         }
  1105.         $this->_fillBuffer();
  1106.         return $this->buffer_rownum;
  1107.     }
  1108.  
  1109.     // }}}
  1110.     // {{{ free()
  1111.  
  1112.     /**
  1113.      * Free the internal resources associated with $result.
  1114.      *
  1115.      * @return boolean true on success, false if $result is invalid
  1116.      * @access public
  1117.      */
  1118.     function free()
  1119.     {
  1120.         $this->buffer = null;
  1121.         $this->buffer_rownum = null;
  1122.         $free = parent::free();
  1123.     }
  1124. }
  1125.  
  1126. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1127. {
  1128.     // }}}
  1129.     // {{{ _execute()
  1130.  
  1131.     /**
  1132.      * Execute a prepared query statement helper method.
  1133.      *
  1134.      * @param mixed $result_class string which specifies which result class to use
  1135.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1136.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1137.      * @access private
  1138.      */
  1139.     function &_execute($result_class = true$result_wrap_class = false)
  1140.     {
  1141.         $this->db->last_query = $this->query;
  1142.         $this->db->debug($this->query'execute');
  1143.         if ($this->db->getOption('disable_query')) {
  1144.             if ($this->is_manip{
  1145.                 $return = 0;
  1146.                 return $return;
  1147.             }
  1148.             $null = null;
  1149.             return $null;
  1150.         }
  1151.  
  1152.         $connection $this->db->getConnection();
  1153.         if (PEAR::isError($connection)) {
  1154.             return $connection;
  1155.         }
  1156.  
  1157.         $result = MDB2_OK;
  1158.         $lobs $quoted_values = array();
  1159.         $i = 0;
  1160.         foreach ($this->values as $parameter => $value{
  1161.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1162.             if ($type == 'clob' || $type == 'blob'{
  1163.                 $lobs[$i]['file'= false;
  1164.                 if (is_resource($value)) {
  1165.                     $fp $value;
  1166.                     $value '';
  1167.                     while (!feof($fp)) {
  1168.                         $value.= fread($fp8192);
  1169.                     }
  1170.                 elseif (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1171.                     $lobs[$i]['file'= true;
  1172.                     if ($match[1== 'file://'{
  1173.                         $value $match[2];
  1174.                     }
  1175.                 }
  1176.                 $lobs[$i]['value'$value;
  1177.                 $lobs[$i]['descriptor'@OCINewDescriptor($connectionOCI_D_LOB);
  1178.                 if (!is_object($lobs[$i]['descriptor'])) {
  1179.                     $result $this->db->raiseError();
  1180.                     break;
  1181.                 }
  1182.                 $lob_type ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1183.                 if (!@OCIBindByName($this->statement':'.$parameter$lobs[$i]['descriptor']-1$lob_type)) {
  1184.                     $result $this->db->raiseError($this->statement);
  1185.                     break;
  1186.                 }
  1187.             else {
  1188.                 $quoted_values[$i$this->db->quote($value$typefalse);
  1189.                 if (PEAR::isError($quoted_values[$i])) {
  1190.                     return $quoted_values[$i];
  1191.                 }
  1192.                 if (!@OCIBindByName($this->statement':'.$parameter$quoted_values[$i])) {
  1193.                     $result $this->db->raiseError($this->statement);
  1194.                     break;
  1195.                 }
  1196.             }
  1197.             ++$i;
  1198.         }
  1199.  
  1200.         if (!PEAR::isError($result)) {
  1201.             $mode (!empty($lobs|| $this->db->in_transaction? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1202.             if (!@OCIExecute($this->statement$mode)) {
  1203.                 $err =$this->db->raiseError($this->statement);
  1204.                 return $err;
  1205.             }
  1206.  
  1207.             if (!empty($lobs)) {
  1208.                 foreach ($lobs as $i => $stream{
  1209.                     if (!is_null($stream['value']&& $stream['value'!== ''{
  1210.                         if ($stream['file']{
  1211.                             $result $lobs[$i]['descriptor']->savefile($stream['value']);
  1212.                         else {
  1213.                             $result $lobs[$i]['descriptor']->save($stream['value']);
  1214.                         }
  1215.                         if (!$result{
  1216.                             $result $this->db->raiseError();
  1217.                             break;
  1218.                         }
  1219.                     }
  1220.                 }
  1221.  
  1222.                 if (!PEAR::isError($result)) {
  1223.                     if (!$this->db->in_transaction{
  1224.                         if (!@OCICommit($connection)) {
  1225.                             $result $this->db->raiseError();
  1226.                         }
  1227.                     else {
  1228.                         ++$this->db->uncommitedqueries;
  1229.                     }
  1230.                 }
  1231.             }
  1232.         }
  1233.  
  1234.         $keys array_keys($lobs);
  1235.         foreach ($keys as $key{
  1236.             $lobs[$key]['descriptor']->free();
  1237.         }
  1238.  
  1239.         if (PEAR::isError($result)) {
  1240.             return $result;
  1241.         }
  1242.  
  1243.         if ($this->is_manip{
  1244.             $affected_rows $this->db->_affectedRows($connection$this->statement);
  1245.             return $affected_rows;
  1246.         }
  1247.  
  1248.         $result =$this->db->_wrapResult($this->statement$this->result_types,
  1249.             $result_class$result_wrap_class$this->row_limit$this->row_offset);
  1250.         return $result;
  1251.     }
  1252.  
  1253.     // }}}
  1254.     // {{{ free()
  1255.  
  1256.     /**
  1257.      * Release resources allocated for the specified prepared query.
  1258.      *
  1259.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1260.      * @access public
  1261.      */
  1262.     function free()
  1263.     {
  1264.         if (!@OCIFreeStatement($this->statement)) {
  1265.             return $this->db->raiseError();
  1266.         }
  1267.         return MDB2_OK;
  1268.     }
  1269. }
  1270. ?>

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