forked from florajs/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcte.js
More file actions
83 lines (68 loc) · 2.34 KB
/
cte.js
File metadata and controls
83 lines (68 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const { expect } = require('chai');
const { Parser } = require('../../');
describe('common table expressions', () => {
const parser = new Parser();
it('should parse single CTE', () => {
const ast = parser.parse(
`
WITH cte AS (SELECT 1)
SELECT * FROM cte
`.trim()
);
expect(ast).to.have.property('with').and.to.be.an('object');
const cte = ast.with;
expect(cte).to.have.property('type', 'with');
expect(cte).to.have.property('value').and.to.be.an('array').and.to.have.lengthOf(1);
const [withListElement] = cte.value;
expect(withListElement).to.have.property('name', 'cte');
expect(withListElement).to.have.property('columns', null);
expect(withListElement).to.have.property('stmt').and.to.be.an('object').and.to.have.property('type', 'select');
});
it('should parse multiple CTEs', () => {
const ast = parser.parse(
`
WITH cte1 AS (SELECT 1), cte2 AS (SELECT 2)
SELECT * FROM cte1 UNION SELECT * FROM cte2
`.trim()
);
expect(ast.with).to.have.property('value').and.to.have.lengthOf(2);
const [cte1, cte2] = ast.with.value;
expect(cte1).to.have.property('name', 'cte1');
expect(cte2).to.have.property('name', 'cte2');
});
it('should parse CTE with column', () => {
const ast = parser.parse(
`
WITH cte (col1) AS (SELECT 1)
SELECT * FROM cte
`.trim()
);
const [cte] = ast.with.value;
expect(cte).to.have.property('columns').and.to.eql(['col1']);
});
it('should parse CTE with multiple columns', () => {
const ast = parser.parse(
`
WITH cte (col1, col2) AS (SELECT 1, 2)
SELECT * FROM cte
`.trim()
);
const [cte] = ast.with.value;
expect(cte.columns).to.eql(['col1', 'col2']);
});
it('should parse recursive CTE', () => {
const ast = parser.parse(
`
WITH RECURSIVE cte(n) AS
(
SELECT 1
UNION
SELECT n + 1 FROM cte WHERE n < 5
)
SELECT * FROM cte
`.trim()
);
expect(ast.with).to.have.property('recursive', true);
});
});