Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default behaviour of JOIN and ORDER BY #8

Merged
merged 5 commits into from
Apr 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions simpleSqlParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
query = query.replace(new RegExp(semi_colon, 'g'), ';');

// Define which words can act as separator
var keywords = ['SELECT', 'FROM', 'DELETE FROM', 'INSERT INTO', 'UPDATE', 'JOIN', 'LEFT JOIN', 'INNER JOIN', 'ORDER BY', 'GROUP BY', 'HAVING', 'WHERE', 'LIMIT', 'VALUES', 'SET'];
var keywords = ['SELECT', 'FROM', 'DELETE FROM', 'INSERT INTO', 'UPDATE', 'JOIN', 'LEFT JOIN', 'RIGHT JOIN', 'INNER JOIN', 'ORDER BY', 'GROUP BY', 'HAVING', 'WHERE', 'LIMIT', 'VALUES', 'SET'];
var parts_name = keywords.map(function (item) {
return item + ' ';
});
Expand Down Expand Up @@ -104,15 +104,15 @@
while (part != -1);
});

// Delete duplicates (caused, for example, by JOIN and LEFT JOIN)
// Delete duplicates (caused, for example, by JOIN and INNER JOIN)
var busy_until = 0;
parts_order.forEach(function (item, key) {
if (busy_until > key) delete parts_order[key];
else {
busy_until = parseInt(key, 10) + item.length;

// Replace JOIN by LEFT JOIN
if (item == 'JOIN') parts_order[key] = 'LEFT JOIN';
// Replace JOIN by INNER JOIN
if (item == 'JOIN') parts_order[key] = 'INNER JOIN';
}
});

Expand Down Expand Up @@ -176,7 +176,7 @@
return result;
};

analysis['LEFT JOIN'] = analysis['JOIN'] = analysis['INNER JOIN'] = function (str) {
analysis['LEFT JOIN'] = analysis['JOIN'] = analysis['INNER JOIN'] = analysis['RIGHT JOIN'] = function (str) {
str = str.split(' ON ');
var table = str[0].split(' AS ');
var result = {};
Expand All @@ -195,13 +195,16 @@
str = str.split(',');
var result = [];
str.forEach(function (item, key) {
var order_by = /([A-Za-z0-9_\.]+)\s+(ASC|DESC){1}/gi;
order_by = order_by.exec(item);
if (order_by !== null) {
var tmp = {};
tmp['column'] = trim(order_by[1]);
tmp['order'] = trim(order_by[2]);
result.push(tmp);
var order_by = /([A-Za-z0-9_\.]+)\s*(ASC|DESC){0,1}/gi;
order_by = order_by.exec(item);
if (order_by !== null) {
var tmp = {};
tmp['column'] = trim(order_by[1]);
tmp['order'] = trim(order_by[2]);
if(order_by[2] === undefined ){
tmp['order']="ASC";
}
result.push(tmp);
}
});
return result;
Expand Down Expand Up @@ -312,6 +315,20 @@
}
delete result['INNER JOIN'];
}
if (typeof result['RIGHT JOIN'] != 'undefined') {
if (typeof result['JOIN'] == 'undefined') result['JOIN'] = [];
if (typeof result['RIGHT JOIN'][0] != 'undefined') {
result['RIGHT JOIN'].forEach(function (item) {
item.type = 'right';
result['JOIN'].push(item);
});
}
else {
result['RIGHT JOIN'].type = 'right';
result['JOIN'].push(result['RIGHT JOIN']);
}
delete result['RIGHT JOIN'];
}

// Parse conditions
if (parseCond) {
Expand Down
62 changes: 55 additions & 7 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
});

test('parse SQL', function() {
expect(27);
expect(29);
var q;

q = 'SELECT * FROM table';
Expand Down Expand Up @@ -448,20 +448,55 @@
as: '',
cond: {left: 'table.id', operator: '=', right: 'table2.id_table'},
},
{
type: 'left',
table: 'table3',
as: '',
cond: {left: 'table.id', operator: '=', right: 'table3.id_table'},
},
{
type: 'inner',
table: 'table4',
as: 't4',
cond: {left: 'table.id', operator: '=', right: 'FUNCTION(table4.id_table, "string()")'},
},
{
type: 'inner',
table: 'table3',
as: '',
cond: {left: 'table.id', operator: '=', right: 'table3.id_table'},
},
],
}, q);

q = 'SELECT * FROM table LEFT JOIN table10 ON table.id = table10.id RIGHT JOIN table2 ON table.id = table2.id INNER JOIN table3 AS t3 ON table.id = FUNCTION(table4.id_table, "string()") JOIN table4 ON table.id=table4.id';
deepEqual(m.sql2ast(q), {
'SELECT': [{name: '*'}],
'FROM': [{
table: 'table',
as: '',
}],
'JOIN': [
{
type: 'left',
table: 'table10',
as: '',
cond: {left: 'table.id', operator: '=', right: 'table10.id'},
},
{
type: 'inner',
table: 'table3',
as: 't3',
cond: {left: 'table.id', operator: '=', right: 'FUNCTION(table4.id_table, "string()")'},
},
{
type: 'inner',
table: 'table4',
as: '',
cond: {left: 'table.id', operator: '=', right: 'table4.id'},
},
{
type: 'right',
table: 'table2',
as: '',
cond: {left: 'table.id', operator: '=', right: 'table2.id'},
},
],
}, q);

q = 'SELECT * FROM table LEFT JOIN table2 AS t2 ON table.id = t2.id_table';
deepEqual(m.sql2ast(q), {
Expand Down Expand Up @@ -511,6 +546,19 @@
{column: 'column2', order: 'DESC'},
],
}, q);

q = 'SELECT * FROM table ORDER BY column1, column2';
deepEqual(m.sql2ast(q), {
'SELECT': [{name: '*'}],
'FROM': [{
table: 'table',
as: '',
}],
'ORDER BY': [
{column: 'column1', order: 'ASC'},
{column: 'column2', order: 'ASC'},
],
}, q);

q = 'SELECT * FROM table GROUP BY column1, column2';
deepEqual(m.sql2ast(q), {
Expand Down