2016-11-22 16:03:46 -05:00
|
|
|
const Helpers = require('./Helpers');
|
2016-09-14 16:50:32 -04:00
|
|
|
const QueryBuilderBase = require('./QueryBuilderBase');
|
2016-02-12 12:50:59 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main object that builds SQL queries.
|
|
|
|
*
|
|
|
|
* @param {Driver} Driver - The syntax driver for the database
|
|
|
|
* @param {Adapter} Adapter - The database module adapter for running queries
|
2016-09-14 16:50:32 -04:00
|
|
|
* @extends QueryBuilderBase
|
2016-02-12 12:50:59 -05:00
|
|
|
*/
|
|
|
|
class QueryBuilder extends QueryBuilderBase {
|
2015-12-03 20:43:42 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ! Miscellaneous Methods
|
|
|
|
// ----------------------------------------------------------------------------
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-11-22 16:03:46 -05:00
|
|
|
/**
|
|
|
|
* Run a set of queries from a file
|
|
|
|
*
|
|
|
|
* @param {string} file - The path to the sql file
|
|
|
|
* @param {string} [separator=';'] - The character separating each query
|
|
|
|
* @return {Promise} - The result of all the queries
|
|
|
|
*/
|
|
|
|
queryFile (file, separator = ';') {
|
|
|
|
return Helpers.readFile(file).then(sqlFile => {
|
|
|
|
const queries = sqlFile.split(separator);
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
queries.forEach(sql => {
|
|
|
|
results.push(this.query(sql));
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all(results);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-12 11:40:21 -05:00
|
|
|
/**
|
2016-03-15 15:37:24 -04:00
|
|
|
* Run an arbitrary sql query. Run as a prepared statement.
|
2016-02-12 11:40:21 -05:00
|
|
|
*
|
|
|
|
* @param {string} sql - The sql to execute
|
2016-11-14 20:24:33 -05:00
|
|
|
* @param {Array} [params] - The query parameters
|
|
|
|
* @return {Promise} - Promise with result of query
|
2016-02-12 11:40:21 -05:00
|
|
|
*/
|
2016-11-14 20:24:33 -05:00
|
|
|
query (sql, params) {
|
2016-11-18 21:59:22 -05:00
|
|
|
return this.adapter.execute(sql, params);
|
2016-02-12 11:40:21 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Reset the object state for a new query
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
resetQuery () {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._resetState();
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Returns the current class state for testing or other purposes
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {Object} - The State object
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
getState () {
|
2015-12-03 20:43:42 -05:00
|
|
|
return this.state;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-03-11 16:32:38 -05:00
|
|
|
/**
|
|
|
|
* Empties the selected database table
|
|
|
|
*
|
|
|
|
* @param {string} table - the name of the table to truncate
|
|
|
|
* @return {void|Promise} - Returns a promise if no callback is supplied
|
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
truncate (table) {
|
|
|
|
return this.query(this.driver.truncate(table));
|
2016-03-11 16:32:38 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Closes the database connection for the current adapter
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
end () {
|
2015-12-03 20:43:42 -05:00
|
|
|
this.adapter.close();
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// ! Query Builder Methods
|
|
|
|
// ------------------------------------------------------------------------
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Specify rows to select in the query
|
|
|
|
*
|
|
|
|
* @param {String|Array} fields - The fields to select from the current table
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.select('foo, bar'); // Select multiple fields with a string
|
|
|
|
* @example query.select(['foo', 'bar']); // Select multiple fileds with an array
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
select (fields) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Split/trim fields by comma
|
|
|
|
fields = (Array.isArray(fields))
|
|
|
|
? fields
|
2016-11-22 16:03:46 -05:00
|
|
|
: fields.split(',').map(Helpers.stringTrim);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Split on 'As'
|
|
|
|
fields.forEach((field, index) => {
|
2016-11-14 21:10:37 -05:00
|
|
|
if (/as/i.test(field)) {
|
2016-11-22 16:03:46 -05:00
|
|
|
fields[index] = field.split(/ as /i).map(Helpers.stringTrim);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
const safeArray = this.driver.quoteIdentifiers(fields);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Join the strings back together
|
|
|
|
safeArray.forEach((field, index) => {
|
2015-12-07 17:03:36 -05:00
|
|
|
if (Array.isArray(field)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
safeArray[index] = safeArray[index].join(' AS ');
|
|
|
|
}
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.selectString += safeArray.join(', ');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Specify the database table to select from
|
|
|
|
*
|
|
|
|
* @param {String} tableName - The table to use for the current query
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.from('tableName');
|
|
|
|
* @example query.from('tableName t'); // Select the table with an alias
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
from (tableName) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Split identifiers on spaces
|
2016-11-22 16:03:46 -05:00
|
|
|
let identArray = tableName.trim().split(' ').map(Helpers.stringTrim);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Quote/prefix identifiers
|
|
|
|
identArray[0] = this.driver.quoteTable(identArray[0]);
|
|
|
|
identArray = this.driver.quoteIdentifiers(identArray);
|
|
|
|
|
|
|
|
// Put it back together
|
|
|
|
this.state.fromString = identArray.join(' ');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Add a 'like/ and like' clause to the query
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field to compare to
|
|
|
|
* @param {String} val - The value to compare to
|
|
|
|
* @param {String} [pos=both] - The placement of the wildcard character(s): before, after, or both
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
like (field, val, pos) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._like(field, val, pos, ' LIKE ', 'AND');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Add a 'not like/ and not like' clause to the query
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field to compare to
|
|
|
|
* @param {String} val - The value to compare to
|
|
|
|
* @param {String} [pos=both] - The placement of the wildcard character(s): before, after, or both
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
notLike (field, val, pos) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._like(field, val, pos, ' NOT LIKE ', 'AND');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Add an 'or like' clause to the query
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field to compare to
|
|
|
|
* @param {String} val - The value to compare to
|
|
|
|
* @param {String} [pos=both] - The placement of the wildcard character(s): before, after, or both
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orLike (field, val, pos) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._like(field, val, pos, ' LIKE ', 'OR');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Add an 'or not like' clause to the query
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field to compare to
|
|
|
|
* @param {String} val - The value to compare to
|
|
|
|
* @param {String} [pos=both] - The placement of the wildcard character(s): before, after, or both
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orNotLike (field, val, pos) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._like(field, val, pos, ' NOT LIKE ', 'OR');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Add a 'having' clause
|
|
|
|
*
|
|
|
|
* @param {String|Object} key - The name of the field and the comparision operator, or an object
|
|
|
|
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
having (key, val = null) {
|
|
|
|
this._having(key, val, 'AND');
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Add an 'or having' clause
|
|
|
|
*
|
|
|
|
* @param {String|Object} key - The name of the field and the comparision operator, or an object
|
|
|
|
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
orHaving (key, val = null) {
|
|
|
|
this._having(key, val, 'OR');
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set a 'where' clause
|
|
|
|
*
|
|
|
|
* @param {String|Object} key - The name of the field and the comparision operator, or an object
|
|
|
|
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
where (key, val) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._where(key, val, 'AND');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set a 'or where' clause
|
|
|
|
*
|
|
|
|
* @param {String|Object} key - The name of the field and the comparision operator, or an object
|
|
|
|
* @param {String|Number} [val] - The value to compare if the value of key is a string
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orWhere (key, val) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._where(key, val, 'OR');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Select a field that is Null
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field that has a NULL value
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
whereIsNull (field) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._whereNull(field, 'IS NULL', 'AND');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Specify that a field IS NOT NULL
|
|
|
|
*
|
|
|
|
* @param {String} field - The name so the field that is not to be null
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
whereIsNotNull (field) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._whereNull(field, 'IS NOT NULL', 'AND');
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Field is null prefixed with 'OR'
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orWhereIsNull (field) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._whereNull(field, 'IS NULL', 'OR');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Field is not null prefixed with 'OR'
|
|
|
|
*
|
|
|
|
* @param {String} field - The name of the field
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orWhereIsNotNull (field) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._whereNull(field, 'IS NOT NULL', 'OR');
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set a 'where in' clause
|
|
|
|
*
|
|
|
|
* @param {String} key - the field to search
|
2016-01-26 20:23:52 -05:00
|
|
|
* @param {Array} values - the array of items to search in
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
whereIn (key, values) {
|
2016-01-26 20:23:52 -05:00
|
|
|
this._whereIn(key, values, 'IN', 'AND');
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set a 'or where in' clause
|
|
|
|
*
|
|
|
|
* @param {String} key - the field to search
|
2016-01-26 20:23:52 -05:00
|
|
|
* @param {Array} values - the array of items to search in
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orWhereIn (key, values) {
|
2016-01-26 20:23:52 -05:00
|
|
|
this._whereIn(key, values, 'IN', 'OR');
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set a 'where not in' clause
|
|
|
|
*
|
|
|
|
* @param {String} key - the field to search
|
2016-01-26 20:23:52 -05:00
|
|
|
* @param {Array} values - the array of items to search in
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
whereNotIn (key, values) {
|
2016-01-26 20:23:52 -05:00
|
|
|
this._whereIn(key, values, 'NOT IN', 'AND');
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set a 'or where not in' clause
|
|
|
|
*
|
|
|
|
* @param {String} key - the field to search
|
2016-01-26 20:23:52 -05:00
|
|
|
* @param {Array} values - the array of items to search in
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orWhereNotIn (key, values) {
|
2016-01-26 20:23:52 -05:00
|
|
|
this._whereIn(key, values, 'NOT IN', 'OR');
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Set values for insertion or updating
|
|
|
|
*
|
|
|
|
* @param {String|Object} key - The key or object to use
|
|
|
|
* @param {String} [val] - The value if using a scalar key
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.set('foo', 'bar'); // Set a key, value pair
|
|
|
|
* @example query.set({foo:'bar'}); // Set with an object
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
set (key, val) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Set the appropriate state variables
|
2016-11-14 21:10:37 -05:00
|
|
|
this._mixedSet('setArrayKeys', 'key', key, val);
|
|
|
|
this._mixedSet('values', 'value', key, val);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Use the keys of the array to make the insert/update string
|
|
|
|
// and escape the field names
|
|
|
|
this.state.setArrayKeys = this.state.setArrayKeys.map(this.driver._quote);
|
|
|
|
|
|
|
|
// Generate the "set" string
|
|
|
|
this.state.setString = this.state.setArrayKeys.join('=?,');
|
|
|
|
this.state.setString += '=?';
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a join clause to the query
|
|
|
|
*
|
|
|
|
* @param {String} table - The table you are joining
|
|
|
|
* @param {String} cond - The join condition.
|
|
|
|
* @param {String} [type='inner'] - The type of join, which defaults to inner
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
join (table, cond, type) {
|
2015-12-07 17:03:36 -05:00
|
|
|
type = type || 'inner';
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Prefix/quote table name
|
2016-11-22 16:03:46 -05:00
|
|
|
table = table.split(' ').map(Helpers.stringTrim);
|
2015-12-03 20:43:42 -05:00
|
|
|
table[0] = this.driver.quoteTable(table[0]);
|
|
|
|
table = table.map(this.driver.quoteIdentifiers);
|
|
|
|
table = table.join(' ');
|
|
|
|
|
|
|
|
// Parse out the join condition
|
2018-02-09 17:29:26 -05:00
|
|
|
const parsedCondition = this.parser.compileJoin(cond);
|
|
|
|
const condition = `${table} ON ${parsedCondition}`;
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Append the join condition to the query map
|
2015-12-07 15:58:31 -05:00
|
|
|
this._appendMap(`\n${type.toUpperCase()} JOIN `, condition, 'join');
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Group the results by the selected field(s)
|
|
|
|
*
|
|
|
|
* @param {String|Array} field - The name of the field to group by
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
groupBy (field) {
|
2016-11-22 16:03:46 -05:00
|
|
|
if (!Helpers.isScalar(field)) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const newGroupArray = field.map(this.driver.quoteIdentifiers);
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.groupArray = this.state.groupArray.concat(newGroupArray);
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.groupArray.push(this.driver.quoteIdentifiers(field));
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
this.state.groupString = ` GROUP BY ${this.state.groupArray.join(',')}`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Order the results by the selected field(s)
|
|
|
|
*
|
|
|
|
* @param {String} field - The field(s) to order by
|
|
|
|
* @param {String} [type='ASC'] - The order direction, ASC or DESC
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orderBy (field, type) {
|
2015-12-03 20:43:42 -05:00
|
|
|
type = type || 'ASC';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Set the fields for later manipulation
|
|
|
|
field = this.driver.quoteIdentifiers(field);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.orderArray[field] = type;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
const orderClauses = [];
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Flatten key/val pairs into an array of space-separated pairs
|
|
|
|
Object.keys(this.state.orderArray).forEach(key => {
|
2015-12-07 15:58:31 -05:00
|
|
|
orderClauses.push(`${key} ${this.state.orderArray[key].toUpperCase()}`);
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Set the final string
|
2015-12-07 15:58:31 -05:00
|
|
|
this.state.orderString = ` ORDER BY ${orderClauses.join(', ')}`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Put a limit on the query
|
|
|
|
*
|
|
|
|
* @param {Number} limit - The maximum number of rows to fetch
|
|
|
|
* @param {Number} [offset] - The row number to start from
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
limit (limit, offset) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.limit = limit;
|
|
|
|
this.state.offset = offset || null;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Adds an open paren to the current query for logical grouping
|
|
|
|
*
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
groupStart () {
|
2018-02-09 17:29:26 -05:00
|
|
|
const conj = (this.state.queryMap.length < 1) ? ' WHERE ' : ' AND ';
|
2015-12-03 20:43:42 -05:00
|
|
|
this._appendMap(conj, '(', 'groupStart');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Adds an open paren to the current query for logical grouping,
|
|
|
|
* prefixed with 'OR'
|
|
|
|
*
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orGroupStart () {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._appendMap('', ' OR (', 'groupStart');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Adds an open paren to the current query for logical grouping,
|
|
|
|
* prefixed with 'OR NOT'
|
|
|
|
*
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
orNotGroupStart () {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._appendMap('', ' OR NOT (', 'groupStart');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Ends a logical grouping started with one of the groupStart methods
|
|
|
|
*
|
|
|
|
* @return {QueryBuilder} - The Query Builder object, for chaining
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
groupEnd () {
|
2015-12-03 20:43:42 -05:00
|
|
|
this._appendMap('', ')', 'groupEnd');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// ! Result Methods
|
|
|
|
// ------------------------------------------------------------------------
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Get the results of the compiled query
|
|
|
|
*
|
|
|
|
* @param {String} [table] - The table to select from
|
|
|
|
* @param {Number} [limit] - A limit for the query
|
|
|
|
* @param {Number} [offset] - An offset for the query
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.get('table_name').then(promiseCallback); // Get all the rows in the table
|
2016-11-14 21:10:37 -05:00
|
|
|
* @example query.get('table_name', 5); // Get 5 rows from the table
|
|
|
|
* @example query.get(); // Get the results of a query generated with other methods
|
2016-11-21 19:48:00 -05:00
|
|
|
* @return {Promise<Result>} - Promise containing the result of the query
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
get (table, limit, offset) {
|
|
|
|
if (table) {
|
|
|
|
this.from(table);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2016-11-14 21:10:37 -05:00
|
|
|
if (limit) {
|
|
|
|
this.limit(limit, offset);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Run the query
|
2016-11-14 21:10:37 -05:00
|
|
|
return this._run('get', table);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Run the generated insert query
|
|
|
|
*
|
|
|
|
* @param {String} table - The table to insert into
|
|
|
|
* @param {Object} [data] - Data to insert, if not already added with the 'set' method
|
2016-11-21 19:48:00 -05:00
|
|
|
* @return {Promise<Result>} - Promise containing the result of the query
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
insert (table, data) {
|
|
|
|
if (data) {
|
|
|
|
this.set(data);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query
|
2016-11-14 21:10:37 -05:00
|
|
|
return this._run('insert', this.driver.quoteTable(table));
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Insert multiple sets of rows at a time
|
|
|
|
*
|
|
|
|
* @param {String} table - The table to insert into
|
|
|
|
* @param {Array} data - The array of objects containing data rows to insert
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}])
|
|
|
|
*.then(promiseCallback);
|
2016-11-21 19:48:00 -05:00
|
|
|
* @return {Promise<Result>} - Promise containing the result of the query
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
insertBatch (table, data) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const batch = this.driver.insertBatch(table, data);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Run the query
|
2016-11-14 21:10:37 -05:00
|
|
|
return this.query(batch.sql, batch.values);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Run the generated update query
|
|
|
|
*
|
|
|
|
* @param {String} table - The table to insert into
|
|
|
|
* @param {Object} [data] - Data to insert, if not already added with the 'set' method
|
2016-11-21 19:48:00 -05:00
|
|
|
* @return {Promise<Result>} - Promise containing the result of the query
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
update (table, data) {
|
|
|
|
if (data) {
|
|
|
|
this.set(data);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query
|
2016-11-14 21:10:37 -05:00
|
|
|
return this._run('update', this.driver.quoteTable(table));
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
/**
|
|
|
|
* Creates a batch update sql statement
|
|
|
|
*
|
|
|
|
* @param {String} table - The table to update
|
|
|
|
* @param {Object} data - Batch insert data
|
|
|
|
* @param {String} updateKey - The field in the table to compare against for updating
|
|
|
|
* @return {Number} Number of rows updated
|
|
|
|
*/
|
|
|
|
updateBatch (table, data, updateKey) {
|
|
|
|
const [sql, insertData, affectedRows] = this.driver.updateBatch(table, data, updateKey);
|
|
|
|
this._run('', table, sql, insertData);
|
|
|
|
return affectedRows;
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Run the generated delete query
|
|
|
|
*
|
|
|
|
* @param {String} table - The table to insert into
|
|
|
|
* @param {Object} [where] - Where clause for delete statement
|
2016-11-21 19:48:00 -05:00
|
|
|
* @return {Promise<Result>} - Promise containing the result of the query
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
delete (table, where) {
|
|
|
|
if (where) {
|
|
|
|
this.where(where);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query
|
2016-11-14 21:10:37 -05:00
|
|
|
return this._run('delete', this.driver.quoteTable(table));
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// ! Methods returning SQL
|
|
|
|
// ------------------------------------------------------------------------
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return generated select query SQL
|
|
|
|
*
|
|
|
|
* @param {String} [table] - the name of the table to retrieve from
|
|
|
|
* @param {Boolean} [reset=true] - Whether to reset the query builder so another query can be built
|
|
|
|
* @return {String} - The compiled sql statement
|
|
|
|
*/
|
2016-11-14 21:10:37 -05:00
|
|
|
getCompiledSelect (table, reset = true) {
|
|
|
|
if (table) {
|
|
|
|
this.from(table);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
|
2016-11-14 21:10:37 -05:00
|
|
|
return this._getCompile('get', table, reset);
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return generated insert query SQL
|
|
|
|
*
|
|
|
|
* @param {String} table - the name of the table to insert into
|
|
|
|
* @param {Boolean} [reset=true] - Whether to reset the query builder so another query can be built
|
|
|
|
* @return {String} - The compiled sql statement
|
|
|
|
*/
|
2018-02-09 17:29:26 -05:00
|
|
|
getCompiledInsert (table, reset = true) {
|
2015-12-03 20:43:42 -05:00
|
|
|
return this._getCompile('insert', this.driver.quoteTable(table), reset);
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return generated update query SQL
|
|
|
|
*
|
|
|
|
* @param {String} table - the name of the table to update
|
|
|
|
* @param {Boolean} [reset=true] - Whether to reset the query builder so another query can be built
|
|
|
|
* @return {String} - The compiled sql statement
|
|
|
|
*/
|
2018-02-09 17:29:26 -05:00
|
|
|
getCompiledUpdate (table, reset = true) {
|
2015-12-03 20:43:42 -05:00
|
|
|
return this._getCompile('update', this.driver.quoteTable(table), reset);
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return generated delete query SQL
|
|
|
|
*
|
|
|
|
* @param {String} table - the name of the table to delete from
|
|
|
|
* @param {Boolean} [reset=true] - Whether to reset the query builder so another query can be built
|
|
|
|
* @return {String} - The compiled sql statement
|
|
|
|
*/
|
2018-02-09 17:29:26 -05:00
|
|
|
getCompiledDelete (table, reset = true) {
|
2015-12-03 20:43:42 -05:00
|
|
|
return this._getCompile('delete', this.driver.quoteTable(table), reset);
|
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = QueryBuilder;
|