@riao/dbal 1.1.3 → 3.0.0
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 +5 -1
- package/builder/builder.d.ts +1 -46
- package/builder/builder.js +1 -125
- package/builder/builder.js.map +1 -1
- package/builder/index.d.ts +1 -0
- package/builder/index.js +3 -1
- package/builder/index.js.map +1 -1
- package/builder/sql-builder.d.ts +40 -0
- package/builder/sql-builder.js +114 -0
- package/builder/sql-builder.js.map +1 -0
- package/builder/statement-builder.d.ts +10 -0
- package/builder/statement-builder.js +24 -0
- package/builder/statement-builder.js.map +1 -0
- package/column/column-options.d.ts +1 -1
- package/comparison/comparison-aliases.d.ts +10 -0
- package/comparison/comparison-aliases.js +41 -0
- package/comparison/comparison-aliases.js.map +1 -0
- package/comparison/comparison-token.d.ts +17 -0
- package/comparison/comparison-token.js +16 -0
- package/comparison/comparison-token.js.map +1 -0
- package/comparison/comparison.d.ts +13 -0
- package/comparison/comparison.js +72 -0
- package/comparison/comparison.js.map +1 -0
- package/comparison/index.d.ts +4 -0
- package/comparison/index.js +25 -0
- package/comparison/index.js.map +1 -0
- package/comparison/is-comparison-token.d.ts +2 -0
- package/comparison/is-comparison-token.js +9 -0
- package/comparison/is-comparison-token.js.map +1 -0
- package/database/database.d.ts +0 -6
- package/database/database.js +1 -8
- package/database/database.js.map +1 -1
- package/ddl/ddl-builder.d.ts +7 -2
- package/ddl/ddl-builder.js +105 -91
- package/ddl/ddl-builder.js.map +1 -1
- package/ddl/ddl-repository.js +15 -14
- package/ddl/ddl-repository.js.map +1 -1
- package/dml/delete.d.ts +2 -2
- package/dml/group-by.d.ts +4 -0
- package/dml/group-by.js +3 -0
- package/dml/group-by.js.map +1 -0
- package/dml/index.d.ts +2 -2
- package/dml/index.js +2 -2
- package/dml/index.js.map +1 -1
- package/dml/insert.d.ts +7 -4
- package/dml/join.d.ts +3 -3
- package/dml/query-builder.d.ts +41 -20
- package/dml/query-builder.js +301 -172
- package/dml/query-builder.js.map +1 -1
- package/dml/query-repository.d.ts +7 -1
- package/dml/query-repository.js +66 -13
- package/dml/query-repository.js.map +1 -1
- package/dml/select.d.ts +9 -6
- package/dml/subquery.d.ts +5 -0
- package/dml/subquery.js +11 -0
- package/dml/subquery.js.map +1 -0
- package/dml/update.d.ts +2 -2
- package/expression/expression-token.d.ts +9 -0
- package/expression/expression-token.js +11 -0
- package/expression/expression-token.js.map +1 -0
- package/expression/expression.d.ts +6 -0
- package/expression/expression.js +3 -0
- package/expression/expression.js.map +1 -0
- package/expression/identifier.d.ts +7 -0
- package/expression/identifier.js +16 -0
- package/expression/identifier.js.map +1 -0
- package/expression/index.d.ts +5 -0
- package/expression/index.js +24 -0
- package/expression/index.js.map +1 -0
- package/expression/is-expression-token.d.ts +2 -0
- package/expression/is-expression-token.js +8 -0
- package/expression/is-expression-token.js.map +1 -0
- package/expression/key-val-expression.d.ts +7 -0
- package/expression/key-val-expression.js +3 -0
- package/expression/key-val-expression.js.map +1 -0
- package/expression/literal.d.ts +2 -0
- package/expression/literal.js +3 -0
- package/expression/literal.js.map +1 -0
- package/expression/logical.d.ts +18 -0
- package/expression/logical.js +31 -0
- package/expression/logical.js.map +1 -0
- package/expression/simple-expression.d.ts +5 -0
- package/expression/simple-expression.js +3 -0
- package/expression/simple-expression.js.map +1 -0
- package/functions/function-aliases.d.ts +3 -0
- package/functions/function-aliases.js +13 -0
- package/functions/function-aliases.js.map +1 -0
- package/functions/{function-interface.d.ts → function-token.d.ts} +8 -7
- package/functions/{function-interface.js → function-token.js} +3 -2
- package/functions/function-token.js.map +1 -0
- package/functions/functions.d.ts +3 -2
- package/functions/functions.js +15 -4
- package/functions/functions.js.map +1 -1
- package/functions/index.d.ts +4 -1
- package/functions/index.js +6 -1
- package/functions/index.js.map +1 -1
- package/functions/is-database-function.d.ts +8 -0
- package/functions/is-database-function.js +15 -0
- package/functions/is-database-function.js.map +1 -0
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/index.js.map +1 -1
- package/migration/create-migration-table.js +2 -1
- package/migration/create-migration-table.js.map +1 -1
- package/package.json +1 -1
- package/repository/repository-options.d.ts +0 -2
- package/repository/repository.d.ts +12 -3
- package/repository/repository.js +32 -1
- package/repository/repository.js.map +1 -1
- package/seed/auto-seed.js +16 -16
- package/seed/auto-seed.js.map +1 -1
- package/seed/create-seed-table.js +2 -1
- package/seed/create-seed-table.js.map +1 -1
- package/tokens/index.d.ts +1 -0
- package/tokens/index.js +6 -0
- package/tokens/index.js.map +1 -0
- package/dml/column-name.d.ts +0 -3
- package/dml/column-name.js +0 -12
- package/dml/column-name.js.map +0 -1
- package/dml/where.d.ts +0 -26
- package/dml/where.js +0 -67
- package/dml/where.js.map +0 -1
- package/functions/function-interface.js.map +0 -1
package/dml/query-builder.js
CHANGED
|
@@ -1,34 +1,111 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatabaseQueryBuilder = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
4
|
+
const statement_builder_1 = require("../builder/statement-builder");
|
|
5
|
+
const comparison_1 = require("../comparison");
|
|
6
|
+
const tokens_1 = require("../tokens");
|
|
7
|
+
const functions_1 = require("../functions");
|
|
8
|
+
const expression_1 = require("../expression");
|
|
9
|
+
const function_token_1 = require("../functions/function-token");
|
|
10
|
+
const subquery_1 = require("./subquery");
|
|
11
|
+
class DatabaseQueryBuilder extends statement_builder_1.StatementBuilder {
|
|
12
|
+
// ------------------------------------------------------------------------
|
|
13
|
+
// Expression
|
|
14
|
+
// ------------------------------------------------------------------------
|
|
15
|
+
expression(expr) {
|
|
16
|
+
if (Array.isArray(expr)) {
|
|
17
|
+
for (const e of expr) {
|
|
18
|
+
this.expression(e);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if ((0, expression_1.isExpressionToken)(expr)) {
|
|
22
|
+
const token = expr;
|
|
23
|
+
if ((0, comparison_1.isComparisonToken)(token)) {
|
|
24
|
+
this.buildComparisonToken(token);
|
|
25
|
+
}
|
|
26
|
+
else if ((0, expression_1.isLogicalToken)(token)) {
|
|
27
|
+
this.logical(token);
|
|
28
|
+
}
|
|
29
|
+
else if ((0, expression_1.isIdentifierToken)(token)) {
|
|
30
|
+
this.sql.columnName(token.name);
|
|
31
|
+
}
|
|
32
|
+
else if ((0, functions_1.isDatabaseFunction)(token)) {
|
|
33
|
+
this.databaseFunction(token);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (expr && typeof expr === 'object') {
|
|
37
|
+
if (expr instanceof Buffer || expr instanceof Date) {
|
|
38
|
+
this.sql.placeholder(expr);
|
|
39
|
+
}
|
|
40
|
+
else if (expr instanceof subquery_1.Subquery) {
|
|
41
|
+
this.subquery(expr);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.keyValueExpression(expr);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.sql.placeholder(expr);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
subquery(subquery) {
|
|
52
|
+
this.sql.openParens();
|
|
53
|
+
this.select(subquery.query);
|
|
54
|
+
this.sql.closeParens();
|
|
55
|
+
this.sql.trimEnd(' ');
|
|
56
|
+
}
|
|
57
|
+
keyValueExpression(kv) {
|
|
58
|
+
if (!Object.keys(kv).length) {
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
this.sql.openParens();
|
|
62
|
+
for (const key in kv) {
|
|
63
|
+
const value = kv[key];
|
|
64
|
+
this.sql.columnName(key);
|
|
65
|
+
this.sql.space();
|
|
66
|
+
if (value === null) {
|
|
67
|
+
this.isNull();
|
|
68
|
+
}
|
|
69
|
+
else if ((0, expression_1.isExpressionToken)(value)) {
|
|
70
|
+
const token = value;
|
|
71
|
+
if ((0, comparison_1.isComparisonToken)(token)) {
|
|
72
|
+
this.expression(value);
|
|
73
|
+
}
|
|
74
|
+
else if ((0, expression_1.isLogicalToken)(token)) {
|
|
75
|
+
this.expression(value);
|
|
76
|
+
}
|
|
77
|
+
else if ((0, expression_1.isIdentifierToken)(token)) {
|
|
78
|
+
this.equal(value);
|
|
79
|
+
}
|
|
80
|
+
else if ((0, functions_1.isDatabaseFunction)(token)) {
|
|
81
|
+
this.equal(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.equal(value);
|
|
86
|
+
}
|
|
87
|
+
this.and();
|
|
88
|
+
}
|
|
89
|
+
this.sql.trimEnd(this.sql.operators.and);
|
|
90
|
+
this.sql.closeParens();
|
|
91
|
+
}
|
|
7
92
|
// ------------------------------------------------------------------------
|
|
8
93
|
// Select
|
|
9
94
|
// ------------------------------------------------------------------------
|
|
10
95
|
selectColumn(column) {
|
|
11
96
|
if (typeof column === 'string') {
|
|
12
|
-
this.columnName(column);
|
|
97
|
+
this.sql.columnName(column);
|
|
13
98
|
}
|
|
14
99
|
else if (typeof column === 'object') {
|
|
15
100
|
if ('column' in column) {
|
|
16
|
-
this.columnName(column.column);
|
|
101
|
+
this.sql.columnName(column.column);
|
|
17
102
|
}
|
|
18
103
|
else if ('query' in column) {
|
|
19
|
-
|
|
20
|
-
this.databaseFunction(column.query);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
this.openParens();
|
|
24
|
-
this.select(column.query);
|
|
25
|
-
this.closeParens();
|
|
26
|
-
this.trimEnd(' ');
|
|
27
|
-
}
|
|
104
|
+
this.expression(column.query);
|
|
28
105
|
}
|
|
29
106
|
if (column.as) {
|
|
30
|
-
this.sql
|
|
31
|
-
this.columnName(column.as);
|
|
107
|
+
this.sql.append(' AS ');
|
|
108
|
+
this.sql.columnName(column.as);
|
|
32
109
|
}
|
|
33
110
|
}
|
|
34
111
|
return this;
|
|
@@ -37,189 +114,210 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
37
114
|
if (columns) {
|
|
38
115
|
for (const column of columns) {
|
|
39
116
|
this.selectColumn(column);
|
|
40
|
-
this.sql
|
|
117
|
+
this.sql.append(', ');
|
|
41
118
|
}
|
|
42
|
-
this.trimEnd(', ');
|
|
43
|
-
this.sql
|
|
119
|
+
this.sql.trimEnd(', ');
|
|
120
|
+
this.sql.space();
|
|
44
121
|
}
|
|
45
122
|
else {
|
|
46
|
-
this.sql
|
|
123
|
+
this.sql.append('* ');
|
|
47
124
|
}
|
|
48
125
|
return this;
|
|
49
126
|
}
|
|
50
127
|
selectFrom(from) {
|
|
51
|
-
this.sql
|
|
52
|
-
this.tableName(from);
|
|
53
|
-
this.sql
|
|
128
|
+
this.sql.append('FROM ');
|
|
129
|
+
this.sql.tableName(from);
|
|
130
|
+
this.sql.space();
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
tableAlias(alias) {
|
|
134
|
+
this.sql.append('AS ');
|
|
135
|
+
this.sql.tableName(alias);
|
|
136
|
+
this.sql.space();
|
|
54
137
|
return this;
|
|
55
138
|
}
|
|
56
139
|
selectStatement() {
|
|
57
|
-
this.sql
|
|
140
|
+
this.sql.append('SELECT ');
|
|
141
|
+
return this;
|
|
142
|
+
}
|
|
143
|
+
distinctStatement() {
|
|
144
|
+
this.sql.append('DISTINCT ');
|
|
58
145
|
return this;
|
|
59
146
|
}
|
|
60
147
|
selectTop(limit) {
|
|
61
148
|
// This will be overridden in mssql for LIMIT func
|
|
62
149
|
return this;
|
|
63
150
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
151
|
+
// ------------------------------------------------------------------------
|
|
152
|
+
// Comparisons
|
|
153
|
+
// ------------------------------------------------------------------------
|
|
154
|
+
equal(value) {
|
|
155
|
+
this.sql.append(this.sql.operators.equals + ' ');
|
|
156
|
+
this.expression(value);
|
|
67
157
|
}
|
|
68
158
|
like(value) {
|
|
69
|
-
this.sql
|
|
70
|
-
this.
|
|
159
|
+
this.sql.append(this.sql.operators.like + ' ');
|
|
160
|
+
this.expression(value);
|
|
71
161
|
}
|
|
72
162
|
lt(value) {
|
|
73
|
-
this.sql
|
|
74
|
-
this.
|
|
163
|
+
this.sql.append(this.sql.operators.lt + ' ');
|
|
164
|
+
this.expression(value);
|
|
75
165
|
}
|
|
76
166
|
lte(value) {
|
|
77
|
-
this.sql
|
|
78
|
-
this.
|
|
167
|
+
this.sql.append(this.sql.operators.lte + ' ');
|
|
168
|
+
this.expression(value);
|
|
79
169
|
}
|
|
80
170
|
gt(value) {
|
|
81
|
-
this.sql
|
|
82
|
-
this.
|
|
171
|
+
this.sql.append(this.sql.operators.gt + ' ');
|
|
172
|
+
this.expression(value);
|
|
83
173
|
}
|
|
84
174
|
gte(value) {
|
|
85
|
-
this.sql
|
|
86
|
-
this.
|
|
175
|
+
this.sql.append(this.sql.operators.gte + ' ');
|
|
176
|
+
this.expression(value);
|
|
87
177
|
}
|
|
88
178
|
notEqual(value) {
|
|
89
|
-
this.sql
|
|
90
|
-
this.
|
|
91
|
-
}
|
|
92
|
-
and() {
|
|
93
|
-
this.sql += this.operators.and + ' ';
|
|
94
|
-
}
|
|
95
|
-
or() {
|
|
96
|
-
this.sql += this.operators.or + ' ';
|
|
179
|
+
this.sql.append(this.sql.operators.notEqual + ' ');
|
|
180
|
+
this.expression(value);
|
|
97
181
|
}
|
|
98
182
|
isNull() {
|
|
99
|
-
this.sql
|
|
183
|
+
this.sql.append(this.sql.operators.is + ' ' + this.sql.operators.null + ' ');
|
|
100
184
|
}
|
|
101
|
-
|
|
102
|
-
this.sql
|
|
103
|
-
this.openParens();
|
|
185
|
+
inArray(values) {
|
|
186
|
+
this.sql.append(this.sql.operators.in + ' ');
|
|
187
|
+
this.sql.openParens();
|
|
104
188
|
for (const value of values) {
|
|
105
|
-
this.
|
|
106
|
-
this.sql
|
|
189
|
+
this.expression(value);
|
|
190
|
+
this.sql.append(', ');
|
|
107
191
|
}
|
|
108
|
-
this.trimEnd(', ');
|
|
109
|
-
this.closeParens();
|
|
192
|
+
this.sql.trimEnd(', ');
|
|
193
|
+
this.sql.closeParens();
|
|
110
194
|
}
|
|
111
|
-
|
|
112
|
-
|
|
195
|
+
between(a, b) {
|
|
196
|
+
this.sql.append(this.sql.operators.between + ' ');
|
|
197
|
+
this.sql.openParens();
|
|
198
|
+
this.expression(a);
|
|
199
|
+
this.sql.append(', ');
|
|
200
|
+
this.expression(b);
|
|
201
|
+
this.sql.closeParens();
|
|
113
202
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
203
|
+
notNull() {
|
|
204
|
+
this.sql.append('NOT NULL ');
|
|
205
|
+
}
|
|
206
|
+
buildComparisonToken(condition) {
|
|
207
|
+
if (condition.op === comparison_1.ComparisonOperator.EQUALS) {
|
|
208
|
+
this.equal(condition.value);
|
|
209
|
+
}
|
|
210
|
+
else if (condition.op === comparison_1.ComparisonOperator.NOT_EQUAL) {
|
|
211
|
+
this.notEqual(condition.value);
|
|
117
212
|
}
|
|
118
|
-
else if (condition.
|
|
213
|
+
else if (condition.op === comparison_1.ComparisonOperator.LIKE) {
|
|
119
214
|
this.like(condition.value);
|
|
120
215
|
}
|
|
121
|
-
else if (condition.
|
|
216
|
+
else if (condition.op === comparison_1.ComparisonOperator.LT) {
|
|
122
217
|
this.lt(condition.value);
|
|
123
218
|
}
|
|
124
|
-
else if (condition.
|
|
219
|
+
else if (condition.op === comparison_1.ComparisonOperator.LTE) {
|
|
125
220
|
this.lte(condition.value);
|
|
126
221
|
}
|
|
127
|
-
else if (condition.
|
|
222
|
+
else if (condition.op === comparison_1.ComparisonOperator.GT) {
|
|
128
223
|
this.gt(condition.value);
|
|
129
224
|
}
|
|
130
|
-
else if (condition.
|
|
225
|
+
else if (condition.op === comparison_1.ComparisonOperator.GTE) {
|
|
131
226
|
this.gte(condition.value);
|
|
132
227
|
}
|
|
133
|
-
else if (condition.
|
|
134
|
-
this.
|
|
228
|
+
else if (condition.op === comparison_1.ComparisonOperator.IN_ARRAY) {
|
|
229
|
+
this.inArray(condition.value);
|
|
135
230
|
}
|
|
136
|
-
else if (condition.
|
|
137
|
-
|
|
138
|
-
(condition.value.riao_condition === 'like' ||
|
|
139
|
-
condition.value.riao_condition === 'in')) {
|
|
140
|
-
this.sql += 'NOT ';
|
|
141
|
-
this.riaoCondition(condition.value);
|
|
142
|
-
}
|
|
143
|
-
else if (this.isRiaoCondition(condition.value)) {
|
|
144
|
-
throw new Error('Cannot use not() with ' + condition.riao_condition);
|
|
145
|
-
}
|
|
146
|
-
else if (typeof condition.value === 'object' ||
|
|
147
|
-
Array.isArray(condition.value)) {
|
|
148
|
-
this.sql += '!';
|
|
149
|
-
this.whereClause(condition.value);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
this.notEqual(condition.value);
|
|
153
|
-
}
|
|
231
|
+
else if (condition.op === comparison_1.ComparisonOperator.BETWEEN) {
|
|
232
|
+
this.between(condition.value.a, condition.value.b);
|
|
154
233
|
}
|
|
155
234
|
return this;
|
|
156
235
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
this.
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
236
|
+
// ------------------------------------------------------------------------
|
|
237
|
+
// Logical
|
|
238
|
+
// ------------------------------------------------------------------------
|
|
239
|
+
and() {
|
|
240
|
+
this.sql.append(this.sql.operators.and + ' ');
|
|
241
|
+
}
|
|
242
|
+
or() {
|
|
243
|
+
this.sql.append(this.sql.operators.or + ' ');
|
|
244
|
+
}
|
|
245
|
+
not(value) {
|
|
246
|
+
if (value === null) {
|
|
247
|
+
this.sql.append(this.sql.operators.is + ' ');
|
|
248
|
+
this.notNull();
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const isComparison = (0, expression_1.isExpressionToken)(value) &&
|
|
252
|
+
(0, comparison_1.isComparisonToken)(value);
|
|
253
|
+
const conditionType = isComparison
|
|
254
|
+
? value === null || value === void 0 ? void 0 : value.op
|
|
255
|
+
: null;
|
|
256
|
+
if (isComparison &&
|
|
257
|
+
(conditionType === comparison_1.ComparisonOperator.LIKE ||
|
|
258
|
+
conditionType === comparison_1.ComparisonOperator.IN_ARRAY)) {
|
|
259
|
+
this.sql.append('NOT ');
|
|
260
|
+
this.buildComparisonToken(value);
|
|
261
|
+
}
|
|
262
|
+
else if (isComparison) {
|
|
263
|
+
throw new Error('Cannot use not() with ' + conditionType);
|
|
264
|
+
}
|
|
265
|
+
else if (typeof value === 'object' || Array.isArray(value)) {
|
|
266
|
+
this.sql.append('!');
|
|
267
|
+
this.expression(value);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
this.notEqual(value);
|
|
191
271
|
}
|
|
192
|
-
|
|
272
|
+
}
|
|
273
|
+
logical(token) {
|
|
274
|
+
if (token.op === expression_1.LogicalOperator.AND) {
|
|
193
275
|
this.and();
|
|
194
276
|
}
|
|
195
|
-
else if (
|
|
277
|
+
else if (token.op === expression_1.LogicalOperator.OR) {
|
|
196
278
|
this.or();
|
|
197
279
|
}
|
|
198
|
-
|
|
280
|
+
else if (token.op === expression_1.LogicalOperator.NOT) {
|
|
281
|
+
this.not(token.expr);
|
|
282
|
+
}
|
|
199
283
|
}
|
|
284
|
+
// ------------------------------------------------------------------------
|
|
285
|
+
// Where
|
|
286
|
+
// ------------------------------------------------------------------------
|
|
200
287
|
where(where) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}
|
|
204
|
-
this.sql += 'WHERE ';
|
|
205
|
-
this.whereClause(where);
|
|
288
|
+
this.sql.append('WHERE ');
|
|
289
|
+
this.expression(where);
|
|
206
290
|
return this;
|
|
207
291
|
}
|
|
208
292
|
limit(nRecords) {
|
|
209
|
-
this.sql
|
|
293
|
+
this.sql.append('LIMIT ' + nRecords + ' ');
|
|
294
|
+
return this;
|
|
295
|
+
}
|
|
296
|
+
groupBy(by) {
|
|
297
|
+
this.sql.append('GROUP BY ');
|
|
298
|
+
for (const key of by) {
|
|
299
|
+
this.sql.columnName(key);
|
|
300
|
+
this.sql.append(', ');
|
|
301
|
+
}
|
|
302
|
+
this.sql.trimEnd(', ');
|
|
303
|
+
this.sql.space();
|
|
210
304
|
return this;
|
|
211
305
|
}
|
|
212
306
|
orderBy(by) {
|
|
213
|
-
this.sql
|
|
307
|
+
this.sql.append('ORDER BY ');
|
|
214
308
|
for (const key in by) {
|
|
215
|
-
this.columnName(key);
|
|
216
|
-
this.sql
|
|
309
|
+
this.sql.columnName(key);
|
|
310
|
+
this.sql.append(' ' + by[key] + ', ');
|
|
217
311
|
}
|
|
218
|
-
this.trimEnd(', ');
|
|
312
|
+
this.sql.trimEnd(', ');
|
|
313
|
+
this.sql.space();
|
|
219
314
|
}
|
|
220
315
|
select(query) {
|
|
221
|
-
var _a;
|
|
316
|
+
var _a, _b;
|
|
222
317
|
this.selectStatement();
|
|
318
|
+
if (query.distinct) {
|
|
319
|
+
this.distinctStatement();
|
|
320
|
+
}
|
|
223
321
|
if (query.limit) {
|
|
224
322
|
this.selectTop(query.limit);
|
|
225
323
|
}
|
|
@@ -227,18 +325,24 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
227
325
|
if (query.table) {
|
|
228
326
|
this.selectFrom(query.table);
|
|
229
327
|
}
|
|
328
|
+
if (query.tableAlias) {
|
|
329
|
+
this.tableAlias(query.tableAlias);
|
|
330
|
+
}
|
|
230
331
|
for (const join of (_a = query.join) !== null && _a !== void 0 ? _a : []) {
|
|
231
332
|
this.join(join);
|
|
232
333
|
}
|
|
233
334
|
if (query.where) {
|
|
234
335
|
this.where(query.where);
|
|
235
336
|
}
|
|
236
|
-
if (query.
|
|
237
|
-
this.
|
|
337
|
+
if ((_b = query.groupBy) === null || _b === void 0 ? void 0 : _b.length) {
|
|
338
|
+
this.groupBy(query.groupBy);
|
|
238
339
|
}
|
|
239
340
|
if (query.orderBy) {
|
|
240
341
|
this.orderBy(query.orderBy);
|
|
241
342
|
}
|
|
343
|
+
if (query.limit) {
|
|
344
|
+
this.limit(query.limit);
|
|
345
|
+
}
|
|
242
346
|
return this;
|
|
243
347
|
}
|
|
244
348
|
// ------------------------------------------------------------------------
|
|
@@ -246,41 +350,39 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
246
350
|
// ------------------------------------------------------------------------
|
|
247
351
|
join(join) {
|
|
248
352
|
var _a;
|
|
249
|
-
this.sql
|
|
250
|
-
this.tableName(join.table);
|
|
251
|
-
this.sql
|
|
353
|
+
this.sql.append(((_a = join.type) !== null && _a !== void 0 ? _a : '') + ' JOIN ');
|
|
354
|
+
this.sql.tableName(join.table);
|
|
355
|
+
this.sql.space();
|
|
252
356
|
if (join.alias) {
|
|
253
|
-
this.
|
|
254
|
-
this.tableName(join.alias);
|
|
255
|
-
this.sql += ' ';
|
|
357
|
+
this.tableAlias(join.alias);
|
|
256
358
|
}
|
|
257
359
|
if (join.on) {
|
|
258
|
-
this.sql
|
|
259
|
-
this.
|
|
360
|
+
this.sql.append('ON ');
|
|
361
|
+
this.expression(join.on);
|
|
260
362
|
}
|
|
261
363
|
}
|
|
262
364
|
// ------------------------------------------------------------------------
|
|
263
365
|
// Insert
|
|
264
366
|
// ------------------------------------------------------------------------
|
|
265
367
|
insertIntoStatement(table) {
|
|
266
|
-
this.sql
|
|
267
|
-
this.tableName(table);
|
|
268
|
-
this.sql
|
|
368
|
+
this.sql.append('INSERT INTO ');
|
|
369
|
+
this.sql.tableName(table);
|
|
370
|
+
this.sql.space();
|
|
269
371
|
return this;
|
|
270
372
|
}
|
|
271
373
|
insertColumnNames(record) {
|
|
272
|
-
this.openParens();
|
|
273
|
-
this.commaSeparate(Object.keys(record).map((name) => this.getEnclosedName(name)));
|
|
274
|
-
this.closeParens();
|
|
374
|
+
this.sql.openParens();
|
|
375
|
+
this.sql.commaSeparate(Object.keys(record).map((name) => this.sql.getEnclosedName(name)));
|
|
376
|
+
this.sql.closeParens();
|
|
275
377
|
return this;
|
|
276
378
|
}
|
|
277
379
|
insertOnDuplicateKeyUpdate(assignment) {
|
|
278
|
-
this.sql
|
|
380
|
+
this.sql.append('ON DUPLICATE KEY UPDATE ');
|
|
279
381
|
this.updateKeyValues(assignment);
|
|
280
382
|
return this;
|
|
281
383
|
}
|
|
282
384
|
insertIfNotExists() {
|
|
283
|
-
this.insertOnDuplicateKeyUpdate({ id: (0,
|
|
385
|
+
this.insertOnDuplicateKeyUpdate({ id: (0, tokens_1.columnName)('id') });
|
|
284
386
|
return this;
|
|
285
387
|
}
|
|
286
388
|
insertOutput(primaryKey) {
|
|
@@ -319,29 +421,29 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
319
421
|
if (options.primaryKey) {
|
|
320
422
|
this.insertOutput(options.primaryKey);
|
|
321
423
|
}
|
|
322
|
-
this.sql
|
|
424
|
+
this.sql.append('VALUES ');
|
|
323
425
|
for (let i = 0; i < insertions.length; i++) {
|
|
324
426
|
const record = insertions[i];
|
|
325
|
-
this.openParens();
|
|
427
|
+
this.sql.openParens();
|
|
326
428
|
for (const key in record) {
|
|
327
|
-
this.
|
|
429
|
+
this.expression(record[key]);
|
|
328
430
|
this.sql = this.sql.trimEnd();
|
|
329
|
-
this.sql
|
|
431
|
+
this.sql.append(', ');
|
|
330
432
|
}
|
|
331
|
-
this.trimEnd(', ');
|
|
332
|
-
this.closeParens();
|
|
433
|
+
this.sql.trimEnd(', ');
|
|
434
|
+
this.sql.closeParens();
|
|
333
435
|
this.sql = this.sql.trimEnd();
|
|
334
|
-
this.sql
|
|
436
|
+
this.sql.append(', ');
|
|
335
437
|
}
|
|
336
|
-
this.trimEnd(', ');
|
|
337
|
-
this.sql
|
|
438
|
+
this.sql.trimEnd(', ');
|
|
439
|
+
this.sql.space();
|
|
338
440
|
if (options.ifNotExists) {
|
|
339
441
|
this.insertIfNotExists();
|
|
340
442
|
}
|
|
341
443
|
else if (options.onDuplicateKeyUpdate) {
|
|
342
444
|
this.insertOnDuplicateKeyUpdate(options.onDuplicateKeyUpdate);
|
|
343
445
|
}
|
|
344
|
-
this.trimEnd(' ');
|
|
446
|
+
this.sql.trimEnd(' ');
|
|
345
447
|
if (options.primaryKey) {
|
|
346
448
|
this.insertReturning(options.primaryKey);
|
|
347
449
|
}
|
|
@@ -351,26 +453,26 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
351
453
|
// Update
|
|
352
454
|
// ------------------------------------------------------------------------
|
|
353
455
|
updateStatement(table) {
|
|
354
|
-
this.sql
|
|
355
|
-
this.tableName(table);
|
|
356
|
-
this.sql
|
|
456
|
+
this.sql.append('UPDATE ');
|
|
457
|
+
this.sql.tableName(table);
|
|
458
|
+
this.sql.space();
|
|
357
459
|
return this;
|
|
358
460
|
}
|
|
359
461
|
updateKeyValues(values) {
|
|
360
462
|
const keys = Object.keys(values);
|
|
361
463
|
for (const key of keys) {
|
|
362
|
-
this.columnName(key);
|
|
363
|
-
this.sql
|
|
364
|
-
this.
|
|
464
|
+
this.sql.columnName(key);
|
|
465
|
+
this.sql.append(' = ');
|
|
466
|
+
this.expression(values[key]);
|
|
365
467
|
this.sql = this.sql.trimEnd();
|
|
366
|
-
this.sql
|
|
468
|
+
this.sql.append(', ');
|
|
367
469
|
}
|
|
368
|
-
this.trimEnd(', ');
|
|
369
|
-
this.sql
|
|
470
|
+
this.sql.trimEnd(', ');
|
|
471
|
+
this.sql.space();
|
|
370
472
|
return this;
|
|
371
473
|
}
|
|
372
474
|
updateSetStatement(values) {
|
|
373
|
-
this.sql
|
|
475
|
+
this.sql.append('SET ');
|
|
374
476
|
this.updateKeyValues(values);
|
|
375
477
|
return this;
|
|
376
478
|
}
|
|
@@ -390,9 +492,9 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
390
492
|
// Delete
|
|
391
493
|
// ------------------------------------------------------------------------
|
|
392
494
|
deleteStatement(table) {
|
|
393
|
-
this.sql
|
|
394
|
-
this.tableName(table);
|
|
395
|
-
this.sql
|
|
495
|
+
this.sql.append('DELETE FROM ');
|
|
496
|
+
this.sql.tableName(table);
|
|
497
|
+
this.sql.space();
|
|
396
498
|
return this;
|
|
397
499
|
}
|
|
398
500
|
delete(options) {
|
|
@@ -406,6 +508,33 @@ class DatabaseQueryBuilder extends builder_1.Builder {
|
|
|
406
508
|
}
|
|
407
509
|
return this;
|
|
408
510
|
}
|
|
511
|
+
// ------------------------------------------------------------------------
|
|
512
|
+
// Database Functions
|
|
513
|
+
// ------------------------------------------------------------------------
|
|
514
|
+
/**
|
|
515
|
+
* Append a database function the query
|
|
516
|
+
*
|
|
517
|
+
* @param fn Database function token
|
|
518
|
+
*/
|
|
519
|
+
databaseFunction(fn) {
|
|
520
|
+
switch (fn.fn) {
|
|
521
|
+
case function_token_1.DatabaseFunctionKeys.COUNT:
|
|
522
|
+
this.count(fn);
|
|
523
|
+
break;
|
|
524
|
+
case function_token_1.DatabaseFunctionKeys.CURRENT_TIMESTAMP:
|
|
525
|
+
this.currentTimestamp(fn);
|
|
526
|
+
break;
|
|
527
|
+
}
|
|
528
|
+
return this;
|
|
529
|
+
}
|
|
530
|
+
count(fn) {
|
|
531
|
+
this.sql.append('COUNT(*)');
|
|
532
|
+
return this;
|
|
533
|
+
}
|
|
534
|
+
currentTimestamp(fn) {
|
|
535
|
+
this.sql.append('CURRENT_TIMESTAMP');
|
|
536
|
+
return this;
|
|
537
|
+
}
|
|
409
538
|
}
|
|
410
539
|
exports.DatabaseQueryBuilder = DatabaseQueryBuilder;
|
|
411
540
|
//# sourceMappingURL=query-builder.js.map
|