From 04f27cba106c976465dbcd2de180bec3dc1ede94 Mon Sep 17 00:00:00 2001 From: Olexandr Shamin Date: Wed, 2 Apr 2014 13:46:00 +0200 Subject: [PATCH 1/5] default JOIN is INNER JOIN --- simpleSqlParser.js | 2 +- tests/tests.js | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/simpleSqlParser.js b/simpleSqlParser.js index 08b60f6..3150867 100644 --- a/simpleSqlParser.js +++ b/simpleSqlParser.js @@ -112,7 +112,7 @@ busy_until = parseInt(key, 10) + item.length; // Replace JOIN by LEFT JOIN - if (item == 'JOIN') parts_order[key] = 'LEFT JOIN'; + if (item == 'JOIN') parts_order[key] = 'INNER JOIN'; } }); diff --git a/tests/tests.js b/tests/tests.js index d28150c..58e2f30 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -448,18 +448,18 @@ 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); From 101f76a009ad071549ccb2f8a6082ac3a676dbfa Mon Sep 17 00:00:00 2001 From: Olexandr Shamin Date: Wed, 2 Apr 2014 13:52:16 +0200 Subject: [PATCH 2/5] added RIGHT JOIN support --- simpleSqlParser.js | 20 +++++++++++++++++--- tests/tests.js | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/simpleSqlParser.js b/simpleSqlParser.js index 3150867..5bb961d 100644 --- a/simpleSqlParser.js +++ b/simpleSqlParser.js @@ -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 + ' '; }); @@ -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 = {}; @@ -312,7 +312,21 @@ } 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) { if (typeof result['WHERE'] == 'string') { diff --git a/tests/tests.js b/tests/tests.js index 58e2f30..c8d6173 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -350,7 +350,7 @@ }); test('parse SQL', function() { - expect(27); + expect(28); var q; q = 'SELECT * FROM table'; @@ -462,6 +462,41 @@ }, ], }, 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), { From 0767d78afa6872348f6880f62a59c383e2c7a356 Mon Sep 17 00:00:00 2001 From: Olexandr Shamin Date: Wed, 2 Apr 2014 17:50:15 +0200 Subject: [PATCH 3/5] default order of ORDER BY is ASC --- simpleSqlParser.js | 17 ++++++++++------- tests/tests.js | 16 +++++++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/simpleSqlParser.js b/simpleSqlParser.js index 5bb961d..b47943c 100644 --- a/simpleSqlParser.js +++ b/simpleSqlParser.js @@ -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; diff --git a/tests/tests.js b/tests/tests.js index c8d6173..d58cc8e 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -350,7 +350,7 @@ }); test('parse SQL', function() { - expect(28); + expect(29); var q; q = 'SELECT * FROM table'; @@ -546,6 +546,20 @@ {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), { From ea736657ddd6ceae6a42dd128c744e41bfb8d0da Mon Sep 17 00:00:00 2001 From: Olexandr Shamin Date: Wed, 2 Apr 2014 20:37:01 +0200 Subject: [PATCH 4/5] fixed incorrect indentation --- simpleSqlParser.js | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/simpleSqlParser.js b/simpleSqlParser.js index b47943c..cbfe407 100644 --- a/simpleSqlParser.js +++ b/simpleSqlParser.js @@ -104,14 +104,14 @@ 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 + // Replace JOIN by INNER JOIN if (item == 'JOIN') parts_order[key] = 'INNER JOIN'; } }); @@ -195,12 +195,12 @@ str = str.split(','); var result = []; str.forEach(function (item, key) { - 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]); + 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"; } @@ -315,21 +315,21 @@ } 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']; - } - + 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) { if (typeof result['WHERE'] == 'string') { From 692a8ac98fa858becebe4dfc0a4a27737cad1ad2 Mon Sep 17 00:00:00 2001 From: Olexandr Shamin Date: Wed, 2 Apr 2014 20:39:32 +0200 Subject: [PATCH 5/5] fixed incorrect indentation in test --- tests/tests.js | 91 +++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 46 deletions(-) diff --git a/tests/tests.js b/tests/tests.js index d58cc8e..b2a0b39 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -464,39 +464,39 @@ }, 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); + 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), { @@ -547,19 +547,18 @@ ], }, 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 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), {