class QueryBuilder

Minimalistic class for building prepared post-FROM SQL query strings

Properties

private array<string,string> $data
private $fromalias
private $joins
private $where
private $orderby
private $orderdesc
private $limit
private $offset

Methods

array
GetData()

No description

string
GetText()

Returns the compiled query as a string

string
__toString()

No description

string
AddData(string $val)

Adds the given value to the internal data array

string
BaseCompare(string $key, $val, string $symbol)

Base function for safely comparing columns to values

static string
EscapeWildcards(string $query)

Returns the given string with escaped SQL wildcard characters

string
IsNull(string $key)

Returns a string asserting the given column is null

string
Like(string $key, string $val, bool $hasMatch = false)

Returns a string comparing the given column to a value using LIKE

string
LessThan(string $key, $val)

Returns a query string asserting the given column is less than the given value

string
GreaterThan(string $key, $val)

Returns a query string asserting the given column is greater than the given value

string
IsTrue(string $key)

Returns a query string asserting the given column is "true" (greater than zero)

string
Equals(string $key, $val)

Returns a query string asserting the given column is equal to the given value

string
NotEquals(string $key, $val)

Returns a query string asserting the given column is not equal to the given value

string
Not(string $arg)

Returns a query string that inverts the logic of the given query

string
Or(string ...$args)

Returns a query string that combines the given arguments using OR

string
And(string ...$args)

Returns a query string that combines the given arguments using AND

string
ManyOr(string $key, array $vals, string $func = 'Equals')

Syntactic sugar function to check many OR conditions at once

string
ManyAnd(array $pairs, string $func = 'Equals')

Syntactic sugar function to check many AND conditions at once

Where(string $where)

Assigns a WHERE clause to the query

string|null
GetWhere()

Returns the current WHERE string

OrderBy(string $orderby, bool|null $desc = null)

Assigns an ORDER BY clause to the query

Limit(int|null $limit)

Assigns a LIMIT clause to the query

Offset(int|null $offset)

Assigns an OFFSET clause to the query (use with LIMIT)

Join(ObjectDatabase $database, string $joinclass, string $joinprop, string $destclass, string $destprop, string|null $destpoly = null, string|null $joinpoly = null)

Adds a JOIN clause to the query (can have > 1)

SelfJoinWhere(ObjectDatabase $database, string $joinclass, string $prop1, string $prop2, string $tmptable = '_tmptable')

Performs a self join on a table (selects an alias table and sets the WHERE query)

Details

at line 12
array GetData()

No description

Return Value

array

See also

QueryBuilder::$data

at line 15
string GetText()

Returns the compiled query as a string

Return Value

string

at line 33
string __toString()

No description

Return Value

string

at line 48
protected string AddData(string $val)

Adds the given value to the internal data array

Parameters

string $val

the actual data value

Return Value

string

the placeholder to go in the query

at line 56
private string BaseCompare(string $key, $val, string $symbol)

Base function for safely comparing columns to values

Parameters

string $key
$val
string $symbol

Return Value

string

at line 62
static string EscapeWildcards(string $query)

Returns the given string with escaped SQL wildcard characters

Parameters

string $query

Return Value

string

at line 68
string IsNull(string $key)

Returns a string asserting the given column is null

Parameters

string $key

Return Value

string

at line 77
string Like(string $key, string $val, bool $hasMatch = false)

Returns a string comparing the given column to a value using LIKE

Parameters

string $key

the name of the column to compare

string $val

the value to check for

bool $hasMatch

if true, the string manages its own SQL wildcard characters else use %$val%

Return Value

string

the built string

at line 85
string LessThan(string $key, $val)

Returns a query string asserting the given column is less than the given value

Parameters

string $key
$val

Return Value

string

at line 88
string GreaterThan(string $key, $val)

Returns a query string asserting the given column is greater than the given value

Parameters

string $key
$val

Return Value

string

at line 91
string IsTrue(string $key)

Returns a query string asserting the given column is "true" (greater than zero)

Parameters

string $key

Return Value

string

at line 94
string Equals(string $key, $val)

Returns a query string asserting the given column is equal to the given value

Parameters

string $key
$val

Return Value

string

at line 101
string NotEquals(string $key, $val)

Returns a query string asserting the given column is not equal to the given value

Parameters

string $key
$val

Return Value

string

at line 108
string Not(string $arg)

Returns a query string that inverts the logic of the given query

Parameters

string $arg

Return Value

string

at line 111
string Or(string ...$args)

Returns a query string that combines the given arguments using OR

Parameters

string ...$args

Return Value

string

at line 114
string And(string ...$args)

Returns a query string that combines the given arguments using AND

Parameters

string ...$args

Return Value

string

at line 123
string ManyOr(string $key, array $vals, string $func = 'Equals')

Syntactic sugar function to check many OR conditions at once

Parameters

string $key

the column to compare against

array $vals

array of possible values for the column

string $func

the function to use to check if the values match

Return Value

string

the built query string

at line 135
string ManyAnd(array $pairs, string $func = 'Equals')

Syntactic sugar function to check many AND conditions at once

Parameters

array $pairs

associative array mapping column names to their desired values

string $func

the function to use to check if the values match

Return Value

string

the built query string

at line 143
QueryBuilder Where(string $where)

Assigns a WHERE clause to the query

Parameters

string $where

Return Value

QueryBuilder

at line 146
string|null GetWhere()

Returns the current WHERE string

Return Value

string|null

at line 149
QueryBuilder OrderBy(string $orderby, bool|null $desc = null)

Assigns an ORDER BY clause to the query

Parameters

string $orderby
bool|null $desc

Return Value

QueryBuilder

at line 153
QueryBuilder Limit(int|null $limit)

Assigns a LIMIT clause to the query

Parameters

int|null $limit

Return Value

QueryBuilder

at line 156
QueryBuilder Offset(int|null $offset)

Assigns an OFFSET clause to the query (use with LIMIT)

Parameters

int|null $offset

Return Value

QueryBuilder

at line 169
QueryBuilder Join(ObjectDatabase $database, string $joinclass, string $joinprop, string $destclass, string $destprop, string|null $destpoly = null, string|null $joinpoly = null)

Adds a JOIN clause to the query (can have > 1)

Parameters

ObjectDatabase $database

reference to the database

string $joinclass

the class of the objects that join us to the destination class

string $joinprop

the column name of the join table that matches the destprop

string $destclass

the class of the destination object

string $destprop

the column name of the destination object that matches the joinprop

string|null $destpoly

if not null, the destprop is a polymorphic reference to this class

string|null $joinpoly

if not null, the joinprop is a polymorphic reference to this class

Return Value

QueryBuilder

at line 200
QueryBuilder SelfJoinWhere(ObjectDatabase $database, string $joinclass, string $prop1, string $prop2, string $tmptable = '_tmptable')

Performs a self join on a table (selects an alias table and sets the WHERE query)

If you need to add extra WHERE to the query, you must add to GetWhere()

Parameters

ObjectDatabase $database

database reference

string $joinclass

the table to join to itself

string $prop1

the column to match to prop2

string $prop2

the column to match to prop1

string $tmptable

the name of the temp table to join to (has prop2)

Return Value

QueryBuilder