24 KiB
limit
Set the limit clause
Parameters
sql
String SQL statement to modifylimit
Number Maximum number of rows to fetchoffset
[Number] Number of rows to skip
Returns String Modified SQL statement
quoteTable
Quote database table name, and set prefix
Parameters
table
String Table name to quote
Returns String Quoted table name
quoteIdentifiers
Use the driver's escape character to quote identifiers
Parameters
Returns (String | Array) Quoted identifier(s)
truncate
Generate SQL to truncate the passed table
Parameters
table
String Table to truncate
Returns String Truncation SQL
insertBatch
Generate SQL to insert a group of rows
Parameters
Returns String Query and data to insert
NodeQuery
Class for connection management
Parameters
config
object connection parameters
constructor
Constructor
Parameters
config
object connection parameters
Examples
let nodeQuery = require('ci-node-query')({
driver: 'mysql',
connection: {
host: 'localhost',
user: 'root',
password: '',
database: 'mysql'
}
});
let nodeQuery = require('ci-node-query')({
driver: 'sqlite',
connection: ':memory:'
});
getQuery
Return an existing query builder instance
Returns QueryBuilder The Query Builder object
QueryBuilder
Extends QueryBuilderBase
Main object that builds SQL queries.
Parameters
Driver
Driver The syntax driver for the databaseAdapter
Adapter The database module adapter for running queries
query
Run an arbitrary sql query. Run as a prepared statement.
Parameters
sql
string The sql to executeparams
[array] The query parameterscallback
[function] Optional callback
Returns (void | Promise) Returns a promise if no callback is supplied
resetQuery
Reset the object state for a new query
Returns void
truncate
Empties the selected database table
Parameters
Returns (void | Promise) Returns a promise if no callback is supplied
end
Closes the database connection for the current adapter
Returns void
select
Specify rows to select in the query
Parameters
Examples
query.select('foo, bar'); // Select multiple fields with a string
query.select(['foo', 'bar']); // Select multiple fileds with an array
Returns QueryBuilder The Query Builder object, for chaining
from
Specify the database table to select from
Parameters
tableName
String The table to use for the current query
Examples
query.from('tableName');
query.from('tableName t'); // Select the table with an alias
Returns QueryBuilder The Query Builder object, for chaining
like
Add a 'like/ and like' clause to the query
Parameters
field
String The name of the field to compare toval
String The value to compare topos
[String] The placement of the wildcard character(s): before, after, or both (optional, defaultboth
)
Returns QueryBuilder The Query Builder object, for chaining
notLike
Add a 'not like/ and not like' clause to the query
Parameters
field
String The name of the field to compare toval
String The value to compare topos
[String] The placement of the wildcard character(s): before, after, or both (optional, defaultboth
)
Returns QueryBuilder The Query Builder object, for chaining
orLike
Add an 'or like' clause to the query
Parameters
field
String The name of the field to compare toval
String The value to compare topos
[String] The placement of the wildcard character(s): before, after, or both (optional, defaultboth
)
Returns QueryBuilder The Query Builder object, for chaining
orNotLike
Add an 'or not like' clause to the query
Parameters
field
String The name of the field to compare toval
String The value to compare topos
[String] The placement of the wildcard character(s): before, after, or both (optional, defaultboth
)
Returns QueryBuilder The Query Builder object, for chaining
having
Add a 'having' clause
Parameters
key
(String | Object) The name of the field and the comparision operator, or an objectval
[(String | Number)] The value to compare if the value of key is a string
Returns QueryBuilder The Query Builder object, for chaining
orHaving
Add an 'or having' clause
Parameters
key
(String | Object) The name of the field and the comparision operator, or an objectval
[(String | Number)] The value to compare if the value of key is a string
Returns QueryBuilder The Query Builder object, for chaining
where
Set a 'where' clause
Parameters
key
(String | Object) The name of the field and the comparision operator, or an objectval
[(String | Number)] The value to compare if the value of key is a string
Returns QueryBuilder The Query Builder object, for chaining
orWhere
Set a 'or where' clause
Parameters
key
(String | Object) The name of the field and the comparision operator, or an objectval
[(String | Number)] The value to compare if the value of key is a string
Returns QueryBuilder The Query Builder object, for chaining
whereIsNull
Select a field that is Null
Parameters
field
String The name of the field that has a NULL value
Returns QueryBuilder The Query Builder object, for chaining
whereIsNotNull
Specify that a field IS NOT NULL
Parameters
field
String The name so the field that is not to be null
Returns QueryBuilder The Query Builder object, for chaining
orWhereIsNull
Field is null prefixed with 'OR'
Parameters
field
String The name of the field
Returns QueryBuilder The Query Builder object, for chaining
orWhereIsNotNull
Field is not null prefixed with 'OR'
Parameters
field
String The name of the field
Returns QueryBuilder The Query Builder object, for chaining
whereIn
Set a 'where in' clause
Parameters
Returns QueryBuilder The Query Builder object, for chaining
orWhereIn
Set a 'or where in' clause
Parameters
Returns QueryBuilder The Query Builder object, for chaining
whereNotIn
Set a 'where not in' clause
Parameters
Returns QueryBuilder The Query Builder object, for chaining
orWhereNotIn
Set a 'or where not in' clause
Parameters
Returns QueryBuilder The Query Builder object, for chaining
set
Set values for insertion or updating
Parameters
Examples
query.set('foo', 'bar'); // Set a key, value pair
query.set({foo:'bar'}); // Set with an object
Returns QueryBuilder The Query Builder object, for chaining
join
Add a join clause to the query
Parameters
table
String The table you are joiningcond
String The join condition.type
[String] The type of join, which defaults to inner (optional, default'inner'
)
Returns QueryBuilder The Query Builder object, for chaining
groupBy
Group the results by the selected field(s)
Parameters
Returns QueryBuilder The Query Builder object, for chaining
orderBy
Order the results by the selected field(s)
Parameters
field
String The field(s) to order bytype
[String] The order direction, ASC or DESC (optional, default'ASC'
)
Returns QueryBuilder The Query Builder object, for chaining
limit
Put a limit on the query
Parameters
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
orGroupStart
Adds an open paren to the current query for logical grouping, prefixed with 'OR'
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
groupEnd
Ends a logical grouping started with one of the groupStart methods
Returns QueryBuilder The Query Builder object, for chaining
get
Get the results of the compiled query
Parameters
table
[String] The table to select fromlimit
[Number] A limit for the queryoffset
[Number] An offset for the querycallback
[Function] A callback for receiving the result
Examples
query.get('table_name').then(promiseCallback); // Get all the rows in the table
query.get('table_name', 5, callback); // Get 5 rows from the table
query.get(callback); // Get the results of a query generated with other methods
Returns (void | Promise) If no callback is passed, a promise is returned
insert
Run the generated insert query
Parameters
table
String The table to insert intodata
[Object] Data to insert, if not already added with the 'set' methodcallback
[Function] Callback for handling response from the database
Returns (void | Promise) If no callback is passed, a promise is returned
insertBatch
Insert multiple sets of rows at a time
Parameters
table
String The table to insert intodata
Array The array of objects containing data rows to insertcallback
[Function] Callback for handling database response
Examples
query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}], callbackFunction);
query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}])
.then(promiseCallback);
Returns (void | Promise) If no callback is passed, a promise is returned
update
Run the generated update query
Parameters
table
String The table to insert intodata
[Object] Data to insert, if not already added with the 'set' methodcallback
[Function] Callback for handling response from the database
Returns (void | Promise) If no callback is passed, a promise is returned
delete
Run the generated delete query
Parameters
table
String The table to insert intowhere
[Object] Where clause for delete statementcallback
[Function] Callback for handling response from the database
Returns (void | Promise) If no callback is passed, a promise is returned
getCompiledSelect
Return generated select query SQL
Parameters
table
[String] the name of the table to retrieve fromreset
[Boolean] Whether to reset the query builder so another query can be built (optional, defaulttrue
)
Returns String The compiled sql statement
getCompiledInsert
Return generated insert query SQL
Parameters
table
String the name of the table to insert intoreset
[Boolean] Whether to reset the query builder so another query can be built (optional, defaulttrue
)
Returns String The compiled sql statement
getCompiledUpdate
Return generated update query SQL
Parameters
table
String the name of the table to updatereset
[Boolean] Whether to reset the query builder so another query can be built (optional, defaulttrue
)
Returns String The compiled sql statement
getCompiledDelete
Return generated delete query SQL
Parameters
table
String the name of the table to delete fromreset
[Boolean] Whether to reset the query builder so another query can be built (optional, defaulttrue
)
Returns String The compiled sql statement
Result
Query result object
Parameters
rowCount
Get the number of rows returned by the query
Returns Number the number of rows in the result
columnCount
Get the number of columns returned by the query
Returns Number the number of columns in the result