# \QueryQueryBuilder

Convenience class for creating sql queries

## Summary

Methods
Properties
Constants
__construct()
__destruct()
__call()
select()
selectMax()
selectMin()
selectAvg()
selectSum()
distinct()
explain()
from()
like()
orLike()
notLike()
orNotLike()
having()
orHaving()
where()
orWhere()
whereIn()
orWhereIn()
whereNotIn()
orWhereNotIn()
set()
join()
groupBy()
orderBy()
limit()
groupStart()
notGroupStart()
orGroupStart()
orNotGroupStart()
groupEnd()
get()
getWhere()
countAll()
countAllResults()
insert()
insertBatch()
update()
updateBatch()
replace()
delete()
getCompiledSelect()
getCompiledInsert()
getCompiledUpdate()
getCompiledDelete()
resetQuery()
$connName
$queries
$db
$parser
$util
$sql
KEY
VALUE
BOTH
_mixedSet()
_select()
_getCompile()
_like()
_having()
_where()
_whereString()
_whereIn()
_run()
_appendMap()
_appendQuery()
_compileType()
_compile()
$selectString
$fromString
$setString
$orderString
$groupString
$setArrayKeys
$orderArray
$groupArray
$values
$whereValues
$limit
$offset
$queryMap
$havingMap
$explain
N/A
No private methods found
$stringVars
$arrayVars
N/A

File
  Query/QueryBuilder.php
Package
  Default
Class hierarchy
  \Query\AbstractQueryBuilder
  \Query\QueryBuilder
Implements
  \Query\QueryBuilderInterface

## Tags

None found

## Constants

### KEY

    KEY

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### VALUE

    VALUE

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### BOTH

    BOTH

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

## Properties

### $connName

    $connName : string

Convenience property for connection management

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $queries

    $queries : array

List of queries executed

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $db

    $db : \Query\Drivers\DriverInterface

The current database driver

#### Type

\Query\Drivers\DriverInterface

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $parser

    $parser : \Query\QueryParser

Query parser class instance

#### Type

\Query\QueryParser

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $util

    $util : \Query\Drivers\AbstractUtil

Alias to driver util class

#### Type

\Query\Drivers\AbstractUtil

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $sql

    $sql : \Query\Drivers\SQLInterface

Alias to driver sql class

#### Type

\Query\Drivers\SQLInterface

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $selectString

    $selectString : string

Compiled 'select' clause

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $fromString

    $fromString : string

Compiled 'from' clause

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $setString

    $setString : string

Compiled arguments for insert / update

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $orderString

    $orderString : string

Order by clause

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $groupString

    $groupString : string

Group by clause

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $setArrayKeys

    $setArrayKeys : array

Keys for insert/update statement

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $orderArray

    $orderArray : array

Key/val pairs for order by clause

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $groupArray

    $groupArray : array

Key/val pairs for group by clause

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $values

    $values : array

Values to apply to prepared statements

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $whereValues

    $whereValues : array

Values to apply to where clauses in prepared statements

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $limit

    $limit : string

Value for limit string

#### Type

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $offset

    $offset : integer

Value for offset in limit string

#### Type

integer

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $queryMap

    $queryMap : array

Query component order mapping for complex select queries

Format: array( 'type' => 'where', 'conjunction' => ' AND ', 'string' => 'k=?' )

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $havingMap

    $havingMap : array

Map for having clause

#### Type

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $explain

    $explain : boolean

Whether to do only an explain on the query

#### Type

boolean

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### $stringVars

    $stringVars : array

String class values to be reset

#### Type

array

#

## Tags

None found

### $arrayVars

    $arrayVars : array

Array class variables to be reset

#### Type

array

#

## Tags

None found

## Methods

### __construct()

    __construct(\Query\Drivers\DriverInterface  $db, \Query\QueryParser  $parser)

Constructor

#### Parameters

\Query\Drivers\DriverInterface | $db |
\Query\QueryParser | $parser |

#

## Tags

None found

### __destruct()

    __destruct()

Destructor

#

## Tags

codeCoverageIgnore |

### __call()

    __call(string  $name, array  $params) : mixed

Calls a function further down the inheritance chain

#### Parameters

string | $name |
array | $params |

#### Throws

\BadMethodCallException

#### Returns

mixed

#

## Tags

None found

### select()

    select(string  $fields) : \Query\QueryBuilderInterface

Specifies rows to select in a query

#### Parameters

string | $fields |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### selectMax()

    selectMax(string  $field, string|boolean  $as = FALSE) : \Query\QueryBuilderInterface

Selects the maximum value of a field from a query

#### Parameters

string | $field |
string|boolean | $as |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### selectMin()

    selectMin(string  $field, string|boolean  $as = FALSE) : \Query\QueryBuilderInterface

Selects the minimum value of a field from a query

#### Parameters

string | $field |
string|boolean | $as |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### selectAvg()

    selectAvg(string  $field, string|boolean  $as = FALSE) : \Query\QueryBuilderInterface

Selects the average value of a field from a query

#### Parameters

string | $field |
string|boolean | $as |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### selectSum()

    selectSum(string  $field, string|boolean  $as = FALSE) : \Query\QueryBuilderInterface

Selects the sum of a field from a query

#### Parameters

string | $field |
string|boolean | $as |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### distinct()

    distinct() : \Query\QueryBuilderInterface

Adds the 'distinct' keyword to a query

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### explain()

    explain() : \Query\QueryBuilderInterface

Tell the database to give you the query plan instead of result set

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### from()

    from(string  $tblname) : \Query\QueryBuilderInterface

Specify the database table to select from

#### Parameters

string | $tblname |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### like()

    like(string  $field, mixed  $val, string  $pos = 'both') : \Query\QueryBuilderInterface

Creates a Like clause in the sql statement

#### Parameters

string | $field |
mixed | $val |
string | $pos |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orLike()

    orLike(string  $field, mixed  $val, string  $pos = 'both') : \Query\QueryBuilderInterface

Generates an OR Like clause

#### Parameters

string | $field |
mixed | $val |
string | $pos |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### notLike()

    notLike(string  $field, mixed  $val, string  $pos = 'both') : \Query\QueryBuilderInterface

Generates a NOT LIKE clause

#### Parameters

string | $field |
mixed | $val |
string | $pos |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orNotLike()

    orNotLike(string  $field, mixed  $val, string  $pos = 'both') : \Query\QueryBuilderInterface

Generates a OR NOT LIKE clause

#### Parameters

string | $field |
mixed | $val |
string | $pos |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### having()

    having(mixed  $key, mixed  $val = array()) : \Query\QueryBuilderInterface

Generates a 'Having' clause

#### Parameters

mixed | $key |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orHaving()

    orHaving(mixed  $key, mixed  $val = array()) : \Query\QueryBuilderInterface

Generates a 'Having' clause prefixed with 'OR'

#### Parameters

mixed | $key |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### where()

    where(mixed  $key, mixed  $val = array(), mixed  $escape = NULL) : \Query\QueryBuilderInterface

Specify condition(s) in the where clause of a query Note: this function works with key / value, or a passed array with key / value pairs

#### Parameters

mixed | $key |
mixed | $val |
mixed | $escape |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orWhere()

    orWhere(string  $key, mixed  $val = array()) : \Query\QueryBuilderInterface

Where clause prefixed with "OR"

#### Parameters

string | $key |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### whereIn()

    whereIn(mixed  $field, mixed  $val = array()) : \Query\QueryBuilderInterface

Where clause with 'IN' statement

#### Parameters

mixed | $field |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orWhereIn()

    orWhereIn(string  $field, mixed  $val = array()) : \Query\QueryBuilderInterface

Where in statement prefixed with "or"

#### Parameters

string | $field |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### whereNotIn()

    whereNotIn(string  $field, mixed  $val = array()) : \Query\QueryBuilderInterface

WHERE NOT IN (FOO) clause

#### Parameters

string | $field |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orWhereNotIn()

    orWhereNotIn(string  $field, mixed  $val = array()) : \Query\QueryBuilderInterface

OR WHERE NOT IN (FOO) clause

#### Parameters

string | $field |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### set()

    set(mixed  $key, mixed  $val = NULL) : \Query\QueryBuilderInterface

Sets values for inserts / updates / deletes

#### Parameters

mixed | $key |
mixed | $val |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### join()

    join(string  $table, string  $condition, string  $type = '') : \Query\QueryBuilderInterface

Creates a join phrase in a compiled query

#### Parameters

string | $table |
string | $condition |
string | $type |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### groupBy()

    groupBy(mixed  $field) : \Query\QueryBuilderInterface

Group the results by the selected field(s)

#### Parameters

mixed | $field |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orderBy()

    orderBy(string  $field, string  $type = "") : \Query\QueryBuilderInterface

Order the results by the selected field(s)

#### Parameters

string | $field |
string | $type |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### limit()

    limit(integer  $limit, integer|boolean  $offset = FALSE) : \Query\QueryBuilderInterface

Set a limit on the current sql statement

#### Parameters

integer | $limit |
integer|boolean | $offset |

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### groupStart()

    groupStart() : \Query\QueryBuilderInterface

Adds a paren to the current query for query grouping

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### notGroupStart()

    notGroupStart() : \Query\QueryBuilderInterface

Adds a paren to the current query for query grouping, prefixed with 'NOT'

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orGroupStart()

    orGroupStart() : \Query\QueryBuilderInterface

Adds a paren to the current query for query grouping, prefixed with 'OR'

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### orNotGroupStart()

    orNotGroupStart() : \Query\QueryBuilderInterface

Adds a paren to the current query for query grouping, prefixed with 'OR NOT'

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### groupEnd()

    groupEnd() : \Query\QueryBuilderInterface

Ends a query group

#### Returns

\Query\QueryBuilderInterface

#

## Tags

None found

### get()

    get(string  $table = '', integer|boolean  $limit = FALSE, integer|boolean  $offset = FALSE) : \PDOStatement

Select and retrieve all records from the current table, and/or execute current compiled query

#### Parameters

string | $table |
integer|boolean | $limit |
integer|boolean | $offset |

#### Returns

\PDOStatement

#

## Tags

None found

### getWhere()

    getWhere(string  $table, array  $where = array(), integer|boolean  $limit = FALSE, integer|boolean  $offset = FALSE) : \PDOStatement

Convenience method for get() with a where clause

#### Parameters

string | $table |
array | $where |
integer|boolean | $limit |
integer|boolean | $offset |

#### Returns

\PDOStatement

#

## Tags

None found

### countAll()

    countAll(string  $table) : integer

Retrieve the number of rows in the selected table

#### Parameters

string | $table |

#### Returns

integer

#

## Tags

None found

### countAllResults()

    countAllResults(string  $table = '', boolean  $reset = TRUE) : integer

Retrieve the number of results for the generated query - used in place of the get() method

#### Parameters

string | $table |

boolean

$reset

- Whether to keep the query after counting the results

#### Returns

integer

#

## Tags

None found

### insert()

    insert(string  $table, mixed  $data = array()) : \PDOStatement

Creates an insert clause, and executes it

#### Parameters

string | $table |
mixed | $data |

#### Returns

\PDOStatement

#

## Tags

None found

### insertBatch()

    insertBatch(string  $table, array  $data = array()) : \PDOStatement

Creates and executes a batch insertion query

#### Parameters

string | $table |
array | $data |

#### Returns

\PDOStatement

#

## Tags

None found

### update()

    update(string  $table, mixed  $data = array()) : \PDOStatement

Creates an update clause, and executes it

#### Parameters

string | $table |
mixed | $data |

#### Returns

\PDOStatement

#

## Tags

None found

### updateBatch()

    updateBatch(string  $table, array|object  $data, string  $where) : integer|null

Creates a batch update, and executes it.

Returns the number of affected rows

#### Parameters

string | $table |
array|object | $data |
string | $where |

#### Returns

integer|null

#

## Tags

None found

### replace()

    replace(string  $table, array  $data = array()) : \PDOStatement|null

Insertion with automatic overwrite, rather than attempted duplication

#### Parameters

string | $table |
array | $data |

#### Returns

\PDOStatement|null

#

## Tags

None found

### delete()

    delete(string  $table, mixed  $where = '') : \PDOStatement

Deletes data from a table

#### Parameters

string | $table |
mixed | $where |

#### Returns

\PDOStatement

#

## Tags

None found

### getCompiledSelect()

    getCompiledSelect(string  $table = '', boolean  $reset = TRUE) : string

Returns the generated 'select' sql query

#### Parameters

string | $table |
boolean | $reset |

#### Returns

string

#

## Tags

None found

### getCompiledInsert()

    getCompiledInsert(string  $table, boolean  $reset = TRUE) : string

Returns the generated 'insert' sql query

#### Parameters

string | $table |
boolean | $reset |

#### Returns

string

#

## Tags

None found

### getCompiledUpdate()

    getCompiledUpdate(string  $table = '', boolean  $reset = TRUE) : string

Returns the generated 'update' sql query

#### Parameters

string | $table |
boolean | $reset |

#### Returns

string

#

## Tags

None found

### getCompiledDelete()

    getCompiledDelete(string  $table = '', boolean  $reset = TRUE) : string

Returns the generated 'delete' sql query

#### Parameters

string | $table |
boolean | $reset |

#### Returns

string

#

## Tags

None found

### resetQuery()

    resetQuery() : void

Clear out the class variables, so the next query can be run

#

## Tags

None found

### _mixedSet()

    _mixedSet(array  $var, mixed  $key, mixed  $val = NULL, integer  $valType = self::BOTH) : array

Set values in the class, with either an array or key value pair

#### Parameters

array | $var |
mixed | $key |
mixed | $val |
integer | $valType |

#### Returns

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _select()

    _select(string  $field, string|boolean  $as = FALSE) : string

Method to simplify select_ methods

#### Parameters

string | $field |
string|boolean | $as |

#### Returns

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _getCompile()

    _getCompile(string  $type, string  $table, boolean  $reset) : string

Helper function for returning sql strings

#### Parameters

string | $type |
string | $table |
boolean | $reset |

#### Returns

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _like()

    _like(string  $field, mixed  $val, string  $pos, string  $like = 'LIKE', string  $conj = 'AND') : \Query\QueryBuilderInterface

Simplify 'like' methods

#### Parameters

string | $field |
mixed | $val |
string | $pos |
string | $like |
string | $conj |

#### Returns

\Query\QueryBuilderInterface

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _having()

    _having(mixed  $key, mixed  $val = array(), string  $conj = 'AND') : \Query\QueryBuilderInterface

Simplify building having clauses

#### Parameters

mixed | $key |
mixed | $val |
string | $conj |

#### Returns

\Query\QueryBuilderInterface

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _where()

    _where(mixed  $key, mixed  $val = array()) : array

Do all the redundant stuff for where/having type methods

#### Parameters

mixed | $key |
mixed | $val |

#### Returns

array

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _whereString()

    _whereString(mixed  $key, mixed  $val = array(), string  $defaultConj = 'AND') : \Query\QueryBuilderInterface

Simplify generating where string

#### Parameters

mixed | $key |
mixed | $val |
string | $defaultConj |

#### Returns

\Query\QueryBuilderInterface

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _whereIn()

    _whereIn(mixed  $key, mixed  $val = array(), string  $in = 'IN', string  $conj = 'AND') : \Query\QueryBuilderInterface

Simplify where_in methods

#### Parameters

mixed | $key |
mixed | $val |

string

$in

- The (not) in fragment

string

$conj

- The where in conjunction

#### Returns

\Query\QueryBuilderInterface

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _run()

    _run(string  $type, string  $table, string  $sql = NULL, array|null  $vals = NULL, boolean  $reset = TRUE) : \PDOStatement

Executes the compiled query

#### Parameters

string | $type |
string | $table |
string | $sql |
array|null | $vals |
boolean | $reset |

#### Returns

\PDOStatement

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _appendMap()

    _appendMap(string  $conjunction = '', string  $string = '', string  $type = '') : void

Add an additional set of mapping pairs to a internal map

#### Parameters

string | $conjunction |
string | $string |
string | $type |

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _appendQuery()

    _appendQuery(array  $vals, string  $sql, integer  $totalTime) : void

Convert the prepared statement into readable sql

#### Parameters

array | $vals |
string | $sql |
integer | $totalTime |

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _compileType()

    _compileType(string  $type = '', string  $table = '') : string

Sub-method for generating sql strings

#### Parameters

string | $type |
string | $table |

#### Returns

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### _compile()

    _compile(string  $type = '', string  $table = '') : string

String together the sql statements for sending to the db

#### Parameters

string | $type |
string | $table |

#### Returns

string

#

Inherited from
  \Query\AbstractQueryBuilder

## Tags

None found

### QueryBuilder.php