• Main Page
  • Data Structures
  • Files
  • File List

mssqlStatement.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require 'mssqlResultset.php';
00004 
00005 /**
00006  * SQL 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: mssqlStatement.php,v 1.6 2008-01-27 12:44:45-05 mt_jordan Exp $
00015  */
00016 
00017 class mssqlStatement
00018 {
00019     /**********************************************
00020      * Internal variables
00021      *********************************************/
00022 
00023     /**
00024      * Database connection object
00025      *
00026      * @access private
00027      * @var    mixed
00028      */
00029     private $objConnection;
00030 
00031     /**
00032      * Select query limit value
00033      *
00034      * @access private
00035      * @var    int
00036      */
00037     private $objLimit = false;
00038 
00039     /**
00040      * Select query offset value
00041      *
00042      * @access private
00043      * @var    int
00044      */
00045      private $objOffset = false;
00046 
00047     /**
00048      * Prepared query binding parameters
00049      *
00050      * @access private
00051      * @var    array
00052      */
00053     private $objParameters = array();
00054 
00055     /**
00056      * Query string
00057      *
00058      * @access private
00059      * @var    string
00060      */
00061     private $objQuery;
00062 
00063     /**
00064      * SQL Server version flag
00065      *
00066      * @access private
00067      * @var    bool
00068      */
00069     private $objSqlVersion;
00070 
00071     /**********************************************
00072      * Class methods
00073      *********************************************/
00074 
00075     /**
00076      * Constructor
00077      *
00078      * @access public
00079      * @param  str   $query
00080      * @param  mixed $connection
00081      * @param  bool  $mssql_version
00082      */
00083     public function __construct( $query, $connection, $autocommit=false, $savepoint=false )
00084     {
00085         $this->objConnection = $connection;
00086         $this->objQuery      = $query;
00087     }
00088 
00089     /**
00090      * Escape query parameters
00091      *
00092      * @access public
00093      * @param  mixed $param
00094      * @param  bool  $no_quotes
00095      */
00096     public function bind( $param, $no_quotes=false )
00097     {
00098         //if $param is numeric str, do not add quotes
00099         if ( ( is_string( $param ) && !is_numeric( $param ) ) && !$no_quotes )
00100         {
00101             if ( get_magic_quotes_gpc() )
00102                 $param = stripslashes( $param );
00103 
00104             $param = str_replace( "'", "''", $param );
00105             $param = "'" . $param . "'";
00106         }
00107 
00108         $this->objParameters[] = $param;
00109     }
00110 
00111     /**
00112      * Execute prepared statement and return resultset methods
00113      *
00114      * @access public
00115      * @return mixed
00116      */
00117     public function execute()
00118     {
00119         $objResult = mssql_query( $this->objPrepareSQL() )
00120         or trigger_error( mssql_get_last_message(), E_USER_WARNING );
00121 
00122         return new mssqlResultset( $objResult, $this->objConnection->connection() );
00123     }
00124 
00125     /**
00126      * Sets limit int value
00127      *
00128      * @access public
00129      * @param  int  $limit
00130      * @param  bool $sql_version
00131      */
00132     public function limit( $limit, $sql_version=false )
00133     {
00134         //make sure $limit is an unsigned int > 0
00135         $this->objLimit = ( is_numeric( $limit ) && $limit > 0 ) ? ( int )$limit : 1;
00136         $this->objSqlVersion = $sql_version;
00137     }
00138 
00139     /**
00140      * Execute non-prepared query and return resultset methods
00141      *
00142      * @access public
00143      * @return mixed
00144      */
00145     public function objExecuteNonPrepared()
00146     {
00147         $objResult = mssql_query( $this->objQuery )
00148         or trigger_error( mssql_get_last_message(), E_USER_WARNING );
00149 
00150         return new mssqlResultset( $objResult, $this->objConnection->connection() );
00151     }
00152 
00153     /**
00154      * Prepare query string and bind parameters
00155      *
00156      * @access private
00157      * @return string
00158      */
00159     private function objPrepareSQL()
00160     {
00161         $objSqlParts = explode( '?', $this->objQuery );
00162         $objQuery = $objSqlParts[0];
00163 
00164         for ( $i = 1; $i < count( $objSqlParts ); $i++ )
00165             $objQuery .= $this->objParameters[$i - 1] . $objSqlParts[$i];
00166 
00167         if ( !$this->objSqlVersion )
00168         {
00169             $objQuery = ( ( $this->objLimit && $this->objOffset ) && substr_count( strtolower( $objQuery ), ':offset' ) ) ? str_ireplace( ':limit', ( $this->objLimit + $this->objOffset ), $objQuery ) : $objQuery;
00170             $objQuery = ( ( $this->objLimit && !$this->objOffset ) || ( ( $this->objLimit && $this->objOffset ) && !substr_count( strtolower( $objQuery ), ':offset' ) ) ) ? str_ireplace( ':limit', $this->objLimit, $objQuery ) : $objQuery;
00171         }
00172         else
00173         {
00174             $objQuery = ( $this->objLimit ) ? str_ireplace( ':limit', $this->objLimit, $objQuery ) : $objQuery;
00175         }
00176 
00177         $objQuery = ( $this->objOffset ) ? str_ireplace( ':offset', $this->objOffset, $objQuery ) : $objQuery;
00178 
00179         return $objQuery;
00180     }
00181 
00182     /**
00183      * Set offset int value
00184      *
00185      * @access public
00186      * @param  int $offset
00187      */
00188     public function offset( $offset )
00189     {
00190         //make sure $offset is an unsigned int > 0
00191         $this->objOffset = ( is_numeric( $offset ) && $offset > 0 ) ? ( int )$offset : 1;
00192     }
00193 
00194     /**
00195      * Dummy method to prevent class error for dbs that do not support sequences - used with prepared statement
00196      *
00197      * @access public
00198      */
00199     public function sequence() {}
00200 }
00201 
00202 ?>

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