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.117 2006/01/22 18:30:16 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'= true;
  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->offset;
  395.         $limit $this->limit;
  396.         $this->offset $this->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->offset;
  433.         $limit $this->limit;
  434.         $this->offset $this->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$is_manip$limit$offset)
  462.     {
  463.         if (preg_match('/^\s*SELECT/i'$query)) {
  464.             if (!preg_match('/\sFROM\s/i'$query)) {
  465.                 $query.= " FROM dual";
  466.             }
  467.             if ($limit > 0{
  468.                 // taken from http://svn.ez.no/svn/ezcomponents/packages/Database
  469.                 $min $offset + 1;
  470.                 $max $offset $limit;
  471.                 $query = "SELECT * FROM (SELECT a.*, ROWNUM rn FROM ($query) a WHERE ROWNUM <= $max) WHERE rn >= $min";
  472.             }
  473.         }
  474.         return $query;
  475.     }
  476.  
  477.     // }}}
  478.     // {{{ _doQuery()
  479.  
  480.     /**
  481.      * Execute a query
  482.      * @param string $query  query
  483.      * @param boolean $is_manip  if the query is a manipulation query
  484.      * @param resource $connection 
  485.      * @param string $database_name 
  486.      * @return result or error object
  487.      * @access protected
  488.      */
  489.     function _doQuery($query$is_manip = false$connection = null$database_name = null)
  490.     {
  491.         $this->last_query $query;
  492.         $this->debug($query'query');
  493.         if ($this->getOption('disable_query')) {
  494.             if ($is_manip{
  495.                 return 0;
  496.             }
  497.             return null;
  498.         }
  499.  
  500.         if (is_null($connection)) {
  501.             $connection $this->getConnection();
  502.             if (PEAR::isError($connection)) {
  503.                 return $connection;
  504.             }
  505.         }
  506.  
  507.         $result @OCIParse($connection$query);
  508.         if (!$result{
  509.             return $this->raiseError(MDB2_ERRORnullnull,
  510.                 'Could not create statement');
  511.         }
  512.  
  513.         $mode $this->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  514.         if (!@OCIExecute($result$mode)) {
  515.             return $this->raiseError($result);
  516.         }
  517.  
  518.         if (is_numeric($this->options['result_buffering'])) {
  519.             @ocisetprefetch($result$this->options['result_buffering']);
  520.         }
  521.         return $result;
  522.     }
  523.  
  524.     // }}}
  525.     // {{{ _affectedRows()
  526.  
  527.     /**
  528.      * returns the number of rows affected
  529.      *
  530.      * @param resource $result 
  531.      * @param resource $connection 
  532.      * @return mixed MDB2 Error Object or the number of rows affected
  533.      * @access private
  534.      */
  535.     function _affectedRows($connection$result = null)
  536.     {
  537.         if (is_null($connection)) {
  538.             $connection $this->getConnection();
  539.             if (PEAR::isError($connection)) {
  540.                 return $connection;
  541.             }
  542.         }
  543.         return @OCIRowCount($result);
  544.     }
  545.  
  546.     // }}}
  547.     // {{{ getServerVersion()
  548.  
  549.     /**
  550.      * return version information about the server
  551.      *
  552.      * @param string     $native  determines if the raw version string should be returned
  553.      * @return mixed array/string with version information or MDB2 error object
  554.      * @access public
  555.      */
  556.     function getServerVersion($native = false)
  557.     {
  558.         $connection $this->getConnection();
  559.         if (PEAR::isError($connection)) {
  560.             return $connection;
  561.         }
  562.         $server_info = ociserverversion($connection);
  563.         if (!$native{
  564.             if (!preg_match('/ (\d+)\.(\d+)\.(\d+)\.([\d\.]+) /'$server_info$tmp)) {
  565.                 return $this->raiseError(MDB2_ERRORnullnull,
  566.                     'Could not parse version information:'.$server_info);
  567.             }
  568.             $server_info = array(
  569.                 'major' => $tmp[1],
  570.                 'minor' => $tmp[2],
  571.                 'patch' => $tmp[3],
  572.                 'extra' => $tmp[4],
  573.                 'native' => $server_info,
  574.             );
  575.         }
  576.         return $server_info;
  577.     }
  578.  
  579.     // }}}
  580.     // {{{ prepare()
  581.  
  582.     /**
  583.      * Prepares a query for multiple execution with execute().
  584.      * With some database backends, this is emulated.
  585.      * prepare() requires a generic query as string like
  586.      * 'INSERT INTO numbers VALUES(?,?)' or
  587.      * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  588.      * The ? and :[a-zA-Z] and  are placeholders which can be set using
  589.      * bindParam() and the query can be send off using the execute() method.
  590.      *
  591.      * @param string $query the query to prepare
  592.      * @param mixed   $types  array that contains the types of the placeholders
  593.      * @param mixed   $result_types  array that contains the types of the columns in
  594.      *                         the result set, if set to MDB2_PREPARE_MANIP the
  595.                               query is handled as a manipulation query
  596.      * @param mixed   $lobs   key (field) value (parameter) pair for all lob placeholders
  597.      * @return mixed resource handle for the prepared query on success, a MDB2
  598.      *         error on failure
  599.      * @access public
  600.      * @see bindParam, execute
  601.      */
  602.     function &prepare($query$types = null$result_types = null$lobs = array())
  603.     {
  604.         $is_manip ($result_types === MDB2_PREPARE_MANIP);
  605.         $offset $this->offset;
  606.         $limit $this->limit;
  607.         $this->offset $this->limit = 0;
  608.         $this->debug($query'prepare');
  609.         $query $this->_modifyQuery($query$is_manip$limit$offset);
  610.         $placeholder_type_guess $placeholder_type = null;
  611.         $question '?';
  612.         $colon ':';
  613.         $positions = array();
  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 (is_null($placeholder_type)) {
  656.                     $placeholder_type $query[$p_position];
  657.                     $question $colon $placeholder_type;
  658.                     if (is_array($types&& !empty($types)) {
  659.                         if ($placeholder_type == ':'{
  660.                             if (is_int(key($types))) {
  661.                                 $types_tmp $types;
  662.                                 $types = array();
  663.                                 $count = -1;
  664.                             }
  665.                         else {
  666.                             $types array_values($types);
  667.                         }
  668.                     }
  669.                 }
  670.                 if ($placeholder_type == ':'{
  671.                     $parameter preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si''\\1'$query);
  672.                     if ($parameter === ''{
  673.                         $err =$this->raiseError(MDB2_ERROR_SYNTAXnullnull,
  674.                             'prepare: named parameter with an empty name');
  675.                         return $err;
  676.                     }
  677.                     $positions[$parameter$p_position;
  678.                     // use parameter name in type array
  679.                     if (isset($count&& isset($types_tmp[++$count])) {
  680.                         $types[$parameter$types_tmp[$count];
  681.                     }
  682.                     $length strlen($parameter+ 1;
  683.                 else {
  684.                     ++$parameter;
  685.                     $length strlen($parameter);
  686.                 }
  687.                 if (isset($types[$parameter])
  688.                     && ($types[$parameter== 'clob' || $types[$parameter== 'blob')
  689.                 {
  690.                     if (!isset($lobs[$parameter])) {
  691.                         $lobs[$parameter$parameter;
  692.                     }
  693.                     $value $this->quote(true$types[$parameter]);
  694.                     $query substr_replace($query$value$p_position$length);
  695.                     $position $p_position strlen($value- 1;
  696.                 elseif ($placeholder_type == '?'{
  697.                     $positions[$p_position;
  698.                     $query substr_replace($query':'.$parameter$p_position1);
  699.                     $position $p_position $length;
  700.                 else {
  701.                     $position $p_position + 1;
  702.                 }
  703.             else {
  704.                 $position $p_position;
  705.             }
  706.         }
  707.         if (is_array($lobs)) {
  708.             $columns $variables '';
  709.             foreach ($lobs as $parameter => $field{
  710.                 $columns.= ($columns ', ' ' RETURNING ').$field;
  711.                 $variables.= ($variables ', ' ' INTO ').':'.$parameter;
  712.             }
  713.             $query.= $columns.$variables;
  714.         }
  715.         $connection $this->getConnection();
  716.         if (PEAR::isError($connection)) {
  717.             return $connection;
  718.         }
  719.         $statement = OCIParse($connection$query);
  720.         if (!$statement{
  721.             $err =$this->raiseError(MDB2_ERRORnullnull,
  722.                 'Could not create statement');
  723.             return $err;
  724.         }
  725.  
  726.         $class_name 'MDB2_Statement_'.$this->phptype;
  727.         $obj =new $class_name($this$statement$positions$query$types$result_types$is_manip$limit$offset);
  728.         return $obj;
  729.     }
  730.  
  731.     // }}}
  732.     // {{{ nextID()
  733.  
  734.     /**
  735.      * returns the next free id of a sequence
  736.      *
  737.      * @param string $seq_name name of the sequence
  738.      * @param boolean $ondemand when true the seqence is
  739.      *                            automatic created, if it
  740.      *                            not exists
  741.      * @return mixed MDB2 Error Object or id
  742.      * @access public
  743.      */
  744.     function nextID($seq_name$ondemand = true)
  745.     {
  746.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  747.         $query = "SELECT $sequence_name.nextval FROM DUAL";
  748.         $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  749.         $result $this->queryOne($query'integer');
  750.         $this->popExpect();
  751.         if (PEAR::isError($result)) {
  752.             if ($ondemand && $result->getCode(== MDB2_ERROR_NOSUCHTABLE{
  753.                 $this->loadModule('Manager'nulltrue);
  754.                 $result $this->manager->createSequence($seq_name1);
  755.                 if (PEAR::isError($result)) {
  756.                     return $result;
  757.                 }
  758.                 return $this->nextId($seq_namefalse);
  759.             }
  760.         }
  761.         return $result;
  762.     }
  763.  
  764.     // }}}
  765.     // {{{ currId()
  766.  
  767.     /**
  768.      * returns the current id of a sequence
  769.      *
  770.      * @param string $seq_name name of the sequence
  771.      * @return mixed MDB2_Error or id
  772.      * @access public
  773.      */
  774.     function currId($seq_name)
  775.     {
  776.         $sequence_name $this->quoteIdentifier($this->getSequenceName($seq_name)true);
  777.         return $this->queryOne("SELECT $sequence_name.currval FROM DUAL");
  778.     }
  779. }
  780.  
  781. class MDB2_Result_oci8 extends MDB2_Result_Common
  782. {
  783.     // }}}
  784.     // {{{ fetchRow()
  785.  
  786.     /**
  787.      * Fetch a row and insert the data into an existing array.
  788.      *
  789.      * @param int       $fetchmode  how the array data should be indexed
  790.      * @param int    $rownum    number of the row where the data can be found
  791.      * @return int data array on success, a MDB2 error on failure
  792.      * @access public
  793.      */
  794.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  795.     {
  796.         if (!is_null($rownum)) {
  797.             $seek $this->seek($rownum);
  798.             if (PEAR::isError($seek)) {
  799.                 return $seek;
  800.             }
  801.         }
  802.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  803.             $fetchmode $this->db->fetchmode;
  804.         }
  805.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  806.             @OCIFetchInto($this->result$rowOCI_ASSOC+OCI_RETURN_NULLS);
  807.             if (is_array($row)
  808.                 && $this->db->options['portability'MDB2_PORTABILITY_FIX_CASE
  809.             {
  810.                 $row array_change_key_case($row$this->db->options['field_case']);
  811.             }
  812.         else {
  813.             @OCIFetchInto($this->result$rowOCI_RETURN_NULLS);
  814.         }
  815.         if (!$row{
  816.             if (is_null($this->result)) {
  817.                 $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  818.                     'fetchRow: resultset has already been freed');
  819.                 return $err;
  820.             }
  821.             $null = null;
  822.             return $null;
  823.  
  824.         }
  825.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  826.             $this->db->_fixResultArrayValues($rowMDB2_PORTABILITY_RTRIM);
  827.         }
  828.         if (!empty($this->values)) {
  829.             $this->_assignBindColumns($row);
  830.         }
  831.         if (!empty($this->types)) {
  832.             $row $this->db->datatype->convertResultRow($this->types$row);
  833.         }
  834.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  835.             $object_class $this->db->options['fetch_class'];
  836.             if ($object_class == 'stdClass'{
  837.                 $row = (object) $row;
  838.             else {
  839.                 $row &new $object_class($row);
  840.             }
  841.         }
  842.         ++$this->rownum;
  843.         return $row;
  844.     }
  845.  
  846.     // }}}
  847.     // {{{ _getColumnNames()
  848.  
  849.     /**
  850.      * Retrieve the names of columns returned by the DBMS in a query result.
  851.      *
  852.      * @return mixed associative array variable
  853.      *       that holds the names of columns. The indexes of the array are
  854.      *       the column names mapped to lower case and the values are the
  855.      *       respective numbers of the columns starting from 0. Some DBMS may
  856.      *       not return any columns when the result set does not contain any
  857.      *       rows.
  858.      * @access private
  859.      */
  860.     function _getColumnNames()
  861.     {
  862.         $columns = array();
  863.         $numcols $this->numCols();
  864.         if (PEAR::isError($numcols)) {
  865.             return $numcols;
  866.         }
  867.         for ($column = 0; $column $numcols$column++{
  868.             $column_name @OCIColumnName($this->result$column + 1);
  869.             $columns[$column_name$column;
  870.         }
  871.         if ($this->db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  872.             $columns array_change_key_case($columns$this->db->options['field_case']);
  873.         }
  874.         return $columns;
  875.     }
  876.  
  877.     // }}}
  878.     // {{{ numCols()
  879.  
  880.     /**
  881.      * Count the number of columns returned by the DBMS in a query result.
  882.      *
  883.      * @return mixed integer value with the number of columns, a MDB2 error
  884.      *       on failure
  885.      * @access public
  886.      */
  887.     function numCols()
  888.     {
  889.         $cols @OCINumCols($this->result);
  890.         if (is_null($cols)) {
  891.             if (is_null($this->result)) {
  892.                 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  893.                     'numCols: resultset has already been freed');
  894.             }
  895.             return $this->db->raiseError();
  896.         }
  897.         return $cols;
  898.     }
  899.  
  900.     // }}}
  901.     // {{{ free()
  902.  
  903.     /**
  904.      * Free the internal resources associated with $result.
  905.      *
  906.      * @return boolean true on success, false if $result is invalid
  907.      * @access public
  908.      */
  909.     function free()
  910.     {
  911.         $free @OCIFreeCursor($this->result);
  912.         if (!$free{
  913.             if (is_null($this->result)) {
  914.                 return MDB2_OK;
  915.             }
  916.             return $this->db->raiseError();
  917.         }
  918.         $this->result = null;
  919.         return MDB2_OK;
  920.     }
  921. }
  922.  
  923. {
  924.     var $buffer;
  925.     var $buffer_rownum = - 1;
  926.  
  927.     // {{{ _fillBuffer()
  928.  
  929.     /**
  930.      * Fill the row buffer
  931.      *
  932.      * @param int $rownum   row number upto which the buffer should be filled
  933.                             if the row number is null all rows are ready into the buffer
  934.      * @return boolean true on success, false on failure
  935.      * @access protected
  936.      */
  937.     function _fillBuffer($rownum = null)
  938.     {
  939.         if (isset($this->buffer&& is_array($this->buffer)) {
  940.             if (is_null($rownum)) {
  941.                 if (!end($this->buffer)) {
  942.                     return false;
  943.                 }
  944.             elseif (isset($this->buffer[$rownum])) {
  945.                 return (bool)$this->buffer[$rownum];
  946.             }
  947.         }
  948.  
  949.         $row = true;
  950.         while ((is_null($rownum|| $this->buffer_rownum < $rownum)
  951.             && ($row @OCIFetchInto($this->result$bufferOCI_RETURN_NULLS))
  952.         {
  953.             ++$this->buffer_rownum;
  954.             $this->buffer[$this->buffer_rownum$buffer;
  955.         }
  956.  
  957.         if (!$row{
  958.             ++$this->buffer_rownum;
  959.             $this->buffer[$this->buffer_rownum= false;
  960.             return false;
  961.         }
  962.         return true;
  963.     }
  964.  
  965.     // }}}
  966.     // {{{ fetchRow()
  967.  
  968.     /**
  969.      * Fetch a row and insert the data into an existing array.
  970.      *
  971.      * @param int       $fetchmode  how the array data should be indexed
  972.      * @param int    $rownum    number of the row where the data can be found
  973.      * @return int data array on success, a MDB2 error on failure
  974.      * @access public
  975.      */
  976.     function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT$rownum = null)
  977.     {
  978.         if (is_null($this->result)) {
  979.             $err =$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  980.                 'fetchRow: resultset has already been freed');
  981.             return $err;
  982.         }
  983.         if (!is_null($rownum)) {
  984.             $seek $this->seek($rownum);
  985.             if (PEAR::isError($seek)) {
  986.                 return $seek;
  987.             }
  988.         }
  989.         $target_rownum $this->rownum + 1;
  990.         if ($fetchmode == MDB2_FETCHMODE_DEFAULT{
  991.             $fetchmode $this->db->fetchmode;
  992.         }
  993.         if (!$this->_fillBuffer($target_rownum)) {
  994.             $null = null;
  995.             return $null;
  996.         }
  997.         $row $this->buffer[$target_rownum];
  998.         if ($fetchmode MDB2_FETCHMODE_ASSOC{
  999.             $column_names $this->getColumnNames();
  1000.             foreach ($column_names as $name => $i{
  1001.                 $column_names[$name$row[$i];
  1002.             }
  1003.             $row $column_names;
  1004.         }
  1005.         if (!empty($this->values)) {
  1006.             $this->_assignBindColumns($row);
  1007.         }
  1008.         if (!empty($this->types)) {
  1009.             $row $this->db->datatype->convertResultRow($this->types$row);
  1010.         }
  1011.         if ($this->db->options['portability'MDB2_PORTABILITY_RTRIM{
  1012.             $this->db->_fixResultArrayValues($rowMDB2_PORTABILITY_RTRIM);
  1013.         }
  1014.         if ($fetchmode === MDB2_FETCHMODE_OBJECT{
  1015.             $object_class $this->db->options['fetch_class'];
  1016.             if ($object_class == 'stdClass'{
  1017.                 $row = (object) $row;
  1018.             else {
  1019.                 $row &new $object_class($row);
  1020.             }
  1021.         }
  1022.         ++$this->rownum;
  1023.         return $row;
  1024.     }
  1025.  
  1026.     // }}}
  1027.     // {{{ seek()
  1028.  
  1029.     /**
  1030.      * seek to a specific row in a result set
  1031.      *
  1032.      * @param int    $rownum    number of the row where the data can be found
  1033.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1034.      * @access public
  1035.      */
  1036.     function seek($rownum = 0)
  1037.     {
  1038.         if (is_null($this->result)) {
  1039.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1040.                 'seek: resultset has already been freed');
  1041.         }
  1042.         $this->rownum $rownum - 1;
  1043.         return MDB2_OK;
  1044.     }
  1045.  
  1046.     // }}}
  1047.     // {{{ valid()
  1048.  
  1049.     /**
  1050.      * check if the end of the result set has been reached
  1051.      *
  1052.      * @return mixed true or false on sucess, a MDB2 error on failure
  1053.      * @access public
  1054.      */
  1055.     function valid()
  1056.     {
  1057.         if (is_null($this->result)) {
  1058.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1059.                 'valid: resultset has already been freed');
  1060.         }
  1061.         if ($this->_fillBuffer($this->rownum + 1)) {
  1062.             return true;
  1063.         }
  1064.         return false;
  1065.     }
  1066.  
  1067.     // }}}
  1068.     // {{{ numRows()
  1069.  
  1070.     /**
  1071.      * returns the number of rows in a result object
  1072.      *
  1073.      * @return mixed MDB2 Error Object or the number of rows
  1074.      * @access public
  1075.      */
  1076.     function numRows()
  1077.     {
  1078.         if (is_null($this->result)) {
  1079.             return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  1080.                 'seek: resultset has already been freed');
  1081.         }
  1082.         $this->_fillBuffer();
  1083.         return $this->buffer_rownum;
  1084.     }
  1085.  
  1086.     // }}}
  1087.     // {{{ free()
  1088.  
  1089.     /**
  1090.      * Free the internal resources associated with $result.
  1091.      *
  1092.      * @return boolean true on success, false if $result is invalid
  1093.      * @access public
  1094.      */
  1095.     function free()
  1096.     {
  1097.         $this->buffer = null;
  1098.         $this->buffer_rownum = null;
  1099.         $free = parent::free();
  1100.     }
  1101. }
  1102.  
  1103. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1104. {
  1105.     // }}}
  1106.     // {{{ _execute()
  1107.  
  1108.     /**
  1109.      * Execute a prepared query statement helper method.
  1110.      *
  1111.      * @param mixed $result_class string which specifies which result class to use
  1112.      * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1113.      * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1114.      * @access private
  1115.      */
  1116.     function &_execute($result_class = true$result_wrap_class = false)
  1117.     {
  1118.         $this->db->last_query = $this->query;
  1119.         $this->db->debug($this->query'execute');
  1120.         if ($this->db->getOption('disable_query')) {
  1121.             if ($this->is_manip{
  1122.                 $return = 0;
  1123.                 return $return;
  1124.             }
  1125.             $null = null;
  1126.             return $null;
  1127.         }
  1128.  
  1129.         $connection $this->db->getConnection();
  1130.         if (PEAR::isError($connection)) {
  1131.             return $connection;
  1132.         }
  1133.  
  1134.         $result = MDB2_OK;
  1135.         $lobs $quoted_values = array();
  1136.         $i = 0;
  1137.         foreach ($this->positions as $parameter => $current_position{
  1138.             if (!array_key_exists($parameter$this->values)) {
  1139.                 return $this->db->raiseError();
  1140.             }
  1141.             $value $this->values[$parameter];
  1142.             $type array_key_exists($parameter$this->types$this->types[$parameter: null;
  1143.             if ($type == 'clob' || $type == 'blob'{
  1144.                 $lobs[$i]['file'= false;
  1145.                 if (is_resource($value)) {
  1146.                     $fp $value;
  1147.                     $value '';
  1148.                     while (!feof($fp)) {
  1149.                         $value.= fread($fp8192);
  1150.                     }
  1151.                 elseif (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1152.                     $lobs[$i]['file'= true;
  1153.                     if ($match[1== 'file://'{
  1154.                         $value $match[2];
  1155.                     }
  1156.                 }
  1157.                 $lobs[$i]['value'$value;
  1158.                 $lobs[$i]['descriptor'@OCINewDescriptor($connectionOCI_D_LOB);
  1159.                 if (!is_object($lobs[$i]['descriptor'])) {
  1160.                     $result $this->db->raiseError();
  1161.                     break;
  1162.                 }
  1163.                 $lob_type ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1164.                 if (!@OCIBindByName($this->statement':'.$parameter$lobs[$i]['descriptor']-1$lob_type)) {
  1165.                     $result $this->db->raiseError($this->statement);
  1166.                     break;
  1167.                 }
  1168.             else {
  1169.                 $quoted_values[$i$this->db->quote($value$typefalse);
  1170.                 if (PEAR::isError($quoted_values[$i])) {
  1171.                     return $quoted_values[$i];
  1172.                 }
  1173.                 if (!@OCIBindByName($this->statement':'.$parameter$quoted_values[$i])) {
  1174.                     $result $this->db->raiseError($this->statement);
  1175.                     break;
  1176.                 }
  1177.             }
  1178.             ++$i;
  1179.         }
  1180.  
  1181.         if (!PEAR::isError($result)) {
  1182.             $mode (!empty($lobs|| $this->db->in_transaction? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1183.             if (!@OCIExecute($this->statement$mode)) {
  1184.                 $err =$this->db->raiseError($this->statement);
  1185.                 return $err;
  1186.             }
  1187.  
  1188.             if (!empty($lobs)) {
  1189.                 foreach ($lobs as $i => $stream{
  1190.                     if (!is_null($stream['value']&& $stream['value'!== ''{
  1191.                         if ($stream['file']{
  1192.                             $result $lobs[$i]['descriptor']->savefile($stream['value']);
  1193.                         else {
  1194.                             $result $lobs[$i]['descriptor']->save($stream['value']);
  1195.                         }
  1196.                         if (!$result{
  1197.                             $result $this->db->raiseError();
  1198.                             break;
  1199.                         }
  1200.                     }
  1201.                 }
  1202.  
  1203.                 if (!PEAR::isError($result)) {
  1204.                     if (!$this->db->in_transaction{
  1205.                         if (!@OCICommit($connection)) {
  1206.                             $result $this->db->raiseError();
  1207.                         }
  1208.                     else {
  1209.                         ++$this->db->uncommitedqueries;
  1210.                     }
  1211.                 }
  1212.             }
  1213.         }
  1214.  
  1215.         $keys array_keys($lobs);
  1216.         foreach ($keys as $key{
  1217.             $lobs[$key]['descriptor']->free();
  1218.         }
  1219.  
  1220.         if (PEAR::isError($result)) {
  1221.             return $result;
  1222.         }
  1223.  
  1224.         if ($this->is_manip{
  1225.             $affected_rows $this->db->_affectedRows($connection$this->statement);
  1226.             return $affected_rows;
  1227.         }
  1228.  
  1229.         $result =$this->db->_wrapResult($this->statement$this->result_types,
  1230.             $result_class$result_wrap_class);
  1231.         return $result;
  1232.     }
  1233.  
  1234.     // }}}
  1235.     // {{{ free()
  1236.  
  1237.     /**
  1238.      * Release resources allocated for the specified prepared query.
  1239.      *
  1240.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1241.      * @access public
  1242.      */
  1243.     function free()
  1244.     {
  1245.         if (!@OCIFreeStatement($this->statement)) {
  1246.             return $this->db->raiseError();
  1247.         }
  1248.         return MDB2_OK;
  1249.     }
  1250. }
  1251. ?>

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