2016-02-10 13:09:06 -05:00
|
|
|
const XRegExp = require('xregexp');
|
2017-02-28 16:11:34 -05:00
|
|
|
const Helpers = require('./Helpers');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2016-01-26 19:29:12 -05:00
|
|
|
/**
|
|
|
|
* Internal object for parsing query fragments
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {Driver} driver - The driver object for the database in use
|
|
|
|
*/
|
|
|
|
class QueryParser {
|
2015-12-02 13:01:31 -05:00
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* @constructor
|
|
|
|
*
|
|
|
|
* @param {Driver} driver - The driver object for the database in use
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
constructor (driver) {
|
2015-12-02 13:01:31 -05:00
|
|
|
this.driver = driver;
|
|
|
|
|
2016-02-10 13:09:06 -05:00
|
|
|
const matchPatterns = {
|
2015-12-07 17:03:36 -05:00
|
|
|
function: /([a-z0-9_]+\((.*)\))/i,
|
2016-09-14 16:50:32 -04:00
|
|
|
operator: /!=?|=|\+|&&?|~|\|\|?|\^|\/|<>|>=?|<=?|-|%|OR|AND|NOT|XOR/ig,
|
|
|
|
literal: /([0-9]+)|'(.*?)'|true|false/ig
|
2015-12-02 13:01:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Full pattern for identifiers
|
|
|
|
// Making sure that literals and functions aren't matched
|
2016-02-10 13:09:06 -05:00
|
|
|
matchPatterns.identifier = XRegExp(
|
|
|
|
`(
|
|
|
|
(?!
|
|
|
|
${matchPatterns['function'].source}|
|
|
|
|
${matchPatterns.literal.source}
|
|
|
|
)
|
2018-02-02 11:50:29 -05:00
|
|
|
([a-z_-]+[0-9]*\\.?)
|
2016-02-10 13:09:06 -05:00
|
|
|
)+`, 'igx');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
// Full pattern for determining ordering of the pieces
|
2016-02-10 13:09:06 -05:00
|
|
|
matchPatterns.joinCombined = XRegExp(
|
|
|
|
`${matchPatterns['function'].source}+| # functions
|
|
|
|
${matchPatterns.literal.source}+| # literal values
|
|
|
|
${matchPatterns.identifier.source} # identifiers
|
|
|
|
|(${matchPatterns.operator.source})+`, 'igx');
|
2015-12-02 13:01:31 -05:00
|
|
|
|
|
|
|
this.matchPatterns = matchPatterns;
|
2015-12-07 17:03:36 -05:00
|
|
|
this.identifierBlacklist = ['true', 'false', 'null'];
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-03 20:43:42 -05:00
|
|
|
* Filter matched patterns
|
|
|
|
*
|
|
|
|
* @param {Array} array - Set of possible matches
|
|
|
|
* @return {Array|null} - Filtered set of possible matches
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
filterMatches (array) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const output = [];
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Return non-array matches
|
2017-02-28 16:11:34 -05:00
|
|
|
if (Helpers.isNull(array)) {
|
2015-12-08 10:06:29 -05:00
|
|
|
return null;
|
|
|
|
}
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
array.forEach(item => {
|
|
|
|
output.push(item);
|
|
|
|
});
|
|
|
|
return output;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Check if the string contains an operator, and if so, return the operator(s).
|
|
|
|
* If there are no matches, return null
|
|
|
|
*
|
|
|
|
* @param {String} string - the string to check
|
|
|
|
* @return {Array|null} - List of operators
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
hasOperator (string) {
|
2015-12-03 20:43:42 -05:00
|
|
|
return this.filterMatches(string.match(this.matchPatterns.operator));
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Tokenize the sql into parts for additional processing
|
|
|
|
*
|
|
|
|
* @param {String} sql - Join sql to parse
|
|
|
|
* @return {Object} - Join condition components
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
parseJoin (sql) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const matches = {};
|
|
|
|
const output = {
|
2015-12-03 20:43:42 -05:00
|
|
|
functions: [],
|
|
|
|
identifiers: [],
|
|
|
|
operators: [],
|
2016-09-14 16:50:32 -04:00
|
|
|
literals: []
|
2015-12-03 20:43:42 -05:00
|
|
|
};
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Get clause components
|
|
|
|
matches.functions = sql.match(new RegExp(this.matchPatterns['function'].source, 'ig'));
|
|
|
|
matches.identifiers = sql.match(this.matchPatterns.identifier);
|
|
|
|
matches.operators = sql.match(this.matchPatterns.operator);
|
|
|
|
matches.literals = sql.match(this.matchPatterns.literal);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Get everything at once for ordering
|
|
|
|
matches.combined = sql.match(this.matchPatterns.joinCombined);
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Flatten the matches to increase relevance
|
|
|
|
Object.keys(matches).forEach(key => {
|
|
|
|
output[key] = this.filterMatches(matches[key]);
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return output;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
/**
|
|
|
|
* Return the output of the parsing of the join condition
|
|
|
|
*
|
2016-11-21 19:48:00 -05:00
|
|
|
* @param {String} condition - The join condition to evaluate
|
2015-12-03 20:43:42 -05:00
|
|
|
* @return {String} - The parsed/escaped join condition
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
compileJoin (condition) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const parts = this.parseJoin(condition);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Quote the identifiers
|
|
|
|
parts.combined.forEach((part, i) => {
|
2017-02-28 16:11:34 -05:00
|
|
|
if (parts.identifiers.indexOf(part) !== -1 && !Helpers.isNumber(part)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
parts.combined[i] = this.driver.quoteIdentifiers(part);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return parts.combined.join(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a where clause to separate functions from values
|
|
|
|
*
|
|
|
|
* @param {Driver} driver - The current db driver
|
|
|
|
* @param {State} state - Query Builder state object
|
|
|
|
* @return {String} - The parsed/escaped where condition
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
parseWhere (driver, state) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const whereMap = state.whereMap;
|
2015-12-03 20:43:42 -05:00
|
|
|
let whereValues = state.rawWhereValues;
|
|
|
|
|
2018-02-09 17:29:26 -05:00
|
|
|
const outputMap = [];
|
|
|
|
const outputValues = [];
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
Object.keys(whereMap).forEach(key => {
|
|
|
|
// Combine fields, operators, functions and values into a full clause
|
|
|
|
// to have a common starting flow
|
|
|
|
let fullClause = '';
|
|
|
|
|
|
|
|
// Add an explicit = sign where one is inferred
|
2016-09-14 16:50:32 -04:00
|
|
|
if (!this.hasOperator(key)) {
|
2015-12-07 17:03:36 -05:00
|
|
|
fullClause = `${key} = ${whereMap[key]}`;
|
|
|
|
} else if (whereMap[key] === key) {
|
2015-12-03 20:43:42 -05:00
|
|
|
fullClause = key;
|
2015-12-07 17:03:36 -05:00
|
|
|
} else {
|
|
|
|
fullClause = `${key} ${whereMap[key]}`;
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Separate the clause into separate pieces
|
2018-02-09 17:29:26 -05:00
|
|
|
const parts = this.parseJoin(fullClause);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Filter explicit literals from lists of matches
|
2015-12-07 17:03:36 -05:00
|
|
|
if (whereValues.indexOf(whereMap[key]) !== -1) {
|
2018-02-09 17:29:26 -05:00
|
|
|
const value = whereMap[key];
|
|
|
|
const identIndex = parts.identifiers.indexOf(value);
|
|
|
|
const litIndex = (Helpers.isArray(parts.literals)) ? parts.literals.indexOf(value) : -1;
|
|
|
|
const combIndex = parts.combined.indexOf(value);
|
|
|
|
const funcIndex = (Helpers.isArray(parts.functions)) ? parts.functions.indexOf(value) : -1;
|
|
|
|
let inOutputArray = outputValues.includes(value);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
// Remove the identifier in question,
|
|
|
|
// and add to the output values array
|
2015-12-07 17:03:36 -05:00
|
|
|
if (identIndex !== -1) {
|
2015-12-03 20:43:42 -05:00
|
|
|
parts.identifiers.splice(identIndex, 1);
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
if (!inOutputArray) {
|
2015-12-03 20:43:42 -05:00
|
|
|
outputValues.push(value);
|
|
|
|
inOutputArray = true;
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Remove the value from the literals list
|
|
|
|
// so it is not added twice
|
2015-12-07 17:03:36 -05:00
|
|
|
if (litIndex !== -1) {
|
2015-12-03 20:43:42 -05:00
|
|
|
parts.literals.splice(litIndex, 1);
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
if (!inOutputArray) {
|
2015-12-03 20:43:42 -05:00
|
|
|
outputValues.push(value);
|
|
|
|
inOutputArray = true;
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
// Remove the value from the combined list
|
|
|
|
// and replace it with a placeholder
|
2015-12-07 17:03:36 -05:00
|
|
|
if (combIndex !== -1 && funcIndex === -1) {
|
2015-12-03 20:43:42 -05:00
|
|
|
// Make sure to skip functions when replacing values
|
2015-12-04 09:13:26 -05:00
|
|
|
parts.combined[combIndex] = '?';
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
2015-12-03 20:43:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter false positive identifiers
|
|
|
|
parts.identifiers = parts.identifiers.filter(item => {
|
2018-02-09 17:29:26 -05:00
|
|
|
const isInCombinedMatches = parts.combined.indexOf(item) !== -1;
|
|
|
|
const isNotInBlackList = this.identifierBlacklist.indexOf(item.toLowerCase()) === -1;
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
return isInCombinedMatches && isNotInBlackList;
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
// Quote identifiers
|
2017-02-28 16:11:34 -05:00
|
|
|
if (Helpers.isArray(parts.identifiers)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
parts.identifiers.forEach(ident => {
|
2018-02-09 17:29:26 -05:00
|
|
|
const index = parts.combined.indexOf(ident);
|
2015-12-07 17:03:36 -05:00
|
|
|
if (index !== -1) {
|
2015-12-03 20:43:42 -05:00
|
|
|
parts.combined[index] = driver.quoteIdentifiers(ident);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace each literal with a placeholder in the map
|
|
|
|
// and add the literal to the values,
|
|
|
|
// This should only apply to literal values that are not
|
|
|
|
// explicitly mapped to values, but have to be parsed from
|
|
|
|
// a where condition,
|
2017-02-28 16:11:34 -05:00
|
|
|
if (Helpers.isArray(parts.literals)) {
|
2015-12-03 20:43:42 -05:00
|
|
|
parts.literals.forEach(lit => {
|
2018-02-09 17:29:26 -05:00
|
|
|
const litIndex = parts.combined.indexOf(lit);
|
2015-12-03 20:43:42 -05:00
|
|
|
|
2015-12-07 17:03:36 -05:00
|
|
|
if (litIndex !== -1) {
|
2015-12-08 10:06:29 -05:00
|
|
|
parts.combined[litIndex] = '?';
|
2015-12-03 20:43:42 -05:00
|
|
|
outputValues.push(lit);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
outputMap.push(parts.combined.join(' '));
|
|
|
|
});
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
state.rawWhereValues = [];
|
|
|
|
state.whereValues = state.whereValues.concat(outputValues);
|
|
|
|
state.whereMap = outputMap;
|
2015-12-02 13:01:31 -05:00
|
|
|
|
2015-12-03 20:43:42 -05:00
|
|
|
return state;
|
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = QueryParser;
|