Skip to content

Commit 7f3b853

Browse files
authored
Merge pull request #37 from Capo93/master
fix: fix support for quoted alias
2 parents 315faf7 + cf6db04 commit 7f3b853

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

src/sqlParser.jison

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ UNION return 'UNION'
126126

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

132132
<<EOF>> return 'EOF'
133133
. return 'INVALID'
@@ -280,13 +280,11 @@ selectExprAliasOpt
280280
: { $$ = {alias: null, hasAs: null} }
281281
| AS IDENTIFIER { $$ = {alias: $2, hasAs: true} }
282282
| IDENTIFIER { $$ = {alias: $1, hasAs: false} }
283-
| AS QUOTED_IDENTIFIER { $$ = {alias: $2, hasAs: true} }
284-
| QUOTED_IDENTIFIER { $$ = {alias: $1, hasAs: false} }
283+
| AS STRING { $$ = {alias: $2, hasAs: true} }
284+
| STRING { $$ = {alias: $2, hasAs: false} }
285285
;
286-
287286
string
288-
: QUOTED_IDENTIFIER { $$ = { type: 'String', value: $1 } }
289-
| STRING { $$ = { type: 'String', value: $1 } }
287+
: STRING { $$ = { type: 'String', value: $1 } }
290288
;
291289
number
292290
: NUMERIC { $$ = { type: 'Number', value: $1 } }

test/main.test.js

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const debug = require('debug')('js-sql-parser');
44
const parser = require('../');
55

6-
const testParser = function(sql) {
6+
const testParser = function (sql) {
77
let firstAst = parser.parse(sql);
88
debug(JSON.stringify(firstAst, null, 2));
99
let firstSql = parser.stringify(firstAst);
@@ -22,24 +22,24 @@ const testParser = function(sql) {
2222
return secondAst;
2323
};
2424

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

30-
it('test1', function() {
30+
it('test1', function () {
3131
testParser('select distinct max_statement_time = 1.2 a ');
3232
});
3333

34-
it('test2', function() {
34+
it('test2', function () {
3535
testParser('select all 0x1f');
3636
});
3737

38-
it('test3', function() {
38+
it('test3', function () {
3939
testParser('select distinctrow "xx", a in (1,2)');
4040
});
4141

42-
it('test4', function() {
42+
it('test4', function () {
4343
testParser(`
4444
select
4545
tag_basic.gender as gender,
@@ -58,17 +58,17 @@ describe('select grammar support', function() {
5858
`);
5959
});
6060

61-
it('test5', function() {
61+
it('test5', function () {
6262
testParser('select function(), function(1, "sd", 0x1F)');
6363
});
6464

65-
it('test6 unicode', function() {
65+
it('test6 unicode', function () {
6666
testParser(`
6767
select in中文 from tags
6868
`);
6969
});
7070

71-
it('test7', function() {
71+
it('test7', function () {
7272
testParser(`
7373
SELECT
7474
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,
@@ -89,7 +89,7 @@ describe('select grammar support', function() {
8989
`);
9090
});
9191

92-
it('test8', function() {
92+
it('test8', function () {
9393
testParser(`
9494
SELECT P1.PAYMENTNO, P1.AMOUNT,
9595
(P1.AMOUNT * 100) / SUM(P2.AMOUNT)
@@ -99,7 +99,7 @@ describe('select grammar support', function() {
9999
`);
100100
});
101101

102-
it('test9', function() {
102+
it('test9', function () {
103103
testParser(`
104104
SELECT PLAYERS.PLAYERNO, NAME,
105105
(SELECT COUNT(*)
@@ -114,7 +114,7 @@ describe('select grammar support', function() {
114114
`);
115115
});
116116

117-
it('test10', function() {
117+
it('test10', function () {
118118
testParser(`
119119
SELECT rd.*, rd.rd_numberofrooms - (
120120
SELECT SUM(rn.reservation_numberofrooms) AS count_reserve_room
@@ -148,11 +148,11 @@ describe('select grammar support', function() {
148148
`);
149149
});
150150

151-
it('test11 SELECT `LEFT`(a, 3) FROM b support.', function() {
151+
it('test11 SELECT `LEFT`(a, 3) FROM b support.', function () {
152152
testParser('SELECT `LEFT`(a, 3) FROM b');
153153
});
154154

155-
it('test12', function() {
155+
it('test12', function () {
156156
testParser(`
157157
select
158158
a.product_id,
@@ -202,7 +202,7 @@ describe('select grammar support', function() {
202202
`);
203203
});
204204

205-
it('test13', function() {
205+
it('test13', function () {
206206
testParser(`
207207
SELECT
208208
a.*, f.ORG_NAME DEPT_NAME,
@@ -286,7 +286,7 @@ describe('select grammar support', function() {
286286
`);
287287
});
288288

289-
it('test14', function() {
289+
it('test14', function () {
290290
testParser(`
291291
SELECT
292292
k.*,
@@ -345,7 +345,7 @@ describe('select grammar support', function() {
345345
`);
346346
});
347347

348-
it('test15', function() {
348+
it('test15', function () {
349349
testParser(`
350350
SELECT P1.PAYMENTNO, P1.AMOUNT, (P1.AMOUNT * 100) / SUM(P2.AMOUNT)
351351
FROM PENALTIES AS P1, PENALTIES AS P2
@@ -354,41 +354,41 @@ describe('select grammar support', function() {
354354
`);
355355
});
356356

357-
it('limit support.', function() {
357+
it('limit support.', function () {
358358
testParser('select a from b limit 2, 3');
359359
});
360360

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

365-
it('restore semicolon.', function() {
365+
it('restore semicolon.', function () {
366366
testParser('select a from b limit 2;');
367367
});
368368

369-
it('recognoce alias for sql-function calls in stringify function.', function() {
369+
it('recognoce alias for sql-function calls in stringify function.', function () {
370370
testParser('SELECT COUNT(*) AS total, a b, b as c, c/2 d, d & e an FROM b');
371371
});
372372

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

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

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

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

391-
it('support quoted alias', function() {
391+
it('support quoted alias', function () {
392392
testParser('select a as `A-A` from b limit 2;');
393393
testParser('select a as `A#A` from b limit 2;');
394394
testParser('select a as `A?A` from b limit 2;');
@@ -398,7 +398,7 @@ describe('select grammar support', function() {
398398
testParser('select a as `A A` from b limit 2;');
399399
});
400400

401-
it('bugfix table alias', function() {
401+
it('bugfix table alias', function () {
402402
testParser(`
403403
SELECT stime, A.names, B.names FROM (
404404
SELECT stime, names FROM iaas_data.iaas_d3c0d0681cc1900
@@ -408,7 +408,18 @@ describe('select grammar support', function() {
408408
`);
409409
});
410410

411-
it('bugfix table alias2', function() {
411+
it('bugfix table alias2', function () {
412412
testParser('select a.* from a t1 join b t2 on t1.a = t2.a')
413-
})
413+
});
414+
415+
it('support quoted alias: multiple alias and orderby support', function () {
416+
testParser('select a as `A A`, b as `B B` from z');
417+
testParser('select a as `A A` from z order by `A A` desc');
418+
testParser('select a as `A A`, b as `B B` from z group by `A A`, `B B` order by `A A` desc');
419+
});
420+
421+
it('support double quoted alias', function () {
422+
testParser('select a as "A A" from z');
423+
testParser('select a as "A A" from z order by "A A" desc');
424+
});
414425
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy