@spinajs/orm-sql 2.0.86 → 2.0.87

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.
Files changed (53) hide show
  1. package/lib/{builders.d.ts → cjs/builders.d.ts} +0 -0
  2. package/lib/cjs/builders.d.ts.map +1 -0
  3. package/lib/cjs/builders.js +42 -0
  4. package/lib/cjs/builders.js.map +1 -0
  5. package/lib/{compilers.d.ts → cjs/compilers.d.ts} +0 -0
  6. package/lib/cjs/compilers.d.ts.map +1 -0
  7. package/lib/cjs/compilers.js +1037 -0
  8. package/lib/cjs/compilers.js.map +1 -0
  9. package/lib/{converters.d.ts → cjs/converters.d.ts} +0 -0
  10. package/lib/cjs/converters.d.ts.map +1 -0
  11. package/lib/cjs/converters.js +47 -0
  12. package/lib/cjs/converters.js.map +1 -0
  13. package/lib/{index.d.ts → cjs/index.d.ts} +0 -0
  14. package/lib/cjs/index.d.ts.map +1 -0
  15. package/lib/cjs/index.js +71 -0
  16. package/lib/cjs/index.js.map +1 -0
  17. package/lib/{statements.d.ts → cjs/statements.d.ts} +0 -0
  18. package/lib/cjs/statements.d.ts.map +1 -0
  19. package/lib/cjs/statements.js +274 -0
  20. package/lib/cjs/statements.js.map +1 -0
  21. package/lib/mjs/builders.d.ts +10 -0
  22. package/lib/mjs/builders.d.ts.map +1 -0
  23. package/lib/{builders.js → mjs/builders.js} +0 -0
  24. package/lib/mjs/builders.js.map +1 -0
  25. package/lib/mjs/compilers.d.ts +226 -0
  26. package/lib/mjs/compilers.d.ts.map +1 -0
  27. package/lib/{compilers.js → mjs/compilers.js} +0 -0
  28. package/lib/mjs/compilers.js.map +1 -0
  29. package/lib/mjs/converters.d.ts +11 -0
  30. package/lib/mjs/converters.d.ts.map +1 -0
  31. package/lib/{converters.js → mjs/converters.js} +0 -0
  32. package/lib/mjs/converters.js.map +1 -0
  33. package/lib/mjs/index.d.ts +7 -0
  34. package/lib/mjs/index.d.ts.map +1 -0
  35. package/lib/{index.js → mjs/index.js} +0 -0
  36. package/lib/mjs/index.js.map +1 -0
  37. package/lib/mjs/statements.d.ts +47 -0
  38. package/lib/mjs/statements.d.ts.map +1 -0
  39. package/lib/{statements.js → mjs/statements.js} +0 -0
  40. package/lib/mjs/statements.js.map +1 -0
  41. package/lib/tsconfig.cjs.tsbuildinfo +1 -0
  42. package/lib/tsconfig.tsbuildinfo +1 -0
  43. package/package.json +15 -6
  44. package/lib/builders.d.ts.map +0 -1
  45. package/lib/builders.js.map +0 -1
  46. package/lib/compilers.d.ts.map +0 -1
  47. package/lib/compilers.js.map +0 -1
  48. package/lib/converters.d.ts.map +0 -1
  49. package/lib/converters.js.map +0 -1
  50. package/lib/index.d.ts.map +0 -1
  51. package/lib/index.js.map +0 -1
  52. package/lib/statements.d.ts.map +0 -1
  53. package/lib/statements.js.map +0 -1
@@ -0,0 +1,1037 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.SqlDropEventQueryCompiler = exports.SqlEventQueryCompiler = exports.SqlAlterColumnQueryCompiler = exports.SqlColumnQueryCompiler = exports.SqlTableQueryCompiler = exports.SqlTableHistoryQueryCompiler = exports.SqlTruncateTableQueryCompiler = exports.SqlTableCloneQueryCompiler = exports.SqlAlterTableQueryCompiler = exports.SqlDropTableQueryCompiler = exports.SqlInsertQueryCompiler = exports.SqlIndexQueryCompiler = exports.SqlOnDuplicateQueryCompiler = exports.SqlDeleteQueryCompiler = exports.SqlUpdateQueryCompiler = exports.SqlSelectQueryCompiler = exports.SqlJoinCompiler = exports.SqlWhereCompiler = exports.SqlColumnsCompiler = exports.SqlGroupByCompiler = exports.SqlLimitQueryCompiler = exports.SqlForeignKeyQueryCompiler = exports.SqlWithRecursiveCompiler = exports.SqlOrderByQueryCompiler = exports.SqlQueryCompiler = exports.SqlTableAliasCompiler = void 0;
16
+ /* eslint-disable @typescript-eslint/no-unsafe-call */
17
+ /* eslint-disable @typescript-eslint/no-unsafe-member-access */
18
+ /* eslint-disable @typescript-eslint/no-empty-interface */
19
+ /* eslint-disable prettier/prettier */
20
+ const exceptions_1 = require("@spinajs/exceptions");
21
+ const orm_1 = require("@spinajs/orm");
22
+ const typescript_mix_1 = require("typescript-mix");
23
+ const di_1 = require("@spinajs/di");
24
+ const lodash_1 = __importDefault(require("lodash"));
25
+ let SqlTableAliasCompiler = class SqlTableAliasCompiler {
26
+ compile(builder, tbl) {
27
+ let table = '';
28
+ if (builder.Database) {
29
+ table += `\`${builder.Database}\`.`;
30
+ }
31
+ table += `\`${tbl ? tbl : builder.Table}\``;
32
+ if (builder.TableAlias) {
33
+ table += ` as \`${builder.TableAlias}\``;
34
+ }
35
+ return table;
36
+ }
37
+ };
38
+ SqlTableAliasCompiler = __decorate([
39
+ (0, di_1.NewInstance)()
40
+ ], SqlTableAliasCompiler);
41
+ exports.SqlTableAliasCompiler = SqlTableAliasCompiler;
42
+ let SqlQueryCompiler = class SqlQueryCompiler extends orm_1.SelectQueryCompiler {
43
+ constructor(_builder, _container) {
44
+ super();
45
+ this._builder = _builder;
46
+ this._container = _container;
47
+ if (_builder === null && _builder === undefined) {
48
+ throw new exceptions_1.InvalidArgument('builder cannot be null or undefined');
49
+ }
50
+ }
51
+ tryConvertValue(v) {
52
+ let val = v;
53
+ const converters = this._container.get('__orm_db_value_converters__');
54
+ if (converters && v && converters.has(v.constructor.name)) {
55
+ const converter = this._container.resolve(converters.get(v.constructor.name));
56
+ val = converter.toDB(val, null, null);
57
+ }
58
+ return val;
59
+ }
60
+ };
61
+ SqlQueryCompiler = __decorate([
62
+ (0, di_1.NewInstance)(),
63
+ __metadata("design:paramtypes", [Object, Object])
64
+ ], SqlQueryCompiler);
65
+ exports.SqlQueryCompiler = SqlQueryCompiler;
66
+ let SqlOrderByQueryCompiler = class SqlOrderByQueryCompiler extends orm_1.OrderByQueryCompiler {
67
+ constructor(builder) {
68
+ super();
69
+ if (!builder) {
70
+ throw new exceptions_1.InvalidOperation('builder cannot be null or undefined');
71
+ }
72
+ this._builder = builder;
73
+ }
74
+ compile() {
75
+ const sort = this._builder.getSort();
76
+ let stmt = '';
77
+ const bindings = [];
78
+ if (sort) {
79
+ stmt = ` ORDER BY ? ?`;
80
+ bindings.push(sort.column, sort.order);
81
+ }
82
+ return {
83
+ bindings,
84
+ expression: stmt,
85
+ };
86
+ }
87
+ };
88
+ SqlOrderByQueryCompiler = __decorate([
89
+ (0, di_1.NewInstance)(),
90
+ __metadata("design:paramtypes", [orm_1.OrderByBuilder])
91
+ ], SqlOrderByQueryCompiler);
92
+ exports.SqlOrderByQueryCompiler = SqlOrderByQueryCompiler;
93
+ let SqlWithRecursiveCompiler = class SqlWithRecursiveCompiler {
94
+ recursive(builder) {
95
+ const statement = builder.CteRecursive.build();
96
+ let exprr = `WITH RECURSIVE recursive_cte(${statement.Statements[0]}) AS`;
97
+ exprr += ` ( `;
98
+ exprr += statement.Statements[1];
99
+ exprr += ` UNION ALL `;
100
+ exprr += statement.Statements[2];
101
+ exprr += ` ) `;
102
+ exprr += 'SELECT * FROM recursive_cte';
103
+ return {
104
+ bindings: statement.Bindings,
105
+ expression: exprr,
106
+ };
107
+ }
108
+ };
109
+ SqlWithRecursiveCompiler = __decorate([
110
+ (0, di_1.NewInstance)()
111
+ ], SqlWithRecursiveCompiler);
112
+ exports.SqlWithRecursiveCompiler = SqlWithRecursiveCompiler;
113
+ let SqlForeignKeyQueryCompiler = class SqlForeignKeyQueryCompiler {
114
+ constructor(_builder) {
115
+ this._builder = _builder;
116
+ if (!_builder) {
117
+ throw new Error('foreign key query builder cannot be null');
118
+ }
119
+ }
120
+ compile() {
121
+ const exprr = `FOREIGN KEY (${this._builder.ForeignKeyField}) REFERENCES ${this._builder.Table}(${this._builder.PrimaryKey}) ON DELETE ${this._builder.OnDeleteAction} ON UPDATE ${this._builder.OnUpdateAction}`;
122
+ return {
123
+ bindings: [],
124
+ expression: exprr,
125
+ };
126
+ }
127
+ };
128
+ SqlForeignKeyQueryCompiler = __decorate([
129
+ (0, di_1.NewInstance)(),
130
+ __metadata("design:paramtypes", [orm_1.ForeignKeyBuilder])
131
+ ], SqlForeignKeyQueryCompiler);
132
+ exports.SqlForeignKeyQueryCompiler = SqlForeignKeyQueryCompiler;
133
+ let SqlLimitQueryCompiler = class SqlLimitQueryCompiler extends orm_1.LimitQueryCompiler {
134
+ constructor(builder) {
135
+ super();
136
+ if (!builder) {
137
+ throw new exceptions_1.InvalidOperation('builder cannot be null or undefined');
138
+ }
139
+ this._builder = builder;
140
+ }
141
+ compile() {
142
+ const limits = this._builder.getLimits();
143
+ const bindings = [];
144
+ let stmt = '';
145
+ if (limits.limit > 0) {
146
+ stmt += ` LIMIT ?`;
147
+ bindings.push(limits.limit);
148
+ }
149
+ else {
150
+ if (limits.offset > 0) {
151
+ stmt += ` LIMIT 18446744073709551615`;
152
+ }
153
+ }
154
+ if (limits.offset > 0) {
155
+ stmt += ` OFFSET ?`;
156
+ bindings.push(limits.offset);
157
+ }
158
+ return {
159
+ bindings,
160
+ expression: stmt,
161
+ };
162
+ }
163
+ };
164
+ SqlLimitQueryCompiler = __decorate([
165
+ (0, di_1.NewInstance)(),
166
+ __metadata("design:paramtypes", [orm_1.LimitBuilder])
167
+ ], SqlLimitQueryCompiler);
168
+ exports.SqlLimitQueryCompiler = SqlLimitQueryCompiler;
169
+ let SqlGroupByCompiler = class SqlGroupByCompiler {
170
+ group(builder) {
171
+ let bindings = [];
172
+ let stmt = ' GROUP BY ';
173
+ const builds = builder.GroupStatements.map((x) => x.build());
174
+ stmt += builds.map((x) => x.Statements).join(',');
175
+ bindings = builds.map((x) => x.Bindings);
176
+ return {
177
+ bindings,
178
+ expression: builds.length != 0 ? stmt : '',
179
+ };
180
+ }
181
+ };
182
+ SqlGroupByCompiler = __decorate([
183
+ (0, di_1.NewInstance)()
184
+ ], SqlGroupByCompiler);
185
+ exports.SqlGroupByCompiler = SqlGroupByCompiler;
186
+ let SqlColumnsCompiler = class SqlColumnsCompiler {
187
+ columns(builder) {
188
+ return {
189
+ bindings: [],
190
+ expression: builder
191
+ .getColumns()
192
+ .map((c) => {
193
+ return c.build().Statements[0];
194
+ })
195
+ .join(','),
196
+ };
197
+ }
198
+ };
199
+ SqlColumnsCompiler = __decorate([
200
+ (0, di_1.NewInstance)()
201
+ ], SqlColumnsCompiler);
202
+ exports.SqlColumnsCompiler = SqlColumnsCompiler;
203
+ let SqlWhereCompiler = class SqlWhereCompiler {
204
+ where(builder) {
205
+ const where = [];
206
+ const bindings = [];
207
+ builder.Statements.map((x) => {
208
+ return x.build();
209
+ }).forEach((r) => {
210
+ where.push(...r.Statements);
211
+ if (Array.isArray(r.Bindings)) {
212
+ bindings.push(...r.Bindings);
213
+ }
214
+ });
215
+ return {
216
+ bindings,
217
+ expression: where.join(` ${builder.Op.toUpperCase()} `),
218
+ };
219
+ }
220
+ };
221
+ SqlWhereCompiler = __decorate([
222
+ (0, di_1.NewInstance)()
223
+ ], SqlWhereCompiler);
224
+ exports.SqlWhereCompiler = SqlWhereCompiler;
225
+ let SqlJoinCompiler = class SqlJoinCompiler {
226
+ join(builder) {
227
+ const result = builder.JoinStatements.map((s) => s.build());
228
+ return {
229
+ bindings: lodash_1.default.flatMap(result, (r) => r.Bindings),
230
+ expression: lodash_1.default.flatMap(result, (r) => r.Statements).join(' '),
231
+ };
232
+ }
233
+ };
234
+ SqlJoinCompiler = __decorate([
235
+ (0, di_1.NewInstance)()
236
+ ], SqlJoinCompiler);
237
+ exports.SqlJoinCompiler = SqlJoinCompiler;
238
+ let SqlSelectQueryCompiler = class SqlSelectQueryCompiler extends SqlQueryCompiler {
239
+ constructor(_container, builder) {
240
+ super(builder, _container);
241
+ }
242
+ compile() {
243
+ if (this._builder.CteRecursive) {
244
+ return this.recursive(this._builder);
245
+ }
246
+ const columns = this.select();
247
+ const from = this.from();
248
+ const limit = this.limit();
249
+ const sort = this.sort();
250
+ const where = this.where(this._builder);
251
+ const join = this.join(this._builder);
252
+ const group = this.group(this._builder);
253
+ const expression = columns + ' ' + from + (join.expression ? ` ${join.expression}` : '') + (where.expression ? ` WHERE ${where.expression}` : '') + group.expression + sort.expression + limit.expression;
254
+ const bindings = [];
255
+ bindings.push(...join.bindings);
256
+ bindings.push(...where.bindings);
257
+ bindings.push(...sort.bindings);
258
+ bindings.push(...limit.bindings);
259
+ bindings.push(...group.bindings);
260
+ return {
261
+ bindings,
262
+ expression: expression.trim(),
263
+ };
264
+ }
265
+ limit() {
266
+ const compiler = this._container.resolve(orm_1.LimitQueryCompiler, [this._builder]);
267
+ return compiler.compile();
268
+ }
269
+ sort() {
270
+ const compiler = this._container.resolve(orm_1.OrderByQueryCompiler, [this._builder]);
271
+ return compiler.compile();
272
+ }
273
+ select() {
274
+ let _stmt = 'SELECT ';
275
+ if (this._builder.IsDistinct) {
276
+ _stmt += 'DISTINCT ';
277
+ }
278
+ if (this._builder.getColumns().length === 0) {
279
+ return _stmt + '*';
280
+ }
281
+ return _stmt + this.columns(this._builder).expression;
282
+ }
283
+ from() {
284
+ return 'FROM ' + this._container.resolve(orm_1.TableAliasCompiler).compile(this._builder);
285
+ }
286
+ };
287
+ __decorate([
288
+ (0, typescript_mix_1.use)(SqlWhereCompiler, SqlColumnsCompiler, orm_1.TableAliasCompiler, SqlJoinCompiler, SqlWithRecursiveCompiler, SqlGroupByCompiler),
289
+ __metadata("design:type", Object)
290
+ ], SqlSelectQueryCompiler.prototype, "this", void 0);
291
+ SqlSelectQueryCompiler = __decorate([
292
+ (0, di_1.NewInstance)(),
293
+ (0, di_1.Inject)(di_1.Container),
294
+ __metadata("design:paramtypes", [Object, orm_1.SelectQueryBuilder])
295
+ ], SqlSelectQueryCompiler);
296
+ exports.SqlSelectQueryCompiler = SqlSelectQueryCompiler;
297
+ let SqlUpdateQueryCompiler = class SqlUpdateQueryCompiler extends SqlQueryCompiler {
298
+ constructor(_container, builder) {
299
+ super(builder, _container);
300
+ this._container = _container;
301
+ }
302
+ compile() {
303
+ const table = this.table();
304
+ const set = this.set();
305
+ const where = this.where(this._builder);
306
+ const bindings = [];
307
+ bindings.push(...set.bindings);
308
+ bindings.push(...where.bindings);
309
+ return {
310
+ bindings,
311
+ expression: `${table} ${set.expression} WHERE ${where.expression}`,
312
+ };
313
+ }
314
+ set() {
315
+ let bindings = [];
316
+ const exprr = [];
317
+ for (const prop of Object.keys(this._builder.Value)) {
318
+ const v = this._builder.Value[`${prop}`];
319
+ exprr.push(`\`${prop}\` = ?`);
320
+ bindings = bindings.concat(this.tryConvertValue(v));
321
+ }
322
+ return {
323
+ bindings,
324
+ expression: exprr.join(','),
325
+ };
326
+ }
327
+ table() {
328
+ return `UPDATE ${this._container.resolve(orm_1.TableAliasCompiler).compile(this._builder)} SET`;
329
+ }
330
+ };
331
+ __decorate([
332
+ (0, typescript_mix_1.use)(SqlWhereCompiler, orm_1.TableAliasCompiler),
333
+ __metadata("design:type", Object)
334
+ ], SqlUpdateQueryCompiler.prototype, "this", void 0);
335
+ SqlUpdateQueryCompiler = __decorate([
336
+ (0, di_1.NewInstance)(),
337
+ (0, di_1.Inject)(di_1.Container),
338
+ __metadata("design:paramtypes", [Object, orm_1.UpdateQueryBuilder])
339
+ ], SqlUpdateQueryCompiler);
340
+ exports.SqlUpdateQueryCompiler = SqlUpdateQueryCompiler;
341
+ let SqlDeleteQueryCompiler = class SqlDeleteQueryCompiler extends SqlQueryCompiler {
342
+ constructor(_container, builder) {
343
+ super(builder, _container);
344
+ }
345
+ compile() {
346
+ const _bindings = [];
347
+ const _from = this.from();
348
+ const _limit = this.limit();
349
+ const _where = this.where(this._builder);
350
+ let _expression = '';
351
+ _expression = _from + (_where.expression ? ` WHERE ${_where.expression}` : '') + _limit.expression;
352
+ _bindings.push(..._where.bindings);
353
+ _bindings.push(..._limit.bindings);
354
+ return {
355
+ bindings: _bindings,
356
+ expression: _expression.trim(),
357
+ };
358
+ }
359
+ limit() {
360
+ const compiler = this._container.resolve(orm_1.LimitQueryCompiler, [this._builder]);
361
+ return compiler.compile();
362
+ }
363
+ from() {
364
+ return `DELETE FROM ${this._container.resolve(orm_1.TableAliasCompiler).compile(this._builder)}`;
365
+ }
366
+ };
367
+ __decorate([
368
+ (0, typescript_mix_1.use)(SqlWhereCompiler, orm_1.TableAliasCompiler),
369
+ __metadata("design:type", Object)
370
+ ], SqlDeleteQueryCompiler.prototype, "this", void 0);
371
+ SqlDeleteQueryCompiler = __decorate([
372
+ (0, di_1.NewInstance)(),
373
+ (0, di_1.Inject)(di_1.Container),
374
+ __metadata("design:paramtypes", [Object, orm_1.DeleteQueryBuilder])
375
+ ], SqlDeleteQueryCompiler);
376
+ exports.SqlDeleteQueryCompiler = SqlDeleteQueryCompiler;
377
+ let SqlOnDuplicateQueryCompiler = class SqlOnDuplicateQueryCompiler {
378
+ constructor(builder) {
379
+ this._builder = builder;
380
+ }
381
+ compile() {
382
+ const columns = this._builder
383
+ .getColumnsToUpdate()
384
+ .map((c) => {
385
+ if (lodash_1.default.isString(c)) {
386
+ return `\`${c}\` = ?`;
387
+ }
388
+ else {
389
+ return c.Query;
390
+ }
391
+ })
392
+ .join(',');
393
+ const parent = this._builder.getParent();
394
+ const valueMap = parent
395
+ .getColumns()
396
+ .map((c) => c.Column);
397
+ const bindings = this._builder.getColumnsToUpdate().map((c) => {
398
+ if (lodash_1.default.isString(c)) {
399
+ return parent.Values[0][valueMap.indexOf(c)];
400
+ }
401
+ else {
402
+ return c.Bindings;
403
+ }
404
+ });
405
+ return {
406
+ bindings,
407
+ expression: `ON DUPLICATE KEY UPDATE ${columns}`,
408
+ };
409
+ }
410
+ };
411
+ SqlOnDuplicateQueryCompiler = __decorate([
412
+ (0, di_1.NewInstance)(),
413
+ __metadata("design:paramtypes", [orm_1.OnDuplicateQueryBuilder])
414
+ ], SqlOnDuplicateQueryCompiler);
415
+ exports.SqlOnDuplicateQueryCompiler = SqlOnDuplicateQueryCompiler;
416
+ let SqlIndexQueryCompiler = class SqlIndexQueryCompiler extends orm_1.IndexQueryCompiler {
417
+ constructor(builder) {
418
+ super();
419
+ this._builder = builder;
420
+ }
421
+ compile() {
422
+ return {
423
+ bindings: [],
424
+ expression: `CREATE ${this._builder.Unique ? 'UNIQUE ' : ''}INDEX \`${this._builder.Name}\` ON \`${this._builder.Table}\` (${this._builder.Columns.map((c) => `\`${c}\``).join(',')});`,
425
+ };
426
+ }
427
+ };
428
+ SqlIndexQueryCompiler = __decorate([
429
+ (0, di_1.NewInstance)(),
430
+ __metadata("design:paramtypes", [orm_1.IndexQueryBuilder])
431
+ ], SqlIndexQueryCompiler);
432
+ exports.SqlIndexQueryCompiler = SqlIndexQueryCompiler;
433
+ let SqlInsertQueryCompiler = class SqlInsertQueryCompiler extends SqlQueryCompiler {
434
+ constructor(_container, builder) {
435
+ super(builder, _container);
436
+ }
437
+ compile() {
438
+ const into = this.into();
439
+ const columns = this.columns();
440
+ const values = this.values();
441
+ const upsort = this.upsort();
442
+ return {
443
+ bindings: values.bindings.concat(upsort.bindings),
444
+ expression: `${into} ${columns} ${values.data} ${upsort.expression}`.trim(),
445
+ };
446
+ }
447
+ upsort() {
448
+ if (this._builder.Update) {
449
+ return this._container.resolve(orm_1.OnDuplicateQueryCompiler, [this._builder.DuplicateQueryBuilder]).compile();
450
+ }
451
+ return {
452
+ bindings: [],
453
+ expression: '',
454
+ };
455
+ }
456
+ values() {
457
+ if (this._builder.Values.length === 0) {
458
+ throw new exceptions_1.InvalidArgument('values count invalid');
459
+ }
460
+ const bindings = [];
461
+ let data = 'VALUES ';
462
+ data += this._builder.Values.map((val) => {
463
+ const toInsert = val
464
+ .filter((v, i) => {
465
+ // eslint-disable-next-line security/detect-object-injection
466
+ const descriptor = this._builder.getColumns()[i].Descriptor;
467
+ if (descriptor) {
468
+ if (!descriptor.Nullable && (v === null || v === undefined) && !descriptor.AutoIncrement) {
469
+ throw new exceptions_1.InvalidArgument(`value column ${descriptor.Name} cannot be null`);
470
+ }
471
+ if (descriptor.AutoIncrement && descriptor.PrimaryKey)
472
+ return false;
473
+ }
474
+ return true;
475
+ })
476
+ .map((v) => {
477
+ if (v === undefined) {
478
+ return 'DEFAULT';
479
+ }
480
+ if (v === null) {
481
+ return 'NULL';
482
+ }
483
+ bindings.push(this.tryConvertValue(v));
484
+ return '?';
485
+ });
486
+ return `(` + toInsert.join(',') + ')';
487
+ }).join(',');
488
+ return {
489
+ bindings,
490
+ data,
491
+ };
492
+ }
493
+ columns() {
494
+ const columns = this._builder
495
+ .getColumns()
496
+ .filter((c) => {
497
+ const descriptor = c.Descriptor;
498
+ if (descriptor && descriptor.AutoIncrement && descriptor.PrimaryKey)
499
+ return false;
500
+ return true;
501
+ })
502
+ .map((c) => {
503
+ return c.Column;
504
+ })
505
+ .map((c) => {
506
+ return `\`${c instanceof orm_1.RawQuery ? c.Query : c}\``;
507
+ });
508
+ if (columns.length === 0) {
509
+ throw new exceptions_1.InvalidArgument('invalid column count');
510
+ }
511
+ return `(` + columns.join(',') + ')';
512
+ }
513
+ into() {
514
+ return `INSERT${this._builder.Ignore ? ' IGNORE' : ''} INTO ${this._container.resolve(orm_1.TableAliasCompiler).compile(this._builder)}`;
515
+ }
516
+ };
517
+ __decorate([
518
+ (0, typescript_mix_1.use)(orm_1.TableAliasCompiler),
519
+ __metadata("design:type", Object)
520
+ ], SqlInsertQueryCompiler.prototype, "this", void 0);
521
+ SqlInsertQueryCompiler = __decorate([
522
+ (0, di_1.NewInstance)(),
523
+ (0, di_1.Inject)(di_1.Container),
524
+ __metadata("design:paramtypes", [Object, orm_1.InsertQueryBuilder])
525
+ ], SqlInsertQueryCompiler);
526
+ exports.SqlInsertQueryCompiler = SqlInsertQueryCompiler;
527
+ let SqlDropTableQueryCompiler = class SqlDropTableQueryCompiler extends orm_1.DropTableCompiler {
528
+ constructor(container, builder) {
529
+ super();
530
+ this.container = container;
531
+ this.builder = builder;
532
+ }
533
+ compile() {
534
+ const exists = this.builder.Exists ? ' IF EXISTS' : '';
535
+ const exprr = `DROP TABLE${exists} ${this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder)}`;
536
+ return {
537
+ bindings: [],
538
+ expression: exprr,
539
+ };
540
+ }
541
+ };
542
+ SqlDropTableQueryCompiler = __decorate([
543
+ (0, di_1.NewInstance)(),
544
+ (0, di_1.Inject)(di_1.Container),
545
+ __metadata("design:paramtypes", [di_1.Container, orm_1.DropTableQueryBuilder])
546
+ ], SqlDropTableQueryCompiler);
547
+ exports.SqlDropTableQueryCompiler = SqlDropTableQueryCompiler;
548
+ let SqlAlterTableQueryCompiler = class SqlAlterTableQueryCompiler extends orm_1.AlterTableQueryCompiler {
549
+ constructor(container, builder) {
550
+ super();
551
+ this.container = container;
552
+ this.builder = builder;
553
+ }
554
+ compile() {
555
+ const _table = this._table();
556
+ let _outputs = [];
557
+ if (this.builder.DroppedColumns.length !== 0) {
558
+ _outputs = _outputs.concat(this.builder.DroppedColumns.map((c) => {
559
+ return {
560
+ bindings: [],
561
+ expression: `${_table} DROP COLUMN ${c}`,
562
+ };
563
+ }));
564
+ }
565
+ if (this.builder.NewTableName) {
566
+ _outputs.push({
567
+ bindings: [],
568
+ expression: `${_table} RENAME TO ${this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder, this.builder.NewTableName)}`,
569
+ });
570
+ }
571
+ if (this.builder.Columns.length !== 0) {
572
+ _outputs = _outputs.concat(this.builder.Columns.map((c) => {
573
+ const compiler = this.container.resolve(orm_1.AlterColumnQueryCompiler, [c]).compile();
574
+ return {
575
+ bindings: compiler.bindings,
576
+ expression: `${_table} ${compiler.expression}`,
577
+ };
578
+ }));
579
+ }
580
+ return _outputs;
581
+ }
582
+ _table() {
583
+ return `ALTER TABLE ${this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder)}`;
584
+ }
585
+ };
586
+ SqlAlterTableQueryCompiler = __decorate([
587
+ (0, di_1.NewInstance)(),
588
+ (0, di_1.Inject)(di_1.Container),
589
+ __metadata("design:paramtypes", [di_1.Container, orm_1.AlterTableQueryBuilder])
590
+ ], SqlAlterTableQueryCompiler);
591
+ exports.SqlAlterTableQueryCompiler = SqlAlterTableQueryCompiler;
592
+ let SqlTableCloneQueryCompiler = class SqlTableCloneQueryCompiler extends orm_1.TableCloneQueryCompiler {
593
+ constructor(container, builder) {
594
+ super();
595
+ this.container = container;
596
+ this.builder = builder;
597
+ }
598
+ compile() {
599
+ const _tblName = this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder, this.builder.CloneSource);
600
+ const _table = this._table();
601
+ const out1 = {
602
+ bindings: [],
603
+ expression: `${_table} LIKE ${_tblName}`,
604
+ };
605
+ if (!this.builder.Shallow) {
606
+ const fOut = this.builder.Filter !== undefined
607
+ ? this.builder.Filter.toDB()
608
+ : {
609
+ bindings: [],
610
+ // if no filter is provided, copy all the data
611
+ expression: `SELECT * FROM ${_tblName}`,
612
+ };
613
+ const fExprr = `INSERT INTO \`${this.builder.Table}\` ${fOut.expression}`;
614
+ return [
615
+ out1,
616
+ {
617
+ bindings: fOut.bindings,
618
+ expression: fExprr,
619
+ },
620
+ ];
621
+ }
622
+ return [out1];
623
+ }
624
+ _table() {
625
+ return `CREATE${this.builder.Temporary ? ' TEMPORARY ' : ' '}TABLE ${this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder)}`;
626
+ }
627
+ };
628
+ __decorate([
629
+ (0, typescript_mix_1.use)(orm_1.TableAliasCompiler),
630
+ __metadata("design:type", Object)
631
+ ], SqlTableCloneQueryCompiler.prototype, "this", void 0);
632
+ SqlTableCloneQueryCompiler = __decorate([
633
+ (0, di_1.NewInstance)(),
634
+ (0, di_1.Inject)(di_1.Container),
635
+ __metadata("design:paramtypes", [di_1.Container, orm_1.CloneTableQueryBuilder])
636
+ ], SqlTableCloneQueryCompiler);
637
+ exports.SqlTableCloneQueryCompiler = SqlTableCloneQueryCompiler;
638
+ let SqlTruncateTableQueryCompiler = class SqlTruncateTableQueryCompiler extends orm_1.TableQueryCompiler {
639
+ constructor(container, builder) {
640
+ super();
641
+ this.container = container;
642
+ this.builder = builder;
643
+ }
644
+ compile() {
645
+ return {
646
+ bindings: [],
647
+ expression: `TRUNCATE TABLE ${this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder)}`,
648
+ };
649
+ }
650
+ };
651
+ SqlTruncateTableQueryCompiler = __decorate([
652
+ (0, di_1.Inject)(di_1.Container),
653
+ (0, di_1.NewInstance)(),
654
+ __metadata("design:paramtypes", [di_1.Container, orm_1.TableQueryBuilder])
655
+ ], SqlTruncateTableQueryCompiler);
656
+ exports.SqlTruncateTableQueryCompiler = SqlTruncateTableQueryCompiler;
657
+ let SqlTableHistoryQueryCompiler = class SqlTableHistoryQueryCompiler extends orm_1.TableHistoryQueryCompiler {
658
+ constructor(container, builder) {
659
+ super();
660
+ this.container = container;
661
+ this.builder = builder;
662
+ }
663
+ compile() {
664
+ const tblAliasCompiler = this.container.resolve(orm_1.TableAliasCompiler);
665
+ const hTtblName = tblAliasCompiler.compile(this.builder, `${this.builder.Table}__history`);
666
+ const tblName = tblAliasCompiler.compile(this.builder, `${this.builder.Table}`);
667
+ const hTriggerName = `${this.builder.Table}__history`;
668
+ const tblTriggerName = this.builder.Table;
669
+ const dropUnique = this.builder.Columns.filter((c) => c.Unique).map((c) => {
670
+ return {
671
+ bindings: [],
672
+ expression: `ALTER TABLE ${hTtblName} DROP INDEX ${c.Name}`,
673
+ };
674
+ });
675
+ const pKey = this.builder.Columns.find((c) => c.PrimaryKey);
676
+ return [
677
+ // clone table
678
+ {
679
+ bindings: [],
680
+ expression: `CREATE TABLE ${hTtblName} LIKE ${tblAliasCompiler.compile(this.builder)}`,
681
+ },
682
+ ...dropUnique,
683
+ {
684
+ bindings: [],
685
+ expression: `ALTER TABLE ${hTtblName}
686
+ CHANGE COLUMN ${pKey.Name} ${pKey.Name} INT NOT NULL ,
687
+ DROP PRIMARY KEY;`,
688
+ },
689
+ // remove primary key & add history columns
690
+ {
691
+ bindings: [],
692
+ expression: `ALTER TABLE ${hTtblName} ADD __action__ VARCHAR(8) DEFAULT 'insert' FIRST, ADD __revision__ INT(6) NOT NULL AFTER __action__, ADD __start__ DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER __revision__, ADD __end__ DATETIME AFTER __start__`,
693
+ },
694
+ {
695
+ bindings: [],
696
+ expression: `ALTER TABLE ${hTtblName} ADD PRIMARY KEY (${pKey.Name}, __revision__)`,
697
+ },
698
+ {
699
+ bindings: [],
700
+ expression: `DELIMITER $$
701
+ CREATE TRIGGER ${hTriggerName}__insert_trigger BEFORE INSERT ON ${hTtblName} FOR EACH ROW
702
+ BEGIN
703
+ DECLARE rev INT;
704
+ SET rev = (SELECT IFNULL(MAX(__revision__), 0) FROM ${hTtblName} WHERE Id = NEW.Id);
705
+ SET NEW.__revision__ = rev + 1;
706
+ END;`,
707
+ },
708
+ // create tracking triggers
709
+ {
710
+ bindings: [],
711
+ expression: `DROP TRIGGER IF EXISTS ${tblTriggerName}__insert_trigger`,
712
+ },
713
+ {
714
+ bindings: [],
715
+ expression: `DROP TRIGGER IF EXISTS ${tblTriggerName}__update_trigger`,
716
+ },
717
+ {
718
+ bindings: [],
719
+ expression: `DROP TRIGGER IF EXISTS ${tblTriggerName}__delete_trigger`,
720
+ },
721
+ // insert into history table & update __end__ date for all operations
722
+ {
723
+ bindings: [],
724
+ expression: `DELIMITER $$
725
+ CREATE TRIGGER ${tblTriggerName}__insert_trigger AFTER INSERT ON ${tblName} FOR EACH ROW BEGIN
726
+ DECLARE rev INT;
727
+ SET rev = (SELECT IFNULL(MAX(__revision__), 0) FROM ${hTtblName} WHERE Id = NEW.Id);
728
+ UPDATE ${hTtblName} SET __end__ = NOW() WHERE Id = NEW.Id AND __revision__ = rev;
729
+ INSERT INTO ${hTtblName} SELECT 'insert', 0, NOW(), NULL, d.* FROM ${tblName} AS d WHERE d.${pKey.Name} = NEW.${pKey.Name};
730
+ END;`,
731
+ },
732
+ {
733
+ bindings: [],
734
+ expression: `DELIMITER $$
735
+ CREATE TRIGGER ${tblTriggerName}__update_trigger AFTER UPDATE ON ${tblName} FOR EACH ROW BEGIN
736
+ DECLARE rev INT;
737
+ SET rev = (SELECT IFNULL(MAX(__revision__), 0) FROM ${hTtblName} WHERE Id = NEW.Id);
738
+ UPDATE ${hTtblName} SET __end__ = NOW() WHERE Id = NEW.Id AND __revision__ = rev;
739
+ INSERT INTO ${hTtblName} SELECT 'update', 0, NOW(), NULL, d.* FROM ${tblName} AS d WHERE d.${pKey.Name} = NEW.${pKey.Name};
740
+ END;`,
741
+ },
742
+ {
743
+ bindings: [],
744
+ expression: `DELIMITER $$
745
+ CREATE TRIGGER ${tblTriggerName}__delete_trigger BEFORE DELETE ON ${tblName} FOR EACH ROW BEGIN
746
+ DECLARE rev INT;
747
+ SET rev = (SELECT IFNULL(MAX(__revision__), 0) FROM ${hTtblName} WHERE Id = NEW.Id);
748
+ UPDATE ${hTtblName} SET __end__ = NOW() WHERE Id = NEW.Id AND __revision__ = rev;
749
+ INSERT INTO ${hTtblName} SELECT 'delete', 0, NOW(), NULL, d.* FROM ${tblName} AS d WHERE d.${pKey.Name} = NEW.${pKey.Name};
750
+ END;`,
751
+ },
752
+ ];
753
+ }
754
+ };
755
+ SqlTableHistoryQueryCompiler = __decorate([
756
+ (0, di_1.NewInstance)(),
757
+ (0, di_1.Inject)(di_1.Container),
758
+ __metadata("design:paramtypes", [di_1.Container, orm_1.TableQueryBuilder])
759
+ ], SqlTableHistoryQueryCompiler);
760
+ exports.SqlTableHistoryQueryCompiler = SqlTableHistoryQueryCompiler;
761
+ let SqlTableQueryCompiler = class SqlTableQueryCompiler extends orm_1.TableQueryCompiler {
762
+ constructor(container, builder) {
763
+ super();
764
+ this.container = container;
765
+ this.builder = builder;
766
+ }
767
+ compile() {
768
+ const _table = this._table();
769
+ const _columns = this._columns();
770
+ const _keys = this._foreignKeys();
771
+ const _primaryKey = this._primaryKeys();
772
+ const createOutput = {
773
+ bindings: [],
774
+ expression: `${_table} (${_columns} ${_primaryKey ? ',' + _primaryKey : ''} ${_keys ? ',' + _keys : ''})`,
775
+ };
776
+ if (this.builder.TrackHistory) {
777
+ const hCompiler = this.container.resolve(orm_1.TableHistoryQueryCompiler, [this.builder]);
778
+ return [createOutput, ...hCompiler.compile()];
779
+ }
780
+ else {
781
+ return [createOutput];
782
+ }
783
+ }
784
+ _columns() {
785
+ return this.builder.Columns.map((c) => {
786
+ return this.container.resolve(orm_1.ColumnQueryCompiler, [c]).compile().expression;
787
+ }).join(',');
788
+ }
789
+ _foreignKeys() {
790
+ return this.builder.ForeignKeys.map((f) => {
791
+ return this.container.resolve(orm_1.ForeignKeyQueryCompiler, [f]).compile().expression;
792
+ }).join(',');
793
+ }
794
+ _primaryKeys() {
795
+ const _keys = this.builder.Columns.filter((x) => x.PrimaryKey)
796
+ .map((c) => `\`${c.Name}\``)
797
+ .join(',');
798
+ if (!lodash_1.default.isEmpty(_keys)) {
799
+ return `PRIMARY KEY (${_keys})`;
800
+ }
801
+ return '';
802
+ }
803
+ _table() {
804
+ return `CREATE${this.builder.Temporary ? ' TEMPORARY ' : ' '}TABLE ${this.builder.CheckExists ? 'IF NOT EXISTS ' : ''}${this.container.resolve(orm_1.TableAliasCompiler).compile(this.builder)}`;
805
+ }
806
+ };
807
+ SqlTableQueryCompiler = __decorate([
808
+ (0, di_1.NewInstance)(),
809
+ (0, di_1.Inject)(di_1.Container),
810
+ __metadata("design:paramtypes", [di_1.Container, orm_1.TableQueryBuilder])
811
+ ], SqlTableQueryCompiler);
812
+ exports.SqlTableQueryCompiler = SqlTableQueryCompiler;
813
+ let SqlColumnQueryCompiler = class SqlColumnQueryCompiler {
814
+ constructor(builder) {
815
+ this.builder = builder;
816
+ this._statementsMappings = {
817
+ set: (builder) => `SET(${builder.Args[0].map((a) => `'${a}\'`).join(',')})`,
818
+ string: (builder) => `VARCHAR(${builder.Args[0] ? builder.Args[0] : 255})`,
819
+ boolean: () => `TINYINT(1)`,
820
+ float: (builder) => {
821
+ const _precision = builder.Args[0] ? builder.Args[0] : 8;
822
+ const _scale = builder.Args[1] ? builder.Args[1] : 2;
823
+ return `${builder.Type.toUpperCase()}(${_precision},${_scale})`;
824
+ },
825
+ double: (builder) => this._statementsMappings.float(builder),
826
+ decimal: (builder) => this._statementsMappings.float(builder),
827
+ enum: (builder) => `${builder.Type.toUpperCase()}(${builder.Args[0].map((a) => `'${a}'`).join(',')})`,
828
+ binary: (builder) => `BINARY(${builder.Args[0] ?? 255}`,
829
+ smallint: (builder) => builder.Type.toUpperCase(),
830
+ tinyint: (builder) => builder.Type.toUpperCase(),
831
+ mediumint: (builder) => builder.Type.toUpperCase(),
832
+ int: (builder) => builder.Type.toUpperCase(),
833
+ bigint: (builder) => builder.Type.toUpperCase(),
834
+ tinytext: (builder) => builder.Type.toUpperCase(),
835
+ mediumtext: (builder) => builder.Type.toUpperCase(),
836
+ longtext: (builder) => builder.Type.toUpperCase(),
837
+ text: (builder) => builder.Type.toUpperCase(),
838
+ bit: (builder) => builder.Type.toUpperCase(),
839
+ date: (builder) => builder.Type.toUpperCase(),
840
+ time: (builder) => builder.Type.toUpperCase(),
841
+ dateTime: (builder) => builder.Type.toUpperCase(),
842
+ timestamp: (builder) => builder.Type.toUpperCase(),
843
+ json: (builder) => builder.Type.toUpperCase(),
844
+ tinyblob: (builder) => builder.Type.toUpperCase(),
845
+ mediumblob: (builder) => builder.Type.toUpperCase(),
846
+ longblob: (builder) => builder.Type.toUpperCase(),
847
+ // COLUMN ADDITIONA PROPS
848
+ unsigned: () => 'UNSIGNED',
849
+ charset: (builder) => `CHARACTER SET '${builder.Charset}'`,
850
+ collation: (builder) => `COLLATE '${builder.Collation}'`,
851
+ notnull: () => `NOT NULL`,
852
+ default: () => this._defaultCompiler(),
853
+ autoincrement: () => `AUTO_INCREMENT`,
854
+ comment: (builder) => `COMMENT '${builder.Comment}'`,
855
+ };
856
+ if (!builder) {
857
+ throw new Error('column query builder cannot be null');
858
+ }
859
+ }
860
+ compile() {
861
+ const _stmt = [];
862
+ _stmt.push(`\`${this.builder.Name}\``);
863
+ _stmt.push(this._statementsMappings[this.builder.Type](this.builder));
864
+ if (this.builder.Unsigned) {
865
+ _stmt.push(this._statementsMappings.unsigned());
866
+ }
867
+ if (this.builder.Charset) {
868
+ _stmt.push(this._statementsMappings.charset(this.builder));
869
+ }
870
+ if (this.builder.Collation) {
871
+ _stmt.push(this._statementsMappings.collation(this.builder));
872
+ }
873
+ if (this.builder.NotNull) {
874
+ _stmt.push(this._statementsMappings.notnull());
875
+ }
876
+ if (this.builder.Default) {
877
+ _stmt.push(this._statementsMappings.default());
878
+ }
879
+ if (this.builder.AutoIncrement) {
880
+ _stmt.push(this._statementsMappings.autoincrement());
881
+ }
882
+ if (this.builder.Comment) {
883
+ _stmt.push(this._statementsMappings.comment(this.builder));
884
+ }
885
+ if (this.builder.Unique) {
886
+ _stmt.push('UNIQUE');
887
+ }
888
+ return {
889
+ bindings: [],
890
+ expression: _stmt.filter((x) => !lodash_1.default.isEmpty(x)).join(' '),
891
+ };
892
+ }
893
+ _defaultCompiler() {
894
+ let _stmt = '';
895
+ if (lodash_1.default.isNil(this.builder.Default) || (lodash_1.default.isNil(this.builder.Default.Query) && lodash_1.default.isNil(this.builder.Default.Value))) {
896
+ return _stmt;
897
+ }
898
+ if (lodash_1.default.isString(this.builder.Default.Value)) {
899
+ _stmt = `DEFAULT '${this.builder.Default.Value.trim()}'`;
900
+ }
901
+ else if (lodash_1.default.isNumber(this.builder.Default.Value)) {
902
+ _stmt = `DEFAULT ${this.builder.Default.Value}`;
903
+ }
904
+ else if (this.builder.Default.Query instanceof orm_1.RawQuery) {
905
+ _stmt = `DEFAULT ${this.builder.Default.Query.Query}`;
906
+ }
907
+ return _stmt;
908
+ }
909
+ };
910
+ SqlColumnQueryCompiler = __decorate([
911
+ (0, di_1.NewInstance)(),
912
+ __metadata("design:paramtypes", [orm_1.ColumnQueryBuilder])
913
+ ], SqlColumnQueryCompiler);
914
+ exports.SqlColumnQueryCompiler = SqlColumnQueryCompiler;
915
+ let SqlAlterColumnQueryCompiler = class SqlAlterColumnQueryCompiler extends SqlColumnQueryCompiler {
916
+ constructor(builder) {
917
+ super(builder);
918
+ }
919
+ compile() {
920
+ const builder = this.builder;
921
+ if (builder.AlterType === orm_1.ColumnAlterationType.Rename) {
922
+ const bld = this.builder;
923
+ return {
924
+ bindings: [],
925
+ expression: `RENAME COLUMN \`${bld.OldName}\` TO \`${bld.Name}\``,
926
+ };
927
+ }
928
+ const cDefinition = super.compile();
929
+ if (builder.AlterType === orm_1.ColumnAlterationType.Add) {
930
+ return {
931
+ bindings: cDefinition.bindings,
932
+ expression: `ADD ${cDefinition.expression} ${builder.AfterColumn ? `AFTER \`${builder.AfterColumn}\`` : ''}`,
933
+ };
934
+ }
935
+ if (builder.AlterType === orm_1.ColumnAlterationType.Modify) {
936
+ return {
937
+ bindings: cDefinition.bindings,
938
+ expression: `MODIFY ${cDefinition.expression}`,
939
+ };
940
+ }
941
+ }
942
+ };
943
+ SqlAlterColumnQueryCompiler = __decorate([
944
+ (0, di_1.NewInstance)(),
945
+ __metadata("design:paramtypes", [orm_1.AlterColumnQueryBuilder])
946
+ ], SqlAlterColumnQueryCompiler);
947
+ exports.SqlAlterColumnQueryCompiler = SqlAlterColumnQueryCompiler;
948
+ let SqlEventQueryCompiler = class SqlEventQueryCompiler extends SqlQueryCompiler {
949
+ constructor(container, builder) {
950
+ super(builder, container);
951
+ }
952
+ compile() {
953
+ const schedule = this._createSchedule();
954
+ const action = this._action();
955
+ return {
956
+ bindings: action.bindings,
957
+ expression: `CREATE EVENT ${this._builder.Name} ON SCHEDULE ${schedule} DO BEGIN ${action.expression} END`,
958
+ };
959
+ }
960
+ _createSchedule() {
961
+ if (this._builder.FromNowInverval) {
962
+ return `AT CURRENT_TIMESTAMP + INTERVAL ${this._getInterval(this._builder.FromNowInverval)}`;
963
+ }
964
+ if (this._builder.At) {
965
+ return `AT ${this._builder.At.toFormat(`yyyy-mm-dd HH:mm:ss`)}`;
966
+ }
967
+ if (this._builder.EveryInterval) {
968
+ return `EVERY ${this._getInterval(this._builder.EveryInterval)}`;
969
+ }
970
+ }
971
+ _getInterval(desc) {
972
+ return Object.getOwnPropertyNames(desc)
973
+ .map((x) => {
974
+ if (desc[`${x}`] > 0) {
975
+ return `${desc[`${x}`]} ${x.toUpperCase()}`;
976
+ }
977
+ return null;
978
+ })
979
+ .find((x) => x !== null);
980
+ }
981
+ _action() {
982
+ if (this._builder.RawSql) {
983
+ const res = this._builder.RawSql.build();
984
+ return {
985
+ expression: res.Statements.join(';'),
986
+ bindings: res.Bindings,
987
+ };
988
+ }
989
+ else {
990
+ const qResult = this._builder.Queries.reduce((prev, curr) => {
991
+ const res = curr.toDB();
992
+ if (Array.isArray(res)) {
993
+ res.forEach((x) => {
994
+ prev.bindings = prev.bindings.concat(x.bindings);
995
+ prev.expression.push(x.expression);
996
+ });
997
+ }
998
+ else {
999
+ prev.bindings = prev.bindings.concat(res.bindings);
1000
+ prev.expression.push(res.expression);
1001
+ }
1002
+ return prev;
1003
+ }, {
1004
+ expression: [],
1005
+ bindings: [],
1006
+ });
1007
+ return {
1008
+ expression: qResult.expression.join(';'),
1009
+ bindings: qResult.bindings,
1010
+ };
1011
+ }
1012
+ }
1013
+ };
1014
+ SqlEventQueryCompiler = __decorate([
1015
+ (0, di_1.NewInstance)(),
1016
+ (0, di_1.Inject)(di_1.Container),
1017
+ __metadata("design:paramtypes", [Object, orm_1.EventQueryBuilder])
1018
+ ], SqlEventQueryCompiler);
1019
+ exports.SqlEventQueryCompiler = SqlEventQueryCompiler;
1020
+ let SqlDropEventQueryCompiler = class SqlDropEventQueryCompiler extends SqlQueryCompiler {
1021
+ constructor(container, builder) {
1022
+ super(builder, container);
1023
+ }
1024
+ compile() {
1025
+ return {
1026
+ bindings: [],
1027
+ expression: `DROP EVENT IF EXISTS ${this._builder.Name}`,
1028
+ };
1029
+ }
1030
+ };
1031
+ SqlDropEventQueryCompiler = __decorate([
1032
+ (0, di_1.NewInstance)(),
1033
+ (0, di_1.Inject)(di_1.Container),
1034
+ __metadata("design:paramtypes", [Object, orm_1.DropEventQueryBuilder])
1035
+ ], SqlDropEventQueryCompiler);
1036
+ exports.SqlDropEventQueryCompiler = SqlDropEventQueryCompiler;
1037
+ //# sourceMappingURL=compilers.js.map