Skip to content

Commit ec760eb

Browse files
committed
Fixed join_source
1 parent 83a8a3e commit ec760eb

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed

sql.pegjs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -292,43 +292,42 @@ select_result =
292292
{ return r[1] }
293293

294294
join_source =
295-
s: ( single_source ( join_op single_source join_constraint )* )
296-
{ var acc = [s[0]];
297-
var cdr = s[1];
295+
s: ( whitespace single_source
296+
( join_op whitespace single_source join_constraint )* )
297+
{ var acc = [s[1]];
298+
var cdr = s[2];
298299
for (var i = 0; cdr != null && i < cdr.length; i++) {
299-
acc[i] = merge(merge(cdr[i][0], cdr[i][1]), cdr[i][2]);
300+
acc[acc.length] = merge(merge(cdr[i][0], cdr[i][2]), cdr[i][3]);
300301
}
301302
return acc;
302303
}
303304

304305
single_source =
305-
r: ( whitespace
306-
( ( s: ( ( t: ( table_ref ( a: ( AS whitespace table_alias )
307-
{ return { alias: a[2] } } )? )
308-
{ return merge(t[1], t[0]) } )
309-
( ( idx: ( INDEXED BY whitespace index_name )
310-
{ return { indexed_by: idx[3] } } )
311-
/ ( ( NOT INDEXED )
312-
{ return { indexed_by: null } } ) )? )
313-
{ return merge(s[1], s[0]) } )
314-
/ ( p: ( lparen select_stmt rparen
315-
( a: ( AS whitespace table_alias )
316-
{ return { alias: a[2] } } )? )
317-
{ return merge(p[3], p[1]) } )
318-
/ ( j: ( lparen join_source rparen )
319-
{ return j[1] } )
320-
) )
321-
{ return r[1] }
306+
( ( s: ( ( t: ( table_ref ( a: ( AS whitespace table_alias )
307+
{ return { alias: a[2] } } )? )
308+
{ return merge(t[1], t[0]) } )
309+
( ( idx: ( INDEXED BY whitespace index_name )
310+
{ return { indexed_by: idx[3] } } )
311+
/ ( ( NOT INDEXED )
312+
{ return { indexed_by: null } } ) )? )
313+
{ return merge(s[1], s[0]) } )
314+
/ ( p: ( lparen select_stmt rparen
315+
( a: ( AS whitespace table_alias )
316+
{ return { alias: a[2] } } )? )
317+
{ return merge(p[3], p[1]) } )
318+
/ ( j: ( lparen join_source rparen )
319+
{ return j[1] } )
320+
)
322321

323322
join_op =
324-
r: ( ( ( j: ( NATURAL ?
323+
r: ( ( ( ( whitespace comma )
324+
{ return "JOIN" } )
325+
/ ( j: ( NATURAL ?
325326
( ( LEFT ( OUTER )? )
326327
/ INNER
327328
/ CROSS )?
328329
JOIN )
329-
{ return flatstr(j) } )
330-
/ ( ( whitespace comma )
331-
{ return "JOIN" } ) ) )
330+
{ return flatstr(j) } ) ) )
332331
{ return { join_op: r } }
333332

334333
join_constraint =

views/index.erb

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,62 @@ module("PEG", {
2222
}
2323
});
2424

25-
test("basics", 1, function() {
25+
test("basics", 2, function() {
2626
var parser = PEG.buildParser(sql_pegjs);
2727
ok(parser);
2828
var s = parser.parse(
29-
"SELECT a.x AS foo, b.y, x.*, * FROM a, b.a, d, d1.a1 AS b1;");
30-
deepEqual(s, []);
29+
"SELECT a.x AS foo, b.y, x.*, * FROM a.x, b.a, d.p, d1.a1 AS b1;");
30+
deepEqual(s, [
31+
{
32+
"stmt": "select",
33+
"select_cores": [
34+
{
35+
"results": [
36+
{
37+
"table": "a",
38+
"column": "x",
39+
"alias": "foo"
40+
},
41+
{
42+
"table": "b",
43+
"column": "y"
44+
},
45+
{
46+
"table": "x",
47+
"column": "*"
48+
},
49+
{
50+
"column": "*"
51+
}
52+
],
53+
"from": [
54+
{
55+
"database": "a",
56+
"table": "x"
57+
},
58+
{
59+
"join_constraint": null,
60+
"database": "b",
61+
"table": "a",
62+
"join_op": "JOIN"
63+
},
64+
{
65+
"join_constraint": null,
66+
"database": "d",
67+
"table": "p",
68+
"join_op": "JOIN"
69+
},
70+
{
71+
"join_constraint": null,
72+
"database": "d1",
73+
"table": "a1",
74+
"alias": "b1",
75+
"join_op": "JOIN"
76+
}
77+
]
78+
}
79+
]
80+
}]);
3181
});
3282
</script>
3383

0 commit comments

Comments
 (0)