forked from taozhi8833998/node-sql-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.spec.js
More file actions
132 lines (128 loc) · 3.92 KB
/
delete.spec.js
File metadata and controls
132 lines (128 loc) · 3.92 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
120
121
122
123
124
125
126
127
128
129
130
131
132
const { expect } = require('chai');
const Parser = require('../src/parser').default
const { deleteToSQL } = require('../src/delete')
describe('delete', () => {
const parser = new Parser();
function getParsedSql(sql, opt) {
const ast = parser.astify(sql, opt);
return parser.sqlify(ast, opt);
}
it('should parse basic usage', () => {
const { tableList, columnList, ast } = parser.parse('delete from a where id = 1');
expect(tableList).to.eql(['delete::null::a']);
expect(columnList).to.eql(['select::null::id', 'delete::a::(.*)']);
expect(ast.type).to.be.eql('delete');
expect(ast.table).to.be.eql([{
db: null,
table: 'a',
as: null,
addition: true
}])
expect(ast.from).to.eql([{
db: null,
table: 'a',
as: null
}]);
expect(ast.where).to.eql({
type: 'binary_expr',
operator: '=',
left: {
collate: null,
type: 'column_ref',
table: null,
column: 'id',
},
right: {
type: 'number',
value: 1,
}
});
});
it('should parse basic usage', () => {
const { tableList, columnList, ast } = parser.parse('delete from a');
expect(tableList).to.eql(['delete::null::a']);
expect(columnList).to.eql(['delete::a::(.*)']);
expect(ast.type).to.be.eql('delete');
expect(ast.table).to.be.eql([{
db: null,
table: 'a',
as: null,
addition: true
}])
expect(ast.from).to.eql([{
db: null,
table: 'a',
as: null
}]);
});
it('should sqlify delete without table', () => {
expect(deleteToSQL({})).to.equal('DELETE')
})
it('should parse table in delete usage', () => {
const { tableList, columnList, ast } = parser.parse('DELETE t1,t2 from t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25');
expect(tableList).to.eql(['delete::null::t1', 'select::null::t2']);
expect(columnList).to.eql(['select::t1::id', 'select::t2::id', 'delete::t1::(.*)']);
expect(ast.type).to.be.eql('delete');
expect(ast.table).to.eql([
{
db: null,
table: 't1',
as: null
},
{
db: null,
table: 't2',
as: null
}
])
expect(ast.from).to.eql([
{
db: null,
table: 't1',
as: null
},
{
db: null,
table: 't2',
as: null,
join: 'LEFT JOIN',
on: {
type: 'binary_expr',
operator: '=',
left: {
collate: null,
type: 'column_ref',
table: 't1',
column: 'id'
},
right: {
collate: null,
type: 'column_ref',
table: 't2',
column: 'id'
}
}
}
],);
expect(ast.where).to.eql({
type: 'binary_expr',
operator: '=',
left: {
collate: null,
type: 'column_ref',
table: 't1',
column: 'id',
},
right: {
type: 'number',
value: 25,
}
});
});
it('should support order by and limit in delete sql', () => {
expect(getParsedSql('delete from t1 where id = 1 order by id')).to.be.equal('DELETE FROM `t1` WHERE `id` = 1 ORDER BY `id` ASC')
expect(getParsedSql('delete from t1 where id = 1 limit 10')).to.be.equal('DELETE FROM `t1` WHERE `id` = 1 LIMIT 10')
expect(getParsedSql('delete from t1 where id = 1 order by id limit 10')).to.be.equal('DELETE FROM `t1` WHERE `id` = 1 ORDER BY `id` ASC LIMIT 10')
expect(getParsedSql('delete from t1 order by id limit 10')).to.be.equal('DELETE FROM `t1` ORDER BY `id` ASC LIMIT 10')
})
});