Skip to content

Commit 4038b9e

Browse files
committed
add sqlite cross join support
1 parent 5060891 commit 4038b9e

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

pegjs/sqlite.pegjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'CREATE': true,
1616
'CONTAINS': true,
1717
'COUNT': true,
18+
'CROSS': true,
1819
'CURRENT_DATE': true,
1920
'CURRENT_TIME': true,
2021
'CURRENT_TIMESTAMP': true,
@@ -1649,6 +1650,7 @@ table_base
16491650

16501651
join_op
16511652
= KW_LEFT __ KW_OUTER? __ KW_JOIN { return 'LEFT JOIN'; }
1653+
/ KW_CROSS __ KW_JOIN { return 'CROSS JOIN'; }
16521654
/ (KW_INNER __)? KW_JOIN { return 'INNER JOIN'; }
16531655

16541656
table_name
@@ -2790,6 +2792,7 @@ KW_COLLATE = "COLLATE"i !ident_start { return 'COLLATE'; }
27902792
KW_ON = "ON"i !ident_start
27912793
KW_LEFT = "LEFT"i !ident_start
27922794
KW_INNER = "INNER"i !ident_start
2795+
KW_CROSS = "CROSS"i !ident_start
27932796
KW_JOIN = "JOIN"i !ident_start
27942797
KW_OUTER = "OUTER"i !ident_start
27952798
KW_OVER = "OVER"i !ident_start

test/sqlite.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,4 +300,46 @@ describe('sqlite', () => {
300300
expect(getParsedSql(sql)).to.be.equal(sql)
301301
});
302302
});
303+
304+
it('should support cross join with table aliases', () => {
305+
const sql = 'SELECT * FROM users AS u CROSS JOIN products AS p';
306+
expect(getParsedSql(sql)).to.be.equal(
307+
'SELECT * FROM "users" AS "u" CROSS JOIN "products" AS "p"'
308+
);
309+
});
310+
311+
it('should support cross join with using clause', () => {
312+
const sql = 'SELECT * FROM users CROSS JOIN products USING (id)';
313+
expect(getParsedSql(sql)).to.be.equal(
314+
'SELECT * FROM "users" CROSS JOIN "products" USING (id)'
315+
);
316+
});
317+
318+
it('should support cross join with on clause', () => {
319+
const sql = 'SELECT * FROM t1 CROSS JOIN t2 ON t1.id = t2.id';
320+
expect(getParsedSql(sql)).to.be.equal(
321+
'SELECT * FROM "t1" CROSS JOIN "t2" ON "t1"."id" = "t2"."id"'
322+
);
323+
});
324+
325+
it('should support multiple cross joins', () => {
326+
const sql = 'SELECT * FROM t1 CROSS JOIN t2 CROSS JOIN t3';
327+
expect(getParsedSql(sql)).to.be.equal(
328+
'SELECT * FROM "t1" CROSS JOIN "t2" CROSS JOIN "t3"'
329+
);
330+
});
331+
332+
it('should support cross join with subquery', () => {
333+
const sql = 'SELECT * FROM users CROSS JOIN (SELECT * FROM products WHERE price > 50) AS p';
334+
expect(getParsedSql(sql)).to.be.equal(
335+
'SELECT * FROM "users" CROSS JOIN (SELECT * FROM "products" WHERE "price" > 50) AS "p"'
336+
);
337+
});
338+
339+
it('should support cross join mixed with INNER JOIN', () => {
340+
const sql = 'SELECT * FROM t1 CROSS JOIN t2 INNER JOIN t3 ON t2.id = t3.id';
341+
expect(getParsedSql(sql)).to.be.equal(
342+
'SELECT * FROM "t1" CROSS JOIN "t2" INNER JOIN "t3" ON "t2"."id" = "t3"."id"'
343+
);
344+
});
303345
})

0 commit comments

Comments
 (0)