• Main Page
  • Data Structures
  • Files
  • File List

firebirdStatement.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require 'firebirdResultset.php';
00004 
00005 /**
00006  * Firebird/Interbase 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: firebirdStatement.php,v 1.5 2008-01-19 23:58:17-05 mt_jordan Exp $
00015  */
00016 
00017 class firebirdStatement
00018 {
00019     /**********************************************
00020      * Internal variables
00021      *********************************************/
00022 
00023     /**
00024      * Transaction resource
00025      *
00026      * @access private
00027      * @var    mixed
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      * Transaction savepoint
00065      *
00066      * @access private
00067      * @var    mixed
00068      */
00069     private $objSavepoint;
00070 
00071     /**
00072      * Sequence name
00073      *
00074      * @access private
00075      * @var    mixed
00076      */
00077     private $objSequence = false;
00078 
00079     /**
00080      * Query string
00081      *
00082      * @access private
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      * @param  str   $savepoint
00099      */
00100     public function __construct( $query, $connection, $autocommit, $savepoint )
00101     {
00102         $this->objAutoCommit = $autocommit;
00103         $this->objConnection = $connection;
00104         $this->objSavepoint  = $savepoint;
00105         $this->objQuery      = $query;
00106     }
00107 
00108     /**
00109      * Set and escape bound query parameters
00110      *
00111      * @access public
00112      * @param  mixed $param
00113      * @param  bool  $no_quotes
00114      */
00115     function bind( $param, $no_quotes=false )
00116     {
00117         //if $param is numeric str, do not add quotes
00118         if ( ( is_string( $param ) && !is_numeric( $param ) ) && !$no_quotes )
00119         {
00120             if ( get_magic_quotes_gpc() )
00121                 $param = stripslashes( $param );
00122 
00123             $param = str_replace( "'", "''", $param );
00124             $param = "'" . $param . "'";
00125         }
00126 
00127         $this->objParameters[] = $param;
00128     }
00129 
00130     /**
00131      * Execute prepared query and return resultset object
00132      *
00133      * @access public
00134      * @return mixed
00135      */
00136     public function execute()
00137     {
00138         if ( is_resource( $this->objAutoCommit ) && stripos( $this->objPrepareSQL(), 'select' ) === false )
00139         {
00140             if ( $this->objSavepoint )
00141                 $objResult = ibase_query( $this->objSavepoint, $this->objPrepareSQL() )
00142                 or trigger_error( ibase_errmsg(), E_USER_WARNING );
00143             else
00144                 $objResult = ibase_query( $this->objAutoCommit, $this->objPrepareSQL() )
00145                 or trigger_error( ibase_errmsg(), E_USER_WARNING );
00146         }
00147         else
00148         {
00149             $objResult = ibase_query( $this->objConnection->connection(), $this->objPrepareSQL() )
00150             or trigger_error( ibase_errmsg(), E_USER_WARNING );
00151         }
00152 
00153         return new firebirdResultset( $objResult, $this->objConnection->connection() );
00154     }
00155 
00156     /**
00157      * Set limit int value
00158      *
00159      * @access public
00160      * @param  int $limit
00161      */
00162     public function limit( $limit )
00163     {
00164         //make sure $limit is an unsigned int > 0
00165         $this->objLimit = ( is_numeric( $limit ) && $limit > 0 ) ? ( int )$limit : 1;
00166     }
00167 
00168     /**
00169      * Execute non-prepared query and return resultset object
00170      *
00171      * @access public
00172      * @return mixed
00173      */
00174     public function objExecuteNonPrepared()
00175     {
00176         if ( is_resource( $this->objAutoCommit ) && stripos( $this->objQuery, 'select' ) === false )
00177         {
00178             if ( $this->objSavepoint )
00179                 $objResult = ibase_query( $this->objSavepoint, $this->objQuery )
00180                 or trigger_error( ibase_errmsg(), E_USER_WARNING );
00181             else
00182                 $objResult = ibase_query( $this->objAutoCommit, $this->objQuery )
00183                 or trigger_error( ibase_errmsg(), E_USER_WARNING );
00184         }
00185         else
00186         {
00187             $objResult = ibase_query( $this->objConnection->connection(), $this->objQuery )
00188             or trigger_error( ibase_errmsg(), E_USER_WARNING );
00189         }
00190 
00191         return new firebirdResultset( $objResult, $this->objConnection->connection() );
00192     }
00193 
00194     /**
00195      * Prepare query string and bind parameters
00196      *
00197      * @access private
00198      * @return str
00199      */
00200     private function objPrepareSQL()
00201     {
00202         $objSqlParts = explode( '?', $this->objQuery );
00203         $objQuery    = $objSqlParts[0];
00204 
00205         for ( $i = 1; $i < count( $objSqlParts ); $i++ )
00206             $objQuery .= $this->objParameters[$i - 1] . $objSqlParts[$i];
00207 
00208         $objQuery = ( $this->objLimit ) ? str_ireplace( ':limit', $this->objLimit, $objQuery ) : $objQuery;
00209         $objQuery = ( $this->objOffset ) ? str_ireplace( ':offset', $this->objOffset, $objQuery ) : $objQuery;
00210         $objQuery = ( $this->objSequence ) ? str_ireplace( ':seq', $this->objSequence, $objQuery ) : $objQuery;
00211 
00212         return $objQuery;
00213     }
00214 
00215     /**
00216      * Set offset int value
00217      *
00218      * @access public
00219      * @param  int $offset
00220      */
00221     public function offset( $offset )
00222     {
00223         //make sure $offset is an unsigned int > 0
00224         $this->objOffset = ( is_numeric( $offset ) && $offset > 0 ) ? ( int )$offset : 1;
00225     }
00226 
00227     /**
00228      * Get value for named sequence
00229      *
00230      * @access public
00231      * @param  str $sequence
00232      */
00233     public function sequence( $sequence )
00234     {
00235         $this->objSequence = ibase_gen_id( $sequence, 1, $this->objConnection->connection() );
00236     }
00237 }
00238 
00239 ?>

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