Content-Length: 470540 | pFad | http://github.com/JavaScriptor/js-sql-parser/pull/37/files/cf6db04054810bb4d70303e78c2721fff2b75442

53 fix: fix support for quoted alias by Capo93 · Pull Request #37 · JavaScriptor/js-sql-parser · GitHub
Skip to content

fix: fix support for quoted alias #37

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 3 commits into from
Feb 4, 2021
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
12 changes: 5 additions & 7 deletions src/sqlParser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ UNION return 'UNION'

[a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]* return 'IDENTIFIER'
\. return 'DOT'
['"][a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]*["'] return 'QUOTED_IDENTIFIER'
[`].+[`] return 'QUOTED_IDENTIFIER'
['"][a-zA-Z_\u4e00-\u9fa5][a-zA-Z0-9_\u4e00-\u9fa5]*["'] return 'IDENTIFIER'
([`])(?:(?=(\\?))\2.)*?\1 return 'IDENTIFIER'

<<EOF>> return 'EOF'
. return 'INVALID'
Expand Down Expand Up @@ -280,13 +280,11 @@ selectExprAliasOpt
: { $$ = {alias: null, hasAs: null} }
| AS IDENTIFIER { $$ = {alias: $2, hasAs: true} }
| IDENTIFIER { $$ = {alias: $1, hasAs: false} }
| AS QUOTED_IDENTIFIER { $$ = {alias: $2, hasAs: true} }
| QUOTED_IDENTIFIER { $$ = {alias: $1, hasAs: false} }
| AS STRING { $$ = {alias: $2, hasAs: true} }
| STRING { $$ = {alias: $2, hasAs: false} }
;

string
: QUOTED_IDENTIFIER { $$ = { type: 'String', value: $1 } }
| STRING { $$ = { type: 'String', value: $1 } }
: STRING { $$ = { type: 'String', value: $1 } }
;
number
: NUMERIC { $$ = { type: 'Number', value: $1 } }
Expand Down
71 changes: 41 additions & 30 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const debug = require('debug')('js-sql-parser');
const parser = require('../');

const testParser = function(sql) {
const testParser = function (sql) {
let firstAst = parser.parse(sql);
debug(JSON.stringify(firstAst, null, 2));
let firstSql = parser.stringify(firstAst);
Expand All @@ -22,24 +22,24 @@ const testParser = function(sql) {
return secondAst;
};

describe('select grammar support', function() {
it('test0', function() {
describe('select grammar support', function () {
it('test0', function () {
testParser('select a from b where c > 1 group by d order by e desc;');
});

it('test1', function() {
it('test1', function () {
testParser('select distinct max_statement_time = 1.2 a ');
});

it('test2', function() {
it('test2', function () {
testParser('select all 0x1f');
});

it('test3', function() {
it('test3', function () {
testParser('select distinctrow "xx", a in (1,2)');
});

it('test4', function() {
it('test4', function () {
testParser(`
select
tag_basic.gender as gender,
Expand All @@ -58,17 +58,17 @@ describe('select grammar support', function() {
`);
});

it('test5', function() {
it('test5', function () {
testParser('select function(), function(1, "sd", 0x1F)');
});

it('test6 unicode', function() {
it('test6 unicode', function () {
testParser(`
select in中文 from tags
`);
});

it('test7', function() {
it('test7', function () {
testParser(`
SELECT
DISTINCT high_priority MAX_STATEMENT_TIME=1 STRAIGHT_JOIN SQL_SMALL_RESULT SQL_BIG_RESULT SQL_BUFFER_RESULT SQL_CACHE SQL_CALC_FOUND_ROWS fm_customer.lname AS name1,
Expand All @@ -89,7 +89,7 @@ describe('select grammar support', function() {
`);
});

it('test8', function() {
it('test8', function () {
testParser(`
SELECT P1.PAYMENTNO, P1.AMOUNT,
(P1.AMOUNT * 100) / SUM(P2.AMOUNT)
Expand All @@ -99,7 +99,7 @@ describe('select grammar support', function() {
`);
});

it('test9', function() {
it('test9', function () {
testParser(`
SELECT PLAYERS.PLAYERNO, NAME,
(SELECT COUNT(*)
Expand All @@ -114,7 +114,7 @@ describe('select grammar support', function() {
`);
});

it('test10', function() {
it('test10', function () {
testParser(`
SELECT rd.*, rd.rd_numberofrooms - (
SELECT SUM(rn.reservation_numberofrooms) AS count_reserve_room
Expand Down Expand Up @@ -148,11 +148,11 @@ describe('select grammar support', function() {
`);
});

it('test11 SELECT `LEFT`(a, 3) FROM b support.', function() {
it('test11 SELECT `LEFT`(a, 3) FROM b support.', function () {
testParser('SELECT `LEFT`(a, 3) FROM b');
});

it('test12', function() {
it('test12', function () {
testParser(`
select
a.product_id,
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('select grammar support', function() {
`);
});

it('test13', function() {
it('test13', function () {
testParser(`
SELECT
a.*, f.ORG_NAME DEPT_NAME,
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('select grammar support', function() {
`);
});

it('test14', function() {
it('test14', function () {
testParser(`
SELECT
k.*,
Expand Down Expand Up @@ -345,7 +345,7 @@ describe('select grammar support', function() {
`);
});

it('test15', function() {
it('test15', function () {
testParser(`
SELECT P1.PAYMENTNO, P1.AMOUNT, (P1.AMOUNT * 100) / SUM(P2.AMOUNT)
FROM PENALTIES AS P1, PENALTIES AS P2
Expand All @@ -354,41 +354,41 @@ describe('select grammar support', function() {
`);
});

it('limit support.', function() {
it('limit support.', function () {
testParser('select a from b limit 2, 3');
});

it('fix not equal.', function() {
it('fix not equal.', function () {
testParser('select a from b where a <> 1 limit 2, 3');
});

it('restore semicolon.', function() {
it('restore semicolon.', function () {
testParser('select a from b limit 2;');
});

it('recognoce alias for sql-function calls in stringify function.', function() {
it('recognoce alias for sql-function calls in stringify function.', 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() {
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() {
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() {
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() {
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'
);
});

it('support quoted alias', function() {
it('support quoted alias', function () {
testParser('select a as `A-A` from b limit 2;');
testParser('select a as `A#A` from b limit 2;');
testParser('select a as `A?A` from b limit 2;');
Expand All @@ -398,7 +398,7 @@ describe('select grammar support', function() {
testParser('select a as `A A` from b limit 2;');
});

it('bugfix table alias', function() {
it('bugfix table alias', function () {
testParser(`
SELECT stime, A.names, B.names FROM (
SELECT stime, names FROM iaas_data.iaas_d3c0d0681cc1900
Expand All @@ -408,7 +408,18 @@ describe('select grammar support', function() {
`);
});

it('bugfix table alias2', function() {
it('bugfix table alias2', function () {
testParser('select a.* from a t1 join b t2 on t1.a = t2.a')
})
});

it('support quoted alias: multiple alias and orderby support', function () {
testParser('select a as `A A`, b as `B B` from z');
testParser('select a as `A A` from z order by `A A` desc');
testParser('select a as `A A`, b as `B B` from z group by `A A`, `B B` order by `A A` desc');
});

it('support double quoted alias', function () {
testParser('select a as "A A" from z');
testParser('select a as "A A" from z order by "A A" desc');
});
});








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/JavaScriptor/js-sql-parser/pull/37/files/cf6db04054810bb4d70303e78c2721fff2b75442

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy