Skip to content

Feat union support #21

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

Merged
merged 2 commits into from
Jul 22, 2019
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
29 changes: 27 additions & 2 deletions src/sqlParser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ SHARE return 'SHARE'
MODE return 'MODE'
OJ return 'OJ'
LIMIT return 'LIMIT'
UNION return 'UNION'

"," return ','
"=" return '='
Expand Down Expand Up @@ -159,8 +160,32 @@ LIMIT return 'LIMIT'
%% /* language grammar */

main
: selectClause EOF { return {nodeType: 'Main', value: $1}; }
| selectClause ';' EOF { return {nodeType: 'Main', value: $1, hasSemicolon: true}; }
: selectClause semicolonOpt EOF { return {nodeType: 'Main', value: $1, hasSemicolon: $2}; }
| unionClause semicolonOpt EOF { return {nodeType: 'Main', value: $1, hasSemicolon: $2}; }
;

semicolonOpt
: ';' { $$ = true }
| { $$ = false }
;

unionClause
: unionClauseNotParenthesized { $$ = $1 }
| unionClauseParenthesized order_by_opt limit_opt { $$ = $1, $$.orderBy = $2, $$.limit = $3; }
;

unionClauseParenthesized
: selectClauseParenthesized UNION distinctOpt selectClauseParenthesized { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 }; }
| selectClauseParenthesized UNION distinctOpt unionClauseParenthesized { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 }; }
;

selectClauseParenthesized
: '(' selectClause ')' { $$ = { type: 'SelectParenthesized', value: $2 }; }
;

unionClauseNotParenthesized
: selectClause UNION distinctOpt selectClause { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 } }
| selectClause UNION distinctOpt unionClauseNotParenthesized { $$ = { type: 'Union', left: $1, distinctOpt: $3, right: $4 } }
;

selectClause
Expand Down
19 changes: 17 additions & 2 deletions src/stringify.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { timingSafeEqual } from "crypto";

if (!sqlParser) {
sqlParser = {};
}
Expand Down Expand Up @@ -55,13 +57,13 @@ Sql.prototype.append = function(word, noPrefix, noSuffix) {
}
}
Sql.prototype.travelMain = function(ast) {
this.travelSelect(ast.value);
this.travel(ast.value);
if (ast.hasSemicolon) {
this.append(';', true);
}
}
Sql.prototype.travelSelect = function(ast) {
this.appendKeyword('select', true);
this.appendKeyword('select');
if (ast.distinctOpt) {
this.appendKeyword(ast.distinctOpt);
}
Expand Down Expand Up @@ -537,3 +539,16 @@ Sql.prototype.travelTableFactor = function (ast) {
this.travel(ast.indexHintOpt);
}
}
Sql.prototype.travelUnion = function (ast) {
this.travel(ast.left);
this.appendKeyword('UNION');
if (ast.distinctOpt) {
this.appendKeyword(ast.distinctOpt)
}
this.travel(ast.right);
}
Sql.prototype.travelSelectParenthesized = function (ast) {
this.appendKeyword('(');
this.travel(ast.value);
this.appendKeyword(')');
}
23 changes: 20 additions & 3 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ const parser = require('../');

const testParser = function (sql) {
let firstAst = parser.parse(sql);
debug(JSON.stringify(firstAst, null, 2));
let firstSql = parser.stringify(firstAst);
debug(firstSql);
let secondAst = parser.parse(firstSql);
debug(parser.stringify(secondAst));
let secondSql = parser.stringify(secondAst);
debug(JSON.stringify(secondAst, null, 2));

if (firstSql !== secondSql) {
console.log('firstSql', firstSql);
console.log('secondSql', secondSql);
throw 'err firstSql don\'t equals secondSql. ';
}

debug(JSON.stringify(secondAst, null, 2));
debug(parser.stringify(secondAst));

return secondAst;
}

Expand Down Expand Up @@ -360,5 +361,21 @@ describe('select grammar support', function () {
testParser('SELECT COUNT(*) AS total, a b, b as c, c/2 d, d & e an FROM b');
});

it ('union support, https://dev.mysql.com/doc/refman/8.0/en/union.html', function () {
testParser('select a from dual union select a from foo;');
});

it ('union Parenthesized support, https://dev.mysql.com/doc/refman/8.0/en/union.html', function () {
testParser('(select a from dual) union (select a from foo) order by a desc limit 100, 100;');
});

it ('union all support, https://dev.mysql.com/doc/refman/8.0/en/union.html', function () {
testParser('(select a from dual) union all (select a from foo) order by a limit 100');
});

it ('union distinct support, https://dev.mysql.com/doc/refman/8.0/en/union.html', function () {
testParser('select a from dual order by a desc limit 1, 1 union distinct select a from foo order by a limit 1');
});

});