@sqb/builder 4.0.3 → 4.0.8

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 (106) hide show
  1. package/LICENSE +191 -21
  2. package/package.json +11 -10
  3. package/dist/Serializable.d.ts +0 -9
  4. package/dist/Serializable.js +0 -6
  5. package/dist/SerializeContext.d.ts +0 -47
  6. package/dist/SerializeContext.js +0 -138
  7. package/dist/classes.d.ts +0 -38
  8. package/dist/classes.js +0 -50
  9. package/dist/enums.d.ts +0 -84
  10. package/dist/enums.js +0 -91
  11. package/dist/extensions.d.ts +0 -4
  12. package/dist/extensions.js +0 -16
  13. package/dist/helpers.d.ts +0 -4
  14. package/dist/helpers.js +0 -31
  15. package/dist/index.d.ts +0 -20
  16. package/dist/index.js +0 -48
  17. package/dist/op.initializers.d.ts +0 -86
  18. package/dist/op.initializers.js +0 -158
  19. package/dist/query/DeleteQuery.d.ts +0 -24
  20. package/dist/query/DeleteQuery.js +0 -53
  21. package/dist/query/InsertQuery.d.ts +0 -23
  22. package/dist/query/InsertQuery.js +0 -76
  23. package/dist/query/Query.d.ts +0 -15
  24. package/dist/query/Query.js +0 -42
  25. package/dist/query/ReturningQuery.d.ts +0 -14
  26. package/dist/query/ReturningQuery.js +0 -44
  27. package/dist/query/SelectQuery.d.ts +0 -90
  28. package/dist/query/SelectQuery.js +0 -283
  29. package/dist/query/UpdateQuery.d.ts +0 -29
  30. package/dist/query/UpdateQuery.js +0 -80
  31. package/dist/sql-objects/BaseField.d.ts +0 -7
  32. package/dist/sql-objects/BaseField.js +0 -11
  33. package/dist/sql-objects/CaseStatement.d.ts +0 -42
  34. package/dist/sql-objects/CaseStatement.js +0 -85
  35. package/dist/sql-objects/CountStatement.d.ts +0 -20
  36. package/dist/sql-objects/CountStatement.js +0 -34
  37. package/dist/sql-objects/Expression.d.ts +0 -7
  38. package/dist/sql-objects/Expression.js +0 -7
  39. package/dist/sql-objects/FieldExpression.d.ts +0 -14
  40. package/dist/sql-objects/FieldExpression.js +0 -53
  41. package/dist/sql-objects/GroupColumn.d.ts +0 -8
  42. package/dist/sql-objects/GroupColumn.js +0 -38
  43. package/dist/sql-objects/JoinStatement.d.ts +0 -17
  44. package/dist/sql-objects/JoinStatement.js +0 -81
  45. package/dist/sql-objects/Operator.d.ts +0 -5
  46. package/dist/sql-objects/Operator.js +0 -7
  47. package/dist/sql-objects/OrderColumn.d.ts +0 -9
  48. package/dist/sql-objects/OrderColumn.js +0 -42
  49. package/dist/sql-objects/ParamExpression.d.ts +0 -23
  50. package/dist/sql-objects/ParamExpression.js +0 -53
  51. package/dist/sql-objects/RawStatement.d.ts +0 -9
  52. package/dist/sql-objects/RawStatement.js +0 -18
  53. package/dist/sql-objects/ReturningColumn.d.ts +0 -9
  54. package/dist/sql-objects/ReturningColumn.js +0 -32
  55. package/dist/sql-objects/TableName.d.ts +0 -11
  56. package/dist/sql-objects/TableName.js +0 -31
  57. package/dist/sql-objects/operators/CompOperator.d.ts +0 -16
  58. package/dist/sql-objects/operators/CompOperator.js +0 -87
  59. package/dist/sql-objects/operators/LogicalOperator.d.ts +0 -16
  60. package/dist/sql-objects/operators/LogicalOperator.js +0 -85
  61. package/dist/sql-objects/operators/OpAnd.d.ts +0 -5
  62. package/dist/sql-objects/operators/OpAnd.js +0 -12
  63. package/dist/sql-objects/operators/OpBetween.d.ts +0 -11
  64. package/dist/sql-objects/operators/OpBetween.js +0 -35
  65. package/dist/sql-objects/operators/OpEq.d.ts +0 -8
  66. package/dist/sql-objects/operators/OpEq.js +0 -13
  67. package/dist/sql-objects/operators/OpExists.d.ts +0 -11
  68. package/dist/sql-objects/operators/OpExists.js +0 -30
  69. package/dist/sql-objects/operators/OpGt.d.ts +0 -8
  70. package/dist/sql-objects/operators/OpGt.js +0 -13
  71. package/dist/sql-objects/operators/OpGte.d.ts +0 -8
  72. package/dist/sql-objects/operators/OpGte.js +0 -13
  73. package/dist/sql-objects/operators/OpILike.d.ts +0 -6
  74. package/dist/sql-objects/operators/OpILike.js +0 -13
  75. package/dist/sql-objects/operators/OpIn.d.ts +0 -9
  76. package/dist/sql-objects/operators/OpIn.js +0 -19
  77. package/dist/sql-objects/operators/OpIs.d.ts +0 -8
  78. package/dist/sql-objects/operators/OpIs.js +0 -13
  79. package/dist/sql-objects/operators/OpIsNot.d.ts +0 -8
  80. package/dist/sql-objects/operators/OpIsNot.js +0 -13
  81. package/dist/sql-objects/operators/OpLike.d.ts +0 -10
  82. package/dist/sql-objects/operators/OpLike.js +0 -22
  83. package/dist/sql-objects/operators/OpLt.d.ts +0 -8
  84. package/dist/sql-objects/operators/OpLt.js +0 -13
  85. package/dist/sql-objects/operators/OpLte.d.ts +0 -8
  86. package/dist/sql-objects/operators/OpLte.js +0 -13
  87. package/dist/sql-objects/operators/OpNe.d.ts +0 -8
  88. package/dist/sql-objects/operators/OpNe.js +0 -13
  89. package/dist/sql-objects/operators/OpNotBetween.d.ts +0 -6
  90. package/dist/sql-objects/operators/OpNotBetween.js +0 -13
  91. package/dist/sql-objects/operators/OpNotExists.d.ts +0 -6
  92. package/dist/sql-objects/operators/OpNotExists.js +0 -13
  93. package/dist/sql-objects/operators/OpNotILike.d.ts +0 -6
  94. package/dist/sql-objects/operators/OpNotILike.js +0 -13
  95. package/dist/sql-objects/operators/OpNotIn.d.ts +0 -6
  96. package/dist/sql-objects/operators/OpNotIn.js +0 -13
  97. package/dist/sql-objects/operators/OpNotLike.d.ts +0 -6
  98. package/dist/sql-objects/operators/OpNotLike.js +0 -13
  99. package/dist/sql-objects/operators/OpOr.d.ts +0 -5
  100. package/dist/sql-objects/operators/OpOr.js +0 -12
  101. package/dist/sqlobject.initializers.d.ts +0 -39
  102. package/dist/sqlobject.initializers.js +0 -90
  103. package/dist/typeguards.d.ts +0 -34
  104. package/dist/typeguards.js +0 -73
  105. package/dist/types.d.ts +0 -33
  106. package/dist/types.js +0 -2
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Query = void 0;
7
- const events_1 = require("events");
8
- const putil_merge_1 = __importDefault(require("putil-merge"));
9
- const putil_flattentext_1 = __importDefault(require("putil-flattentext"));
10
- const Serializable_1 = require("../Serializable");
11
- const SerializeContext_1 = require("../SerializeContext");
12
- class Query extends Serializable_1.Serializable {
13
- constructor() {
14
- super();
15
- events_1.EventEmitter.call(this);
16
- }
17
- /**
18
- * Generates Sql script
19
- */
20
- generate(options) {
21
- const ctx = new SerializeContext_1.SerializeContext(options);
22
- if (this._params)
23
- ctx.params = { ...ctx.params, ...this._params };
24
- ctx.serializeHooks = this.listeners('serialize');
25
- /* generate output */
26
- const sql = this._serialize(ctx);
27
- return {
28
- sql: (0, putil_flattentext_1.default)(sql, { noWrap: !ctx.prettyPrint }),
29
- params: ctx.preparedParams,
30
- paramOptions: ctx.paramOptions,
31
- returningFields: ctx.returningFields
32
- };
33
- }
34
- values(obj) {
35
- if (typeof obj !== 'object' || Array.isArray(obj))
36
- throw new TypeError('Invalid argument');
37
- this._params = obj;
38
- return this;
39
- }
40
- }
41
- exports.Query = Query;
42
- (0, putil_merge_1.default)(Query.prototype, events_1.EventEmitter.prototype, { descriptor: true });
@@ -1,14 +0,0 @@
1
- import { Query } from './Query';
2
- import { ReturningColumn } from '../sql-objects/ReturningColumn';
3
- import { SerializeContext } from '../SerializeContext';
4
- export declare abstract class ReturningQuery extends Query {
5
- _returningColumns?: ReturningColumn[];
6
- /**
7
- *
8
- */
9
- returning(...columns: string[]): this;
10
- /**
11
- *
12
- */
13
- protected __serializeReturning(ctx: SerializeContext): string;
14
- }
@@ -1,44 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReturningQuery = void 0;
4
- const Query_1 = require("./Query");
5
- const enums_1 = require("../enums");
6
- const ReturningColumn_1 = require("../sql-objects/ReturningColumn");
7
- const helpers_1 = require("../helpers");
8
- class ReturningQuery extends Query_1.Query {
9
- /**
10
- *
11
- */
12
- returning(...columns) {
13
- if (!columns)
14
- return this;
15
- // noinspection JSMismatchedCollectionQueryUpdate
16
- this._returningColumns = columns.length ?
17
- columns.reduce((a, v) => {
18
- if (v)
19
- a.push(new ReturningColumn_1.ReturningColumn(v));
20
- return a;
21
- }, []) : undefined;
22
- return this;
23
- }
24
- /**
25
- *
26
- */
27
- __serializeReturning(ctx) {
28
- if (!(this._returningColumns && this._returningColumns.length))
29
- return '';
30
- const arr = [];
31
- ctx.returningFields = [];
32
- for (const t of this._returningColumns) {
33
- const s = t._serialize(ctx);
34
- /* istanbul ignore else */
35
- if (s)
36
- arr.push(s);
37
- }
38
- return ctx.serialize(enums_1.SerializationType.RETURNING_BLOCK, arr, () => {
39
- const s = (0, helpers_1.printArray)(arr);
40
- return s ? 'returning ' + s : '';
41
- });
42
- }
43
- }
44
- exports.ReturningQuery = ReturningQuery;
@@ -1,90 +0,0 @@
1
- import { Query } from './Query';
2
- import { RawStatement } from '../sql-objects/RawStatement';
3
- import { SerializationType } from '../enums';
4
- import { JoinStatement } from '../sql-objects/JoinStatement';
5
- import { LogicalOperator } from '../sql-objects/operators/LogicalOperator';
6
- import { GroupColumn } from '../sql-objects/GroupColumn';
7
- import { OrderColumn } from '../sql-objects/OrderColumn';
8
- import { Serializable } from '../Serializable';
9
- import { SerializeContext } from '../SerializeContext';
10
- export declare class SelectQuery extends Query {
11
- _tables?: Serializable[];
12
- _columns?: Serializable[];
13
- _joins?: JoinStatement[];
14
- _where?: LogicalOperator;
15
- _groupBy?: (GroupColumn | RawStatement)[];
16
- _orderBy?: (OrderColumn | RawStatement)[];
17
- _limit?: number;
18
- _offset?: number;
19
- _alias?: string;
20
- _distinct?: boolean;
21
- constructor(...column: (string | string[] | Serializable)[]);
22
- get _type(): SerializationType;
23
- /**
24
- * Adds columns to query.
25
- */
26
- addColumn(...column: (string | string[] | Serializable)[]): this;
27
- /**
28
- * Defines "from" part of query.
29
- */
30
- from(...table: (string | RawStatement | SelectQuery)[]): this;
31
- /**
32
- * Adds "join" statements to query
33
- */
34
- join(...join: JoinStatement[]): this;
35
- /**
36
- * Defines "where" part of query
37
- */
38
- where(...condition: (Serializable | Object)[]): this;
39
- /**
40
- * Defines "where" part of query
41
- */
42
- groupBy(...field: (string | RawStatement)[]): this;
43
- /**
44
- * Defines "order by" part of query.
45
- */
46
- orderBy(...field: (string | RawStatement)[]): this;
47
- /**
48
- * Sets alias for sub-select queries
49
- */
50
- as(alias: any): this;
51
- /**
52
- * Sets limit for query
53
- */
54
- limit(limit: number): this;
55
- /**
56
- * Sets offset for query
57
- */
58
- offset(offset: number): this;
59
- /**
60
- * Enables distinct mode
61
- */
62
- distinct(): this;
63
- onFetch(listener: (...args: any[]) => void): this;
64
- onceFetch(listener: (...args: any[]) => void): this;
65
- /**
66
- * Performs serialization
67
- */
68
- _serialize(ctx: SerializeContext): string;
69
- /**
70
- *
71
- */
72
- protected __serializeSelectColumns(ctx: SerializeContext): string;
73
- /**
74
- *
75
- */
76
- protected __serializeFrom(ctx: SerializeContext): string;
77
- /**
78
- *
79
- */
80
- protected __serializeJoins(ctx: SerializeContext): string;
81
- /**
82
- *
83
- */
84
- protected __serializeWhere(ctx: SerializeContext): string;
85
- /**
86
- *
87
- */
88
- protected __serializeGroupColumns(ctx: SerializeContext): string;
89
- protected __serializeOrderColumns(ctx: SerializeContext): string;
90
- }
@@ -1,283 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SelectQuery = void 0;
4
- const putil_varhelpers_1 = require("putil-varhelpers");
5
- const Query_1 = require("./Query");
6
- const TableName_1 = require("../sql-objects/TableName");
7
- const FieldExpression_1 = require("../sql-objects/FieldExpression");
8
- const enums_1 = require("../enums");
9
- const GroupColumn_1 = require("../sql-objects/GroupColumn");
10
- const OrderColumn_1 = require("../sql-objects/OrderColumn");
11
- const OpAnd_1 = require("../sql-objects/operators/OpAnd");
12
- const typeguards_1 = require("../typeguards");
13
- const helpers_1 = require("../helpers");
14
- class SelectQuery extends Query_1.Query {
15
- constructor(...column) {
16
- super();
17
- if (column.length)
18
- this.addColumn(...column);
19
- }
20
- get _type() {
21
- return enums_1.SerializationType.SELECT_QUERY;
22
- }
23
- /**
24
- * Adds columns to query.
25
- */
26
- addColumn(...column) {
27
- const self = this;
28
- this._columns = this._columns || [];
29
- for (const arg of column) {
30
- if (!arg)
31
- continue;
32
- if (Array.isArray(arg))
33
- self.addColumn(...arg);
34
- else
35
- this._columns.push((0, typeguards_1.isSerializable)(arg) ? arg : new FieldExpression_1.FieldExpression(arg));
36
- }
37
- return this;
38
- }
39
- /**
40
- * Defines "from" part of query.
41
- */
42
- from(...table) {
43
- this._tables = [];
44
- for (const arg of table) {
45
- if (!arg)
46
- continue;
47
- this._tables.push(typeof arg === 'string' ? new TableName_1.TableName(arg) : arg);
48
- }
49
- return this;
50
- }
51
- /**
52
- * Adds "join" statements to query
53
- */
54
- join(...join) {
55
- this._joins = this._joins || [];
56
- for (const arg of join) {
57
- if (!arg)
58
- continue;
59
- if (!(0, typeguards_1.isJoinStatement)(arg))
60
- throw new TypeError('Join statement required');
61
- this._joins.push(arg);
62
- }
63
- return this;
64
- }
65
- /**
66
- * Defines "where" part of query
67
- */
68
- where(...condition) {
69
- this._where = this._where || new OpAnd_1.OpAnd();
70
- this._where.add(...condition);
71
- return this;
72
- }
73
- /**
74
- * Defines "where" part of query
75
- */
76
- groupBy(...field) {
77
- this._groupBy = this._groupBy || [];
78
- for (const arg of field) {
79
- if (!arg)
80
- continue;
81
- this._groupBy.push(typeof arg === 'string' ? new GroupColumn_1.GroupColumn(arg) : arg);
82
- }
83
- return this;
84
- }
85
- /**
86
- * Defines "order by" part of query.
87
- */
88
- orderBy(...field) {
89
- this._orderBy = this._orderBy || [];
90
- for (const arg of field) {
91
- if (!arg)
92
- continue;
93
- this._orderBy.push(typeof arg === 'string' ? new OrderColumn_1.OrderColumn(arg) : arg);
94
- }
95
- return this;
96
- }
97
- /**
98
- * Sets alias for sub-select queries
99
- */
100
- as(alias) {
101
- this._alias = alias;
102
- return this;
103
- }
104
- /**
105
- * Sets limit for query
106
- */
107
- limit(limit) {
108
- this._limit = (0, putil_varhelpers_1.coerceToInt)(limit);
109
- return this;
110
- }
111
- /**
112
- * Sets offset for query
113
- */
114
- offset(offset) {
115
- this._offset = (0, putil_varhelpers_1.coerceToInt)(offset);
116
- return this;
117
- }
118
- /**
119
- * Enables distinct mode
120
- */
121
- distinct() {
122
- this._distinct = true;
123
- return this;
124
- }
125
- onFetch(listener) {
126
- this.on('fetch', listener);
127
- return this;
128
- }
129
- onceFetch(listener) {
130
- this.once('fetch', listener);
131
- return this;
132
- }
133
- /**
134
- * Performs serialization
135
- */
136
- _serialize(ctx) {
137
- const o = {
138
- columns: this.__serializeSelectColumns(ctx),
139
- from: this.__serializeFrom(ctx),
140
- join: this.__serializeJoins(ctx),
141
- where: this.__serializeWhere(ctx),
142
- groupBy: this.__serializeGroupColumns(ctx),
143
- orderBy: this.__serializeOrderColumns(ctx),
144
- limit: this._limit,
145
- offset: this._offset
146
- };
147
- return ctx.serialize(this._type, o, () => {
148
- let out = 'select';
149
- if (this._distinct)
150
- out += ' distinct';
151
- // columns part
152
- /* istanbul ignore else */
153
- if (o.columns) {
154
- out += (o.columns.indexOf('\n') >= 0) ?
155
- '\n\t' + o.columns + '\b' :
156
- ' ' + o.columns;
157
- }
158
- // from part
159
- if (o.from) {
160
- out += (o.columns.length > 60 ||
161
- o.columns.indexOf('\n') >= 0 ? '\n' : ' ') +
162
- o.from;
163
- }
164
- // join part
165
- if (o.join)
166
- out += '\n' + o.join;
167
- // where part
168
- if (o.where)
169
- out += '\n' + o.where;
170
- // group by part
171
- if (o.groupBy)
172
- out += '\n' + o.groupBy;
173
- // order by part
174
- if (o.orderBy)
175
- out += '\n' + o.orderBy;
176
- return out;
177
- });
178
- }
179
- /**
180
- *
181
- */
182
- __serializeSelectColumns(ctx) {
183
- const arr = [];
184
- if (this._columns)
185
- for (const t of this._columns) {
186
- const s = t._serialize(ctx);
187
- if (s) {
188
- if (t instanceof SelectQuery) {
189
- if (!t._alias)
190
- throw new TypeError('Alias required for sub-select in columns');
191
- arr.push('(' + s + ') ' + t._alias);
192
- }
193
- else
194
- arr.push(s);
195
- }
196
- }
197
- return ctx.serialize(enums_1.SerializationType.SELECT_QUERY_COLUMNS, arr, () => (0, helpers_1.printArray)(arr) || '*');
198
- }
199
- /**
200
- *
201
- */
202
- __serializeFrom(ctx) {
203
- const arr = [];
204
- if (this._tables)
205
- for (const t of this._tables) {
206
- const s = t._serialize(ctx);
207
- /* istanbul ignore else */
208
- if (s) {
209
- if (t instanceof SelectQuery) {
210
- if (!t._alias)
211
- throw new TypeError('Alias required for sub-select in "from"');
212
- arr.push('\n\t(' + s + ') ' + t._alias);
213
- }
214
- else
215
- arr.push(s);
216
- }
217
- }
218
- return ctx.serialize(enums_1.SerializationType.SELECT_QUERY_FROM, arr, () => {
219
- const s = arr.join(',');
220
- return s ? ('from' + (s.substring(0, 1) !== '\n' ? ' ' : '') + s) : '';
221
- });
222
- }
223
- /**
224
- *
225
- */
226
- __serializeJoins(ctx) {
227
- const arr = [];
228
- if (this._joins)
229
- for (const t of this._joins) {
230
- const s = t._serialize(ctx);
231
- /* istanbul ignore else */
232
- if (s)
233
- arr.push(s);
234
- }
235
- return ctx.serialize(enums_1.SerializationType.SELECT_QUERY_JOIN, arr, () => {
236
- return arr.join('\n');
237
- });
238
- }
239
- /**
240
- *
241
- */
242
- __serializeWhere(ctx) {
243
- if (!this._where)
244
- return '';
245
- const s = this._where._serialize(ctx);
246
- return ctx.serialize(enums_1.SerializationType.CONDITIONS_BLOCK, s, () => {
247
- /* istanbul ignore next */
248
- return s ? 'where ' + s : '';
249
- });
250
- }
251
- /**
252
- *
253
- */
254
- __serializeGroupColumns(ctx) {
255
- const arr = [];
256
- if (this._groupBy)
257
- for (const t of this._groupBy) {
258
- const s = t._serialize(ctx);
259
- /* istanbul ignore else */
260
- if (s)
261
- arr.push(s);
262
- }
263
- return ctx.serialize(enums_1.SerializationType.SELECT_QUERY_GROUPBY, arr, () => {
264
- const s = (0, helpers_1.printArray)(arr);
265
- return s ? 'group by ' + s : '';
266
- });
267
- }
268
- __serializeOrderColumns(ctx) {
269
- const arr = [];
270
- if (this._orderBy)
271
- for (const t of this._orderBy) {
272
- const s = t._serialize(ctx);
273
- /* istanbul ignore else */
274
- if (s)
275
- arr.push(s);
276
- }
277
- return ctx.serialize(enums_1.SerializationType.SELECT_QUERY_ORDERBY, arr, () => {
278
- const s = (0, helpers_1.printArray)(arr);
279
- return s ? 'order by ' + s : '';
280
- });
281
- }
282
- }
283
- exports.SelectQuery = SelectQuery;
@@ -1,29 +0,0 @@
1
- import { ReturningQuery } from './ReturningQuery';
2
- import { SerializationType } from '../enums';
3
- import { RawStatement } from '../sql-objects/RawStatement';
4
- import { TableName } from '../sql-objects/TableName';
5
- import { LogicalOperator } from '../sql-objects/operators/LogicalOperator';
6
- import { SerializeContext } from '../SerializeContext';
7
- export declare class UpdateQuery extends ReturningQuery {
8
- _table: TableName | RawStatement;
9
- _input: any;
10
- _where?: LogicalOperator;
11
- constructor(tableName: string | RawStatement, input: any);
12
- get _type(): SerializationType;
13
- /**
14
- * Defines "where" part of query
15
- */
16
- where(...operator: any[]): this;
17
- /**
18
- * Performs serialization
19
- */
20
- _serialize(ctx: SerializeContext): string;
21
- /**
22
- *
23
- */
24
- protected __serializeValues(ctx: SerializeContext): string;
25
- /**
26
- *
27
- */
28
- protected __serializeWhere(ctx: SerializeContext): string;
29
- }
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.UpdateQuery = void 0;
4
- const ReturningQuery_1 = require("./ReturningQuery");
5
- const enums_1 = require("../enums");
6
- const TableName_1 = require("../sql-objects/TableName");
7
- const OpAnd_1 = require("../sql-objects/operators/OpAnd");
8
- const typeguards_1 = require("../typeguards");
9
- const helpers_1 = require("../helpers");
10
- class UpdateQuery extends ReturningQuery_1.ReturningQuery {
11
- constructor(tableName, input) {
12
- super();
13
- if (!tableName || !(typeof tableName === 'string' || (0, typeguards_1.isRawStatement)(tableName)))
14
- throw new TypeError('String or Raw instance required as first argument (tableName) for UpdateQuery');
15
- if (!input || !((typeof input === 'object' && !Array.isArray(input)) ||
16
- input.isSelect))
17
- throw new TypeError('Object or Raw instance required as second argument (input) for UpdateQuery');
18
- this._table = typeof tableName === 'string' ? new TableName_1.TableName(tableName) : tableName;
19
- this._input = input;
20
- }
21
- get _type() {
22
- return enums_1.SerializationType.UPDATE_QUERY;
23
- }
24
- /**
25
- * Defines "where" part of query
26
- */
27
- where(...operator) {
28
- this._where = this._where || new OpAnd_1.OpAnd();
29
- this._where.add(...operator);
30
- return this;
31
- }
32
- /**
33
- * Performs serialization
34
- */
35
- _serialize(ctx) {
36
- const o = {
37
- table: this._table._serialize(ctx),
38
- values: this.__serializeValues(ctx),
39
- where: this.__serializeWhere(ctx),
40
- returning: this.__serializeReturning(ctx)
41
- };
42
- let out = 'update ' + o.table + ' set \n\t' + o.values + '\b';
43
- if (o.where)
44
- out += '\n' + o.where;
45
- if (o.returning)
46
- out += '\n' + o.returning;
47
- return out;
48
- }
49
- /**
50
- *
51
- */
52
- __serializeValues(ctx) {
53
- const arr = [];
54
- const allValues = this._input;
55
- for (const n of Object.getOwnPropertyNames(allValues)) {
56
- const value = ctx.anyToSQL(allValues[n]);
57
- arr.push({
58
- field: n,
59
- value: (0, typeguards_1.isSelectQuery)(allValues[n]) ? '(' + value + ')' : value
60
- });
61
- }
62
- return ctx.serialize(enums_1.SerializationType.UPDATE_QUERY_VALUES, arr, () => {
63
- const a = arr.map(o => o.field + ' = ' + o.value);
64
- return (0, helpers_1.printArray)(a, ',');
65
- });
66
- }
67
- /**
68
- *
69
- */
70
- __serializeWhere(ctx) {
71
- if (!this._where)
72
- return '';
73
- const s = this._where._serialize(ctx);
74
- return ctx.serialize(enums_1.SerializationType.CONDITIONS_BLOCK, s, () => {
75
- /* istanbul ignore next */
76
- return s ? 'where ' + s : '';
77
- });
78
- }
79
- }
80
- exports.UpdateQuery = UpdateQuery;
@@ -1,7 +0,0 @@
1
- import { Expression } from './Expression';
2
- export declare abstract class BaseField extends Expression {
3
- _field: string;
4
- _schema?: string;
5
- _table?: string;
6
- _descending?: boolean;
7
- }
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseField = void 0;
4
- const Expression_1 = require("./Expression");
5
- class BaseField extends Expression_1.Expression {
6
- constructor() {
7
- super(...arguments);
8
- this._field = '';
9
- }
10
- }
11
- exports.BaseField = BaseField;
@@ -1,42 +0,0 @@
1
- import { Serializable } from '../Serializable';
2
- import { SerializationType } from '../enums';
3
- import { LogicalOperator } from './operators/LogicalOperator';
4
- import { Operator } from './Operator';
5
- import { RawStatement } from './RawStatement';
6
- import { SerializeContext } from '../SerializeContext';
7
- export declare class CaseStatement extends Serializable {
8
- _expressions: {
9
- condition: Serializable;
10
- value: any;
11
- }[];
12
- _elseValue: any;
13
- _condition?: LogicalOperator;
14
- _alias?: string;
15
- constructor();
16
- get _type(): SerializationType;
17
- /**
18
- * Defines "when" part of Case expression.
19
- */
20
- when(...condition: (Operator | RawStatement)[]): this;
21
- /**
22
- * Defines "then" part of Case expression.
23
- */
24
- then(value: any): this;
25
- /**
26
- * Defines "else" part of Case expression.
27
- */
28
- else(value: any): this;
29
- /**
30
- * Sets alias to case expression.
31
- */
32
- as(alias: string): this;
33
- /**
34
- * Performs serialization
35
- *
36
- * @param {Object} ctx
37
- * @return {string}
38
- * @override
39
- */
40
- _serialize(ctx: SerializeContext): string;
41
- protected __defaultSerialize(ctx: SerializeContext, o: any): string;
42
- }