• Main Page
  • Data Structures
  • Files
  • File List

sqliteStatement.php

Go to the documentation of this file.
00001 <?php
00002 
00003 require 'sqliteResultset.php';
00004 
00005 /**
00006  * SQLite 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: sqliteStatement.php,v 1.6 2008-01-19 10:29:07-05 mt_jordan Exp $
00015  */
00016 
00017 class sqliteStatement
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      * elect query offset value
00041      *
00042      * @access private
00043      * @var    int
00044      */
00045     private $objOffset = false;
00046 
00047     /**
00048      * Prepared statement 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    str
00060      */
00061     private $objQuery;
00062 
00063     /**********************************************
00064      * Class methods
00065      *********************************************/
00066 
00067     /**
00068      * Constructor
00069      *
00070      * @access public
00071      * @param  str   $query
00072      * @param  mixed $connection
00073      */
00074     public function __construct( $query, $connection )
00075     {
00076         $this->objConnection = $connection;
00077         $this->objQuery      = $query;
00078     }
00079 
00080     /**
00081      * Escape query parameters
00082      *
00083      * @access public
00084      * @param  mixed $param
00085      * @param  bool  $no_quotes
00086      */
00087     function bind( $param, $no_quotes=false )
00088     {
00089         //if $param is numeric str, do not add quotes
00090         if ( ( is_string( $param ) && !is_numeric( $param ) ) && !$no_quotes )
00091         {
00092             if ( get_magic_quotes_gpc() )
00093             {
00094                 $param = stripslashes( $param );
00095                 $param = "'" . sqlite_escape_string( $param ) . "'";
00096             }
00097             else
00098             {
00099                 $param = "'" . sqlite_escape_string( $param ) . "'";
00100             }
00101         }
00102 
00103         $this->objParameters[] = $param;
00104     }
00105 
00106     /**
00107      * Execute prepared query and return resultset methods
00108      *
00109      * @access public
00110      * @return mixed
00111      */
00112     public function execute()
00113     {
00114         $objResult = sqlite_query( $this->objPrepareSQL(), $this->objConnection->connection() )
00115         or trigger_error( sqlite_error_string( sqlite_last_error( $this->objConnection->connection() ) ), E_USER_WARNING );
00116 
00117         return new sqliteResultset( $objResult, $this->objConnection->connection() );
00118     }
00119 
00120     /**
00121      * Set limit int value
00122      *
00123      * @access public
00124      * @param  int $limit
00125      */
00126     public function limit( $limit )
00127     {
00128         //make sure $limit is an unsigned int > 0
00129         $this->objLimit = ( is_numeric( $limit ) && $limit > 0 ) ? ( int )$limit : 1;
00130     }
00131 
00132     /**
00133      * Execute non-prepared SQL query and return resultset methods
00134      *
00135      * @access public
00136      * @return mixed
00137      */
00138     public function objExecuteNonPrepared()
00139     {
00140         $objResult = sqlite_query( $this->objQuery, $this->objConnection->connection() )
00141         or trigger_error( sqlite_error_string( sqlite_last_error( $this->objConnection->connection() ) ), E_USER_WARNING );
00142 
00143         return new sqliteResultset( $objResult, $this->objConnection->connection() );
00144     }
00145 
00146     /**
00147      * Prepare SQL query string and bind parameters
00148      *
00149      * @access private
00150      * @return str
00151      */
00152     private function objPrepareSQL()
00153     {
00154         $objSqlParts = explode( '?', $this->objQuery );
00155         $objQuery = $objSqlParts[0];
00156 
00157         for ( $i = 1; $i < count( $objSqlParts ); $i++ )
00158             $objQuery .= $this->objParameters[$i - 1] . $objSqlParts[$i];
00159 
00160         $objQuery = ( $this->objLimit ) ? str_ireplace( ':limit', $this->objLimit, $objQuery ) : $objQuery;
00161         $objQuery = ( $this->objOffset ) ? str_ireplace( ':offset', $this->objOffset, $objQuery ) : $objQuery;
00162 
00163         return $objQuery;
00164     }
00165 
00166     /**
00167      * Set offset int value
00168      *
00169      * @access public
00170      * @param  int $offset
00171      */
00172     public function offset( $offset )
00173     {
00174         //make sure $offset is an unsigned int > 0
00175         $this->objOffset = ( is_numeric( $offset ) && $offset > 0 ) ? ( int )$offset : 1;
00176     }
00177 
00178     /**
00179      * Dummy method to prevent class error for dbs that do not support sequences - used with prepared statement
00180      *
00181      * @access public
00182      */
00183     public function sequence() {}
00184 }
00185 
00186 ?>

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