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

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