• Main Page
  • Data Structures
  • Files
  • File List

oracleStatement.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require 'oracleResultset.php';
00004 
00005 /**
00006  * Oracle server query statement class
00007  *
00008  * @package objSQL
00009  * @version 2.1.0
00010  * @author MT Jordan <mtjo62@gmail.com>
00011  * @link http://objsql.sourceforge.net
00012  * @copyright 2004-2010 openSource Partners
00013  * @license LGPL
00014  * @revision $Id: oracleStatement.php,v 1.5 2008-01-26 22:31:55-05 mt_jordan Exp $
00015  */
00016 
00017 class oracleStatement
00018 {
00019     /**********************************************
00020      * Internal variables
00021      *********************************************/
00022 
00023     /**
00024      * Autocommit transaction flag - false: transaction initiated
00025      *
00026      * @access private
00027      * @var    bool
00028      */
00029     private $objAutoCommit;
00030 
00031     /**
00032      * Database connection object
00033      *
00034      * @access private
00035      * @var    mixed
00036      */
00037     private $objConnection;
00038 
00039     /**
00040      * Select query limit value
00041      *
00042      * @access private
00043      * @var    int
00044      */
00045     private $objLimit = false;
00046 
00047     /**
00048      * Select query offset value
00049      *
00050      * @access private
00051      * @var    int
00052      */
00053     private $objOffset = false;
00054 
00055     /**
00056      * Prepared statement binding parameters
00057      *
00058      * @access private
00059      * @var    array
00060      */
00061     private $objParameters = array();
00062 
00063     /**
00064      * Query resultset object
00065      *
00066      * @access private
00067      * @var    mixed
00068      */
00069     private $objResult = false;
00070 
00071     /**
00072      * Sequence name
00073      *
00074      * @access private
00075      * @var    str
00076      */
00077     private $objSequence = false;
00078 
00079     /**
00080      * Query string
00081      *
00082      * @access public
00083      * @var    str
00084      */
00085     private $objQuery;
00086 
00087     /**********************************************
00088      * Class methods
00089      *********************************************/
00090 
00091     /**
00092      * Constructor
00093      *
00094      * @access public
00095      * @param  str   $query
00096      * @param  mixed $connection
00097      * @param  bool  $autocommit
00098      */
00099     public function __construct( $query, $connection, $autocommit )
00100     {
00101         $this->objAutoCommit = $autocommit;
00102         $this->objConnection = $connection;
00103         $this->objQuery      = $query;
00104     }
00105 
00106     /**
00107      * Escape query parameters
00108      *
00109      * @access public
00110      * @param  mixed $param
00111      * @param  bool  $no_quotes
00112      */
00113     public function bind( $param, $no_quotes=false )
00114     {
00115         if ( ( is_string( $param ) && !is_numeric( $param ) ) && !$no_quotes && substr_count( strtolower( $param ), 'nextval' ) == 0 )
00116         {
00117             if ( get_magic_quotes_gpc() )
00118                 $param = stripslashes( $param );
00119 
00120             $param = str_replace( "'", "''", $param );
00121             $param = "'" . $param . "'";
00122         }
00123 
00124         $this->objParameters[] = $param;
00125     }
00126 
00127     /**
00128      * Execute prepared statement and return resultset methods
00129      *
00130      * @access public
00131      * @return mixed
00132      */
00133     public function execute()
00134     {
00135         $this->objResult = oci_parse( $this->objConnection->connection(), $this->objPrepareSQL() );
00136 
00137         if ( !$this->objResult )
00138         {
00139             $objError = oci_error( $this->objConnection->connection() );
00140             trigger_error( $objError['message'], E_USER_WARNING );
00141         }
00142 
00143         if ( !$this->objAutoCommit )
00144             $objStmt = oci_execute( $this->objResult, OCI_DEFAULT );
00145         else
00146             $objStmt = oci_execute( $this->objResult );
00147 
00148         if ( !$objStmt )
00149         {
00150             $objError = oci_error( $this->objResult );
00151             trigger_error( $objError['message'], E_USER_WARNING );
00152         }
00153 
00154         return new oracleResultset( $this->objResult, $this->objConnection->connection() );
00155 
00156     }
00157 
00158     /**
00159      * Set limit int value
00160      *
00161      * @access public
00162      * @param  int $limit
00163      */
00164     public function limit( $limit )
00165     {
00166         //make sure $limit is an unsigned int > 0
00167         $this->objLimit = ( is_numeric( $limit ) && $limit > 0 ) ? ( int )$limit : 1;
00168     }
00169 
00170     /**
00171      * Execute non-prepared query and return resultset methods
00172      *
00173      * @access public
00174      * @return mixed
00175      */
00176     public function objExecuteNonPrepared()
00177     {
00178         $this->objResult = oci_parse( $this->objConnection->connection(), $this->objQuery );
00179 
00180         if ( !$this->objResult )
00181         {
00182             $objError = oci_error( $this->objConnection->connection() );
00183             trigger_error( $objError['message'], E_USER_WARNING );
00184         }
00185 
00186         if ( !$this->objAutoCommit )
00187             $objStmt = oci_execute( $this->objResult, OCI_DEFAULT );
00188         else
00189             $objStmt = oci_execute( $this->objResult );
00190 
00191         if ( !$objStmt )
00192         {
00193             $objError = oci_error( $this->objResult );
00194             trigger_error( $objError['message'], E_USER_WARNING );
00195         }
00196 
00197         return new oracleResultset( $this->objResult, $this->objConnection->connection() );
00198     }
00199 
00200     /**
00201      * Prepare query string and bind parameters
00202      *
00203      * @access private
00204      * @return str
00205      */
00206     private function objPrepareSQL()
00207     {
00208         $objSqlParts = explode( '?', $this->objQuery );
00209         $objQuery = $objSqlParts[0];
00210 
00211         for ( $i = 1; $i < count( $objSqlParts ); $i++ )
00212             $objQuery .= $this->objParameters[$i - 1] . $objSqlParts[$i];
00213 
00214         $objQuery = ( ( $this->objLimit && $this->objOffset ) && substr_count( strtolower( $objQuery ), ':offset' ) ) ? str_ireplace( ':limit', ( $this->objLimit + $this->objOffset ) - 1, $objQuery ) : $objQuery;
00215         $objQuery = ( ( $this->objLimit && !$this->objOffset ) || ( ( $this->objLimit && $this->objOffset ) && !substr_count( strtolower( $objQuery ), ':offset' ) ) ) ? str_ireplace( ':limit', $this->objLimit, $objQuery ) : $objQuery;
00216         $objQuery = ( $this->objOffset ) ? str_ireplace( ':offset', $this->objOffset, $objQuery ) : $objQuery;
00217         $objQuery = ( $this->objSequence ) ? str_ireplace( ':seq', $this->objSequence, $objQuery ) : $objQuery;
00218 
00219         return $objQuery;
00220     }
00221 
00222     /**
00223      * Set offset int value
00224      *
00225      * @access public
00226      * @param  int $offset
00227      */
00228     public function offset( $offset )
00229     {
00230         //make sure $offset is an unsigned int > 0
00231         $this->objOffset = ( is_numeric( $offset ) && $offset > 0 ) ? ( int )$offset : 1;
00232     }
00233 
00234     /**
00235      * Set property for named sequence
00236      *
00237      * @access public
00238      * @param  str $sequence
00239      */
00240     public function sequence( $sequence )
00241     {
00242          $this->objSequence = $sequence . '.NEXTVAL';
00243     }
00244 
00245 }
00246 
00247 ?>

Generated on Sat Jul 10 2010 15:14:39 for objSQL 2.1.0 by  doxygen 1.7.1