@sqb/builder 4.0.9 → 4.0.13

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 (105) hide show
  1. package/dist/Serializable.d.ts +9 -0
  2. package/dist/Serializable.js +6 -0
  3. package/dist/SerializeContext.d.ts +47 -0
  4. package/dist/SerializeContext.js +138 -0
  5. package/dist/classes.d.ts +38 -0
  6. package/dist/classes.js +41 -0
  7. package/dist/enums.d.ts +84 -0
  8. package/dist/enums.js +91 -0
  9. package/dist/extensions.d.ts +4 -0
  10. package/dist/extensions.js +16 -0
  11. package/dist/helpers.d.ts +4 -0
  12. package/dist/helpers.js +31 -0
  13. package/dist/index.d.ts +20 -0
  14. package/dist/index.js +27 -0
  15. package/dist/op.initializers.d.ts +86 -0
  16. package/dist/op.initializers.js +158 -0
  17. package/dist/query/DeleteQuery.d.ts +24 -0
  18. package/dist/query/DeleteQuery.js +53 -0
  19. package/dist/query/InsertQuery.d.ts +23 -0
  20. package/dist/query/InsertQuery.js +76 -0
  21. package/dist/query/Query.d.ts +15 -0
  22. package/dist/query/Query.js +40 -0
  23. package/dist/query/ReturningQuery.d.ts +14 -0
  24. package/dist/query/ReturningQuery.js +44 -0
  25. package/dist/query/SelectQuery.d.ts +90 -0
  26. package/dist/query/SelectQuery.js +283 -0
  27. package/dist/query/UpdateQuery.d.ts +29 -0
  28. package/dist/query/UpdateQuery.js +80 -0
  29. package/dist/sql-objects/BaseField.d.ts +7 -0
  30. package/dist/sql-objects/BaseField.js +11 -0
  31. package/dist/sql-objects/CaseStatement.d.ts +42 -0
  32. package/dist/sql-objects/CaseStatement.js +85 -0
  33. package/dist/sql-objects/CountStatement.d.ts +20 -0
  34. package/dist/sql-objects/CountStatement.js +34 -0
  35. package/dist/sql-objects/Expression.d.ts +7 -0
  36. package/dist/sql-objects/Expression.js +7 -0
  37. package/dist/sql-objects/FieldExpression.d.ts +14 -0
  38. package/dist/sql-objects/FieldExpression.js +53 -0
  39. package/dist/sql-objects/GroupColumn.d.ts +8 -0
  40. package/dist/sql-objects/GroupColumn.js +38 -0
  41. package/dist/sql-objects/JoinStatement.d.ts +17 -0
  42. package/dist/sql-objects/JoinStatement.js +81 -0
  43. package/dist/sql-objects/Operator.d.ts +5 -0
  44. package/dist/sql-objects/Operator.js +7 -0
  45. package/dist/sql-objects/OrderColumn.d.ts +9 -0
  46. package/dist/sql-objects/OrderColumn.js +42 -0
  47. package/dist/sql-objects/ParamExpression.d.ts +23 -0
  48. package/dist/sql-objects/ParamExpression.js +53 -0
  49. package/dist/sql-objects/RawStatement.d.ts +9 -0
  50. package/dist/sql-objects/RawStatement.js +18 -0
  51. package/dist/sql-objects/ReturningColumn.d.ts +9 -0
  52. package/dist/sql-objects/ReturningColumn.js +32 -0
  53. package/dist/sql-objects/TableName.d.ts +11 -0
  54. package/dist/sql-objects/TableName.js +31 -0
  55. package/dist/sql-objects/operators/CompOperator.d.ts +16 -0
  56. package/dist/sql-objects/operators/CompOperator.js +87 -0
  57. package/dist/sql-objects/operators/LogicalOperator.d.ts +16 -0
  58. package/dist/sql-objects/operators/LogicalOperator.js +83 -0
  59. package/dist/sql-objects/operators/OpAnd.d.ts +5 -0
  60. package/dist/sql-objects/operators/OpAnd.js +12 -0
  61. package/dist/sql-objects/operators/OpBetween.d.ts +11 -0
  62. package/dist/sql-objects/operators/OpBetween.js +35 -0
  63. package/dist/sql-objects/operators/OpEq.d.ts +8 -0
  64. package/dist/sql-objects/operators/OpEq.js +13 -0
  65. package/dist/sql-objects/operators/OpExists.d.ts +11 -0
  66. package/dist/sql-objects/operators/OpExists.js +30 -0
  67. package/dist/sql-objects/operators/OpGt.d.ts +8 -0
  68. package/dist/sql-objects/operators/OpGt.js +13 -0
  69. package/dist/sql-objects/operators/OpGte.d.ts +8 -0
  70. package/dist/sql-objects/operators/OpGte.js +13 -0
  71. package/dist/sql-objects/operators/OpILike.d.ts +6 -0
  72. package/dist/sql-objects/operators/OpILike.js +13 -0
  73. package/dist/sql-objects/operators/OpIn.d.ts +9 -0
  74. package/dist/sql-objects/operators/OpIn.js +19 -0
  75. package/dist/sql-objects/operators/OpIs.d.ts +8 -0
  76. package/dist/sql-objects/operators/OpIs.js +13 -0
  77. package/dist/sql-objects/operators/OpIsNot.d.ts +8 -0
  78. package/dist/sql-objects/operators/OpIsNot.js +13 -0
  79. package/dist/sql-objects/operators/OpLike.d.ts +10 -0
  80. package/dist/sql-objects/operators/OpLike.js +22 -0
  81. package/dist/sql-objects/operators/OpLt.d.ts +8 -0
  82. package/dist/sql-objects/operators/OpLt.js +13 -0
  83. package/dist/sql-objects/operators/OpLte.d.ts +8 -0
  84. package/dist/sql-objects/operators/OpLte.js +13 -0
  85. package/dist/sql-objects/operators/OpNe.d.ts +8 -0
  86. package/dist/sql-objects/operators/OpNe.js +13 -0
  87. package/dist/sql-objects/operators/OpNotBetween.d.ts +6 -0
  88. package/dist/sql-objects/operators/OpNotBetween.js +13 -0
  89. package/dist/sql-objects/operators/OpNotExists.d.ts +6 -0
  90. package/dist/sql-objects/operators/OpNotExists.js +13 -0
  91. package/dist/sql-objects/operators/OpNotILike.d.ts +6 -0
  92. package/dist/sql-objects/operators/OpNotILike.js +13 -0
  93. package/dist/sql-objects/operators/OpNotIn.d.ts +6 -0
  94. package/dist/sql-objects/operators/OpNotIn.js +13 -0
  95. package/dist/sql-objects/operators/OpNotLike.d.ts +6 -0
  96. package/dist/sql-objects/operators/OpNotLike.js +13 -0
  97. package/dist/sql-objects/operators/OpOr.d.ts +5 -0
  98. package/dist/sql-objects/operators/OpOr.js +12 -0
  99. package/dist/sqlobject.initializers.d.ts +39 -0
  100. package/dist/sqlobject.initializers.js +90 -0
  101. package/dist/typeguards.d.ts +34 -0
  102. package/dist/typeguards.js +73 -0
  103. package/dist/types.d.ts +33 -0
  104. package/dist/types.js +2 -0
  105. package/package.json +2 -2
@@ -0,0 +1,283 @@
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;
@@ -0,0 +1,29 @@
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
+ }
@@ -0,0 +1,80 @@
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;
@@ -0,0 +1,7 @@
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
+ }
@@ -0,0 +1,11 @@
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;
@@ -0,0 +1,42 @@
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
+ }
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CaseStatement = void 0;
4
+ const Serializable_1 = require("../Serializable");
5
+ const enums_1 = require("../enums");
6
+ const OpAnd_1 = require("./operators/OpAnd");
7
+ class CaseStatement extends Serializable_1.Serializable {
8
+ constructor() {
9
+ super();
10
+ this._expressions = [];
11
+ }
12
+ get _type() {
13
+ return enums_1.SerializationType.CASE_STATEMENT;
14
+ }
15
+ /**
16
+ * Defines "when" part of Case expression.
17
+ */
18
+ when(...condition) {
19
+ if (condition.length)
20
+ this._condition = new OpAnd_1.OpAnd(...condition);
21
+ else
22
+ this._condition = undefined;
23
+ return this;
24
+ }
25
+ /**
26
+ * Defines "then" part of Case expression.
27
+ */
28
+ then(value) {
29
+ if (this._condition)
30
+ this._expressions.push({
31
+ condition: this._condition,
32
+ value
33
+ });
34
+ return this;
35
+ }
36
+ /**
37
+ * Defines "else" part of Case expression.
38
+ */
39
+ else(value) {
40
+ this._elseValue = value;
41
+ return this;
42
+ }
43
+ /**
44
+ * Sets alias to case expression.
45
+ */
46
+ as(alias) {
47
+ this._alias = alias;
48
+ return this;
49
+ }
50
+ /**
51
+ * Performs serialization
52
+ *
53
+ * @param {Object} ctx
54
+ * @return {string}
55
+ * @override
56
+ */
57
+ _serialize(ctx) {
58
+ if (!this._expressions.length)
59
+ return '';
60
+ const q = {
61
+ expressions: [],
62
+ elseValue: this._elseValue !== undefined ?
63
+ ctx.anyToSQL(this._elseValue) : undefined
64
+ };
65
+ for (const x of this._expressions) {
66
+ const o = {
67
+ condition: x.condition._serialize(ctx),
68
+ value: ctx.anyToSQL(x.value)
69
+ };
70
+ q.expressions.push(o);
71
+ }
72
+ return ctx.serialize(this._type, q, () => this.__defaultSerialize(ctx, q));
73
+ }
74
+ __defaultSerialize(ctx, o) {
75
+ let out = 'case\n\t';
76
+ for (const x of o.expressions) {
77
+ out += 'when ' + x.condition + ' then ' + x.value + '\n';
78
+ }
79
+ if (o.elseValue !== undefined)
80
+ out += 'else ' + o.elseValue + '\n';
81
+ out += '\bend' + (this._alias ? ' ' + this._alias : '');
82
+ return out;
83
+ }
84
+ }
85
+ exports.CaseStatement = CaseStatement;
@@ -0,0 +1,20 @@
1
+ import { Serializable } from '../Serializable';
2
+ import { SerializationType } from '../enums';
3
+ import { SerializeContext } from '../SerializeContext';
4
+ export declare class CountStatement extends Serializable {
5
+ _alias?: string;
6
+ get _type(): SerializationType;
7
+ /**
8
+ * Sets alias to case expression.
9
+ */
10
+ as(alias: string): this;
11
+ /**
12
+ * Performs serialization
13
+ *
14
+ * @param {Object} ctx
15
+ * @return {string}
16
+ * @override
17
+ */
18
+ _serialize(ctx: SerializeContext): string;
19
+ protected __defaultSerialize(ctx: SerializeContext, o: any): string;
20
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CountStatement = void 0;
4
+ const Serializable_1 = require("../Serializable");
5
+ const enums_1 = require("../enums");
6
+ class CountStatement extends Serializable_1.Serializable {
7
+ get _type() {
8
+ return enums_1.SerializationType.COUNT_STATEMENT;
9
+ }
10
+ /**
11
+ * Sets alias to case expression.
12
+ */
13
+ as(alias) {
14
+ this._alias = alias;
15
+ return this;
16
+ }
17
+ /**
18
+ * Performs serialization
19
+ *
20
+ * @param {Object} ctx
21
+ * @return {string}
22
+ * @override
23
+ */
24
+ _serialize(ctx) {
25
+ return ctx.serialize(this._type, undefined, () => this.__defaultSerialize(ctx, undefined));
26
+ }
27
+ // noinspection JSUnusedLocalSymbols
28
+ __defaultSerialize(
29
+ /* eslint-disable-next-line */
30
+ ctx, o) {
31
+ return 'count(*)';
32
+ }
33
+ }
34
+ exports.CountStatement = CountStatement;
@@ -0,0 +1,7 @@
1
+ import { Serializable } from '../Serializable';
2
+ import { DataType } from '../enums';
3
+ export declare abstract class Expression extends Serializable {
4
+ _dataType?: DataType;
5
+ _isArray?: boolean;
6
+ _isDataSet?: boolean;
7
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Expression = void 0;
4
+ const Serializable_1 = require("../Serializable");
5
+ class Expression extends Serializable_1.Serializable {
6
+ }
7
+ exports.Expression = Expression;
@@ -0,0 +1,14 @@
1
+ import { DataType, SerializationType } from '../enums';
2
+ import { BaseField } from './BaseField';
3
+ import { SerializeContext } from '../SerializeContext';
4
+ export declare class FieldExpression extends BaseField {
5
+ _alias?: string;
6
+ constructor(expression: string, dataType?: DataType, isArray?: boolean);
7
+ constructor(args: {
8
+ expression: string;
9
+ dataType?: DataType;
10
+ isArray?: boolean;
11
+ });
12
+ get _type(): SerializationType;
13
+ _serialize(ctx: SerializeContext): string;
14
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FieldExpression = void 0;
4
+ const enums_1 = require("../enums");
5
+ const BaseField_1 = require("./BaseField");
6
+ const TABLE_COLUMN_PATTERN = /^((?:[a-zA-Z_][\w$_]*\.){0,2}) *([0-9a-zA-Z_][\w$_]*|\*) *(?:as)? *([a-zA-Z_][\w$_]*)?$/;
7
+ class FieldExpression extends BaseField_1.BaseField {
8
+ constructor(arg0, arg1, arg2) {
9
+ super();
10
+ let expression;
11
+ if (typeof arg0 === 'object') {
12
+ expression = arg0.expression;
13
+ this._dataType = arg0.dataType;
14
+ this._isArray = arg0.isArray;
15
+ }
16
+ else {
17
+ expression = arg0;
18
+ this._dataType = arg1;
19
+ this._isArray = arg2;
20
+ }
21
+ const m = expression.match(TABLE_COLUMN_PATTERN);
22
+ if (!m)
23
+ throw new TypeError(`${expression} does not match table column format`);
24
+ this._field = m[2];
25
+ if (m[1]) {
26
+ const a = m[1].split(/\./g);
27
+ a.pop();
28
+ this._table = a.pop();
29
+ this._schema = a.pop();
30
+ }
31
+ this._alias = this._field !== '*' ? m[3] : '';
32
+ }
33
+ get _type() {
34
+ return enums_1.SerializationType.SELECT_COLUMN;
35
+ }
36
+ _serialize(ctx) {
37
+ const o = {
38
+ schema: this._schema,
39
+ table: this._table,
40
+ field: this._field,
41
+ alias: this._alias,
42
+ isReservedWord: !!(this._field && ctx.isReservedWord(this._field))
43
+ };
44
+ return ctx.serialize(this._type, o, () => {
45
+ const prefix = ctx.escapeReserved(this._schema ? this._schema + '.' : '') +
46
+ (this._table ? this._table + '.' : '');
47
+ return prefix +
48
+ (!prefix && o.isReservedWord ? '"' + this._field + '"' : this._field) +
49
+ (this._alias ? ' as ' + this._alias : '');
50
+ });
51
+ }
52
+ }
53
+ exports.FieldExpression = FieldExpression;
@@ -0,0 +1,8 @@
1
+ import { SerializationType } from '../enums';
2
+ import { BaseField } from './BaseField';
3
+ import { SerializeContext } from '../SerializeContext';
4
+ export declare class GroupColumn extends BaseField {
5
+ constructor(value: string);
6
+ get _type(): SerializationType;
7
+ _serialize(ctx: SerializeContext): string;
8
+ }