forked from LiskArchive/json-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0_base.js
More file actions
304 lines (253 loc) · 7.67 KB
/
0_base.js
File metadata and controls
304 lines (253 loc) · 7.67 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
'use strict';
var jsonSql = require('../lib')();
var Builder = require('../lib').Builder;
var expect = require('chai').expect;
describe('Builder', function() {
it('should have fields', function() {
expect(jsonSql).to.be.ok;
expect(jsonSql).to.be.an.instanceof(Builder);
expect(jsonSql.dialect).to.be.ok;
expect(jsonSql._query).to.be.equal('');
expect(jsonSql._values).to.be.eql({});
expect(jsonSql.dialect.blocks).to.be.ok;
expect(jsonSql.dialect.templates).to.be.ok;
expect(jsonSql.dialect.conditions).to.be.ok;
expect(jsonSql.dialect.modifiers).to.be.ok;
expect(jsonSql.dialect.logicalOperators).to.be.ok;
});
it('should throw error with wrong `type` property', function() {
expect(function() {
jsonSql.build({
type: 'wrong'
});
}).to.throw('Unknown template type "wrong"');
});
it('should throw error without `table`, `query` and `select` properties', function() {
expect(function() {
jsonSql.build({});
}).to.throw('Neither `table`, `query`, `select`, `expression` properties ' +
'are not set in `select` clause');
});
it('should throw error with both `table` and `select` properties', function() {
expect(function() {
jsonSql.build({
table: 'users',
select: {table: 'payments'}
});
}).to.throw('Wrong using `table`, `select` properties together in `select` clause');
});
it('should throw error with both `table` and `query` properties', function() {
expect(function() {
jsonSql.build({
table: 'users',
query: {table: 'payments'}
});
}).to.throw('Wrong using `table`, `query` properties together in `select` clause');
});
it('should throw error with both `query` and `select` properties', function() {
expect(function() {
jsonSql.build({
query: {table: 'payments'},
select: {table: 'payments'}
});
}).to.throw('Wrong using `query`, `select` properties together in `select` clause');
});
it('should throw error without `name` property in `with` clause', function() {
expect(function() {
jsonSql.build({
'with': [{
select: {
table: 'payments'
}
}],
table: 'users'
});
}).to.throw('`name` property is not set in `with` clause');
});
it('should throw error without `query` and `select` properties in `with` clause', function() {
expect(function() {
jsonSql.build({
'with': [{
name: 'payments'
}],
table: 'users'
});
}).to.throw('Neither `query`, `select`, `expression` properties are not set in `with` clause');
});
it('should throw error with both `query` and `select` properties in `with` clause', function() {
expect(function() {
jsonSql.build({
'with': [{
name: 'payments',
query: {table: 'table1'},
select: {table: 'table2'}
}],
table: 'users'
});
}).to.throw('Wrong using `query`, `select` properties together in `with` clause');
});
it('should be ok with array in `with` clause', function() {
var result = jsonSql.build({
'with': [{
name: 'payments',
select: {
table: 'payments'
}
}],
table: 'users'
});
expect(result.query).to.be.equal('with "payments" as (select * from "payments") select * from ' +
'"users";');
expect(result.values).to.be.eql({});
});
it('should be ok with object in `with` clause', function() {
var result = jsonSql.build({
'with': {
payments: {
select: {
table: 'payments'
}
}
},
table: 'users'
});
expect(result.query).to.be.equal('with "payments" as (select * from "payments") select * from ' +
'"users";');
expect(result.values).to.be.eql({});
});
it('should create array values with option `namedValues` = false', function() {
jsonSql.configure({
namedValues: false
});
expect(jsonSql._values).to.be.eql([]);
var result = jsonSql.build({
table: 'users',
condition: {name: 'John'}
});
expect(result.query).to.be.equal('select * from "users" where "name" = ${1};');
expect(result.values).to.be.eql(['John']);
});
it('should use prefix `@` for values with option `valuesPrefix` = @', function() {
jsonSql.configure({
valuesPrefix: '@'
});
var result = jsonSql.build({
table: 'users',
condition: {name: 'John'}
});
expect(result.query).to.be.equal('select * from "users" where "name" = @{p1};');
expect(result.values).to.be.eql({p1: 'John'});
});
it('should return prefixed values with method `prefixValues`', function() {
var result = jsonSql.build({
table: 'users',
condition: {name: 'John'}
});
expect(result.query).to.be.equal('select * from "users" where "name" = @{p1};');
expect(result.values).to.be.eql({p1: 'John'});
expect(result.prefixValues()).to.be.eql({'@{p1}': 'John'});
});
it('should return array values with method `getValuesArray`', function() {
var result = jsonSql.build({
table: 'users',
condition: {name: 'John'}
});
expect(result.query).to.be.equal('select * from "users" where "name" = @{p1};');
expect(result.values).to.be.eql({p1: 'John'});
expect(result.getValuesArray()).to.be.eql(['John']);
});
it('should return object values with method `getValuesObject`', function() {
jsonSql.configure({
valuesPrefix: '$',
namedValues: false
});
expect(jsonSql._values).to.be.eql([]);
var result = jsonSql.build({
table: 'users',
condition: {name: 'John'}
});
expect(result.query).to.be.equal('select * from "users" where "name" = ${1};');
expect(result.values).to.be.eql(['John']);
expect(result.prefixValues()).to.be.eql({'${1}': 'John'});
expect(result.getValuesObject()).to.be.eql({1: 'John'});
});
it('should create query without values with option `separatedValues` = false', function() {
jsonSql.configure({
separatedValues: false
});
expect(jsonSql._values).to.not.be.ok;
expect(jsonSql._placeholderId).to.not.be.ok;
var result = jsonSql.build({
type: 'insert',
table: 'users',
values: {name: 'John', surname: 'Doe'}
});
expect(result.query).to.be.equal('insert into "users" ("name", "surname") values ' +
'(\'John\', \'Doe\');');
expect(result.values).to.not.be.ok;
});
it('should create query without wrapping identifiers with option `wrappedIdentifiers` = false',
function() {
jsonSql.configure({
wrappedIdentifiers: false
});
var result = jsonSql.build({
type: 'insert',
table: 'users',
values: {name: 'John'}
});
expect(result.query).to.be.equal('insert into users (name) values (${p1});');
}
);
it('shouldn\'t wrap identifiers that already wrapped', function() {
jsonSql.configure({
wrappedIdentifiers: true
});
var result = jsonSql.build({
type: 'insert',
table: '"users"',
values: {
'"name"': 'John',
'"users"."age"': 22
}
});
expect(result.query).to.be.equal('insert into "users" ("name", "users"."age") values (${p1}, 22);');
});
it('shouldn\'t split identifiers by dots inside quotes', function() {
jsonSql.configure({
wrappedIdentifiers: true
});
var result = jsonSql.build({
type: 'insert',
table: '"users"',
values: {
'"users.age"': 22
}
});
expect(result.query).to.be.equal('insert into "users" ("users.age") values (22);');
});
it('shouldn\'t wrap asterisk identifier parts', function() {
jsonSql.configure({
wrappedIdentifiers: true
});
var result = jsonSql.build({
fields: ['users.*'],
table: '"users"'
});
expect(result.query).to.be.equal('select "users".* from "users";');
});
it('should split identifiers by dots and wrap each part', function() {
jsonSql.configure({
wrappedIdentifiers: true
});
var result = jsonSql.build({
type: 'insert',
table: '"users"',
values: {
'name': 'John',
'users.age': 22
}
});
expect(result.query).to.be.equal('insert into "users" ("name", "users"."age") values (${p1}, 22);');
});
});