query-core 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/build.ts CHANGED
@@ -1,171 +1,179 @@
1
- import {Attribute, Attributes, Statement, StringMap} from './metadata';
1
+ import { Attribute, Attributes, Statement, StringMap } from "./metadata"
2
2
 
3
3
  // tslint:disable-next-line:class-name
4
4
  export class resource {
5
- static string?: boolean;
6
- static ignoreDatetime?: boolean;
5
+ static string?: boolean
6
+ static ignoreDatetime?: boolean
7
7
  }
8
8
  export function params(length: number, p: (i: number) => string, from?: number): string[] {
9
9
  if (from === undefined || from == null) {
10
- from = 0;
10
+ from = 0
11
11
  }
12
- const ps: string[] = [];
12
+ const ps: string[] = []
13
13
  for (let i = 1; i <= length; i++) {
14
- ps.push(p(i + from));
14
+ ps.push(p(i + from))
15
15
  }
16
- return ps;
16
+ return ps
17
17
  }
18
- export function select<T>(obj: T, table: string, ks: Attribute[], buildParam: (i: number) => string, i?: number): Statement|undefined {
18
+ export function select<T>(obj: T, table: string, ks: Attribute[], buildParam: (i: number) => string, i?: number): Statement | undefined {
19
19
  if (!i) {
20
- i = 1;
20
+ i = 1
21
21
  }
22
22
  if (ks.length === 1) {
23
- const field = (ks[0].column ? ks[0].column : ks[0].name);
24
- if (typeof obj === 'number') {
25
- const query = `select * from ${table} where ${field} = ${obj}`;
26
- return { query, params: [] };
23
+ const field = ks[0].column ? ks[0].column : ks[0].name
24
+ if (typeof obj === "number") {
25
+ const query = `select * from ${table} where ${field} = ${obj}`
26
+ return { query, params: [] }
27
27
  } else {
28
- const query = `select * from ${table} where ${field} = ${buildParam(i)}`;
29
- return { query, params: [obj] };
28
+ const query = `select * from ${table} where ${field} = ${buildParam(i)}`
29
+ return { query, params: [obj] }
30
30
  }
31
31
  } else if (ks.length > 1) {
32
- const cols: string[] = [];
33
- const args: any[] = [];
32
+ const cols: string[] = []
33
+ const args: any[] = []
34
34
  for (const k of ks) {
35
35
  if (k.name) {
36
- const field = (k.column ? k.column : k.name);
37
- cols.push(`${field} = ${buildParam(i++)}`);
38
- args.push((obj as any)[k.name]);
36
+ const field = k.column ? k.column : k.name
37
+ cols.push(`${field} = ${buildParam(i++)}`)
38
+ args.push((obj as any)[k.name])
39
39
  }
40
40
  }
41
- const query = `select * from ${table} where ${cols.join(' and ')}`;
42
- return { query, params: args };
41
+ const query = `select * from ${table} where ${cols.join(" and ")}`
42
+ return { query, params: args }
43
43
  } else {
44
- return undefined;
44
+ return undefined
45
45
  }
46
46
  }
47
- export function exist<T>(obj: T, table: string, ks: Attribute[], buildParam: (i: number) => string, col?: string, i?: number): Statement|undefined {
47
+ export function exist<T>(obj: T, table: string, ks: Attribute[], buildParam: (i: number) => string, col?: string, i?: number): Statement | undefined {
48
48
  if (!i) {
49
- i = 1;
49
+ i = 1
50
50
  }
51
51
  if (!col || col.length === 0) {
52
- col = '*';
52
+ col = "*"
53
53
  }
54
54
  if (ks.length === 1) {
55
- const field = (ks[0].column ? ks[0].column : ks[0].name);
56
- if (typeof obj === 'number') {
57
- const query = `select ${col} from ${table} where ${field} = ${obj}`;
58
- return { query, params: [] };
55
+ const field = ks[0].column ? ks[0].column : ks[0].name
56
+ if (typeof obj === "number") {
57
+ const query = `select ${col} from ${table} where ${field} = ${obj}`
58
+ return { query, params: [] }
59
59
  } else {
60
- const query = `select ${col} from ${table} where ${field} = ${buildParam(i)}`;
61
- return { query, params: [obj] };
60
+ const query = `select ${col} from ${table} where ${field} = ${buildParam(i)}`
61
+ return { query, params: [obj] }
62
62
  }
63
63
  } else if (ks.length > 1) {
64
- const cols: string[] = [];
65
- const args: any[] = [];
64
+ const cols: string[] = []
65
+ const args: any[] = []
66
66
  for (const k of ks) {
67
67
  if (k.name) {
68
- const field = (k.column ? k.column : k.name);
69
- cols.push(`${field} = ${buildParam(i++)}`);
70
- args.push((obj as any)[k.name]);
68
+ const field = k.column ? k.column : k.name
69
+ cols.push(`${field} = ${buildParam(i++)}`)
70
+ args.push((obj as any)[k.name])
71
71
  }
72
72
  }
73
- const query = `select * from ${table} where ${cols.join(' and ')}`;
74
- return { query, params: args };
73
+ const query = `select * from ${table} where ${cols.join(" and ")}`
74
+ return { query, params: args }
75
75
  } else {
76
- return undefined;
76
+ return undefined
77
77
  }
78
78
  }
79
- export function buildToDelete<T>(obj: T, table: string, ks: Attribute[], buildParam: (i: number) => string, i?: number): Statement|undefined {
79
+ export function buildToDelete<T>(obj: T, table: string, ks: Attribute[], buildParam: (i: number) => string, i?: number): Statement | undefined {
80
80
  if (!i) {
81
- i = 1;
81
+ i = 1
82
82
  }
83
83
  if (ks.length === 1) {
84
- const field = (ks[0].column ? ks[0].column : ks[0].name);
85
- if (typeof obj === 'number') {
86
- const query = `delete from ${table} where ${field} = ${obj}`;
87
- return { query, params: [] };
84
+ const field = ks[0].column ? ks[0].column : ks[0].name
85
+ if (typeof obj === "number") {
86
+ const query = `delete from ${table} where ${field} = ${obj}`
87
+ return { query, params: [] }
88
88
  } else {
89
- const query = `delete from ${table} where ${field} = ${buildParam(i)}`;
90
- return { query, params: [obj] };
89
+ const query = `delete from ${table} where ${field} = ${buildParam(i)}`
90
+ return { query, params: [obj] }
91
91
  }
92
92
  } else if (ks.length > 1) {
93
- const cols: string[] = [];
94
- const args: any[] = [];
93
+ const cols: string[] = []
94
+ const args: any[] = []
95
95
  for (const k of ks) {
96
96
  if (k.name) {
97
- const field = (k.column ? k.column : k.name);
98
- cols.push(`${field} = ${buildParam(i++)}`);
99
- args.push((obj as any)[k.name]);
97
+ const field = k.column ? k.column : k.name
98
+ cols.push(`${field} = ${buildParam(i++)}`)
99
+ args.push((obj as any)[k.name])
100
100
  }
101
101
  }
102
- const query = `delete from ${table} where ${cols.join(' and ')}`;
103
- return { query, params: args };
102
+ const query = `delete from ${table} where ${cols.join(" and ")}`
103
+ return { query, params: args }
104
104
  } else {
105
- return undefined;
105
+ return undefined
106
106
  }
107
107
  }
108
- export function insert<T>(exec: (sql: string, args?: any[]) => Promise<number>, obj: T, table: string, attrs: Attributes, buildParam: (i: number) => string, ver?: string, i?: number): Promise<number> {
109
- const stm = buildToInsert(obj, table, attrs, buildParam, ver, i);
108
+ export function insert<T>(
109
+ exec: (sql: string, args?: any[]) => Promise<number>,
110
+ obj: T,
111
+ table: string,
112
+ attrs: Attributes,
113
+ buildParam: (i: number) => string,
114
+ ver?: string,
115
+ i?: number,
116
+ ): Promise<number> {
117
+ const stm = buildToInsert(obj, table, attrs, buildParam, ver, i)
110
118
  if (!stm) {
111
- return Promise.resolve(0);
119
+ return Promise.resolve(0)
112
120
  } else {
113
- return exec(stm.query, stm.params);
121
+ return exec(stm.query, stm.params)
114
122
  }
115
123
  }
116
- export function buildToInsert<T>(obj: T, table: string, attrs: Attributes, buildParam: (i: number) => string, ver?: string, i?: number): Statement|undefined {
124
+ export function buildToInsert<T>(obj: T, table: string, attrs: Attributes, buildParam: (i: number) => string, ver?: string, i?: number): Statement | undefined {
117
125
  if (!i) {
118
- i = 1;
119
- }
120
- const o: any = obj;
121
- const ks = Object.keys(attrs);
122
- const cols: string[] = [];
123
- const values: string[] = [];
124
- const args: any[] = [];
125
- let isVersion = false;
126
+ i = 1
127
+ }
128
+ const o: any = obj
129
+ const ks = Object.keys(attrs)
130
+ const cols: string[] = []
131
+ const values: string[] = []
132
+ const args: any[] = []
133
+ let isVersion = false
126
134
  for (const k of ks) {
127
- let v = o[k];
128
- const attr = attrs[k];
135
+ let v = o[k]
136
+ const attr = attrs[k]
129
137
  if (attr && !attr.ignored && !attr.noinsert) {
130
138
  if (v === undefined || v == null) {
131
- v = attr.default;
139
+ v = attr.default
132
140
  }
133
141
  if (v !== undefined && v != null) {
134
- const field = (attr.column ? attr.column : k);
135
- cols.push(field);
142
+ const field = attr.column ? attr.column : k
143
+ cols.push(field)
136
144
  if (k === ver) {
137
- isVersion = true;
138
- values.push(`${1}`);
145
+ isVersion = true
146
+ values.push(`${1}`)
139
147
  } else {
140
- if (v === '') {
141
- values.push(`''`);
142
- } else if (typeof v === 'number') {
143
- values.push(toString(v));
144
- } else if (typeof v === 'boolean') {
148
+ if (v === "") {
149
+ values.push(`''`)
150
+ } else if (typeof v === "number") {
151
+ values.push(toString(v))
152
+ } else if (typeof v === "boolean") {
145
153
  if (attr.true === undefined) {
146
154
  if (v === true) {
147
- values.push(`true`);
155
+ values.push(`true`)
148
156
  } else {
149
- values.push(`false`);
157
+ values.push(`false`)
150
158
  }
151
159
  } else {
152
- const p = buildParam(i++);
153
- values.push(p);
160
+ const p = buildParam(i++)
161
+ values.push(p)
154
162
  if (v === true) {
155
- const v2 = (attr.true ? attr.true : '1');
156
- args.push(v2);
163
+ const v2 = attr.true ? attr.true : "1"
164
+ args.push(v2)
157
165
  } else {
158
- const v2 = (attr.false ? attr.false : '0');
159
- args.push(v2);
166
+ const v2 = attr.false ? attr.false : "0"
167
+ args.push(v2)
160
168
  }
161
169
  }
162
170
  } else {
163
- if (resource.ignoreDatetime && typeof v === 'string' && attr.type === 'datetime') {
164
- values.push(`'${v}'`);
171
+ if (resource.ignoreDatetime && typeof v === "string" && attr.type === "datetime") {
172
+ values.push(`'${v}'`)
165
173
  } else {
166
- const p = buildParam(i++);
167
- values.push(p);
168
- args.push(v);
174
+ const p = buildParam(i++)
175
+ values.push(p)
176
+ args.push(v)
169
177
  }
170
178
  }
171
179
  }
@@ -173,528 +181,564 @@ export function buildToInsert<T>(obj: T, table: string, attrs: Attributes, build
173
181
  }
174
182
  }
175
183
  if (!isVersion && ver && ver.length > 0) {
176
- const attr = attrs[ver];
177
- const field = (attr.column ? attr.column : ver);
178
- cols.push(field);
179
- values.push(`${1}`);
184
+ const attr = attrs[ver]
185
+ const field = attr.column ? attr.column : ver
186
+ cols.push(field)
187
+ values.push(`${1}`)
180
188
  }
181
189
  if (cols.length === 0) {
182
- return undefined;
190
+ return undefined
183
191
  } else {
184
- const query = `insert into ${table}(${cols.join(',')})values(${values.join(',')})`;
185
- return { query, params: args };
192
+ const query = `insert into ${table}(${cols.join(",")})values(${values.join(",")})`
193
+ return { query, params: args }
186
194
  }
187
195
  }
188
- export function insertBatch<T>(exec: (sql: string, args?: any[]) => Promise<number>, objs: T[], table: string, attrs: Attributes, buildParam: ((i: number) => string)|boolean, ver?: string, i?: number): Promise<number> {
189
- const stm = buildToInsertBatch(objs, table, attrs, buildParam, ver, i);
196
+ export function insertBatch<T>(
197
+ exec: (sql: string, args?: any[]) => Promise<number>,
198
+ objs: T[],
199
+ table: string,
200
+ attrs: Attributes,
201
+ buildParam: ((i: number) => string) | boolean,
202
+ ver?: string,
203
+ i?: number,
204
+ ): Promise<number> {
205
+ const stm = buildToInsertBatch(objs, table, attrs, buildParam, ver, i)
190
206
  if (!stm) {
191
- return Promise.resolve(0);
207
+ return Promise.resolve(0)
192
208
  } else {
193
- return exec(stm.query, stm.params);
209
+ return exec(stm.query, stm.params)
194
210
  }
195
211
  }
196
212
  function buildOracleParam(i: number): string {
197
- return ':' + i;
213
+ return ":" + i
198
214
  }
199
- export function buildToInsertBatch<T>(objs: T[], table: string, attrs: Attributes, buildParam: ((i: number) => string) | boolean, ver?: string, i?: number): Statement|undefined {
215
+ export function buildToInsertBatch<T>(
216
+ objs: T[],
217
+ table: string,
218
+ attrs: Attributes,
219
+ buildParam: ((i: number) => string) | boolean,
220
+ ver?: string,
221
+ i?: number,
222
+ ): Statement | undefined {
200
223
  if (!i) {
201
- i = 1;
224
+ i = 1
202
225
  }
203
- const ks = Object.keys(attrs);
204
- const args: any[] = [];
205
- if (buildParam && typeof buildParam === 'function') {
206
- const cols: string[] = [];
207
- const rows: string[] = [];
226
+ const ks = Object.keys(attrs)
227
+ const args: any[] = []
228
+ if (buildParam && typeof buildParam === "function") {
229
+ const cols: string[] = []
230
+ const rows: string[] = []
208
231
  for (const k of ks) {
209
- const attr = attrs[k];
232
+ const attr = attrs[k]
210
233
  if (attr && !attr.ignored && !attr.noinsert) {
211
- const field = (attr.column ? attr.column : k);
212
- cols.push(field);
234
+ const field = attr.column ? attr.column : k
235
+ cols.push(field)
213
236
  }
214
237
  }
215
238
  for (const obj of objs) {
216
- const values: string[] = [];
239
+ const values: string[] = []
217
240
  for (const k of ks) {
218
- const attr = attrs[k];
241
+ const attr = attrs[k]
219
242
  if (attr && !attr.ignored && !attr.noinsert) {
220
- let v = (obj as any)[k];
243
+ let v = (obj as any)[k]
221
244
  if (v === undefined || v === null) {
222
- v = attr.default;
245
+ v = attr.default
223
246
  }
224
247
  // let x: string;
225
248
  if (attr.version) {
226
- values.push('1');
249
+ values.push("1")
227
250
  } else if (v === undefined || v == null) {
228
- values.push('null');
229
- } else if (v === '') {
230
- values.push(`''`);
231
- } else if (typeof v === 'number') {
232
- values.push(toString(v));
233
- } else if (typeof v === 'boolean') {
251
+ values.push("null")
252
+ } else if (v === "") {
253
+ values.push(`''`)
254
+ } else if (typeof v === "number") {
255
+ values.push(toString(v))
256
+ } else if (typeof v === "boolean") {
234
257
  if (attr.true === undefined) {
235
258
  if (v === true) {
236
- values.push(`true`);
259
+ values.push(`true`)
237
260
  } else {
238
- values.push(`false`);
261
+ values.push(`false`)
239
262
  }
240
263
  } else {
241
- const p = buildParam(i++);
242
- values.push(p);
264
+ const p = buildParam(i++)
265
+ values.push(p)
243
266
  if (v === true) {
244
- const v2 = (attr.true ? attr.true : '1');
245
- args.push(v2);
267
+ const v2 = attr.true ? attr.true : "1"
268
+ args.push(v2)
246
269
  } else {
247
- const v2 = (attr.false ? attr.false : '0');
248
- args.push(v2);
270
+ const v2 = attr.false ? attr.false : "0"
271
+ args.push(v2)
249
272
  }
250
273
  }
251
274
  } else {
252
- if (resource.ignoreDatetime && typeof v === 'string' && attr.type === 'datetime') {
253
- values.push(`'${v}'`);
275
+ if (resource.ignoreDatetime && typeof v === "string" && attr.type === "datetime") {
276
+ values.push(`'${v}'`)
254
277
  } else {
255
- const p = buildParam(i++);
256
- values.push(p);
257
- args.push(v);
278
+ const p = buildParam(i++)
279
+ values.push(p)
280
+ args.push(v)
258
281
  }
259
282
  }
260
283
  }
261
284
  }
262
- rows.push(`(${values.join(',')})`);
285
+ rows.push(`(${values.join(",")})`)
263
286
  }
264
- const query = `insert into ${table}(${cols.join(',')})values ${rows.join(',')}`;
265
- return { query, params: args };
287
+ const query = `insert into ${table}(${cols.join(",")})values ${rows.join(",")}`
288
+ return { query, params: args }
266
289
  } else {
267
- let notSkipInvalid = false;
290
+ let notSkipInvalid = false
268
291
  if (buildParam === true) {
269
- notSkipInvalid = true;
292
+ notSkipInvalid = true
270
293
  }
271
- const rows: string[] = [];
294
+ const rows: string[] = []
272
295
  for (const obj of objs) {
273
- const cols: string[] = [];
274
- const values: string[] = [];
275
- let isVersion = false;
296
+ const cols: string[] = []
297
+ const values: string[] = []
298
+ let isVersion = false
276
299
  for (const k of ks) {
277
- let v = (obj as any)[k];
278
- const attr = attrs[k];
300
+ let v = (obj as any)[k]
301
+ const attr = attrs[k]
279
302
  if (attr && !attr.ignored && !attr.noinsert) {
280
303
  if (v === undefined || v == null) {
281
- v = attr.default;
304
+ v = attr.default
282
305
  }
283
306
  if (v !== undefined && v != null) {
284
- const field = (attr.column ? attr.column : k);
285
- cols.push(field);
307
+ const field = attr.column ? attr.column : k
308
+ cols.push(field)
286
309
  if (k === ver) {
287
- isVersion = true;
288
- values.push(`${1}`);
310
+ isVersion = true
311
+ values.push(`${1}`)
289
312
  } else {
290
- if (v === '') {
291
- values.push(`''`);
292
- } else if (typeof v === 'number') {
293
- values.push(toString(v));
294
- } else if (typeof v === 'boolean') {
313
+ if (v === "") {
314
+ values.push(`''`)
315
+ } else if (typeof v === "number") {
316
+ values.push(toString(v))
317
+ } else if (typeof v === "boolean") {
295
318
  if (attr.true === undefined) {
296
319
  if (v === true) {
297
- values.push(`true`);
320
+ values.push(`true`)
298
321
  } else {
299
- values.push(`false`);
322
+ values.push(`false`)
300
323
  }
301
324
  } else {
302
- const p = buildOracleParam(i++);
303
- values.push(p);
325
+ const p = buildOracleParam(i++)
326
+ values.push(p)
304
327
  if (v === true) {
305
- const v2 = (attr.true ? attr.true : '1');
306
- args.push(v2);
328
+ const v2 = attr.true ? attr.true : "1"
329
+ args.push(v2)
307
330
  } else {
308
- const v2 = (attr.false ? attr.false : '0');
309
- args.push(v2);
331
+ const v2 = attr.false ? attr.false : "0"
332
+ args.push(v2)
310
333
  }
311
334
  }
312
335
  } else {
313
- const p = buildOracleParam(i++);
314
- values.push(p);
315
- args.push(v);
336
+ const p = buildOracleParam(i++)
337
+ values.push(p)
338
+ args.push(v)
316
339
  }
317
340
  }
318
341
  }
319
342
  }
320
343
  }
321
344
  if (!isVersion && ver && ver.length > 0) {
322
- const attr = attrs[ver];
323
- const field = (attr.column ? attr.column : ver);
324
- cols.push(field);
325
- values.push(`${1}`);
345
+ const attr = attrs[ver]
346
+ const field = attr.column ? attr.column : ver
347
+ cols.push(field)
348
+ values.push(`${1}`)
326
349
  }
327
350
  if (cols.length === 0) {
328
351
  if (notSkipInvalid) {
329
- return undefined;
352
+ return undefined
330
353
  }
331
354
  } else {
332
- const s = `into ${table}(${cols.join(',')})values(${values.join(',')})`;
333
- rows.push(s);
355
+ const s = `into ${table}(${cols.join(",")})values(${values.join(",")})`
356
+ rows.push(s)
334
357
  }
335
358
  }
336
359
  if (rows.length === 0) {
337
- return undefined;
360
+ return undefined
338
361
  }
339
- const query = `insert all ${rows.join(' ')} select * from dual`;
340
- return { query, params: args };
362
+ const query = `insert all ${rows.join(" ")} select * from dual`
363
+ return { query, params: args }
341
364
  }
342
365
  }
343
- export function update<T>(exec: (sql: string, args?: any[]) => Promise<number>, obj: T, table: string, attrs: Attributes, buildParam: (i: number) => string, ver?: string, i?: number): Promise<number> {
344
- const stm = buildToUpdate(obj, table, attrs, buildParam, ver, i);
366
+ export function update<T>(
367
+ exec: (sql: string, args?: any[]) => Promise<number>,
368
+ obj: T,
369
+ table: string,
370
+ attrs: Attributes,
371
+ buildParam: (i: number) => string,
372
+ ver?: string,
373
+ i?: number,
374
+ ): Promise<number> {
375
+ const stm = buildToUpdate(obj, table, attrs, buildParam, ver, i)
345
376
  if (!stm) {
346
- return Promise.resolve(0);
377
+ return Promise.resolve(0)
347
378
  } else {
348
- return exec(stm.query, stm.params);
379
+ return exec(stm.query, stm.params)
349
380
  }
350
381
  }
351
- export function buildToUpdate<T>(obj: T, table: string, attrs: Attributes, buildParam: (i: number) => string, ver?: string, i?: number): Statement|undefined {
382
+ export function buildToUpdate<T>(obj: T, table: string, attrs: Attributes, buildParam: (i: number) => string, ver?: string, i?: number): Statement | undefined {
352
383
  if (!i) {
353
- i = 1;
354
- }
355
- const o: any = obj;
356
- const ks = Object.keys(attrs);
357
- const pks: Attribute[] = [];
358
- const colSet: string[] = [];
359
- const colQuery: string[] = [];
360
- const args: any[] = [];
384
+ i = 1
385
+ }
386
+ const o: any = obj
387
+ const ks = Object.keys(attrs)
388
+ const pks: Attribute[] = []
389
+ const colSet: string[] = []
390
+ const colQuery: string[] = []
391
+ const args: any[] = []
361
392
  for (const k of ks) {
362
- const v = o[k];
393
+ const v = o[k]
363
394
  if (v !== undefined) {
364
- const attr = attrs[k];
365
- attr.name = k;
395
+ const attr = attrs[k]
396
+ attr.name = k
366
397
  if (attr && !attr.ignored && k !== ver) {
367
398
  if (attr.key) {
368
- pks.push(attr);
399
+ pks.push(attr)
369
400
  } else if (!attr.noupdate) {
370
- const field = (attr.column ? attr.column : k);
371
- let x: string;
401
+ const field = attr.column ? attr.column : k
402
+ let x: string
372
403
  if (v == null) {
373
- x = 'null';
374
- } else if (v === '') {
375
- x = `''`;
376
- } else if (typeof v === 'number') {
377
- x = toString(v);
378
- } else if (typeof v === 'boolean') {
404
+ x = "null"
405
+ } else if (v === "") {
406
+ x = `''`
407
+ } else if (typeof v === "number") {
408
+ x = toString(v)
409
+ } else if (typeof v === "boolean") {
379
410
  if (attr.true === undefined) {
380
411
  if (v === true) {
381
- x = `true`;
412
+ x = `true`
382
413
  } else {
383
- x = `false`;
414
+ x = `false`
384
415
  }
385
416
  } else {
386
- x = buildParam(i++);
417
+ x = buildParam(i++)
387
418
  if (v === true) {
388
- const v2 = (attr.true ? attr.true : '1');
389
- args.push(v2);
419
+ const v2 = attr.true ? attr.true : "1"
420
+ args.push(v2)
390
421
  } else {
391
- const v2 = (attr.false ? attr.false : '0');
392
- args.push(v2);
422
+ const v2 = attr.false ? attr.false : "0"
423
+ args.push(v2)
393
424
  }
394
425
  }
395
426
  } else {
396
- if (resource.ignoreDatetime && typeof v === 'string' && attr.type === 'datetime') {
397
- x = `'${v}'`;
427
+ if (resource.ignoreDatetime && typeof v === "string" && attr.type === "datetime") {
428
+ x = `'${v}'`
398
429
  } else {
399
- x = buildParam(i++);
400
- args.push(v);
430
+ x = buildParam(i++)
431
+ args.push(v)
401
432
  }
402
433
  }
403
- colSet.push(`${field}=${x}`);
434
+ colSet.push(`${field}=${x}`)
404
435
  }
405
436
  }
406
437
  }
407
438
  }
408
439
  for (const pk of pks) {
409
- const na = (pk.name ? pk.name : '');
410
- const v = o[na];
440
+ const na = pk.name ? pk.name : ""
441
+ const v = o[na]
411
442
  if (!v) {
412
- return undefined;
443
+ return undefined
413
444
  } else {
414
- const attr = attrs[na];
415
- const field = (attr.column ? attr.column : pk.name);
416
- let x: string;
445
+ const attr = attrs[na]
446
+ const field = attr.column ? attr.column : pk.name
447
+ let x: string
417
448
  if (v == null) {
418
- x = 'null';
419
- } else if (v === '') {
420
- x = `''`;
421
- } else if (typeof v === 'number') {
422
- x = toString(v);
449
+ x = "null"
450
+ } else if (v === "") {
451
+ x = `''`
452
+ } else if (typeof v === "number") {
453
+ x = toString(v)
423
454
  } else {
424
- x = buildParam(i++);
425
- if (typeof v === 'boolean') {
455
+ x = buildParam(i++)
456
+ if (typeof v === "boolean") {
426
457
  if (v === true) {
427
- const v2 = (attr.true ? '' + attr.true : '1');
428
- args.push(v2);
458
+ const v2 = attr.true ? "" + attr.true : "1"
459
+ args.push(v2)
429
460
  } else {
430
- const v2 = (attr.false ? '' + attr.false : '0');
431
- args.push(v2);
461
+ const v2 = attr.false ? "" + attr.false : "0"
462
+ args.push(v2)
432
463
  }
433
464
  } else {
434
- args.push(v);
465
+ args.push(v)
435
466
  }
436
467
  }
437
- colQuery.push(`${field}=${x}`);
468
+ colQuery.push(`${field}=${x}`)
438
469
  }
439
470
  }
440
471
  if (ver && ver.length > 0) {
441
- const v = o[ver];
442
- if (typeof v === 'number' && !isNaN(v)) {
443
- const attr = attrs[ver];
472
+ const v = o[ver]
473
+ if (typeof v === "number" && !isNaN(v)) {
474
+ const attr = attrs[ver]
444
475
  if (attr) {
445
- const field = (attr.column ? attr.column : ver);
446
- colSet.push(`${field}=${(1 + v)}`);
447
- colQuery.push(`${field}=${v}`);
476
+ const field = attr.column ? attr.column : ver
477
+ colSet.push(`${field}=${1 + v}`)
478
+ colQuery.push(`${field}=${v}`)
448
479
  }
449
480
  }
450
481
  }
451
482
  if (colSet.length === 0 || colQuery.length === 0) {
452
- return undefined;
483
+ return undefined
453
484
  } else {
454
- const query = `update ${table} set ${colSet.join(',')} where ${colQuery.join(' and ')}`;
455
- return { query, params: args };
485
+ const query = `update ${table} set ${colSet.join(",")} where ${colQuery.join(" and ")}`
486
+ return { query, params: args }
456
487
  }
457
488
  }
458
- export function updateBatch<T>(exec: (statements: Statement[]) => Promise<number>, objs: T[], table: string, attrs: Attributes, buildParam: (i: number) => string, notSkipInvalid?: boolean): Promise<number> {
459
- const stmts = buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid);
489
+ export function updateBatch<T>(
490
+ exec: (statements: Statement[]) => Promise<number>,
491
+ objs: T[],
492
+ table: string,
493
+ attrs: Attributes,
494
+ buildParam: (i: number) => string,
495
+ notSkipInvalid?: boolean,
496
+ ): Promise<number> {
497
+ const stmts = buildToUpdateBatch(objs, table, attrs, buildParam, notSkipInvalid)
460
498
  if (!stmts || stmts.length === 0) {
461
- return Promise.resolve(0);
499
+ return Promise.resolve(0)
462
500
  } else {
463
- return exec(stmts);
501
+ return exec(stmts)
464
502
  }
465
503
  }
466
- export function buildToUpdateBatch<T>(objs: T[], table: string, attrs: Attributes, buildParam: (i: number) => string, notSkipInvalid?: boolean): Statement[]|undefined {
467
- const sts: Statement[] = [];
468
- const meta = metadata(attrs);
504
+ export function buildToUpdateBatch<T>(
505
+ objs: T[],
506
+ table: string,
507
+ attrs: Attributes,
508
+ buildParam: (i: number) => string,
509
+ notSkipInvalid?: boolean,
510
+ ): Statement[] | undefined {
511
+ const sts: Statement[] = []
512
+ const meta = metadata(attrs)
469
513
  if (!meta.keys || meta.keys.length === 0) {
470
- return undefined;
514
+ return undefined
471
515
  }
472
516
  for (const obj of objs) {
473
- const o: any = obj;
474
- let i = 1;
475
- const ks = Object.keys(o);
476
- const colSet: string[] = [];
477
- const colQuery: string[] = [];
478
- const args: any[] = [];
517
+ const o: any = obj
518
+ let i = 1
519
+ const ks = Object.keys(o)
520
+ const colSet: string[] = []
521
+ const colQuery: string[] = []
522
+ const args: any[] = []
479
523
  for (const k of ks) {
480
- const v = o[k];
524
+ const v = o[k]
481
525
  if (v !== undefined) {
482
- const attr = attrs[k];
483
- attr.name = k;
526
+ const attr = attrs[k]
527
+ attr.name = k
484
528
  if (attr && !attr.ignored && !attr.key && !attr.version && !attr.noupdate) {
485
- const field = (attr.column ? attr.column : k);
486
- let x: string;
529
+ const field = attr.column ? attr.column : k
530
+ let x: string
487
531
  if (v == null) {
488
- x = 'null';
489
- } else if (v === '') {
490
- x = `''`;
491
- } else if (typeof v === 'number') {
492
- x = toString(v);
493
- } else if (typeof v === 'boolean') {
532
+ x = "null"
533
+ } else if (v === "") {
534
+ x = `''`
535
+ } else if (typeof v === "number") {
536
+ x = toString(v)
537
+ } else if (typeof v === "boolean") {
494
538
  if (attr.true === undefined) {
495
539
  if (v === true) {
496
- x = `true`;
540
+ x = `true`
497
541
  } else {
498
- x = `false`;
542
+ x = `false`
499
543
  }
500
544
  } else {
501
- x = buildParam(i++);
545
+ x = buildParam(i++)
502
546
  if (v === true) {
503
- const v2 = (attr.true ? attr.true : '1');
504
- args.push(v2);
547
+ const v2 = attr.true ? attr.true : "1"
548
+ args.push(v2)
505
549
  } else {
506
- const v2 = (attr.false ? attr.false : '0');
507
- args.push(v2);
550
+ const v2 = attr.false ? attr.false : "0"
551
+ args.push(v2)
508
552
  }
509
553
  }
510
554
  } else {
511
- x = buildParam(i++);
512
- args.push(v);
555
+ x = buildParam(i++)
556
+ args.push(v)
513
557
  }
514
- colSet.push(`${field}=${x}`);
558
+ colSet.push(`${field}=${x}`)
515
559
  }
516
560
  }
517
561
  }
518
- let valid = true;
562
+ let valid = true
519
563
  for (const pk of meta.keys) {
520
- const na = (pk.name ? pk.name : '');
521
- const v = o[na];
564
+ const na = pk.name ? pk.name : ""
565
+ const v = o[na]
522
566
  if (!v) {
523
- valid = false;
567
+ valid = false
524
568
  } else {
525
- const attr = attrs[na];
526
- const field = (attr.column ? attr.column : pk.name);
527
- let x: string;
569
+ const attr = attrs[na]
570
+ const field = attr.column ? attr.column : pk.name
571
+ let x: string
528
572
  if (v == null) {
529
- x = 'null';
530
- } else if (v === '') {
531
- x = `''`;
532
- } else if (typeof v === 'number') {
533
- x = toString(v);
573
+ x = "null"
574
+ } else if (v === "") {
575
+ x = `''`
576
+ } else if (typeof v === "number") {
577
+ x = toString(v)
534
578
  } else {
535
- x = buildParam(i++);
536
- if (typeof v === 'boolean') {
579
+ x = buildParam(i++)
580
+ if (typeof v === "boolean") {
537
581
  if (v === true) {
538
- const v2 = (attr.true ? '' + attr.true : '1');
539
- args.push(v2);
582
+ const v2 = attr.true ? "" + attr.true : "1"
583
+ args.push(v2)
540
584
  } else {
541
- const v2 = (attr.false ? '' + attr.false : '0');
542
- args.push(v2);
585
+ const v2 = attr.false ? "" + attr.false : "0"
586
+ args.push(v2)
543
587
  }
544
588
  } else {
545
- args.push(v);
589
+ args.push(v)
546
590
  }
547
591
  }
548
- colQuery.push(`${field}=${x}`);
592
+ colQuery.push(`${field}=${x}`)
549
593
  }
550
594
  }
551
595
  if (!valid || colSet.length === 0 || colQuery.length === 0) {
552
596
  if (notSkipInvalid) {
553
- return undefined;
597
+ return undefined
554
598
  }
555
599
  } else {
556
- const ver = meta.version;
600
+ const ver = meta.version
557
601
  if (ver && ver.length > 0) {
558
- const v = o[ver];
559
- if (typeof v === 'number' && !isNaN(v)) {
560
- const attr = attrs[ver];
602
+ const v = o[ver]
603
+ if (typeof v === "number" && !isNaN(v)) {
604
+ const attr = attrs[ver]
561
605
  if (attr) {
562
- const field = (attr.column ? attr.column : ver);
563
- colSet.push(`${field}=${(1 + v)}`);
564
- colQuery.push(`${field}=${v}`);
606
+ const field = attr.column ? attr.column : ver
607
+ colSet.push(`${field}=${1 + v}`)
608
+ colQuery.push(`${field}=${v}`)
565
609
  }
566
610
  }
567
611
  }
568
- const query = `update ${table} set ${colSet.join(',')} where ${colQuery.join(' and ')}`;
569
- const stm: Statement = { query, params: args };
570
- sts.push(stm);
612
+ const query = `update ${table} set ${colSet.join(",")} where ${colQuery.join(" and ")}`
613
+ const stm: Statement = { query, params: args }
614
+ sts.push(stm)
571
615
  }
572
616
  }
573
- return sts;
617
+ return sts
574
618
  }
575
- export function version(attrs: Attributes): Attribute|undefined {
576
- const ks = Object.keys(attrs);
619
+ export function version(attrs: Attributes): Attribute | undefined {
620
+ const ks = Object.keys(attrs)
577
621
  for (const k of ks) {
578
- const attr = attrs[k];
622
+ const attr = attrs[k]
579
623
  if (attr.version) {
580
- attr.name = k;
581
- return attr;
624
+ attr.name = k
625
+ return attr
582
626
  }
583
627
  }
584
- return undefined;
628
+ return undefined
585
629
  }
586
- export function key(attrs: Attributes): Attribute|undefined {
587
- const ks = Object.keys(attrs);
630
+ export function key(attrs: Attributes): Attribute | undefined {
631
+ const ks = Object.keys(attrs)
588
632
  for (const k of ks) {
589
- const attr = attrs[k];
590
- attr.name = k;
633
+ const attr = attrs[k]
634
+ attr.name = k
591
635
  if (attr.key) {
592
- return attr;
636
+ return attr
593
637
  }
594
638
  }
595
- return undefined;
639
+ return undefined
596
640
  }
597
641
  export function keys(attrs: Attributes): Attribute[] {
598
- const ks = Object.keys(attrs);
599
- const ats: Attribute[] = [];
642
+ const ks = Object.keys(attrs)
643
+ const ats: Attribute[] = []
600
644
  for (const k of ks) {
601
- const attr = attrs[k];
602
- attr.name = k;
645
+ const attr = attrs[k]
646
+ attr.name = k
603
647
  if (attr.key) {
604
- ats.push(attr);
648
+ ats.push(attr)
605
649
  }
606
650
  }
607
- return ats;
651
+ return ats
608
652
  }
609
653
  export function buildMap(attrs: Attributes): StringMap {
610
- const mp: StringMap = {};
611
- const ks = Object.keys(attrs);
654
+ const mp: StringMap = {}
655
+ const ks = Object.keys(attrs)
612
656
  for (const k of ks) {
613
- const attr = attrs[k];
614
- attr.name = k;
615
- const field = (attr.column ? attr.column : k);
616
- const s = field.toLowerCase();
657
+ const attr = attrs[k]
658
+ attr.name = k
659
+ const field = attr.column ? attr.column : k
660
+ const s = field.toLowerCase()
617
661
  if (s !== k) {
618
- mp[s] = k;
662
+ mp[s] = k
619
663
  }
620
664
  }
621
- return mp;
665
+ return mp
622
666
  }
623
667
  export interface Metadata {
624
- keys: Attribute[];
625
- bools?: Attribute[];
626
- map?: StringMap;
627
- version?: string;
628
- fields?: string[];
668
+ keys: Attribute[]
669
+ bools?: Attribute[]
670
+ map?: StringMap
671
+ version?: string
672
+ fields?: string[]
629
673
  }
630
674
  export function metadata(attrs: Attributes): Metadata {
631
- const mp: StringMap = {};
632
- const ks = Object.keys(attrs);
633
- const ats: Attribute[] = [];
634
- const bools: Attribute[] = [];
635
- const fields: string[] = [];
636
- let isMap = false;
637
- const m: Metadata = {keys: ats, fields};
675
+ const mp: StringMap = {}
676
+ const ks = Object.keys(attrs)
677
+ const ats: Attribute[] = []
678
+ const bools: Attribute[] = []
679
+ const fields: string[] = []
680
+ let isMap = false
681
+ const m: Metadata = { keys: ats, fields }
638
682
  for (const k of ks) {
639
- const attr = attrs[k];
640
- attr.name = k;
683
+ const attr = attrs[k]
684
+ attr.name = k
641
685
  if (attr.key) {
642
- ats.push(attr);
686
+ ats.push(attr)
643
687
  }
644
688
  if (!attr.ignored) {
645
- fields.push(k);
689
+ fields.push(k)
646
690
  }
647
- if (attr.type === 'boolean') {
648
- bools.push(attr);
691
+ if (attr.type === "boolean") {
692
+ bools.push(attr)
649
693
  }
650
694
  if (attr.version) {
651
- m.version = k;
695
+ m.version = k
652
696
  }
653
- const field = (attr.column ? attr.column : k);
654
- const s = field.toLowerCase();
697
+ const field = attr.column ? attr.column : k
698
+ const s = field.toLowerCase()
655
699
  if (s !== k) {
656
- mp[s] = k;
657
- isMap = true;
700
+ mp[s] = k
701
+ isMap = true
658
702
  }
659
703
  }
660
704
  if (isMap) {
661
- m.map = mp;
705
+ m.map = mp
662
706
  }
663
707
  if (bools.length > 0) {
664
- m.bools = bools;
708
+ m.bools = bools
665
709
  }
666
- return m;
710
+ return m
667
711
  }
668
712
  export function attributes(attrs: string[], isKey?: boolean) {
669
- const ks: Attribute[] = [];
713
+ const ks: Attribute[] = []
670
714
  for (const s of attrs) {
671
- const a: Attribute = {name: s, column: s, key: isKey};
672
- ks.push(a);
715
+ const a: Attribute = { name: s, column: s, key: isKey }
716
+ ks.push(a)
673
717
  }
674
- return ks;
718
+ return ks
675
719
  }
676
720
  export function param(i: number): string {
677
- return '?';
721
+ return "?"
678
722
  }
679
723
  export function setValue<T, V>(obj: T, path: string, value: V): void {
680
- const paths = path.split('.');
681
- let o: any = obj;
724
+ const paths = path.split(".")
725
+ let o: any = obj
682
726
  for (let i = 0; i < paths.length - 1; i++) {
683
- const p = paths[i];
727
+ const p = paths[i]
684
728
  if (p in o) {
685
- o = o[p];
729
+ o = o[p]
686
730
  } else {
687
- o[p] = {};
688
- o = o[p];
731
+ o[p] = {}
732
+ o = o[p]
689
733
  }
690
734
  }
691
- o[paths[paths.length - 1]] = value;
735
+ o[paths[paths.length - 1]] = value
692
736
  }
693
- const n = 'NaN';
737
+ const n = "NaN"
694
738
  export function toString(v: number): string {
695
- let x = '' + v;
739
+ let x = "" + v
696
740
  if (x === n) {
697
- x = 'null';
741
+ x = "null"
698
742
  }
699
- return x;
743
+ return x
700
744
  }