2015-12-03 20:43:42 -05:00
|
|
|
'use strict';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-02-12 12:50:59 -05:00
|
|
|
const getArgs = require('getargs');
|
|
|
|
const helpers = require('./helpers');
|
|
|
|
const State = require('./State');
|
|
|
|
const QueryParser = require('./QueryParser');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2016-02-12 12:50:59 -05:00
|
|
|
class QueryBuilderBase {
|
2016-01-26 19:29:12 -05:00
|
|
|
/**
|
|
|
|
* @private
|
2015-12-03 20:43:42 -05:00
|
|
|
* @constructor
|
2016-01-26 19:29:12 -05:00
|
|
|
* @param {Driver} Driver - The syntax driver for the database
|
|
|
|
* @param {Adapter} Adapter - The database module adapter for running queries
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-01-26 19:29:12 -05:00
|
|
|
constructor(Driver, Adapter) {
|
|
|
|
this.driver = Driver;
|
|
|
|
this.adapter = Adapter;
|
2015-12-03 20:43:42 -05:00
|
|
|
this.parser = new QueryParser(this.driver);
|
|
|
|
this.state = new State();
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Complete the sql building based on the type provided
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {String} type - Type of SQL query
|
|
|
|
* @param {String} table - The table to run the query on
|
|
|
|
* @return {String} - The compiled sql
|
|
|
|
*/
|
|
|
|
_compile(type, table) {
|
|
|
|
// Put together the basic query
|
|
|
|
let sql = this._compileType(type, table);
|
|
|
|
|
|
|
|
// Set each subClause
|
|
|
|
['queryMap', 'groupString', 'orderString', 'havingMap'].forEach(clause => {
|
|
|
|
let param = this.state[clause];
|
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (! helpers.isScalar(param)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
Object.keys(param).forEach(part => {
|
|
|
|
sql += param[part].conjunction + param[part].string;
|
|
|
|
});
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
2015-12-03 20:43:42 -05:00
|
|
|
sql += param;
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Append the limit, if it exists
|
2015-12-07 17:03:36 -05:00
|
|
|
if (helpers.isNumber(this.state.limit)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
sql = this.driver.limit(sql, this.state.limit, this.state.offset);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return sql;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_compileType(type, table) {
|
|
|
|
let sql = '';
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
switch (type) {
|
|
|
|
case 'insert':
|
2015-12-03 20:43:42 -05:00
|
|
|
let params = Array(this.state.setArrayKeys.length).fill('?');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
sql = `INSERT INTO ${table} (`;
|
|
|
|
sql += this.state.setArrayKeys.join(',');
|
2015-12-07 15:58:31 -05:00
|
|
|
sql += `) VALUES (${params.join(',')})`;
|
2015-12-03 20:43:42 -05:00
|
|
|
break;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
case 'update':
|
2015-12-03 20:43:42 -05:00
|
|
|
sql = `UPDATE ${table} SET ${this.state.setString}`;
|
|
|
|
break;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
case 'delete':
|
2015-12-03 20:43:42 -05:00
|
|
|
sql = `DELETE FROM ${table}`;
|
|
|
|
break;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
default:
|
|
|
|
sql = `SELECT * FROM ${this.state.fromString}`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Set the select string
|
2015-12-07 17:03:36 -05:00
|
|
|
if (this.state.selectString.length > 0) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Replace the star with the selected fields
|
|
|
|
sql = sql.replace('*', this.state.selectString);
|
|
|
|
}
|
2015-12-07 17:03:36 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
break;
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return sql;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_like(field, val, pos, like, conj) {
|
|
|
|
field = this.driver.quoteIdentifiers(field);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
like = `${field} ${like} ?`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-08 10:40:52 -05:00
|
|
|
if (pos === 'before') {
|
2015-12-03 20:43:42 -05:00
|
|
|
val = `%${val}`;
|
2015-12-08 10:40:52 -05:00
|
|
|
} else if (pos === 'after') {
|
2015-12-03 20:43:42 -05:00
|
|
|
val = `${val}%`;
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
2015-12-03 20:43:42 -05:00
|
|
|
val = `%${val}%`;
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
conj = (this.state.queryMap.length < 1) ? ' WHERE ' : ` ${conj} `;
|
|
|
|
this._appendMap(conj, like, 'like');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.whereValues.push(val);
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Append a clause to the query map
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {String} conjunction - linking keyword for the clause
|
|
|
|
* @param {String} string - pre-compiled sql fragment
|
|
|
|
* @param {String} type - type of sql clause
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
_appendMap(conjunction, string, type) {
|
|
|
|
this.state.queryMap.push({
|
|
|
|
type: type,
|
|
|
|
conjunction: conjunction,
|
2015-12-07 17:03:36 -05:00
|
|
|
string: string,
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Handle key/value pairs in an object the same way as individual arguments,
|
|
|
|
* when appending to state
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {Array} - modified state array
|
|
|
|
*/
|
|
|
|
_mixedSet(/* $letName, $valType, $key, [$val] */) {
|
2016-03-09 15:05:38 -05:00
|
|
|
const argPattern = '$letName:string, $valType:string, $key:object|string|number, [$val]';
|
|
|
|
let args = getArgs(argPattern, arguments);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
let obj = {};
|
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (helpers.isScalar(args.$key) && !helpers.isUndefined(args.$val)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Convert key/val pair to a simple object
|
|
|
|
obj[args.$key] = args.$val;
|
2015-12-07 17:03:36 -05:00
|
|
|
} else if (helpers.isScalar(args.$key) && helpers.isUndefined(args.$val)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// If just a string for the key, and no value, create a simple object with duplicate key/val
|
|
|
|
obj[args.$key] = args.$key;
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
2015-12-03 20:43:42 -05:00
|
|
|
obj = args.$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(obj).forEach(k => {
|
|
|
|
// If a single value for the return
|
2015-12-07 17:03:36 -05:00
|
|
|
if (['key', 'value'].indexOf(args.$valType) !== -1) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let pushVal = (args.$valType === 'key') ? k : obj[k];
|
|
|
|
this.state[args.$letName].push(pushVal);
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state[args.$letName][k] = obj[k];
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
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 this.state[args.$letName];
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_whereMixedSet(/*key, val*/) {
|
|
|
|
let args = getArgs('key:string|object, [val]', arguments);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.whereMap = [];
|
|
|
|
this.state.rawWhereValues = [];
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this._mixedSet('whereMap', 'both', args.key, args.val);
|
|
|
|
this._mixedSet('rawWhereValues', 'value', args.key, args.val);
|
|
|
|
}
|
|
|
|
|
|
|
|
_fixConjunction(conj) {
|
|
|
|
let lastItem = this.state.queryMap[this.state.queryMap.length - 1];
|
|
|
|
let conjunctionList = helpers.arrayPluck(this.state.queryMap, 'conjunction');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (this.state.queryMap.length === 0 || (! helpers.regexInArray(conjunctionList, /^ ?WHERE/i))) {
|
|
|
|
conj = ' WHERE ';
|
|
|
|
} else if (lastItem.type === 'groupStart') {
|
2015-12-03 20:43:42 -05:00
|
|
|
conj = '';
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
2015-12-07 15:58:31 -05:00
|
|
|
conj = ` ${conj} `;
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return conj;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_where(key, val, defaultConj) {
|
|
|
|
// Normalize key and value and insert into this.state.whereMap
|
|
|
|
this._whereMixedSet(key, val);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Parse the where condition to account for operators,
|
|
|
|
// functions, identifiers, and literal values
|
|
|
|
this.state = this.parser.parseWhere(this.driver, this.state);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this.state.whereMap.forEach(clause => {
|
|
|
|
let conj = this._fixConjunction(defaultConj);
|
|
|
|
this._appendMap(conj, clause, 'where');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.state.whereMap = {};
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_whereNull(field, stmt, conj) {
|
|
|
|
field = this.driver.quoteIdentifiers(field);
|
2015-12-07 15:58:31 -05:00
|
|
|
let item = `${field} ${stmt}`;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this._appendMap(this._fixConjunction(conj), item, 'whereNull');
|
|
|
|
}
|
|
|
|
|
|
|
|
_having(/*key, val, conj*/) {
|
|
|
|
let args = getArgs('key:string|object, [val]:string|number, [conj]:string', arguments);
|
|
|
|
args.conj = args.conj || 'AND';
|
|
|
|
args.val = args.val || null;
|
|
|
|
|
|
|
|
// Normalize key/val and put in state.whereMap
|
|
|
|
this._whereMixedSet(args.key, args.val);
|
|
|
|
|
|
|
|
// Parse the having condition to account for operators,
|
|
|
|
// functions, identifiers, and literal values
|
|
|
|
this.state = this.parser.parseWhere(this.driver, this.state);
|
|
|
|
|
|
|
|
this.state.whereMap.forEach(clause => {
|
|
|
|
// Put in the having map
|
|
|
|
this.state.havingMap.push({
|
|
|
|
conjunction: (this.state.havingMap.length > 0) ? ` ${args.conj} ` : ' HAVING ',
|
2015-12-07 17:03:36 -05:00
|
|
|
string: clause,
|
2015-12-02 13:01:31 -05:00
|
|
|
});
|
2015-12-03 20:43:42 -05:00
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Clear the where Map
|
|
|
|
this.state.whereMap = {};
|
2015-12-07 17:03:36 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-07 17:03:36 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_whereIn(/*key, val, inClause, conj*/) {
|
|
|
|
let args = getArgs('key:string, val:array, inClause:string, conj:string', arguments);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
args.key = this.driver.quoteIdentifiers(args.key);
|
2015-12-07 12:03:42 -05:00
|
|
|
let params = Array(args.val.length);
|
2015-12-03 20:43:42 -05:00
|
|
|
params.fill('?');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
args.val.forEach(value => {
|
|
|
|
this.state.whereValues.push(value);
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 15:58:31 -05:00
|
|
|
args.conj = (this.state.queryMap.length > 0) ? ` ${args.conj} ` : ' WHERE ';
|
|
|
|
let str = `${args.key} ${args.inClause} (${params.join(',')}) `;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
this._appendMap(args.conj, str, 'whereIn');
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_run(type, table, callback, sql, vals) {
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (! sql) {
|
2015-12-03 20:43:42 -05:00
|
|
|
sql = this._compile(type, table);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (! vals) {
|
2015-12-03 20:43:42 -05:00
|
|
|
vals = this.state.values.concat(this.state.whereValues);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Reset the state so another query can be built
|
|
|
|
this._resetState();
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Pass the sql and values to the adapter to run on the database
|
2016-01-26 19:29:12 -05:00
|
|
|
if (callback) {
|
2016-02-12 11:40:21 -05:00
|
|
|
return this.query(sql, vals, callback);
|
2016-01-26 19:29:12 -05:00
|
|
|
} else {
|
2016-02-12 11:40:21 -05:00
|
|
|
return this.query(sql, vals);
|
2016-01-26 19:29:12 -05:00
|
|
|
}
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_getCompile(type, table, reset) {
|
|
|
|
reset = reset || false;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
let sql = this._compile(type, table);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (reset) {
|
|
|
|
this._resetState();
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return sql;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
_resetState() {
|
|
|
|
this.state = new State();
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
class QueryBuilder extends QueryBuilderBase {
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @constructor
|
|
|
|
* @param {Driver} Driver - The syntax driver for the database
|
|
|
|
* @param {Adapter} Adapter - The database module adapter for running queries
|
|
|
|
*/
|
|
|
|
constructor(Driver, Adapter) {
|
|
|
|
super(Driver, Adapter);
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ! Miscellaneous Methods
|
|
|
|
// ----------------------------------------------------------------------------
|
2015-12-02 13:01:31 -05:00
|
|
|
|
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
|
|
|
|
* @param {array} [params] - The query parameters
|
|
|
|
* @param {function} [callback] - Optional callback
|
|
|
|
* @return {void|Promise} - Returns a promise if no callback is supplied
|
|
|
|
*/
|
|
|
|
query(/*sql:string, [params]:array, [callback]:function*/) {
|
|
|
|
return this.adapter.execute.apply(this.adapter, arguments);
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Reset the object state for a new query
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
resetQuery() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
getState() {
|
|
|
|
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
|
|
|
|
* @param {function} [callback] - Optional callback
|
|
|
|
* @return {void|Promise} - Returns a promise if no callback is supplied
|
|
|
|
*/
|
|
|
|
truncate(/*table:string, [callback]:function*/) {
|
|
|
|
getArgs('table:string, [callback]:function', arguments);
|
|
|
|
let args = [].slice.apply(arguments);
|
|
|
|
let sql = this.driver.truncate(args.shift());
|
|
|
|
args.unshift(sql);
|
|
|
|
return this.query.apply(this, args);
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Closes the database connection for the current adapter
|
|
|
|
*
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
end() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
select(fields) {
|
|
|
|
|
|
|
|
// Split/trim fields by comma
|
|
|
|
fields = (Array.isArray(fields))
|
|
|
|
? fields
|
2015-12-07 17:03:36 -05:00
|
|
|
: fields.split(',').map(helpers.stringTrim);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Split on 'As'
|
|
|
|
fields.forEach((field, index) => {
|
2015-12-07 17:03:36 -05:00
|
|
|
if (field.match(/as/i)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
fields[index] = field.split(/ as /i).map(helpers.stringTrim);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let safeArray = this.driver.quoteIdentifiers(fields);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
from(tableName) {
|
|
|
|
// Split identifiers on spaces
|
|
|
|
let identArray = tableName.trim().split(' ').map(helpers.stringTrim);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
like(field, val, pos) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
notLike(field, val, pos) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orLike(field, val, pos) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orNotLike(field, val, pos) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
having(/*key, [val]*/) {
|
|
|
|
let args = getArgs('key:string|object, [val]:string|number', arguments);
|
|
|
|
|
|
|
|
this._having(args.key, args.val, 'AND');
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orHaving(/*key, [val]*/) {
|
|
|
|
let args = getArgs('key:string|object, [val]:string|number', arguments);
|
|
|
|
|
|
|
|
this._having(args.key, args.val, 'OR');
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
where(key, val) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orWhere(key, val) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
whereIsNull(field) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
whereIsNotNull(field) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orWhereIsNull(field) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orWhereIsNotNull(field) {
|
|
|
|
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-01-26 20:23:52 -05:00
|
|
|
whereIn(key, values) {
|
|
|
|
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-01-26 20:23:52 -05:00
|
|
|
orWhereIn(key, values) {
|
|
|
|
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-01-26 20:23:52 -05:00
|
|
|
whereNotIn(key, values) {
|
|
|
|
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-01-26 20:23:52 -05:00
|
|
|
orWhereNotIn(key, values) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
set(/* $key, [$val] */) {
|
|
|
|
let args = getArgs('$key, [$val]', arguments);
|
|
|
|
|
|
|
|
// Set the appropriate state variables
|
|
|
|
this._mixedSet('setArrayKeys', 'key', args.$key, args.$val);
|
|
|
|
this._mixedSet('values', 'value', args.$key, args.$val);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
table = table.split(' ').map(helpers.stringTrim);
|
|
|
|
table[0] = this.driver.quoteTable(table[0]);
|
|
|
|
table = table.map(this.driver.quoteIdentifiers);
|
|
|
|
table = table.join(' ');
|
|
|
|
|
|
|
|
// Parse out the join condition
|
|
|
|
let parsedCondition = this.parser.compileJoin(cond);
|
2015-12-07 15:58:31 -05:00
|
|
|
let 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
|
|
|
|
*/
|
|
|
|
groupBy(field) {
|
2015-12-07 17:03:36 -05:00
|
|
|
if (! helpers.isScalar(field)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
let newGroupArray = field.map(this.driver.quoteIdentifiers);
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orderBy(field, type) {
|
|
|
|
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
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
let 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
|
|
|
|
*/
|
|
|
|
limit(limit, offset) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
groupStart() {
|
|
|
|
let conj = (this.state.queryMap.length < 1) ? ' WHERE ' : ' AND ';
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orGroupStart() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
orNotGroupStart() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
groupEnd() {
|
|
|
|
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 19:29:12 -05:00
|
|
|
* @param {Function} [callback] - A callback for receiving the result
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.get('table_name').then(promiseCallback); // Get all the rows in the table
|
|
|
|
* @example query.get('table_name', 5, callback); // Get 5 rows from the table
|
|
|
|
* @example query.get(callback); // Get the results of a query generated with other methods
|
2016-01-26 19:29:12 -05:00
|
|
|
* @return {void|Promise} - If no callback is passed, a promise is returned
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-01-26 19:29:12 -05:00
|
|
|
get(/* [table], [limit], [offset], [callback] */) {
|
2016-03-09 15:05:38 -05:00
|
|
|
const argPattern = '[table]:string, [limit]:number, [offset]:number, [callback]:function';
|
|
|
|
let args = getArgs(argPattern, arguments);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
if (args.table) {
|
|
|
|
this.from(args.table);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
if (args.limit) {
|
|
|
|
this.limit(args.limit, args.offset);
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Run the query
|
2016-01-26 19:29:12 -05:00
|
|
|
return this._run('get', args.table, args.callback);
|
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-01-26 19:29:12 -05:00
|
|
|
* @param {Function} [callback] - Callback for handling response from the database
|
|
|
|
* @return {void|Promise} - If no callback is passed, a promise is returned
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
|
|
|
insert(/* table, data, callback */) {
|
2016-01-26 19:29:12 -05:00
|
|
|
let args = getArgs('table:string, [data]:object, [callback]:function', arguments);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
if (args.data) {
|
|
|
|
this.set(args.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query
|
2016-01-26 19:29:12 -05:00
|
|
|
return this._run('insert', this.driver.quoteTable(args.table), args.callback);
|
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 19:29:12 -05:00
|
|
|
* @param {Function} [callback] - Callback for handling database response
|
2015-12-03 20:43:42 -05:00
|
|
|
* @example query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}], callbackFunction);
|
2016-01-26 20:23:52 -05:00
|
|
|
* @example query.insertBatch('foo',[{id:1,val:'bar'},{id:2,val:'baz'}])
|
|
|
|
*.then(promiseCallback);
|
2016-01-26 19:29:12 -05:00
|
|
|
* @return {void|Promise} - If no callback is passed, a promise is returned
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
|
|
|
insertBatch(/* table, data, callback */) {
|
2016-01-26 19:29:12 -05:00
|
|
|
let args = getArgs('table:string, data:array, [callback]:function', arguments);
|
2015-12-03 20:43:42 -05:00
|
|
|
let batch = this.driver.insertBatch(args.table, args.data);
|
|
|
|
|
|
|
|
// Run the query
|
2016-01-26 19:29:12 -05:00
|
|
|
return this._run('', '', args.callback, 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-01-26 19:29:12 -05:00
|
|
|
* @param {Function} [callback] - Callback for handling response from the database
|
|
|
|
* @return {void|Promise} - If no callback is passed, a promise is returned
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
|
|
|
update(/*table, data, callback*/) {
|
2016-01-26 19:29:12 -05:00
|
|
|
let args = getArgs('table:string, [data]:object, [callback]:function', arguments);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
if (args.data) {
|
|
|
|
this.set(args.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query
|
2016-01-26 19:29:12 -05:00
|
|
|
return this._run('update', this.driver.quoteTable(args.table), args.callback);
|
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 delete query
|
|
|
|
*
|
|
|
|
* @param {String} table - The table to insert into
|
|
|
|
* @param {Object} [where] - Where clause for delete statement
|
2016-01-26 19:29:12 -05:00
|
|
|
* @param {Function} [callback] - Callback for handling response from the database
|
|
|
|
* @return {void|Promise} - If no callback is passed, a promise is returned
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-03-15 15:37:24 -04:00
|
|
|
delete(/*table, [where], [callback]*/) {
|
2016-01-26 19:29:12 -05:00
|
|
|
let args = getArgs('table:string, [where]:object, [callback]:function', arguments);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (args.where) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this.where(args.where);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the query
|
2016-01-26 19:29:12 -05:00
|
|
|
return this._run('delete', this.driver.quoteTable(args.table), args.callback);
|
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
|
|
|
|
*/
|
|
|
|
getCompiledSelect(/*table, reset*/) {
|
|
|
|
let args = getArgs('[table]:string, [reset]:boolean', arguments);
|
2015-12-07 17:03:36 -05:00
|
|
|
if (args.table) {
|
2015-12-03 20:43:42 -05:00
|
|
|
this.from(args.table);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._getCompile('get', args.table, args.reset);
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
getCompiledInsert(table, reset) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
getCompiledUpdate(table, reset) {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
getCompiledDelete(table, reset) {
|
|
|
|
return this._getCompile('delete', this.driver.quoteTable(table), reset);
|
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = QueryBuilder;
|