QueryBuilder

State

helpers(o)

Determine whether a variable is of the type specified in the function name, eg isNumber

Types available are Null, Undefined, Object, Array, String, Number, Boolean, Function, RegExp, NaN and Infinite

Parameters

  • mixed o :

Returns

Boolean

Static members

.arrayPluck(arr, key)

Get a list of values with a common key from an array of objects

arrayPluck(arr, key)

Get a list of values with a common key from an array of objects

Parameters

  • Array arr :

    The array of objects to search

  • String key :

    The key of the object to get

Returns

Array :

The new array of plucked values

.isScalar(obj)

Determine whether an object is scalar

isScalar(obj)

Determine whether an object is scalar

Parameters

  • mixed obj :

    Object to test

Returns

bool :

Is object scalar

.regexInArray(arr, pattern)

Determine if a value matching the passed regular expression is in the passed array

regexInArray(arr, pattern)

Determine if a value matching the passed regular expression is in the passed array

Parameters

  • Array arr :

    The array to search

  • RegExp pattern :

    The pattern to match

Returns

Boolean :

If an array item matches the pattern

.stringTrim(str)

Wrap String.prototype.trim in a way that is easily mappable

stringTrim(str)

Wrap String.prototype.trim in a way that is easily mappable

Parameters

  • String str :

    The string to trim

Returns

String :

The trimmed string

.type(o)

Get the type of the variable passed

type(o)

Get the type of the variable passed

Parameters

  • mixed o :

    Object to type check

Returns

String :

Type of the object

.upperCaseFirst(str)

Make the first letter of the string uppercase

upperCaseFirst(str)

Make the first letter of the string uppercase

Parameters

  • String str :

    The string to modify

Returns

String :

The modified string

compileJoin(condition)

Return the output of the parsing of the join condition

Parameters

  • String condition :

    The join condition to evalate

Returns

String :

The parsed/escaped join condition

constructor(driver)

Parameters

  • Driver driver :

    The driver object for the database in use

Returns

void

delete(table, [where], callback)

Run the generated delete query

Parameters

  • String table :

    The table to insert into

  • [Object] where :

    Where clause for delete statement

  • Function callback :

    Callback for handling response from the database

Returns

void

end

Closes the database connection for the current adapter

Returns

void

filterMatches(array)

Filter matched patterns

Parameters

  • Array array :

    Set of possible matches

Returns

Array or :

Filtered set of possible matches

from(tableName)

Specify the database table to select from

Parameters

  • String tableName :

    The table to use for the current query

Returns

QueryBuilder :

The Query Builder object, for chaining

get([table], [limit], [offset], callback)

Get the results of the compiled query

Parameters

  • [String] table :

    The table to select from

  • [Number] limit :

    A limit for the query

  • [Number] offset :

    An offset for the query

  • Function callback :

    A callback for receiving the result

Returns

void

getCompiledDelete(table, [reset])

Return generated delete query SQL

Parameters

  • String table :

    the name of the table to delete from

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

getCompiledInsert(table, [reset])

Return generated insert query SQL

Parameters

  • String table :

    the name of the table to insert into

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

getCompiledSelect([table], [reset])

Return generated select query SQL

Parameters

  • [String] table :

    the name of the table to retrieve from

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

getCompiledUpdate(table, [reset])

Return generated update query SQL

Parameters

  • String table :

    the name of the table to update

  • [Boolean] reset (default true) :

    Whether to reset the query builder so another query can be built

Returns

String :

The compiled sql statement

groupBy(field)

Group the results by the selected field(s)

Parameters

  • String or Array field :

    The name of the field to group by

Returns

QueryBuilder :

The Query Builder object, for chaining

groupEnd

Ends a logical grouping started with one of the groupStart methods

Returns

QueryBuilder :

The Query Builder object, for chaining

groupStart

Adds an open paren to the current query for logical grouping

Returns

QueryBuilder :

The Query Builder object, for chaining

hasOperator(string)

Check if the string contains an operator, and if so, return the operator(s). If there are no matches, return null

Parameters

  • String string :

    the string to check

Returns

Array or :

List of operators

having(key, [val])

Add a 'having' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

insert(table, [data], callback)

Run the generated insert query

Parameters

  • String table :

    The table to insert into

  • [Object] data :

    Data to insert, if not already added with the 'set' method

  • Function callback :

    Callback for handling response from the database

Returns

void

insertBatch(table, data, callback)

Insert multiple sets of rows at a time

Parameters

  • String table :

    The table to insert into

  • Array data :

    The array of objects containing data rows to insert

  • Function callback :

    Callback for handling database response

Returns

void

Examples

query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}], callbackFunction);

join(table, cond, [type])

Add a join clause to the query

Parameters

  • String table :

    The table you are joining

  • String cond :

    The join condition.

  • [String] type (default 'inner') :

    The type of join, which defaults to inner

Returns

QueryBuilder :

The Query Builder object, for chaining

like(field, val, [pos])

Add a 'like/ and like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

limit(limit, [offset])

Put a limit on the query

Parameters

  • Number limit :

    The maximum number of rows to fetch

  • [Number] offset :

    The row number to start from

Returns

QueryBuilder :

The Query Builder object, for chaining

notLike(field, val, [pos])

Add a 'not like/ and not like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

orGroupStart

Adds an open paren to the current query for logical grouping, prefixed with 'OR'

Returns

QueryBuilder :

The Query Builder object, for chaining

orHaving(key, [val])

Add an 'or having' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

orLike(field, val, [pos])

Add an 'or like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

orNotGroupStart

Adds an open paren to the current query for logical grouping, prefixed with 'OR NOT'

Returns

QueryBuilder :

The Query Builder object, for chaining

orNotLike(field, val, [pos])

Add an 'or not like' clause to the query

Parameters

  • String field :

    The name of the field to compare to

  • String val :

    The value to compare to

  • [String] pos (default both) :

    The placement of the wildcard character(s): before, after, or both

Returns

QueryBuilder :

The Query Builder object, for chaining

orWhere(key, [val])

Set a 'or where' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

orWhereIn(key, val)

Set a 'or where in' clause

Parameters

  • String key :

    the field to search

  • Array val :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining

orWhereIsNotNull(field)

Field is not null prefixed with 'OR'

Parameters

  • String field :

    The name of the field

Returns

QueryBuilder :

The Query Builder object, for chaining

orWhereIsNull(field)

Field is null prefixed with 'OR'

Parameters

  • String field :

    The name of the field

Returns

QueryBuilder :

The Query Builder object, for chaining

orWhereNotIn(key, val)

Set a 'or where not in' clause

Parameters

  • String key :

    the field to search

  • Array val :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining

orderBy(field, [type])

Order the results by the selected field(s)

Parameters

  • String field :

    The field(s) to order by

  • [String] type (default 'ASC') :

    The order direction, ASC or DESC

Returns

QueryBuilder :

The Query Builder object, for chaining

parseJoin(sql)

Tokenize the sql into parts for additional processing

Parameters

  • String sql :

    Join sql to parse

Returns

Object :

Join condition components

parseWhere(driver, state)

Parse a where clause to separate functions from values

Parameters

  • Driver driver :

    The current db driver

  • State state :

    Query Builder state object

Returns

String :

The parsed/escaped where condition

resetQuery

Reset the object state for a new query

Returns

void

select(fields)

Specify rows to select in the query

Parameters

  • String or Array fields :

    The fields to select from the current table

Returns

QueryBuilder :

The Query Builder object, for chaining

set(key, [val])

Set values for insertion or updating

Parameters

  • String or Object key :

    The key or object to use

  • [String] val :

    The value if using a scalar key

Returns

QueryBuilder :

The Query Builder object, for chaining

update(table, [data], callback)

Run the generated update query

Parameters

  • String table :

    The table to insert into

  • [Object] data :

    Data to insert, if not already added with the 'set' method

  • Function callback :

    Callback for handling response from the database

Returns

void

where(key, [val])

Set a 'where' clause

Parameters

  • String or Object key :

    The name of the field and the comparision operator, or an object

  • [String or Number] val :

    The value to compare if the value of key is a string

Returns

QueryBuilder :

The Query Builder object, for chaining

whereIn(key, val)

Set a 'where in' clause

Parameters

  • String key :

    the field to search

  • Array val :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining

whereIsNotNull(field)

Specify that a field IS NOT NULL

Parameters

  • String field :

    The name so the field that is not to be null

Returns

QueryBuilder :

The Query Builder object, for chaining

whereIsNull(field)

Select a field that is Null

Parameters

  • String field :

    The name of the field that has a NULL value

Returns

QueryBuilder :

The Query Builder object, for chaining

whereNotIn(key, val)

Set a 'where not in' clause

Parameters

  • String key :

    the field to search

  • Array val :

    the array of items to search in

Returns

QueryBuilder :

The Query Builder object, for chaining