simple-sql-parser
Advanced tools
Comparing version 1.1.2 to 1.1.3
{ | ||
"name": "simple-sql-parser", | ||
"description": "Javascript library to parse CRUD (Create Retrieve Update Delete) SQL queries.", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"main": "simpleSqlParser.js", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -218,3 +218,17 @@ (function(exports) { | ||
}; | ||
analysis['GROUP BY'] = function (str) { | ||
str = str.split(','); | ||
var result = []; | ||
str.forEach(function (item, key) { | ||
var group_by = /([A-Za-z0-9_\.]+)/gi; | ||
group_by = group_by.exec(item); | ||
if (group_by !== null) { | ||
var tmp = {}; | ||
tmp['column'] = trim(group_by[1]); | ||
result.push(tmp); | ||
} | ||
}); | ||
return result; | ||
}; | ||
analysis['LIMIT'] = function (str) { | ||
@@ -257,3 +271,3 @@ var limit = /((\d+)\s*,\s*)?(\d+)/gi; | ||
// TODO: handle GROUP BY and HAVING | ||
// TODO: handle HAVING | ||
@@ -603,3 +617,15 @@ // Analyze parts | ||
} | ||
function group_by(ast) { | ||
if (typeof ast['GROUP BY'] != 'undefined') { | ||
var result = ' GROUP BY '; | ||
var groups = ast['GROUP BY'].map(function (item) { | ||
return item.column; | ||
}); | ||
result += groups.join(', '); | ||
return result; | ||
} | ||
else return ''; | ||
} | ||
function limit(ast) { | ||
@@ -649,3 +675,3 @@ if (typeof ast['LIMIT'] != 'undefined' && typeof ast['LIMIT'].nb != 'undefined' && parseInt(ast['LIMIT'].nb, 10) > 0) { | ||
if (typeof ast['SELECT'] != 'undefined' && typeof ast['FROM'] != 'undefined') { | ||
result = select(ast) + from(ast) + join(ast) + where(ast) + order_by(ast) + limit(ast); | ||
result = select(ast) + from(ast) + join(ast) + where(ast) + group_by(ast) + order_by(ast) + limit(ast); | ||
} | ||
@@ -652,0 +678,0 @@ else if (typeof ast['INSERT INTO'] != 'undefined') { |
25252
639