forked from florajs/sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn.js
More file actions
119 lines (102 loc) · 3.86 KB
/
column.js
File metadata and controls
119 lines (102 loc) · 3.86 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
const { expect } = require('chai');
const { Parser } = require('../../');
describe('column clause', () => {
const parser = new Parser();
let ast;
it('should parse "*" shorthand', () => {
ast = parser.parse('SELECT * FROM t');
expect(ast.columns).to.equal('*');
});
it('should parse "table.*" column expressions', () => {
ast = parser.parse('SELECT t.* FROM t');
expect(ast.columns).to.eql([{ expr: { type: 'column_ref', table: 't', column: '*' }, as: null }]);
});
it('should parse aliases w/o "AS" keyword', () => {
ast = parser.parse('SELECT a aa FROM t');
expect(ast.columns).to.eql([{ expr: { type: 'column_ref', table: null, column: 'a' }, as: 'aa' }]);
});
it('should parse aliases w/ "AS" keyword', () => {
ast = parser.parse('SELECT b.c as bc FROM t');
expect(ast.columns).to.eql([{ expr: { type: 'column_ref', table: 'b', column: 'c' }, as: 'bc' }]);
});
it('should parse multiple columns', () => {
ast = parser.parse('SELECT b.c as bc, 1+3 FROM t');
expect(ast.columns).to.eql([
{ expr: { type: 'column_ref', table: 'b', column: 'c' }, as: 'bc' },
{
expr: {
type: 'binary_expr',
operator: '+',
left: { type: 'number', value: 1 },
right: { type: 'number', value: 3 }
},
as: null
}
]);
});
describe('aggregate functions', () => {
it('should parse COUNT(*)', () => {
ast = parser.parse('SELECT COUNT(*) FROM t');
expect(ast.columns).to.eql([
{
expr: {
type: 'aggr_func',
name: 'COUNT',
args: {
expr: {
type: 'star',
value: '*'
}
}
},
as: null
}
]);
});
['avg', 'count', 'group_concat', 'max', 'min', 'sum'].forEach((aggrFunction) => {
it(`should parse ${aggrFunction.toUpperCase()} function`, () => {
ast = parser.parse(`SELECT ${aggrFunction}(col) FROM t`);
expect(ast.columns).to.eql([
{
expr: {
type: 'aggr_func',
name: aggrFunction.toUpperCase(),
quantifier: null,
args: {
expr: {
type: 'column_ref',
table: null,
column: 'col'
}
}
},
as: null
}
]);
});
});
['DISTINCT', 'ALL'].forEach((quantifier) => {
it(`should parse ${quantifier} quantifier`, () => {
ast = parser.parse(`SELECT GROUP_CONCAT(${quantifier} col) FROM t`);
expect(ast.columns).to.eql([
{
expr: {
type: 'aggr_func',
name: 'GROUP_CONCAT',
quantifier,
args: {
expr: {
type: 'column_ref',
table: null,
column: 'col'
}
}
},
as: null
}
]);
});
});
});
});