# JSDoc: Class: QueryBuilder

# Class: QueryBuilder

## QueryBuilder(Driver, Adapter)

Main object that builds SQL queries.

## Constructor

#### new QueryBuilder(Driver, Adapter)

##### Parameters:

Name | Type | Description
--- | --- | ---
`Driver` | Driver | The syntax driver for the database
`Adapter` | Adapter | The database module adapter for running queries

Source:
  - QueryBuilder.js, line 11

### Extends

- QueryBuilderBase

### Methods

#### delete(table, whereopt) → {Promise.<Result>}

Run the generated delete query

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`table` | String |  | The table to insert into
`where` | Object | <optional> | Where clause for delete statement

Source:
  - QueryBuilder.js, line 595

##### Returns:

- Promise containing the result of the query

Type
  Promise.<Result>

#### end() → {void}

Closes the database connection for the current adapter

Source:
  - QueryBuilder.js, line 81

##### Returns:

Type
  void

#### from(tableName) → {QueryBuilder}

Specify the database table to select from

##### Parameters:

Name | Type | Description
--- | --- | ---
`tableName` | String | The table to use for the current query

Source:
  - QueryBuilder.js, line 132

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

##### Examples

    query.from('tableName');

    query.from('tableName t'); // Select the table with an alias

#### get(tableopt, limitopt, offsetopt) → {Promise.<Result>}

Get the results of the compiled query

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`table` | String | <optional> | The table to select from
`limit` | Number | <optional> | A limit for the query
`offset` | Number | <optional> | An offset for the query

Source:
  - QueryBuilder.js, line 513

##### Returns:

- Promise containing the result of the query

Type
  Promise.<Result>

##### Examples

    query.get('table_name').then(promiseCallback); // Get all the rows in the table

    query.get('table_name', 5); // Get 5 rows from the table

    query.get(); // Get the results of a query generated with other methods

#### getCompiledDelete(table, resetopt) → {String}

Return generated delete query SQL

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`table` | String |  |  | the name of the table to delete from
`reset` | Boolean | <optional> | true | Whether to reset the query builder so another query can be built

Source:
  - QueryBuilder.js, line 652

##### Returns:

- The compiled sql statement

Type
  String

#### getCompiledInsert(table, resetopt) → {String}

Return generated insert query SQL

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`table` | String |  |  | the name of the table to insert into
`reset` | Boolean | <optional> | true | Whether to reset the query builder so another query can be built

Source:
  - QueryBuilder.js, line 630

##### Returns:

- The compiled sql statement

Type
  String

#### getCompiledSelect(tableopt, resetopt) → {String}

Return generated select query SQL

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`table` | String | <optional> |  | the name of the table to retrieve from
`reset` | Boolean | <optional> | true | Whether to reset the query builder so another query can be built

Source:
  - QueryBuilder.js, line 615

##### Returns:

- The compiled sql statement

Type
  String

#### getCompiledUpdate(table, resetopt) → {String}

Return generated update query SQL

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`table` | String |  |  | the name of the table to update
`reset` | Boolean | <optional> | true | Whether to reset the query builder so another query can be built

Source:
  - QueryBuilder.js, line 641

##### Returns:

- The compiled sql statement

Type
  String

#### groupBy(field) → {QueryBuilder}

Group the results by the selected field(s)

##### Parameters:

Name | Type | Description
--- | --- | ---
`field` | String | Array | The name of the field to group by

Source:
  - QueryBuilder.js, line 396

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### groupEnd() → {QueryBuilder}

Ends a logical grouping started with one of the groupStart methods

Source:
  - QueryBuilder.js, line 492

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### groupStart() → {QueryBuilder}

Adds an open paren to the current query for logical grouping

Source:
  - QueryBuilder.js, line 456

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### having(key, valopt) → {QueryBuilder}

Add a 'having' clause

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`key` | String | Object |  |  | The name of the field and the comparision operator, or an object
`val` | String | Number | <optional> | null | The value to compare if the value of key is a string

Source:
  - QueryBuilder.js, line 205

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### insert(table, dataopt) → {Promise.<Result>}

Run the generated insert query

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`table` | String |  | The table to insert into
`data` | Object | <optional> | Data to insert, if not already added with the 'set' method

Source:
  - QueryBuilder.js, line 533

##### Returns:

- Promise containing the result of the query

Type
  Promise.<Result>

#### insertBatch(table, data) → {Promise.<Result>}

Insert multiple sets of rows at a time

##### Parameters:

Name | Type | Description
--- | --- | ---
`table` | String | The table to insert into
`data` | Array | The array of objects containing data rows to insert

Source:
  - QueryBuilder.js, line 551

##### Returns:

- Promise containing the result of the query

Type
  Promise.<Result>

##### Example

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

#### join(table, cond, typeopt) → {QueryBuilder}

Add a join clause to the query

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`table` | String |  |  | The table you are joining
`cond` | String |  |  | The join condition.
`type` | String | <optional> | 'inner' | The type of join, which defaults to inner

Source:
  - QueryBuilder.js, line 371

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### like(field, val, posopt) → {QueryBuilder}

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

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`field` | String |  |  | The name of the field to compare to
`val` | String |  |  | The value to compare to
`pos` | String | <optional> | both | The placement of the wildcard character(s): before, after, or both

Source:
  - QueryBuilder.js, line 154

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### limit(limit, offsetopt) → {QueryBuilder}

Put a limit on the query

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`limit` | Number |  | The maximum number of rows to fetch
`offset` | Number | <optional> | The row number to start from

Source:
  - QueryBuilder.js, line 444

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### notLike(field, val, posopt) → {QueryBuilder}

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

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`field` | String |  |  | The name of the field to compare to
`val` | String |  |  | The value to compare to
`pos` | String | <optional> | both | The placement of the wildcard character(s): before, after, or both

Source:
  - QueryBuilder.js, line 167

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orderBy(field, typeopt) → {QueryBuilder}

Order the results by the selected field(s)

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`field` | String |  |  | The field(s) to order by
`type` | String | <optional> | 'ASC' | The order direction, ASC or DESC

Source:
  - QueryBuilder.js, line 416

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orGroupStart() → {QueryBuilder}

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

Source:
  - QueryBuilder.js, line 469

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orHaving(key, valopt) → {QueryBuilder}

Add an 'or having' clause

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`key` | String | Object |  |  | The name of the field and the comparision operator, or an object
`val` | String | Number | <optional> | null | The value to compare if the value of key is a string

Source:
  - QueryBuilder.js, line 217

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orLike(field, val, posopt) → {QueryBuilder}

Add an 'or like' clause to the query

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`field` | String |  |  | The name of the field to compare to
`val` | String |  |  | The value to compare to
`pos` | String | <optional> | both | The placement of the wildcard character(s): before, after, or both

Source:
  - QueryBuilder.js, line 180

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orNotGroupStart() → {QueryBuilder}

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

Source:
  - QueryBuilder.js, line 481

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orNotLike(field, val, posopt) → {QueryBuilder}

Add an 'or not like' clause to the query

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`field` | String |  |  | The name of the field to compare to
`val` | String |  |  | The value to compare to
`pos` | String | <optional> | both | The placement of the wildcard character(s): before, after, or both

Source:
  - QueryBuilder.js, line 193

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orWhere(key, valopt) → {QueryBuilder}

Set a 'or where' clause

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | String | Object |  | The name of the field and the comparision operator, or an object
`val` | String | Number | <optional> | The value to compare if the value of key is a string

Source:
  - QueryBuilder.js, line 241

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orWhereIn(key, values) → {QueryBuilder}

Set a 'or where in' clause

##### Parameters:

Name | Type | Description
--- | --- | ---
`key` | String | the field to search
`values` | Array | the array of items to search in

Source:
  - QueryBuilder.js, line 309

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orWhereIsNotNull(field) → {QueryBuilder}

Field is not null prefixed with 'OR'

##### Parameters:

Name | Type | Description
--- | --- | ---
`field` | String | The name of the field

Source:
  - QueryBuilder.js, line 285

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orWhereIsNull(field) → {QueryBuilder}

Field is null prefixed with 'OR'

##### Parameters:

Name | Type | Description
--- | --- | ---
`field` | String | The name of the field

Source:
  - QueryBuilder.js, line 274

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### orWhereNotIn(key, values) → {QueryBuilder}

Set a 'or where not in' clause

##### Parameters:

Name | Type | Description
--- | --- | ---
`key` | String | the field to search
`values` | Array | the array of items to search in

Source:
  - QueryBuilder.js, line 333

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### query(sql, paramsopt) → {Promise}

Run an arbitrary sql query. Run as a prepared statement.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`sql` | string |  | The sql to execute
`params` | Array | <optional> | The query parameters

Source:
  - QueryBuilder.js, line 43

##### Returns:

- Promise with result of query

Type
  Promise

#### queryFile(file, separatoropt) → {Promise}

Run a set of queries from a file

##### Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`file` | string |  |  | The path to the sql file
`separator` | string | <optional> | ';' | The character separating each query

Source:
  - QueryBuilder.js, line 23

##### Returns:

- The result of all the queries

Type
  Promise

#### resetQuery() → {void}

Reset the object state for a new query

Source:
  - QueryBuilder.js, line 52

##### Returns:

Type
  void

#### select(fields) → {QueryBuilder}

Specify rows to select in the query

##### Parameters:

Name | Type | Description
--- | --- | ---
`fields` | String | Array | The fields to select from the current table

Source:
  - QueryBuilder.js, line 97

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

##### Examples

    query.select('foo, bar'); // Select multiple fields with a string

    query.select(['foo', 'bar']); // Select multiple fileds with an array

#### set(key, valopt) → {QueryBuilder}

Set values for insertion or updating

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | String | Object |  | The key or object to use
`val` | String | <optional> | The value if using a scalar key

Source:
  - QueryBuilder.js, line 347

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

##### Examples

    query.set('foo', 'bar'); // Set a key, value pair

    query.set({foo:'bar'}); // Set with an object

#### truncate(table) → {void|Promise}

Empties the selected database table

##### Parameters:

Name | Type | Description
--- | --- | ---
`table` | string | the name of the table to truncate

Source:
  - QueryBuilder.js, line 72

##### Returns:

- Returns a promise if no callback is supplied

Type
  void | Promise

#### update(table, dataopt) → {Promise.<Result>}

Run the generated update query

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`table` | String |  | The table to insert into
`data` | Object | <optional> | Data to insert, if not already added with the 'set' method

Source:
  - QueryBuilder.js, line 565

##### Returns:

- Promise containing the result of the query

Type
  Promise.<Result>

#### updateBatch(table, data, updateKey) → {Number}

Creates a batch update sql statement

##### Parameters:

Name | Type | Description
--- | --- | ---
`table` | String | The table to update
`data` | Object | Batch insert data
`updateKey` | String | The field in the table to compare against for updating

Source:
  - QueryBuilder.js, line 582

##### Returns:

Number of rows updated

Type
  Number

#### where(key, valopt) → {QueryBuilder}

Set a 'where' clause

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`key` | String | Object |  | The name of the field and the comparision operator, or an object
`val` | String | Number | <optional> | The value to compare if the value of key is a string

Source:
  - QueryBuilder.js, line 229

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### whereIn(key, values) → {QueryBuilder}

Set a 'where in' clause

##### Parameters:

Name | Type | Description
--- | --- | ---
`key` | String | the field to search
`values` | Array | the array of items to search in

Source:
  - QueryBuilder.js, line 297

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### whereIsNotNull(field) → {QueryBuilder}

Specify that a field IS NOT NULL

##### Parameters:

Name | Type | Description
--- | --- | ---
`field` | String | The name so the field that is not to be null

Source:
  - QueryBuilder.js, line 263

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### whereIsNull(field) → {QueryBuilder}

Select a field that is Null

##### Parameters:

Name | Type | Description
--- | --- | ---
`field` | String | The name of the field that has a NULL value

Source:
  - QueryBuilder.js, line 252

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder

#### whereNotIn(key, values) → {QueryBuilder}

Set a 'where not in' clause

##### Parameters:

Name | Type | Description
--- | --- | ---
`key` | String | the field to search
`values` | Array | the array of items to search in

Source:
  - QueryBuilder.js, line 321

##### Returns:

- The Query Builder object, for chaining

Type
  QueryBuilder