Rebar Project - MySQL Engine
Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
Query Class Reference
Inheritance diagram for Query:
ControlQuery ReadQuery WriteQuery CommitQuery LockQuery RollbackQuery TransactQuery UnlockQuery SelectQuery DeleteQuery InsertQuery UpdateQuery

Public Member Functions

 setParameters ($parameters)
 
 enableLimits ($limits)
 
 makeQuery (&$query_string, &$dtype_string)
 

Static Public Attributes

static $OPER_EQ = ' = '
 
static $OPER_NE = ' <> '
 
static $OPER_GT = ' > '
 
static $OPER_GE = ' >= '
 
static $OPER_LT = ' < '
 
static $OPER_LE = ' <= '
 
static $BOOL_AND = ' and '
 
static $BOOL_OR = ' or '
 
static $DTYPE_D = 'd'
 
static $DTYPE_I = 'i'
 
static $DTYPE_S = 's'
 
static $ORDER_A = ' asc'
 
static $ORDER_D = ' desc'
 
static $LOCK_R = ' read'
 
static $LOCK_W = ' write'
 

Protected Member Functions

 makeParameterString ($parameters, &$query_string, &$dtype_string, $where=true)
 
 makeLimitString (&$query_string, &$dtype_string)
 

Detailed Description

SVN

Id
Query.php 14 2017-02-02 21:06:24Z phil
Author
Phil Riesch phil@.nosp@m.phil.nosp@m.riesc.nosp@m.h.co.nosp@m.m

LICENSE

The PHP Rebar Project Copyright 2017 Philip Riesch phil@.nosp@m.phil.nosp@m.riesc.nosp@m.h.co.nosp@m.m

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

– Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

– Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

– Neither the name of Philip J. Riesch nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PHILIP J. RIESCH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

DESCRIPTION

The Query class is the primary abstraction of all possible MySQL queries that can be made on a MySQL database.

This class defines all of the constants that can be used for parameter operators, data types, order constants, and lock constants.

Member Function Documentation

Query::enableLimits (   $limits)

Enable limits on the query.

Parameters
$limitsboolean Whether limits should be enabled on the query.
Query::makeLimitString ( $query_string,
$dtype_string 
)
protected

Helper function that generates the limit string (SQL limit clause)

Parameters
&$query_stringstring
&$dtype_stringstring
Query::makeParameterString (   $parameters,
$query_string,
$dtype_string,
  $where = true 
)
protected

Helper function for makeQuery that recursively generates the parameter string (SQL where clause)

Parameters
$parametersarray
&$query_stringstring
&$dtype_stringstring
$whereboolean
Query::makeQuery ( $query_string,
$dtype_string 
)
abstract

Used by the MysqlFacet class when it is executing queries.

Parameters
&$query_stringstring The generated SQL query
&$dtype_stringstring The generated input data type string
Query::setParameters (   $parameters)

Set parameters on the query.

Parameters control what would be the contents of the 'where' clause in a SQL statement.

Parameters can be either a single compare, multiple compares joined by boolean operators, or they can be nested statements that form complex boolean expressions.

The parameters are controled with a structured array. In general: the array will have the following format:

The first item in the array needs 3 objects: the field to compare, the operator to use, and the data type of the value that we are going to pass when we execute the query. The second item needs 4 objects. The first object is the boolean join that relates this statement to the first statement, and the second, third, and fourth object is the same as the first statement.

Consider as an example the following SQL snippet:

... where NumericalTableField >= ? ...

We are comparing whether NumericalTableField is greater than or equal to some integer. Represented as a parameter format array this query would be:

array(array('NumericalTableField', Query::$OPER_GE, Query::$DTYPE_I))

Let's complicate it a bit. Say that there is a text field in which we only wish to execute the above statement on rows in the table that match some text value: maybe some sort of tag or something...

... where TextTagValue = ? and NumericalTableField >= ? ...

The parameter array structure would then look like:

array( array('TextTagValue', Query::$OPER_EQ, Query::$DTYPE_S), array(Query::$BOOL_AND, 'NumericalTableField', Query::$OPER_GE, Query::$DTYPE_I) )

Get the picture? Notice how the second array has that first value that determines the boolean relationship that this statement has with the statement that precedes it.

Let's get complicated. Say that there is some range of numbers. And in our theoretical table we are interested in rows that fall outside of this range, i.e. they are greater than the range or less than the range, but not equal to the range. So we would need an 'or' statement to accomplish this. But we also want to match that text tag. So we need a complex query to do it.

In SQL that would look like:

... where TextTagValue = ? and (NumericalTableField < ? or NumericalTableField > ?) ...

The parameter specification allows you to nest queries by using additional arrays in place of query specifications. The above query would be written as the following:

array( array('TextTagValue', Query::$OPER_EQ, Query::$DTYPE_S), array(Query::$BOOL_AND, array( array('NumericalTableField', Query::$OPER_LT, Query::$DTYPE_I), array(Query::$BOOL_OR, 'NumericalTableField', Query::$OPER_GT, Query::$DTYPE_I) )) )

This structure can be repeated indefinitely to create whatever kind of where clause that your query requires.

Parameters
$parametersarray|null The parameter specification array

Member Data Documentation

Query::$BOOL_AND = ' and '
static

Parameter boolean and operator

Query::$BOOL_OR = ' or '
static

Parameter boolean or operator

Query::$DTYPE_D = 'd'
static

Data type dobule (float) flag

Query::$DTYPE_I = 'i'
static

Data type integer flag

Query::$DTYPE_S = 's'
static

Data type string flag

Query::$LOCK_R = ' read'
static

Table lock read flag

Query::$LOCK_W = ' write'
static

Table lock write flag

Query::$OPER_EQ = ' = '
static

Parameter equal operator

Query::$OPER_GE = ' >= '
static

Parameter greater than or equal operator

Query::$OPER_GT = ' > '
static

Parameter greater than operator

Query::$OPER_LE = ' <= '
static

Parameter less than or equal operator

Query::$OPER_LT = ' < '
static

Parameter less than operator

Query::$OPER_NE = ' <> '
static

Parameter not equal operator

Query::$ORDER_A = ' asc'
static

Ordering ascending flag

Query::$ORDER_D = ' desc'
static

Ordering descending flag


The documentation for this class was generated from the following file: