@sqb/builder 4.2.1 → 4.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/cjs/classes.ns.js +41 -0
- package/{dist → cjs}/enums.js +6 -3
- package/{dist → cjs}/extensions.js +0 -0
- package/{dist → cjs}/helpers.js +0 -0
- package/cjs/index.js +27 -0
- package/{dist → cjs}/op.initializers.js +42 -42
- package/cjs/package.json +3 -0
- package/{dist/query/DeleteQuery.js → cjs/query/delete-query.js} +13 -11
- package/{dist/query/InsertQuery.js → cjs/query/insert-query.js} +15 -13
- package/{dist/query/Query.js → cjs/query/query.js} +5 -4
- package/{dist/query/ReturningQuery.js → cjs/query/returning-query.js} +9 -8
- package/cjs/query/select-query.js +294 -0
- package/{dist/query/UpdateQuery.js → cjs/query/update-query.js} +17 -14
- package/{dist/Serializable.js → cjs/serializable.js} +0 -0
- package/{dist/SerializeContext.js → cjs/serialize-context.js} +36 -26
- package/cjs/sql-objects/base-field.js +11 -0
- package/{dist/sql-objects/CaseStatement.js → cjs/sql-objects/case-statement.js} +10 -6
- package/{dist/sql-objects/CoalesceStatement.js → cjs/sql-objects/coalesce-statement.js} +6 -4
- package/{dist/sql-objects/CountStatement.js → cjs/sql-objects/count-statement.js} +5 -4
- package/cjs/sql-objects/expression.js +10 -0
- package/{dist/sql-objects/FieldExpression.js → cjs/sql-objects/field-expression.js} +5 -4
- package/{dist/sql-objects/GroupColumn.js → cjs/sql-objects/group-column.js} +4 -4
- package/{dist/sql-objects/JoinStatement.js → cjs/sql-objects/join-statement.js} +20 -18
- package/{dist/sql-objects/LowerStatement.js → cjs/sql-objects/lower-statement.js} +6 -4
- package/cjs/sql-objects/max-statement.js +41 -0
- package/cjs/sql-objects/min-statement.js +41 -0
- package/{dist/sql-objects/Operator.js → cjs/sql-objects/operator.js} +2 -2
- package/{dist/sql-objects/operators/CompOperator.js → cjs/sql-objects/operators/comp-operator.js} +17 -13
- package/{dist/sql-objects/operators/LogicalOperator.js → cjs/sql-objects/operators/logical-operator.js} +11 -11
- package/cjs/sql-objects/operators/op-and.js +9 -0
- package/{dist/sql-objects/operators/OpBetween.js → cjs/sql-objects/operators/op-between.js} +5 -5
- package/cjs/sql-objects/operators/op-eq.js +13 -0
- package/{dist/sql-objects/operators/OpExists.js → cjs/sql-objects/operators/op-exists.js} +7 -7
- package/cjs/sql-objects/operators/op-gt.js +13 -0
- package/cjs/sql-objects/operators/op-gte.js +13 -0
- package/cjs/sql-objects/operators/op-ilike.js +10 -0
- package/cjs/sql-objects/operators/op-in.js +19 -0
- package/cjs/sql-objects/operators/op-is-not.js +13 -0
- package/cjs/sql-objects/operators/op-is.js +13 -0
- package/{dist/sql-objects/operators/OpLike.js → cjs/sql-objects/operators/op-like.js} +7 -7
- package/cjs/sql-objects/operators/op-lt.js +13 -0
- package/cjs/sql-objects/operators/op-lte.js +13 -0
- package/cjs/sql-objects/operators/op-ne.js +13 -0
- package/cjs/sql-objects/operators/op-not-between.js +10 -0
- package/cjs/sql-objects/operators/op-not-exists.js +10 -0
- package/cjs/sql-objects/operators/op-not-ilike.js +10 -0
- package/cjs/sql-objects/operators/op-not-in.js +10 -0
- package/cjs/sql-objects/operators/op-not-like.js +10 -0
- package/cjs/sql-objects/operators/op-or.js +9 -0
- package/{dist/sql-objects/OrderColumn.js → cjs/sql-objects/order-column.js} +5 -4
- package/{dist/sql-objects/ParamExpression.js → cjs/sql-objects/param-expression.js} +7 -4
- package/{dist/sql-objects/RawStatement.js → cjs/sql-objects/raw-statement.js} +5 -4
- package/{dist/sql-objects/ReturningColumn.js → cjs/sql-objects/returning-column.js} +5 -4
- package/cjs/sql-objects/sequence-getter-statement.js +51 -0
- package/cjs/sql-objects/string-agg-statement.js +82 -0
- package/{dist/sql-objects/TableName.js → cjs/sql-objects/table-name.js} +7 -4
- package/{dist/sql-objects/UpperStatement.js → cjs/sql-objects/upper-statement.js} +6 -4
- package/cjs/sqlobject.initializers.js +129 -0
- package/{dist → cjs}/typeguards.js +21 -21
- package/{dist → cjs}/types.js +0 -0
- package/esm/classes.ns.d.ts +38 -0
- package/esm/classes.ns.js +38 -0
- package/{dist → esm}/enums.d.ts +6 -3
- package/esm/enums.js +95 -0
- package/{dist → esm}/extensions.d.ts +1 -1
- package/esm/extensions.js +11 -0
- package/{dist → esm}/helpers.d.ts +0 -0
- package/esm/helpers.js +27 -0
- package/esm/index.d.ts +20 -0
- package/esm/index.js +20 -0
- package/{dist → esm}/op.initializers.d.ts +22 -22
- package/esm/op.initializers.js +126 -0
- package/{dist/query/DeleteQuery.d.ts → esm/query/delete-query.d.ts} +6 -6
- package/esm/query/delete-query.js +51 -0
- package/{dist/query/InsertQuery.d.ts → esm/query/insert-query.d.ts} +5 -5
- package/esm/query/insert-query.js +73 -0
- package/{dist/query/Query.d.ts → esm/query/query.d.ts} +2 -2
- package/esm/query/query.js +36 -0
- package/{dist/query/ReturningQuery.d.ts → esm/query/returning-query.d.ts} +3 -3
- package/esm/query/returning-query.js +41 -0
- package/{dist/query/SelectQuery.d.ts → esm/query/select-query.d.ts} +9 -9
- package/{dist/query/SelectQuery.js → esm/query/select-query.js} +38 -32
- package/{dist/query/UpdateQuery.d.ts → esm/query/update-query.d.ts} +6 -6
- package/esm/query/update-query.js +79 -0
- package/{dist/Serializable.d.ts → esm/serializable.d.ts} +2 -2
- package/esm/serializable.js +2 -0
- package/{dist/SerializeContext.d.ts → esm/serialize-context.d.ts} +1 -1
- package/esm/serialize-context.js +145 -0
- package/{dist/sql-objects/BaseField.d.ts → esm/sql-objects/base-field.d.ts} +1 -1
- package/esm/sql-objects/base-field.js +7 -0
- package/{dist/sql-objects/CaseStatement.d.ts → esm/sql-objects/case-statement.d.ts} +6 -6
- package/esm/sql-objects/case-statement.js +85 -0
- package/{dist/sql-objects/CoalesceStatement.d.ts → esm/sql-objects/coalesce-statement.d.ts} +3 -3
- package/esm/sql-objects/coalesce-statement.js +42 -0
- package/{dist/sql-objects/CountStatement.d.ts → esm/sql-objects/count-statement.d.ts} +3 -3
- package/esm/sql-objects/count-statement.js +31 -0
- package/{dist/sql-objects/Expression.d.ts → esm/sql-objects/expression.d.ts} +2 -2
- package/esm/sql-objects/expression.js +6 -0
- package/{dist/sql-objects/FieldExpression.d.ts → esm/sql-objects/field-expression.d.ts} +3 -3
- package/esm/sql-objects/field-expression.js +50 -0
- package/{dist/sql-objects/GroupColumn.d.ts → esm/sql-objects/group-column.d.ts} +3 -3
- package/esm/sql-objects/group-column.js +34 -0
- package/{dist/sql-objects/JoinStatement.d.ts → esm/sql-objects/join-statement.d.ts} +7 -7
- package/esm/sql-objects/join-statement.js +79 -0
- package/{dist/sql-objects/LowerStatement.d.ts → esm/sql-objects/lower-statement.d.ts} +3 -3
- package/esm/sql-objects/lower-statement.js +37 -0
- package/esm/sql-objects/max-statement.d.ts +22 -0
- package/esm/sql-objects/max-statement.js +37 -0
- package/esm/sql-objects/min-statement.d.ts +22 -0
- package/esm/sql-objects/min-statement.js +37 -0
- package/{dist/sql-objects/Operator.d.ts → esm/sql-objects/operator.d.ts} +2 -2
- package/esm/sql-objects/operator.js +3 -0
- package/{dist/sql-objects/operators/CompOperator.d.ts → esm/sql-objects/operators/comp-operator.d.ts} +4 -4
- package/esm/sql-objects/operators/comp-operator.js +86 -0
- package/{dist/sql-objects/operators/LogicalOperator.d.ts → esm/sql-objects/operators/logical-operator.d.ts} +4 -4
- package/esm/sql-objects/operators/logical-operator.js +79 -0
- package/esm/sql-objects/operators/op-and.d.ts +5 -0
- package/esm/sql-objects/operators/op-and.js +5 -0
- package/{dist/sql-objects/operators/OpBetween.d.ts → esm/sql-objects/operators/op-between.d.ts} +4 -4
- package/esm/sql-objects/operators/op-between.js +31 -0
- package/{dist/sql-objects/operators/OpEq.d.ts → esm/sql-objects/operators/op-eq.d.ts} +3 -3
- package/esm/sql-objects/operators/op-eq.js +9 -0
- package/{dist/sql-objects/operators/OpExists.d.ts → esm/sql-objects/operators/op-exists.d.ts} +4 -4
- package/esm/sql-objects/operators/op-exists.js +26 -0
- package/{dist/sql-objects/operators/OpGt.d.ts → esm/sql-objects/operators/op-gt.d.ts} +3 -3
- package/esm/sql-objects/operators/op-gt.js +9 -0
- package/{dist/sql-objects/operators/OpGte.d.ts → esm/sql-objects/operators/op-gte.d.ts} +3 -3
- package/esm/sql-objects/operators/op-gte.js +9 -0
- package/{dist/sql-objects/operators/OpILike.d.ts → esm/sql-objects/operators/op-ilike.d.ts} +2 -2
- package/esm/sql-objects/operators/op-ilike.js +6 -0
- package/{dist/sql-objects/operators/OpIn.d.ts → esm/sql-objects/operators/op-in.d.ts} +3 -3
- package/esm/sql-objects/operators/op-in.js +15 -0
- package/{dist/sql-objects/operators/OpIsNot.d.ts → esm/sql-objects/operators/op-is-not.d.ts} +3 -3
- package/esm/sql-objects/operators/op-is-not.js +9 -0
- package/{dist/sql-objects/operators/OpIs.d.ts → esm/sql-objects/operators/op-is.d.ts} +3 -3
- package/esm/sql-objects/operators/op-is.js +9 -0
- package/{dist/sql-objects/operators/OpLike.d.ts → esm/sql-objects/operators/op-like.d.ts} +4 -4
- package/esm/sql-objects/operators/op-like.js +18 -0
- package/{dist/sql-objects/operators/OpLt.d.ts → esm/sql-objects/operators/op-lt.d.ts} +3 -3
- package/esm/sql-objects/operators/op-lt.js +9 -0
- package/{dist/sql-objects/operators/OpLte.d.ts → esm/sql-objects/operators/op-lte.d.ts} +3 -3
- package/esm/sql-objects/operators/op-lte.js +9 -0
- package/{dist/sql-objects/operators/OpNe.d.ts → esm/sql-objects/operators/op-ne.d.ts} +3 -3
- package/esm/sql-objects/operators/op-ne.js +9 -0
- package/{dist/sql-objects/operators/OpNotBetween.d.ts → esm/sql-objects/operators/op-not-between.d.ts} +2 -2
- package/esm/sql-objects/operators/op-not-between.js +6 -0
- package/{dist/sql-objects/operators/OpNotExists.d.ts → esm/sql-objects/operators/op-not-exists.d.ts} +2 -2
- package/esm/sql-objects/operators/op-not-exists.js +6 -0
- package/{dist/sql-objects/operators/OpNotILike.d.ts → esm/sql-objects/operators/op-not-ilike.d.ts} +2 -2
- package/esm/sql-objects/operators/op-not-ilike.js +6 -0
- package/{dist/sql-objects/operators/OpNotIn.d.ts → esm/sql-objects/operators/op-not-in.d.ts} +2 -2
- package/esm/sql-objects/operators/op-not-in.js +6 -0
- package/{dist/sql-objects/operators/OpNotLike.d.ts → esm/sql-objects/operators/op-not-like.d.ts} +2 -2
- package/esm/sql-objects/operators/op-not-like.js +6 -0
- package/esm/sql-objects/operators/op-or.d.ts +5 -0
- package/esm/sql-objects/operators/op-or.js +5 -0
- package/{dist/sql-objects/OrderColumn.d.ts → esm/sql-objects/order-column.d.ts} +3 -3
- package/esm/sql-objects/order-column.js +39 -0
- package/{dist/sql-objects/ParamExpression.d.ts → esm/sql-objects/param-expression.d.ts} +3 -3
- package/esm/sql-objects/param-expression.js +52 -0
- package/{dist/sql-objects/RawStatement.d.ts → esm/sql-objects/raw-statement.d.ts} +3 -3
- package/esm/sql-objects/raw-statement.js +15 -0
- package/{dist/sql-objects/ReturningColumn.d.ts → esm/sql-objects/returning-column.d.ts} +3 -3
- package/esm/sql-objects/returning-column.js +29 -0
- package/esm/sql-objects/sequence-getter-statement.d.ts +24 -0
- package/esm/sql-objects/sequence-getter-statement.js +47 -0
- package/{dist/sql-objects/StringAggStatement.d.ts → esm/sql-objects/string-agg-statement.d.ts} +4 -4
- package/{dist/sql-objects/StringAggStatement.js → esm/sql-objects/string-agg-statement.js} +15 -15
- package/{dist/sql-objects/TableName.d.ts → esm/sql-objects/table-name.d.ts} +3 -3
- package/esm/sql-objects/table-name.js +30 -0
- package/{dist/sql-objects/UpperStatement.d.ts → esm/sql-objects/upper-statement.d.ts} +3 -3
- package/esm/sql-objects/upper-statement.js +37 -0
- package/{dist → esm}/sqlobject.initializers.d.ts +23 -16
- package/esm/sqlobject.initializers.js +101 -0
- package/{dist → esm}/typeguards.d.ts +17 -17
- package/esm/typeguards.js +57 -0
- package/{dist → esm}/types.d.ts +2 -2
- package/esm/types.js +1 -0
- package/package.json +48 -33
- package/dist/classes.d.ts +0 -38
- package/dist/classes.js +0 -41
- package/dist/index.d.ts +0 -20
- package/dist/index.js +0 -27
- package/dist/sql-objects/BaseField.js +0 -11
- package/dist/sql-objects/Expression.js +0 -7
- package/dist/sql-objects/operators/OpAnd.d.ts +0 -5
- package/dist/sql-objects/operators/OpAnd.js +0 -12
- package/dist/sql-objects/operators/OpEq.js +0 -13
- package/dist/sql-objects/operators/OpGt.js +0 -13
- package/dist/sql-objects/operators/OpGte.js +0 -13
- package/dist/sql-objects/operators/OpILike.js +0 -13
- package/dist/sql-objects/operators/OpIn.js +0 -19
- package/dist/sql-objects/operators/OpIs.js +0 -13
- package/dist/sql-objects/operators/OpIsNot.js +0 -13
- package/dist/sql-objects/operators/OpLt.js +0 -13
- package/dist/sql-objects/operators/OpLte.js +0 -13
- package/dist/sql-objects/operators/OpNe.js +0 -13
- package/dist/sql-objects/operators/OpNotBetween.js +0 -13
- package/dist/sql-objects/operators/OpNotExists.js +0 -13
- package/dist/sql-objects/operators/OpNotILike.js +0 -13
- package/dist/sql-objects/operators/OpNotIn.js +0 -13
- package/dist/sql-objects/operators/OpNotLike.js +0 -13
- package/dist/sql-objects/operators/OpOr.d.ts +0 -5
- package/dist/sql-objects/operators/OpOr.js +0 -12
- package/dist/sqlobject.initializers.js +0 -110
|
@@ -1,24 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { coerceToInt } from 'putil-varhelpers';
|
|
2
|
+
import { SerializationType } from '../enums.js';
|
|
3
|
+
import { printArray } from '../helpers.js';
|
|
4
|
+
import { FieldExpression } from '../sql-objects/field-expression.js';
|
|
5
|
+
import { GroupColumn } from '../sql-objects/group-column.js';
|
|
6
|
+
import { OpAnd } from '../sql-objects/operators/op-and.js';
|
|
7
|
+
import { OrderColumn } from '../sql-objects/order-column.js';
|
|
8
|
+
import { TableName } from '../sql-objects/table-name.js';
|
|
9
|
+
import { isJoinStatement, isSerializable } from '../typeguards.js';
|
|
10
|
+
import { Query } from './query.js';
|
|
11
|
+
export class SelectQuery extends Query {
|
|
12
|
+
_tables;
|
|
13
|
+
_columns;
|
|
14
|
+
_joins;
|
|
15
|
+
_where;
|
|
16
|
+
_groupBy;
|
|
17
|
+
_orderBy;
|
|
18
|
+
_limit;
|
|
19
|
+
_offset;
|
|
20
|
+
_alias;
|
|
21
|
+
_distinct;
|
|
15
22
|
constructor(...column) {
|
|
16
23
|
super();
|
|
17
24
|
if (column.length)
|
|
18
25
|
this.addColumn(...column);
|
|
19
26
|
}
|
|
20
27
|
get _type() {
|
|
21
|
-
return
|
|
28
|
+
return SerializationType.SELECT_QUERY;
|
|
22
29
|
}
|
|
23
30
|
/**
|
|
24
31
|
* Adds columns to query.
|
|
@@ -32,7 +39,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
32
39
|
if (Array.isArray(arg))
|
|
33
40
|
self.addColumn(...arg);
|
|
34
41
|
else
|
|
35
|
-
this._columns.push(
|
|
42
|
+
this._columns.push(isSerializable(arg) ? arg : new FieldExpression(arg));
|
|
36
43
|
}
|
|
37
44
|
return this;
|
|
38
45
|
}
|
|
@@ -44,7 +51,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
44
51
|
for (const arg of table) {
|
|
45
52
|
if (!arg)
|
|
46
53
|
continue;
|
|
47
|
-
this._tables.push(typeof arg === 'string' ? new
|
|
54
|
+
this._tables.push(typeof arg === 'string' ? new TableName(arg) : arg);
|
|
48
55
|
}
|
|
49
56
|
return this;
|
|
50
57
|
}
|
|
@@ -56,7 +63,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
56
63
|
for (const arg of join) {
|
|
57
64
|
if (!arg)
|
|
58
65
|
continue;
|
|
59
|
-
if (!
|
|
66
|
+
if (!isJoinStatement(arg))
|
|
60
67
|
throw new TypeError('Join statement required');
|
|
61
68
|
this._joins.push(arg);
|
|
62
69
|
}
|
|
@@ -66,7 +73,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
66
73
|
* Defines "where" part of query
|
|
67
74
|
*/
|
|
68
75
|
where(...condition) {
|
|
69
|
-
this._where = this._where || new
|
|
76
|
+
this._where = this._where || new OpAnd();
|
|
70
77
|
this._where.add(...condition);
|
|
71
78
|
return this;
|
|
72
79
|
}
|
|
@@ -78,7 +85,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
78
85
|
for (const arg of field) {
|
|
79
86
|
if (!arg)
|
|
80
87
|
continue;
|
|
81
|
-
this._groupBy.push(typeof arg === 'string' ? new
|
|
88
|
+
this._groupBy.push(typeof arg === 'string' ? new GroupColumn(arg) : arg);
|
|
82
89
|
}
|
|
83
90
|
return this;
|
|
84
91
|
}
|
|
@@ -90,7 +97,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
90
97
|
for (const arg of field) {
|
|
91
98
|
if (!arg)
|
|
92
99
|
continue;
|
|
93
|
-
this._orderBy.push(typeof arg === 'string' ? new
|
|
100
|
+
this._orderBy.push(typeof arg === 'string' ? new OrderColumn(arg) : arg);
|
|
94
101
|
}
|
|
95
102
|
return this;
|
|
96
103
|
}
|
|
@@ -105,14 +112,14 @@ class SelectQuery extends Query_1.Query {
|
|
|
105
112
|
* Sets limit for query
|
|
106
113
|
*/
|
|
107
114
|
limit(limit) {
|
|
108
|
-
this._limit =
|
|
115
|
+
this._limit = coerceToInt(limit);
|
|
109
116
|
return this;
|
|
110
117
|
}
|
|
111
118
|
/**
|
|
112
119
|
* Sets offset for query
|
|
113
120
|
*/
|
|
114
121
|
offset(offset) {
|
|
115
|
-
this._offset =
|
|
122
|
+
this._offset = coerceToInt(offset);
|
|
116
123
|
return this;
|
|
117
124
|
}
|
|
118
125
|
/**
|
|
@@ -195,7 +202,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
195
202
|
arr.push(s);
|
|
196
203
|
}
|
|
197
204
|
}
|
|
198
|
-
return ctx.serialize(
|
|
205
|
+
return ctx.serialize(SerializationType.SELECT_QUERY_COLUMNS, arr, () => printArray(arr) || '*');
|
|
199
206
|
}
|
|
200
207
|
/**
|
|
201
208
|
*
|
|
@@ -216,7 +223,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
216
223
|
arr.push(s);
|
|
217
224
|
}
|
|
218
225
|
}
|
|
219
|
-
return ctx.serialize(
|
|
226
|
+
return ctx.serialize(SerializationType.SELECT_QUERY_FROM, arr, () => {
|
|
220
227
|
const s = arr.join(',');
|
|
221
228
|
return s ? ('from' + (s.substring(0, 1) !== '\n' ? ' ' : '') + s) : '';
|
|
222
229
|
});
|
|
@@ -233,7 +240,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
233
240
|
if (s)
|
|
234
241
|
arr.push(s);
|
|
235
242
|
}
|
|
236
|
-
return ctx.serialize(
|
|
243
|
+
return ctx.serialize(SerializationType.SELECT_QUERY_JOIN, arr, () => {
|
|
237
244
|
return arr.join('\n');
|
|
238
245
|
});
|
|
239
246
|
}
|
|
@@ -244,7 +251,7 @@ class SelectQuery extends Query_1.Query {
|
|
|
244
251
|
if (!this._where)
|
|
245
252
|
return '';
|
|
246
253
|
const s = this._where._serialize(ctx);
|
|
247
|
-
return ctx.serialize(
|
|
254
|
+
return ctx.serialize(SerializationType.CONDITIONS_BLOCK, s, () => {
|
|
248
255
|
/* istanbul ignore next */
|
|
249
256
|
return s ? 'where ' + s : '';
|
|
250
257
|
});
|
|
@@ -261,8 +268,8 @@ class SelectQuery extends Query_1.Query {
|
|
|
261
268
|
if (s)
|
|
262
269
|
arr.push(s);
|
|
263
270
|
}
|
|
264
|
-
return ctx.serialize(
|
|
265
|
-
const s =
|
|
271
|
+
return ctx.serialize(SerializationType.SELECT_QUERY_GROUPBY, arr, () => {
|
|
272
|
+
const s = printArray(arr);
|
|
266
273
|
return s ? 'group by ' + s : '';
|
|
267
274
|
});
|
|
268
275
|
}
|
|
@@ -275,10 +282,9 @@ class SelectQuery extends Query_1.Query {
|
|
|
275
282
|
if (s)
|
|
276
283
|
arr.push(s);
|
|
277
284
|
}
|
|
278
|
-
return ctx.serialize(
|
|
279
|
-
const s =
|
|
285
|
+
return ctx.serialize(SerializationType.SELECT_QUERY_ORDERBY, arr, () => {
|
|
286
|
+
const s = printArray(arr);
|
|
280
287
|
return s ? 'order by ' + s : '';
|
|
281
288
|
});
|
|
282
289
|
}
|
|
283
290
|
}
|
|
284
|
-
exports.SelectQuery = SelectQuery;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { SerializationType } from '../enums';
|
|
2
|
-
import { SerializeContext } from '../
|
|
3
|
-
import { LogicalOperator } from '../sql-objects/operators/
|
|
4
|
-
import { RawStatement } from '../sql-objects/
|
|
5
|
-
import { TableName } from '../sql-objects/
|
|
6
|
-
import { ReturningQuery } from './
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { SerializeContext } from '../serialize-context.js';
|
|
3
|
+
import { LogicalOperator } from '../sql-objects/operators/logical-operator.js';
|
|
4
|
+
import { RawStatement } from '../sql-objects/raw-statement.js';
|
|
5
|
+
import { TableName } from '../sql-objects/table-name.js';
|
|
6
|
+
import { ReturningQuery } from './returning-query.js';
|
|
7
7
|
export declare class UpdateQuery extends ReturningQuery {
|
|
8
8
|
_table: TableName | RawStatement;
|
|
9
9
|
_input: any;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { printArray } from '../helpers.js';
|
|
3
|
+
import { OpAnd } from '../sql-objects/operators/op-and.js';
|
|
4
|
+
import { TableName } from '../sql-objects/table-name.js';
|
|
5
|
+
import { isRawStatement } from '../typeguards.js';
|
|
6
|
+
import { ReturningQuery } from './returning-query.js';
|
|
7
|
+
export class UpdateQuery extends ReturningQuery {
|
|
8
|
+
_table;
|
|
9
|
+
_input;
|
|
10
|
+
_where;
|
|
11
|
+
constructor(tableName, input) {
|
|
12
|
+
super();
|
|
13
|
+
if (!tableName || !(typeof tableName === 'string' || 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(tableName) : tableName;
|
|
19
|
+
this._input = input;
|
|
20
|
+
}
|
|
21
|
+
get _type() {
|
|
22
|
+
return SerializationType.UPDATE_QUERY;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Defines "where" part of query
|
|
26
|
+
*/
|
|
27
|
+
where(...operator) {
|
|
28
|
+
this._where = this._where || new 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
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return ctx.serialize(SerializationType.UPDATE_QUERY_VALUES, arr, () => {
|
|
63
|
+
const a = arr.map(o => o.field + ' = ' + o.value);
|
|
64
|
+
return 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(SerializationType.CONDITIONS_BLOCK, s, () => {
|
|
75
|
+
/* istanbul ignore next */
|
|
76
|
+
return s ? 'where ' + s : '';
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { SerializationType } from './enums';
|
|
2
|
-
import type { SerializeContext } from './
|
|
1
|
+
import type { SerializationType } from './enums.js';
|
|
2
|
+
import type { SerializeContext } from './serialize-context.js';
|
|
3
3
|
export declare abstract class Serializable {
|
|
4
4
|
abstract _type: SerializationType;
|
|
5
5
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DefaultSerializeFunction, GenerateOptions, ParamOptions } from './types';
|
|
1
|
+
import { DefaultSerializeFunction, GenerateOptions, ParamOptions } from './types.js';
|
|
2
2
|
export declare class SerializeContext implements GenerateOptions {
|
|
3
3
|
readonly reservedWords: string[];
|
|
4
4
|
dialect?: string;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { SerializationType } from './enums.js';
|
|
2
|
+
import { serializers } from './extensions.js';
|
|
3
|
+
import { Serializable } from './serializable.js';
|
|
4
|
+
import { isLogicalOperator, isQuery, isSerializable } from './typeguards.js';
|
|
5
|
+
export class SerializeContext {
|
|
6
|
+
reservedWords = [
|
|
7
|
+
'schema', 'table', 'field', 'index', 'foreign', 'key',
|
|
8
|
+
'select', 'insert', 'update', 'delete', 'with', 'merge',
|
|
9
|
+
'join', 'inner', 'outer', 'left', 'right', 'full',
|
|
10
|
+
'from', 'where', 'order', 'by', 'group', 'having',
|
|
11
|
+
'acs', 'ascending', 'dsc', 'descending', 'distinct',
|
|
12
|
+
'and', 'or', 'not', 'between', 'null', 'like', 'ilike',
|
|
13
|
+
'count', 'sum', 'average', 'avg', 'cascade', 'authorization',
|
|
14
|
+
'create', 'add', 'drop', 'alter', 'index', 'private', 'sequence',
|
|
15
|
+
'default', 'constraint', 'references', 'primary', 'foreign',
|
|
16
|
+
'user', 'password'
|
|
17
|
+
];
|
|
18
|
+
dialect;
|
|
19
|
+
prettyPrint;
|
|
20
|
+
params;
|
|
21
|
+
dialectVersion;
|
|
22
|
+
strictParams;
|
|
23
|
+
serializeHooks;
|
|
24
|
+
paramOptions;
|
|
25
|
+
preparedParams;
|
|
26
|
+
returningFields;
|
|
27
|
+
strictParamGenId;
|
|
28
|
+
constructor(opts) {
|
|
29
|
+
if (opts)
|
|
30
|
+
Object.assign(this, opts);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Performs a fallback mechanism, tries hook functions, extensions than default function to serialize
|
|
34
|
+
*/
|
|
35
|
+
serialize(type, o, fallback) {
|
|
36
|
+
if (this.serializeHooks) {
|
|
37
|
+
for (const hook of this.serializeHooks) {
|
|
38
|
+
const s = hook(this, type, o, fallback);
|
|
39
|
+
if (s != null)
|
|
40
|
+
return s;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
for (const ext of serializers) {
|
|
44
|
+
if (ext.dialect === this.dialect && ext.serialize) {
|
|
45
|
+
const s = ext.serialize(this, type, o, fallback);
|
|
46
|
+
if (s != null)
|
|
47
|
+
return s;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return fallback(this, o);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Serializes object
|
|
54
|
+
*/
|
|
55
|
+
anyToSQL(v) {
|
|
56
|
+
if (v == null)
|
|
57
|
+
return 'null';
|
|
58
|
+
if (Array.isArray(v)) {
|
|
59
|
+
const vv = v.map(x => this.anyToSQL(x));
|
|
60
|
+
return this.serialize(SerializationType.ARRAY, vv, () => '(' + v.join(',')) + ')';
|
|
61
|
+
}
|
|
62
|
+
if (typeof v === 'object') {
|
|
63
|
+
if (isSerializable(v)) {
|
|
64
|
+
const s = v._serialize(this);
|
|
65
|
+
return s ? (isQuery(v) || isLogicalOperator(v) ? '(' + s + ')' : s) :
|
|
66
|
+
/* istanbul ignore next */
|
|
67
|
+
'';
|
|
68
|
+
}
|
|
69
|
+
if (v instanceof Date) {
|
|
70
|
+
return this.serialize(SerializationType.DATE_VALUE, v, () => this.dateToSQL(v));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (typeof v === 'string') {
|
|
74
|
+
return this.serialize(SerializationType.STRING_VALUE, v, () => this.stringToSQL(v));
|
|
75
|
+
}
|
|
76
|
+
if (typeof v === 'boolean') {
|
|
77
|
+
return this.serialize(SerializationType.BOOLEAN_VALUE, v, () => this.booleanToSQL(v));
|
|
78
|
+
}
|
|
79
|
+
if (typeof v === 'number') {
|
|
80
|
+
return this.serialize(SerializationType.NUMBER_VALUE, v, () => this.numberToSQL(v));
|
|
81
|
+
}
|
|
82
|
+
if (v instanceof Serializable)
|
|
83
|
+
return v._serialize(this);
|
|
84
|
+
return v;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
89
|
+
stringToSQL(val) {
|
|
90
|
+
return '\'' + String(val).replace(/'/g, '\'\'') + '\'';
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
*
|
|
94
|
+
*/
|
|
95
|
+
booleanToSQL(val) {
|
|
96
|
+
return val ? 'true' : 'false';
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
*
|
|
100
|
+
*/
|
|
101
|
+
numberToSQL(val) {
|
|
102
|
+
return '' + val;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
107
|
+
dateToSQL(date) {
|
|
108
|
+
const d = date.getUTCDate();
|
|
109
|
+
const m = date.getUTCMonth() + 1;
|
|
110
|
+
const y = date.getUTCFullYear();
|
|
111
|
+
const h = date.getUTCHours();
|
|
112
|
+
const n = date.getUTCMinutes();
|
|
113
|
+
const s = date.getUTCSeconds();
|
|
114
|
+
let str = y + '-' + (m <= 9 ? '0' + m : m) + '-' + (d <= 9 ? '0' + d : d);
|
|
115
|
+
/* istanbul ignore else */
|
|
116
|
+
if (h + n + s)
|
|
117
|
+
str += ' ' + (h <= 9 ? '0' + h : h) + ':' +
|
|
118
|
+
(n <= 9 ? '0' + n : n) + ':' +
|
|
119
|
+
(s <= 9 ? '0' + s : s);
|
|
120
|
+
return '\'' + str + '\'';
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Check if a string value is a reserved word
|
|
124
|
+
*/
|
|
125
|
+
isReservedWord(s) {
|
|
126
|
+
if (!s)
|
|
127
|
+
return false;
|
|
128
|
+
if (this.reservedWords.includes(s.toLowerCase()))
|
|
129
|
+
return true;
|
|
130
|
+
for (const ext of serializers) {
|
|
131
|
+
if (ext.dialect === this.dialect && ext.isReservedWord) {
|
|
132
|
+
if (ext.isReservedWord(this, s))
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
escapeReserved(s) {
|
|
139
|
+
if (!s)
|
|
140
|
+
return '';
|
|
141
|
+
if (this.isReservedWord(s))
|
|
142
|
+
return '"' + s + '"';
|
|
143
|
+
return s;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { SerializationType } from '../enums';
|
|
2
|
-
import { Serializable } from '../
|
|
3
|
-
import { SerializeContext } from '../
|
|
4
|
-
import { Operator } from './
|
|
5
|
-
import { LogicalOperator } from './operators/
|
|
6
|
-
import { RawStatement } from './
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
|
+
import { SerializeContext } from '../serialize-context.js';
|
|
4
|
+
import { Operator } from './operator.js';
|
|
5
|
+
import { LogicalOperator } from './operators/logical-operator.js';
|
|
6
|
+
import { RawStatement } from './raw-statement.js';
|
|
7
7
|
export declare class CaseStatement extends Serializable {
|
|
8
8
|
_expressions: {
|
|
9
9
|
condition: Serializable;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
|
+
import { OpAnd } from './operators/op-and.js';
|
|
4
|
+
export class CaseStatement extends Serializable {
|
|
5
|
+
_expressions;
|
|
6
|
+
_elseValue;
|
|
7
|
+
_condition;
|
|
8
|
+
_alias;
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
this._expressions = [];
|
|
12
|
+
}
|
|
13
|
+
get _type() {
|
|
14
|
+
return SerializationType.CASE_STATEMENT;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Defines "when" part of Case expression.
|
|
18
|
+
*/
|
|
19
|
+
when(...condition) {
|
|
20
|
+
if (condition.length)
|
|
21
|
+
this._condition = new OpAnd(...condition);
|
|
22
|
+
else
|
|
23
|
+
this._condition = undefined;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Defines "then" part of Case expression.
|
|
28
|
+
*/
|
|
29
|
+
then(value) {
|
|
30
|
+
if (this._condition)
|
|
31
|
+
this._expressions.push({
|
|
32
|
+
condition: this._condition,
|
|
33
|
+
value
|
|
34
|
+
});
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Defines "else" part of Case expression.
|
|
39
|
+
*/
|
|
40
|
+
else(value) {
|
|
41
|
+
this._elseValue = value;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets alias to case expression.
|
|
46
|
+
*/
|
|
47
|
+
as(alias) {
|
|
48
|
+
this._alias = alias;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Performs serialization
|
|
53
|
+
*
|
|
54
|
+
* @param {Object} ctx
|
|
55
|
+
* @return {string}
|
|
56
|
+
* @override
|
|
57
|
+
*/
|
|
58
|
+
_serialize(ctx) {
|
|
59
|
+
if (!this._expressions.length)
|
|
60
|
+
return '';
|
|
61
|
+
const q = {
|
|
62
|
+
expressions: [],
|
|
63
|
+
elseValue: this._elseValue !== undefined ?
|
|
64
|
+
ctx.anyToSQL(this._elseValue) : undefined
|
|
65
|
+
};
|
|
66
|
+
for (const x of this._expressions) {
|
|
67
|
+
const o = {
|
|
68
|
+
condition: x.condition._serialize(ctx),
|
|
69
|
+
value: ctx.anyToSQL(x.value)
|
|
70
|
+
};
|
|
71
|
+
q.expressions.push(o);
|
|
72
|
+
}
|
|
73
|
+
return ctx.serialize(this._type, q, () => this.__defaultSerialize(ctx, q));
|
|
74
|
+
}
|
|
75
|
+
__defaultSerialize(ctx, o) {
|
|
76
|
+
let out = 'case\n\t';
|
|
77
|
+
for (const x of o.expressions) {
|
|
78
|
+
out += 'when ' + x.condition + ' then ' + x.value + '\n';
|
|
79
|
+
}
|
|
80
|
+
if (o.elseValue !== undefined)
|
|
81
|
+
out += 'else ' + o.elseValue + '\n';
|
|
82
|
+
out += '\bend' + (this._alias ? ' ' + this._alias : '');
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { SerializationType } from '../enums';
|
|
2
|
-
import { Serializable } from '../
|
|
3
|
-
import { SerializeContext } from '../
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
|
+
import { SerializeContext } from '../serialize-context.js';
|
|
4
4
|
export declare class CoalesceStatement extends Serializable {
|
|
5
5
|
_expressions: any[];
|
|
6
6
|
_alias?: string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
|
+
export class CoalesceStatement extends Serializable {
|
|
4
|
+
_expressions;
|
|
5
|
+
_alias;
|
|
6
|
+
constructor(...expressions) {
|
|
7
|
+
super();
|
|
8
|
+
this._expressions = expressions;
|
|
9
|
+
}
|
|
10
|
+
get _type() {
|
|
11
|
+
return SerializationType.COALESCE_STATEMENT;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Sets alias to case expression.
|
|
15
|
+
*/
|
|
16
|
+
as(alias) {
|
|
17
|
+
this._alias = alias;
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Performs serialization
|
|
22
|
+
*
|
|
23
|
+
* @param {Object} ctx
|
|
24
|
+
* @return {string}
|
|
25
|
+
* @override
|
|
26
|
+
*/
|
|
27
|
+
_serialize(ctx) {
|
|
28
|
+
if (!this._expressions.length)
|
|
29
|
+
return '';
|
|
30
|
+
const q = {
|
|
31
|
+
expressions: [],
|
|
32
|
+
};
|
|
33
|
+
for (const x of this._expressions) {
|
|
34
|
+
q.expressions.push(ctx.anyToSQL(x));
|
|
35
|
+
}
|
|
36
|
+
return ctx.serialize(this._type, q, () => this.__defaultSerialize(ctx, q));
|
|
37
|
+
}
|
|
38
|
+
__defaultSerialize(ctx, o) {
|
|
39
|
+
return 'coalesce(' + o.expressions.join(', ') + ')' +
|
|
40
|
+
(this._alias ? ' ' + this._alias : '');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { SerializationType } from '../enums';
|
|
2
|
-
import { Serializable } from '../
|
|
3
|
-
import { SerializeContext } from '../
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
|
+
import { SerializeContext } from '../serialize-context.js';
|
|
4
4
|
export declare class CountStatement extends Serializable {
|
|
5
5
|
_alias?: string;
|
|
6
6
|
get _type(): SerializationType;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { SerializationType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
|
+
export class CountStatement extends Serializable {
|
|
4
|
+
_alias;
|
|
5
|
+
get _type() {
|
|
6
|
+
return SerializationType.COUNT_STATEMENT;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Sets alias to case expression.
|
|
10
|
+
*/
|
|
11
|
+
as(alias) {
|
|
12
|
+
this._alias = alias;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Performs serialization
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} ctx
|
|
19
|
+
* @return {string}
|
|
20
|
+
* @override
|
|
21
|
+
*/
|
|
22
|
+
_serialize(ctx) {
|
|
23
|
+
return ctx.serialize(this._type, undefined, () => this.__defaultSerialize(ctx, undefined));
|
|
24
|
+
}
|
|
25
|
+
// noinspection JSUnusedLocalSymbols
|
|
26
|
+
__defaultSerialize(
|
|
27
|
+
/* eslint-disable-next-line */
|
|
28
|
+
ctx, o) {
|
|
29
|
+
return 'count(*)';
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { DataType } from '../enums';
|
|
2
|
-
import { Serializable } from '../
|
|
1
|
+
import { DataType } from '../enums.js';
|
|
2
|
+
import { Serializable } from '../serializable.js';
|
|
3
3
|
export declare abstract class Expression extends Serializable {
|
|
4
4
|
_dataType?: DataType;
|
|
5
5
|
_isArray?: boolean;
|