2014-10-27 10:35:16 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var helpers = require('./helpers');
|
|
|
|
|
|
|
|
var matchPatterns = {
|
|
|
|
'function': /([a-zA-Z0-9_]+\((.*?)\))/i,
|
|
|
|
identifier: /([a-zA-Z0-9_\-]+\.?)+/ig,
|
|
|
|
operator: /\=|AND|&&?|~|\|\|?|\^|\/|>=?|<=?|-|%|OR|\+|NOT|\!=?|<>|XOR/i
|
|
|
|
};
|
|
|
|
|
|
|
|
// Full pattern for determining ordering of the pieces
|
|
|
|
matchPatterns.combined = new RegExp(matchPatterns['function'].source + "+|"
|
|
|
|
+ matchPatterns.identifier.source
|
|
|
|
+ '|(' + matchPatterns.operator.source + ')+', 'ig');
|
|
|
|
|
|
|
|
var filterMatches = function(array) {
|
|
|
|
var output = [];
|
|
|
|
|
|
|
|
// Return non-array matches
|
|
|
|
if (helpers.isScalar(array) || helpers.isNull(array) || helpers.isUndefined(array)) return output;
|
|
|
|
|
|
|
|
array.forEach(function(item) {
|
|
|
|
if ( ! helpers.isUndefined(item))
|
|
|
|
{
|
|
|
|
output.push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {Driver} - The driver object for the database in use
|
|
|
|
* @module query-parser
|
|
|
|
*/
|
|
|
|
var QueryParser = function(driver) {
|
|
|
|
|
|
|
|
// That 'new' keyword is annoying
|
|
|
|
if ( ! (this instanceof QueryParser)) return new QueryParser(driver);
|
|
|
|
|
2014-10-28 14:40:03 -04:00
|
|
|
/**
|
|
|
|
* Tokenize the sql into parts for additional processing
|
|
|
|
*
|
|
|
|
* @param {String} sql
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
this.parseJoin = function(sql) {
|
|
|
|
var matches = {};
|
|
|
|
var output = {};
|
|
|
|
|
|
|
|
// Get clause components
|
|
|
|
matches['function'] = sql.match(matchPatterns['function']);
|
|
|
|
matches.identifiers = sql.match(matchPatterns.identifier);
|
|
|
|
matches.operators = sql.match(matchPatterns.operator);
|
|
|
|
|
|
|
|
// Get everything at once for ordering
|
|
|
|
matches.combined = sql.match(matchPatterns.combined);
|
|
|
|
|
|
|
|
// Flatten the matches to increase relevance
|
|
|
|
Object.keys(matches).forEach(function(key) {
|
|
|
|
output[key] = filterMatches(matches[key]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
};
|
|
|
|
|
2014-10-27 10:35:16 -04:00
|
|
|
/**
|
|
|
|
* Return the output of the parsing of the join condition
|
|
|
|
*
|
|
|
|
* @param {String} condition - The join condition to evalate
|
|
|
|
* @return {String} - The parsed/escaped join condition
|
|
|
|
*/
|
|
|
|
this.compileJoin = function(condition) {
|
2014-10-28 14:40:03 -04:00
|
|
|
var parts = this.parseJoin(condition);
|
2014-10-27 10:35:16 -04:00
|
|
|
var count = parts.identifiers.length;
|
|
|
|
var i;
|
|
|
|
|
|
|
|
// Quote the identifiers
|
|
|
|
parts.combined.forEach(function(part, i) {
|
|
|
|
if (parts.identifiers.indexOf(part) !== -1 && ! helpers.isNumber(part))
|
|
|
|
{
|
|
|
|
parts.combined[i] = driver.quoteIdentifiers(part);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return parts.combined.join('');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = QueryParser;
|