Skip to content

Commit c7e494c

Browse files
committed
Support using for joins
1 parent 5ebe6e2 commit c7e494c

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

parser.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ return {
9090
let sql='('+this.toSql(parsed.left);
9191
if(parsed.side) sql+=' '+parsed.side+' ';
9292
sql+='join '+this.toSql(parsed.right);
93-
if(parsed.on) sql+=' on '+this.toSql(parsed.on);
94-
sql+=')';
93+
9594
if(parsed.alias) sql += ' as '+this.toSql(parsed.alias);
95+
96+
if(parsed.using) sql += ' using (' + parsed.on.map(x=>this.toSql(x)).join(',') + ')';
97+
else if(parsed.on) sql+=' on '+this.toSql(parsed.on);
98+
99+
sql+=')';
96100
return sql;
97101
}
98102
case 'table': {

sql.ne

+14-5
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,17 @@ table_ref_commalist ->
115115
| table_ref_commalist _ "," _ table_ref {% d => ({ table_refs: (d[0].table_refs||[]).concat(d[4]) }) %}
116116

117117
@{%
118-
function tableRef(d, onOffset) {
118+
function tableRef(d, onOffset, alias, using) {
119119
if(!onOffset) onOffset = 0;
120120
const ref = {
121121
type: 'table_ref',
122122
side: ((d[1]||[])[1]),
123123
left: d[0],
124124
right: d[4],
125-
on: d[onOffset+8]
125+
on: d[onOffset+8],
126+
using
126127
};
127-
if(onOffset) ref.alias = d[6];
128+
if(alias) ref.alias = d[6];
128129
return ref;
129130
}
130131
%}
@@ -134,8 +135,16 @@ table_ref ->
134135
| table {% d => d[0] %}
135136
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ table __ ON __ expr {% x=>tableRef(x,0) %}
136137
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ table __ ON ("(" _ expr _ ")") {% x=>tableRef(x,0) %}
137-
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ query_spec (AS __ | __) identifier __ ON __ expr {% x=>tableRef(x,2) %}
138-
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ query_spec (AS __ | __) identifier __ ON ("(" _ expr _ ")") {% x=>tableRef(x,2) %}
138+
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ query_spec (AS __ | __) identifier __ ON __ expr {% x=>tableRef(x,2,true) %}
139+
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ query_spec (AS __ | __) identifier __ ON ("(" _ expr _ ")") {% x=>tableRef(x,2,true) %}
140+
141+
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ table __ USING _ "(" _ identifier_comma_list _ ")" {% x=>tableRef(x,2, false,true) %}
142+
| table_ref (__ LEFT __ | __ RIGHT __ | __ INNER __ | __) JOIN __ query_spec (AS __ | __) identifier __ USING _ "(" _ identifier_comma_list _ ")" {% x=>tableRef(x,4, true,true) %}
143+
144+
145+
identifier_comma_list ->
146+
identifier {% d => [d[0]] %}
147+
| identifier_comma_list _ "," _ identifier {% d => d[0].concat(d[2]) %}
139148

140149
table ->
141150
identifier {% d => ({type: 'table', table: d[0].value}) %}

test/test1.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,14 @@ const tests = [
197197
},
198198
{
199199
sql: `select a.x,b.y from a left join (select x,y from c) b on a.x=b.x`,
200-
toSql: '(select `a`.`x`, `b`.`y` from ((`a` left join (select `x`, `y` from (`c`)) on (`a`.`x` = `b`.`x`)) as `b`))',
200+
toSql: '(select `a`.`x`, `b`.`y` from ((`a` left join (select `x`, `y` from (`c`)) as `b` on (`a`.`x` = `b`.`x`))))',
201201
expected: {
202202
sourceTables: ['a','c']
203203
}
204+
},
205+
{
206+
sql: `select a.x, b.y from a left join b using (x)`,
207+
toSql: '(select `a`.`x`, `b`.`y` from ((`a` left join `b` using (`x`))))'
204208
}
205209
];
206210

0 commit comments

Comments
 (0)