2017-02-28 16:11:34 -05:00
|
|
|
const Helpers = require('./Helpers');
|
2016-09-14 16:50:32 -04:00
|
|
|
const QueryParser = require('./QueryParser');
|
|
|
|
const State = require('./State');
|
|
|
|
|
|
|
|
class 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) {
|
|
|
|
this.driver = Driver;
|
|
|
|
this.adapter = Adapter;
|
|
|
|
this.parser = new QueryParser(this.driver);
|
|
|
|
this.state = new State();
|
|
|
|
}
|
|
|
|
|
|
|
|
_like (field, val, pos, like, conj) {
|
|
|
|
field = this.driver.quoteIdentifiers(field);
|
|
|
|
|
|
|
|
like = `${field} ${like} ?`;
|
|
|
|
|
|
|
|
if (pos === 'before') {
|
|
|
|
val = `%${val}`;
|
|
|
|
} else if (pos === 'after') {
|
|
|
|
val = `${val}%`;
|
|
|
|
} else {
|
|
|
|
val = `%${val}%`;
|
|
|
|
}
|
|
|
|
|
|
|
|
conj = (this.state.queryMap.length < 1) ? ' WHERE ' : ` ${conj} `;
|
|
|
|
this._appendMap(conj, like, 'like');
|
|
|
|
|
|
|
|
this.state.whereValues.push(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
string: string
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle key/value pairs in an object the same way as individual arguments,
|
|
|
|
* when appending to state
|
|
|
|
*
|
|
|
|
* @private
|
2018-02-02 11:50:29 -05:00
|
|
|
* @param {mixed} letName Lorem Ipsum
|
|
|
|
* @param {mixed} valType Lorem Ipsum
|
|
|
|
* @param {mixed} key Lorem Ipsum
|
|
|
|
* @param {mixed} val Lorem Ipsum
|
2016-09-14 16:50:32 -04:00
|
|
|
* @return {Array} - modified state array
|
|
|
|
*/
|
2016-11-14 21:22:29 -05:00
|
|
|
_mixedSet (letName, valType, key, val) {
|
2016-09-14 16:50:32 -04:00
|
|
|
let obj = {};
|
|
|
|
|
2017-02-28 16:11:34 -05:00
|
|
|
if (Helpers.isScalar(key) && !Helpers.isUndefined(val)) {
|
2016-09-14 16:50:32 -04:00
|
|
|
// Convert key/val pair to a simple object
|
2016-11-14 21:22:29 -05:00
|
|
|
obj[key] = val;
|
2017-02-28 16:11:34 -05:00
|
|
|
} else if (Helpers.isScalar(key) && Helpers.isUndefined(val)) {
|
2016-09-14 16:50:32 -04:00
|
|
|
// If just a string for the key, and no value, create a simple object with duplicate key/val
|
2016-11-14 21:22:29 -05:00
|
|
|
obj[key] = key;
|
2016-09-14 16:50:32 -04:00
|
|
|
} else {
|
2016-11-14 21:22:29 -05:00
|
|
|
obj = key;
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(obj).forEach(k => {
|
|
|
|
// If a single value for the return
|
2016-11-14 21:22:29 -05:00
|
|
|
if (['key', 'value'].indexOf(valType) !== -1) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const pushVal = (valType === 'key') ? k : obj[k];
|
2016-11-14 21:22:29 -05:00
|
|
|
this.state[letName].push(pushVal);
|
2016-09-14 16:50:32 -04:00
|
|
|
} else {
|
2016-11-14 21:22:29 -05:00
|
|
|
this.state[letName][k] = obj[k];
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
return this.state[letName];
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
_whereMixedSet (key, val) {
|
2016-09-14 16:50:32 -04:00
|
|
|
this.state.whereMap = [];
|
|
|
|
this.state.rawWhereValues = [];
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
this._mixedSet('whereMap', 'both', key, val);
|
|
|
|
this._mixedSet('rawWhereValues', 'value', key, val);
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_fixConjunction (conj) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const lastItem = this.state.queryMap[this.state.queryMap.length - 1];
|
|
|
|
const conjunctionList = Helpers.arrayPluck(this.state.queryMap, 'conjunction');
|
2016-09-14 16:50:32 -04:00
|
|
|
|
2017-02-28 16:11:34 -05:00
|
|
|
if (this.state.queryMap.length === 0 || (!Helpers.regexInArray(conjunctionList, /^ ?WHERE/i))) {
|
2016-09-14 16:50:32 -04:00
|
|
|
conj = ' WHERE ';
|
|
|
|
} else if (lastItem.type === 'groupStart') {
|
|
|
|
conj = '';
|
|
|
|
} else {
|
|
|
|
conj = ` ${conj} `;
|
|
|
|
}
|
|
|
|
|
|
|
|
return conj;
|
|
|
|
}
|
|
|
|
|
|
|
|
_where (key, val, defaultConj) {
|
|
|
|
// Normalize key and value and insert into this.state.whereMap
|
|
|
|
this._whereMixedSet(key, val);
|
|
|
|
|
|
|
|
// Parse the where condition to account for operators,
|
|
|
|
// functions, identifiers, and literal values
|
|
|
|
this.state = this.parser.parseWhere(this.driver, this.state);
|
|
|
|
|
|
|
|
this.state.whereMap.forEach(clause => {
|
2018-02-09 17:29:26 -05:00
|
|
|
const conj = this._fixConjunction(defaultConj);
|
2016-09-14 16:50:32 -04:00
|
|
|
this._appendMap(conj, clause, 'where');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.state.whereMap = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
_whereNull (field, stmt, conj) {
|
|
|
|
field = this.driver.quoteIdentifiers(field);
|
2018-02-09 17:29:26 -05:00
|
|
|
const item = `${field} ${stmt}`;
|
2016-09-14 16:50:32 -04:00
|
|
|
|
|
|
|
this._appendMap(this._fixConjunction(conj), item, 'whereNull');
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:10:37 -05:00
|
|
|
_having (key, val = null, conj = 'AND') {
|
2016-09-14 16:50:32 -04:00
|
|
|
// Normalize key/val and put in state.whereMap
|
2016-11-14 21:10:37 -05:00
|
|
|
this._whereMixedSet(key, val);
|
2016-09-14 16:50:32 -04:00
|
|
|
|
|
|
|
// 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({
|
2016-11-14 21:10:37 -05:00
|
|
|
conjunction: (this.state.havingMap.length > 0) ? ` ${conj} ` : ' HAVING ',
|
2016-09-14 16:50:32 -04:00
|
|
|
string: clause
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Clear the where Map
|
|
|
|
this.state.whereMap = {};
|
|
|
|
}
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
_whereIn (key, val, inClause, conj) {
|
|
|
|
key = this.driver.quoteIdentifiers(key);
|
2018-02-09 17:29:26 -05:00
|
|
|
const params = Array(val.length);
|
2016-09-14 16:50:32 -04:00
|
|
|
params.fill('?');
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
val.forEach(value => {
|
2016-09-14 16:50:32 -04:00
|
|
|
this.state.whereValues.push(value);
|
|
|
|
});
|
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
conj = (this.state.queryMap.length > 0) ? ` ${conj} ` : ' WHERE ';
|
2018-02-09 17:29:26 -05:00
|
|
|
const str = `${key} ${inClause} (${params.join(',')}) `;
|
2016-09-14 16:50:32 -04:00
|
|
|
|
2016-11-14 21:22:29 -05:00
|
|
|
this._appendMap(conj, str, 'whereIn');
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
|
|
|
|
2016-11-14 21:10:37 -05:00
|
|
|
_run (type, table, sql, vals) {
|
2016-09-14 16:50:32 -04:00
|
|
|
if (!sql) {
|
|
|
|
sql = this._compile(type, table);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vals) {
|
|
|
|
vals = this.state.values.concat(this.state.whereValues);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the state so another query can be built
|
|
|
|
this._resetState();
|
|
|
|
|
|
|
|
// Pass the sql and values to the adapter to run on the database
|
2016-11-14 21:10:37 -05:00
|
|
|
return this.query(sql, vals);
|
2016-09-14 16:50:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_getCompile (type, table, reset) {
|
|
|
|
reset = reset || false;
|
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
const sql = this._compile(type, table);
|
2016-09-14 16:50:32 -04:00
|
|
|
|
|
|
|
if (reset) {
|
|
|
|
this._resetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:29:26 -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 => {
|
|
|
|
const param = this.state[clause];
|
|
|
|
|
|
|
|
if (!Helpers.isScalar(param)) {
|
|
|
|
Object.keys(param).forEach(part => {
|
|
|
|
sql += param[part].conjunction + param[part].string;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sql += param;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Append the limit, if it exists
|
|
|
|
if (Helpers.isNumber(this.state.limit)) {
|
|
|
|
sql = this.driver.limit(sql, this.state.limit, this.state.offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
_compileType (type, table) {
|
|
|
|
let sql = '';
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'insert':
|
|
|
|
const params = Array(this.state.setArrayKeys.length).fill('?');
|
|
|
|
|
|
|
|
sql = `INSERT INTO ${table} (`;
|
|
|
|
sql += this.state.setArrayKeys.join(',');
|
|
|
|
sql += `) VALUES (${params.join(',')})`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'update':
|
|
|
|
sql = `UPDATE ${table} SET ${this.state.setString}`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'delete':
|
|
|
|
sql = `DELETE FROM ${table}`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
sql = `SELECT * FROM ${this.state.fromString}`;
|
|
|
|
|
|
|
|
// Set the select string
|
|
|
|
if (this.state.selectString.length > 0) {
|
|
|
|
// Replace the star with the selected fields
|
|
|
|
sql = sql.replace('*', this.state.selectString);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
_resetState () {
|
|
|
|
this.state = new State();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = QueryBuilderBase;
|