@mrnafisia/type-query 1.0.13 → 1.0.16

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/dist/entity.js CHANGED
@@ -1,970 +1,971 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
- if (ar || !(i in from)) {
16
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
- ar[i] = from[i];
18
- }
19
- }
20
- return to.concat(ar || Array.prototype.slice.call(from));
21
- };
22
- var __importDefault = (this && this.__importDefault) || function (mod) {
23
- return (mod && mod.__esModule) ? mod : { "default": mod };
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.partialQuery = exports.resolveColumn = exports.getTableDataOfJoinSelectColumn = exports.resolveExpression = exports.resolveReturning = exports.resolveResult = exports.createQueryResult = exports.ReservedExpressionKeys = exports.createJoinSelectEntity = exports.createEntity = void 0;
27
- var U_1 = __importDefault(require("./U"));
28
- var decimal_js_1 = __importDefault(require("decimal.js"));
29
- var context_1 = require("./context");
30
- var never_catch_1 = require("never-catch");
31
- var dictionary_1 = require("./dictionary");
32
- // entity
33
- var createEntity = function (table) { return ({
34
- table: table,
35
- context: (0, context_1.createContext)(table),
36
- select: function (returning, where, options) {
37
- var _this = this;
38
- var _a, _b, _c;
39
- var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false;
40
- var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false;
41
- var order = (_c = (options === null || options === void 0 ? void 0 : options.order)) !== null && _c !== void 0 ? _c : [];
42
- var start = options === null || options === void 0 ? void 0 : options.start;
43
- var step = options === null || options === void 0 ? void 0 : options.step;
44
- var _returning = typeof returning === 'function' ? returning(this.context) : returning;
45
- var createQuery = function (params) {
46
- var tokens = ['SELECT'];
47
- // select
48
- var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
49
- if (!resolvedReturning.ok) {
50
- return (0, never_catch_1.err)("<select> -> ".concat(resolvedReturning.error));
51
- }
52
- params.push.apply(params, resolvedReturning.value.params);
53
- tokens.push(resolvedReturning.value.text);
54
- // from
55
- tokens.push("FROM \"".concat(table.schema, "\".\"").concat(table.title, "\""));
56
- // where
57
- var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere);
58
- if (!resolvedWhereResult.ok) {
59
- return (0, never_catch_1.err)("<select>[where] -> ".concat(resolvedWhereResult.error));
60
- }
61
- if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
62
- return (0, never_catch_1.err)("<select>[where] -> neutral");
63
- }
64
- params.push.apply(params, resolvedWhereResult.value.params);
65
- tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'TRUE' : resolvedWhereResult.value.text);
66
- // order
67
- if (order.length !== 0) {
68
- var ordersTextArray = [];
69
- for (var _i = 0, order_1 = order; _i < order_1.length; _i++) {
70
- var orderElement = order_1[_i];
71
- var by = orderElement.by, direction = orderElement.direction;
72
- ordersTextArray.push("".concat(resolveColumn(table, by, false), " ").concat((0, dictionary_1.toOrderDirection)(direction)));
73
- }
74
- tokens.push('ORDER BY', ordersTextArray.join(', '));
75
- }
76
- // pagination
77
- if (start !== undefined) {
78
- if (start < 0) {
79
- return (0, never_catch_1.err)("<select>[start] -> invalid");
80
- }
81
- tokens.push('OFFSET', start.toString());
82
- }
83
- if (step !== undefined) {
84
- if (step <= 0) {
85
- return (0, never_catch_1.err)("<select>[step] -> invalid");
86
- }
87
- tokens.push('LIMIT', step.toString());
88
- }
89
- tokens.push(';');
90
- var sql = tokens.join(' ');
91
- return (0, never_catch_1.ok)({ sql: sql, params: params });
92
- };
93
- return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
94
- },
95
- insert: function (rows, returning, options) {
96
- var _this = this;
97
- var _a, _b, _c;
98
- var nullableDefaultColumns = (_a = (options === null || options === void 0 ? void 0 : options.nullableDefaultColumns)) !== null && _a !== void 0 ? _a : [];
99
- var ignoreInValues = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInValues)) !== null && _b !== void 0 ? _b : false;
100
- var ignoreInReturning = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInValues)) !== null && _c !== void 0 ? _c : false;
101
- var _returning = typeof returning === 'function' ? returning(this.context) : returning;
102
- var createQuery = function (params) {
103
- var tokens = ["INSERT INTO \"".concat(table.schema, "\".\"").concat(table.title, "\"")];
104
- // columns
105
- var insertingColumns = [];
106
- var columnsTextArray = [];
107
- for (var column in table.columns) {
108
- if (nullableDefaultColumns.includes(column) || !table.columns[column].nullable) {
109
- insertingColumns.push(column);
110
- columnsTextArray.push(resolveColumn(table, column, false));
111
- }
112
- }
113
- tokens.push("( ".concat(columnsTextArray.join(', '), " )"), 'VALUES');
114
- // values
115
- if (rows.length === 0) {
116
- return (0, never_catch_1.err)('<insert>[values] -> empty');
117
- }
118
- var valuesTextArray = [];
119
- var _rows = typeof rows === 'function' ? rows(_this.context) : rows;
120
- for (var _i = 0, _rows_1 = _rows; _i < _rows_1.length; _i++) {
121
- var _row = _rows_1[_i];
122
- var rowTokens = [];
123
- for (var _a = 0, insertingColumns_1 = insertingColumns; _a < insertingColumns_1.length; _a++) {
124
- var insertingColumn = insertingColumns_1[_a];
125
- if (_row[insertingColumn] === undefined) {
126
- var column = table.columns[insertingColumn];
127
- switch (column.default) {
128
- case 'value':
129
- rowTokens.push(U_1.default.stringify(column.value, true));
130
- continue;
131
- case true:
132
- case 'auto-increment':
133
- rowTokens.push('DEFAULT');
134
- continue;
135
- case 'created-at':
136
- case 'updated-at':
137
- rowTokens.push(U_1.default.stringify(new Date(), true));
138
- continue;
139
- }
140
- if (column.nullable) {
141
- rowTokens.push(U_1.default.stringify(null));
142
- continue;
143
- }
144
- // never going to happen!
145
- return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> no-value"));
146
- }
147
- else {
148
- var resolvedExpressionResult = resolveExpression(_row[insertingColumn], params.length + 1, ignoreInValues);
149
- if (!resolvedExpressionResult.ok) {
150
- return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> ").concat(resolvedExpressionResult.error));
151
- }
152
- if (resolvedExpressionResult.value.text === '') {
153
- return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> neutral"));
154
- }
155
- params.push.apply(params, resolvedExpressionResult.value.params);
156
- rowTokens.push(resolvedExpressionResult.value.text);
157
- }
158
- }
159
- valuesTextArray.push("( ".concat(rowTokens.join(', '), " )"));
160
- }
161
- tokens.push(valuesTextArray.join(', '));
162
- // returning
163
- if (_returning.length !== 0) {
164
- var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
165
- if (!resolvedReturning.ok) {
166
- return (0, never_catch_1.err)("<insert> -> ".concat(resolvedReturning.error));
167
- }
168
- params.push.apply(params, resolvedReturning.value.params);
169
- tokens.push('RETURNING', resolvedReturning.value.text);
170
- }
171
- tokens.push(';');
172
- var sql = tokens.join(' ');
173
- return (0, never_catch_1.ok)({ sql: sql, params: params });
174
- };
175
- return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
176
- },
177
- update: function (sets, where, returning, options) {
178
- var _this = this;
179
- var _a, _b, _c;
180
- var ignoreInSets = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInSets)) !== null && _a !== void 0 ? _a : false;
181
- var ignoreInWhere = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _b !== void 0 ? _b : false;
182
- var ignoreInReturning = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _c !== void 0 ? _c : false;
183
- var _returning = typeof returning === 'function' ? returning(this.context) : returning;
184
- var createQuery = function (params) {
185
- var tokens = ["UPDATE \"".concat(table.schema, "\".\"").concat(table.title, "\" SET")];
186
- // set
187
- var _set = typeof sets === 'function' ? sets(_this.context) : sets;
188
- var setsTextArray = [];
189
- var key;
190
- for (key in _set) {
191
- var setExpressionResult = resolveExpression(_set[key], params.length + 1, ignoreInSets);
192
- if (!setExpressionResult.ok) {
193
- return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> ").concat(setExpressionResult.error));
194
- }
195
- if (setExpressionResult.value.text === '') {
196
- if (ignoreInSets) {
197
- continue;
198
- }
199
- else {
200
- return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> neutral"));
201
- }
202
- }
203
- params.push.apply(params, setExpressionResult.value.params);
204
- setsTextArray.push("".concat(resolveColumn(table, key, false), " = ").concat(setExpressionResult.value.text));
205
- }
206
- for (var column in table.columns) {
207
- switch (table.columns[column].default) {
208
- case 'updated-at':
209
- setsTextArray.push("".concat(resolveColumn(table, column, false), " = ").concat(U_1.default.stringify(new Date(), true)));
210
- break;
211
- }
212
- }
213
- if (setsTextArray.length === 0) {
214
- return (0, never_catch_1.err)('<update>[sets] -> empty');
215
- }
216
- tokens.push(setsTextArray.join(', '));
217
- // where
218
- var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere);
219
- if (!resolvedWhereResult.ok) {
220
- return (0, never_catch_1.err)("<update>[where] -> ".concat(resolvedWhereResult.error));
221
- }
222
- if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
223
- return (0, never_catch_1.err)("<update>[where] -> neutral");
224
- }
225
- params.push.apply(params, resolvedWhereResult.value.params);
226
- tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text);
227
- // returning
228
- if (_returning.length !== 0) {
229
- var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
230
- if (!resolvedReturning.ok) {
231
- return (0, never_catch_1.err)("<update> -> ".concat(resolvedReturning.error));
232
- }
233
- params.push.apply(params, resolvedReturning.value.params);
234
- tokens.push('RETURNING', resolvedReturning.value.text);
235
- }
236
- tokens.push(';');
237
- var sql = tokens.join(' ');
238
- return (0, never_catch_1.ok)({ sql: sql, params: params });
239
- };
240
- return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
241
- },
242
- delete: function (where, returning, options) {
243
- var _this = this;
244
- var _a, _b;
245
- var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false;
246
- var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false;
247
- var _returning = typeof returning === 'function' ? returning(this.context) : returning;
248
- var createQuery = function (params) {
249
- var tokens = ["DELETE FROM \"".concat(table.schema, "\".\"").concat(table.title, "\"")];
250
- // where
251
- var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere);
252
- if (!resolvedWhereResult.ok) {
253
- return (0, never_catch_1.err)("<delete>[where] -> ".concat(resolvedWhereResult.error));
254
- }
255
- if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
256
- return (0, never_catch_1.err)("<delete>[where] -> neutral");
257
- }
258
- params.push.apply(params, resolvedWhereResult.value.params);
259
- tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text);
260
- // returning
261
- if (_returning.length !== 0) {
262
- var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
263
- if (!resolvedReturning.ok) {
264
- return (0, never_catch_1.err)("<delete> -> ".concat(resolvedReturning.error));
265
- }
266
- params.push.apply(params, resolvedReturning.value.params);
267
- tokens.push('RETURNING', resolvedReturning.value.text);
268
- }
269
- tokens.push(';');
270
- var sql = tokens.join(' ');
271
- return (0, never_catch_1.ok)({ sql: sql, params: params });
272
- };
273
- return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
274
- },
275
- join: function (mainAlias, joinType, joinTable, joinAlias, on) {
276
- var _a;
277
- return createJoinSelectEntity({ table: table, alias: mainAlias }, [{ table: joinTable, joinType: joinType, alias: joinAlias, on: on }], (_a = {}, _a[mainAlias] = (0, context_1.createContext)(table, mainAlias), _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a));
278
- }
279
- }); };
280
- exports.createEntity = createEntity;
281
- var createJoinSelectEntity = function (main, joinTables, contexts) { return ({
282
- contexts: contexts,
283
- select: function (returning, where, options) {
284
- var _this = this;
285
- var _a, _b, _c, _d;
286
- var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false;
287
- var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false;
288
- var ignoreInJoin = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInJoin)) !== null && _c !== void 0 ? _c : false;
289
- var order = (_d = (options === null || options === void 0 ? void 0 : options.order)) !== null && _d !== void 0 ? _d : [];
290
- var start = options === null || options === void 0 ? void 0 : options.start;
291
- var step = options === null || options === void 0 ? void 0 : options.step;
292
- var allTables = __spreadArray([main], joinTables, true);
293
- var _returning = typeof returning === 'function' ? returning(this.contexts) : returning;
294
- var createQuery = function (params) {
295
- var tokens = ['SELECT'];
296
- // select
297
- var resolvedReturning = resolveReturning(function (column) {
298
- var _a = getTableDataOfJoinSelectColumn(allTables, column), table = _a.table, alias = _a.alias;
299
- var columnKey = column.substring((alias + '_').length);
300
- return { type: table.columns[columnKey].type, title: table.columns[columnKey].title, alias: alias };
301
- }, _returning, params.length + 1, ignoreInReturning);
302
- if (!resolvedReturning.ok) {
303
- return (0, never_catch_1.err)("<join-select>[columns] -> ".concat(resolvedReturning.error));
304
- }
305
- params.push.apply(params, resolvedReturning.value.params);
306
- tokens.push(resolvedReturning.value.text, "FROM \"".concat(main.table.schema, "\".\"").concat(main.table.title, "\" \"").concat(main.alias, "\""));
307
- // join
308
- for (var _i = 0, joinTables_1 = joinTables; _i < joinTables_1.length; _i++) {
309
- var joinTable = joinTables_1[_i];
310
- var onExpressionResult = resolveExpression(typeof joinTable.on === 'function' ? joinTable.on(_this.contexts) : joinTable.on, params.length + 1, ignoreInJoin);
311
- if (!onExpressionResult.ok) {
312
- return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> ").concat(onExpressionResult.error));
313
- }
314
- if (onExpressionResult.value.text === '' && !ignoreInJoin) {
315
- return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> neutral"));
316
- }
317
- params.push.apply(params, onExpressionResult.value.params);
318
- tokens.push("".concat((0, dictionary_1.toJoinType)(joinTable.joinType), " \"").concat(joinTable.table.schema, "\".\"").concat(joinTable.table.title, "\" \"").concat(joinTable.alias, "\" ON ").concat(onExpressionResult.value.text));
319
- }
320
- // where
321
- var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.contexts) : where, params.length + 1, ignoreInWhere);
322
- if (!resolvedWhereResult.ok) {
323
- return (0, never_catch_1.err)("<join-select>[where] -> ".concat(resolvedWhereResult.error));
324
- }
325
- if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
326
- return (0, never_catch_1.err)("<join-select>[where] -> neutral");
327
- }
328
- params.push.apply(params, resolvedWhereResult.value.params);
329
- tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text);
330
- // order
331
- if (order.length !== 0) {
332
- var ordersTextArray = [];
333
- for (var _a = 0, order_2 = order; _a < order_2.length; _a++) {
334
- var orderElement = order_2[_a];
335
- var by = orderElement.by, direction = orderElement.direction;
336
- var _b = getTableDataOfJoinSelectColumn(allTables, by), alias = _b.alias, title = _b.table.title;
337
- ordersTextArray.push("".concat(alias !== undefined ? "\"".concat(alias, "\".") : '', "\"").concat(title, "\" ").concat(direction));
338
- }
339
- tokens.push('ORDER BY', ordersTextArray.join(', '));
340
- }
341
- // pagination
342
- if (start !== undefined) {
343
- if (start < 0) {
344
- return (0, never_catch_1.err)("<join-select>[start] -> invalid");
345
- }
346
- tokens.push('OFFSET', start.toString());
347
- }
348
- if (step !== undefined) {
349
- if (step <= 0) {
350
- return (0, never_catch_1.err)("<join-select>[step] -> invalid");
351
- }
352
- tokens.push('LIMIT', step.toString());
353
- }
354
- tokens.push(';');
355
- var sql = tokens.join(' ');
356
- return (0, never_catch_1.ok)({ sql: sql, params: params });
357
- };
358
- return createQueryResult(function (column) {
359
- var _a = getTableDataOfJoinSelectColumn(allTables, column), alias = _a.alias, columns = _a.table.columns;
360
- return columns[column.substring((alias + '_').length)].type;
361
- }, createQuery, _returning);
362
- },
363
- join: function (joinType, joinTable, joinAlias, on) {
364
- var _a;
365
- joinTables.push({ table: joinTable, on: on, joinType: joinType, alias: joinAlias });
366
- return createJoinSelectEntity(main, joinTables, __assign(__assign({}, contexts), (_a = {}, _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a)));
367
- }
368
- }); };
369
- exports.createJoinSelectEntity = createJoinSelectEntity;
370
- // utils
371
- var ReservedExpressionKeys = ['val', '=n', '!=n', '=t', '=f', 'not', '+', '-', '*', '/', '||', 'and', 'or',
372
- '**', 'fun', 'swt', 'col', 'raw', '=', '!=', '>', '>=', '<', '<=', 'lk', '@>', '<@', '?', 'j-', 'in', 'nin',
373
- 'lka', 'lks', '?|', '?&', 'j-a', 'bt', 'qry', 'exists'];
374
- exports.ReservedExpressionKeys = ReservedExpressionKeys;
375
- var createQueryResult = function (getColumnType, createQuery, returning) {
376
- var query = undefined;
377
- return ({
378
- getData: function (params) {
379
- if (params === void 0) { params = []; }
380
- if (query === undefined) {
381
- var createQueryResult_1 = createQuery(params);
382
- if (createQueryResult_1.ok) {
383
- query = createQueryResult_1.value;
384
- }
385
- else {
386
- return createQueryResult_1;
387
- }
388
- }
389
- return (0, never_catch_1.ok)(query);
390
- },
391
- exec: function (client, mode, params) {
392
- if (params === void 0) { params = []; }
393
- if (query === undefined) {
394
- var createQueryResult_2 = createQuery(params);
395
- if (createQueryResult_2.ok) {
396
- query = createQueryResult_2.value;
397
- }
398
- else {
399
- return Promise.resolve(createQueryResult_2);
400
- }
401
- }
402
- return client.query(query.sql, query.params)
403
- .then(function (_a) {
404
- var rows = _a.rows;
405
- return resolveResult(getColumnType, returning, rows, mode);
406
- })
407
- .catch(function (e) { return (0, never_catch_1.err)(e); });
408
- }
409
- });
410
- };
411
- exports.createQueryResult = createQueryResult;
412
- /*
413
- * take getColumnType instead of table to support join-select too.
414
- * this cause hard to call this function directly, but it is only solution I could come up with.
415
- */
416
- var resolveResult = function (getColumnType, columns, rows, mode) {
417
- // check size in count and get mode
418
- if (mode[0] === 'count') {
419
- return rows.length === mode[1] ? (0, never_catch_1.ok)(undefined) : (0, never_catch_1.err)(false);
420
- }
421
- if (mode[0] === 'get' && rows.length !== (mode[1] === 'one' ? 1 : mode[1])) {
422
- return (0, never_catch_1.err)(false);
423
- }
424
- // parse result
425
- rows.forEach(function (_, i) {
426
- columns.forEach(function (column) {
427
- if (typeof column !== 'object') {
428
- rows[i][column] = U_1.default.parse(rows[i][column], getColumnType(column));
429
- }
430
- });
431
- });
432
- // return first element for [get, one] mode and all for [get, number] and []
433
- if (mode[0] === 'get' && mode[1] === 'one') {
434
- return (0, never_catch_1.ok)(rows[0]);
435
- }
436
- else {
437
- return (0, never_catch_1.ok)(rows);
438
- }
439
- };
440
- exports.resolveResult = resolveResult;
441
- /*
442
- * take getColumnTypeTitleAlias instead of table to support join-select too.
443
- * this cause hard to call this function directly, but it is only solution I could come up with.
444
- */
445
- var resolveReturning = function (getColumnTypeTitleAlias, columns, paramsStart, ignore) {
446
- var tokens = [];
447
- var params = [];
448
- for (var _i = 0, columns_1 = columns; _i < columns_1.length; _i++) {
449
- var column = columns_1[_i];
450
- if (typeof column === 'object') {
451
- var resolvedExpResult = resolveExpression(column.exp, paramsStart, ignore);
452
- if (!resolvedExpResult.ok) {
453
- return (0, never_catch_1.err)("<returning>[".concat(column.as, "] -> ").concat(resolvedExpResult.error));
454
- }
455
- if (resolvedExpResult.value.text === '') {
456
- return (0, never_catch_1.err)("<returning>[".concat(column.as, "] -> neutral"));
457
- }
458
- params.push.apply(params, resolvedExpResult.value.params);
459
- paramsStart += resolvedExpResult.value.params.length;
460
- tokens.push("( ".concat(resolvedExpResult.value.text, " ) AS \"").concat(column.as, "\""));
461
- }
462
- else {
463
- var _a = getColumnTypeTitleAlias(column), type = _a.type, title = _a.title, alias = _a.alias;
464
- var customParse = void 0;
465
- switch (type) {
466
- case 'date':
467
- case 'timestamp with time zone':
468
- case 'timestamp without time zone':
469
- customParse = '::TEXT';
470
- break;
471
- }
472
- if (title === undefined) {
473
- if (alias !== undefined) {
474
- tokens.push("\"".concat(alias, "\".") +
475
- "\"".concat(column.substring((alias + '_').length), "\"") +
476
- "".concat(customParse !== undefined ? "".concat(customParse, " AS \"").concat(column, "\"") : ''));
477
- }
478
- else {
479
- tokens.push("\"".concat(column, "\"") +
480
- "".concat(customParse !== undefined ? "".concat(customParse, " AS \"").concat(column, "\"") : ''));
481
- }
482
- }
483
- else {
484
- tokens.push((alias !== undefined ? "\"".concat(alias, "\".") : '') +
485
- "\"".concat(title, "\"") +
486
- (customParse !== null && customParse !== void 0 ? customParse : '') +
487
- " AS \"".concat(column, "\""));
488
- }
489
- }
490
- }
491
- if (tokens.length === 0) {
492
- return (0, never_catch_1.err)('<returning> -> empty');
493
- }
494
- return (0, never_catch_1.ok)(partialQuery(tokens.join(', '), params));
495
- };
496
- exports.resolveReturning = resolveReturning;
497
- /*
498
- ** currently all errors are handled with types and no dynamic check is required.
499
- ** errors return only when ignore is false and an expression needs ignorance.
500
- ** so, some errors written in function, but they will never be returned.
501
- ** e.g.
502
- ** if (e1Result.value.text === '') {
503
- ** return ignore ? ok(partialQuery()) : err(`<${toDescription(expression[0])}> -> neutral`);
504
- ** }
505
- * if result is neutral then ignore is true, so error in this example will never be returned.
506
- * but they exist in case new errors with dynamic check added.
507
- */
508
- var resolveExpression = function (expression, paramsStart, ignore) {
509
- if (ignore === void 0) { ignore = false; }
510
- // primitive expression
511
- if (expression === undefined) {
512
- return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)('undefined');
513
- }
514
- if (expression === null || typeof expression === 'boolean' || expression instanceof decimal_js_1.default
515
- || expression instanceof Date || typeof expression === 'number' || typeof expression === 'bigint') {
516
- return (0, never_catch_1.ok)(partialQuery("".concat(U_1.default.stringify(expression, true))));
517
- }
518
- if (typeof expression === 'string' || !(Array.isArray(expression) && ReservedExpressionKeys.includes(expression[0]))) {
519
- return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), [U_1.default.stringify(expression, false)]));
520
- }
521
- // wrapped expression
522
- var tokens = [];
523
- var params = [];
524
- var e1Result, e2Result, e3Result;
525
- switch (expression[0]) {
526
- case 'val':
527
- if (expression[1] === undefined) {
528
- return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)('val'), "> -> undefined"));
529
- }
530
- params.push(U_1.default.stringify(expression[1], false));
531
- return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), params));
532
- case '=n':
533
- case '!=n':
534
- case '=t':
535
- case '=f':
536
- case 'not':
537
- e1Result = resolveExpression(expression[1], paramsStart, ignore);
538
- if (!e1Result.ok) {
539
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> ").concat(e1Result.error));
540
- }
541
- if (e1Result.value.text === '') {
542
- return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> neutral"));
543
- }
544
- params.push.apply(params, e1Result.value.params);
545
- paramsStart += e1Result.value.params.length;
546
- switch (expression[0]) {
547
- case '=n':
548
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " IS NULL"), params));
549
- case '!=n':
550
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " IS NOT NULL"), params));
551
- case '=t':
552
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text), params));
553
- case '=f':
554
- case 'not':
555
- return (0, never_catch_1.ok)(partialQuery("NOT ".concat(e1Result.value.text), params));
556
- }
557
- case '+':
558
- case '-':
559
- case '*':
560
- case '/':
561
- case '||':
562
- case 'and':
563
- case 'or':
564
- case '**':
565
- if (expression[1] === undefined) {
566
- return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> undefined"));
567
- }
568
- for (var _i = 0, _a = expression[1]; _i < _a.length; _i++) {
569
- var v1 = _a[_i];
570
- var v1Result = resolveExpression(v1, paramsStart, ignore);
571
- if (!v1Result.ok) {
572
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[").concat(expression[1].indexOf(v1), "] -> ").concat(v1Result.error));
573
- }
574
- if (v1Result.value.text === '') {
575
- if (ignore) {
576
- continue;
577
- }
578
- else {
579
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[").concat(expression[1].indexOf(v1), "] -> neutral"));
580
- }
581
- }
582
- params.push.apply(params, v1Result.value.params);
583
- paramsStart += v1Result.value.params.length;
584
- tokens.push(v1Result.value.text);
585
- }
586
- switch (tokens.length) {
587
- case 0:
588
- return ignore ? (0, never_catch_1.ok)(partialQuery())
589
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> no operands given"));
590
- case 1:
591
- return (0, never_catch_1.ok)(partialQuery(tokens[0], params));
592
- default:
593
- switch (expression[0]) {
594
- case '+':
595
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' + '), " )"), params));
596
- case '-':
597
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' - '), " )"), params));
598
- case '*':
599
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' * '), " )"), params));
600
- case '/':
601
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' / '), " )"), params));
602
- case '||':
603
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' || '), " )"), params));
604
- case 'and':
605
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' AND '), " )"), params));
606
- case 'or':
607
- return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' OR '), " )"), params));
608
- case '**':
609
- var tmp = tokens.pop();
610
- tokens.splice(0, 0, 'a');
611
- return (0, never_catch_1.ok)({
612
- text: tokens.join(', power( ').substring(3) + ', ' + tmp + ' )'.repeat(tokens.length - 1),
613
- params: params
614
- });
615
- }
616
- }
617
- case 'fun':
618
- if (expression[1] === undefined) {
619
- return ignore ? (0, never_catch_1.ok)(partialQuery())
620
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[name] -> undefined"));
621
- }
622
- if (expression[2] === undefined) {
623
- return ignore ? (0, never_catch_1.ok)(partialQuery())
624
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters] -> undefined"));
625
- }
626
- for (var _b = 0, _c = expression[2]; _b < _c.length; _b++) {
627
- var v2 = _c[_b];
628
- var v2Result = resolveExpression(v2, paramsStart, ignore);
629
- if (!v2Result.ok) {
630
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters][").concat(expression[2].indexOf(v2), "] -> ").concat(v2Result.error));
631
- }
632
- if (v2Result.value.text === '') {
633
- return ignore ? (0, never_catch_1.ok)(partialQuery())
634
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters][").concat(expression[2].indexOf(v2), "] -> neutral"));
635
- }
636
- params.push.apply(params, v2Result.value.params);
637
- paramsStart += v2Result.value.params.length;
638
- tokens.push(v2Result.value.text);
639
- }
640
- return (0, never_catch_1.ok)(partialQuery("".concat(expression[1], "( ").concat(tokens.join(', '), " )").concat(expression[3]), params));
641
- case 'swt':
642
- var cases = expression[1];
643
- var otherwise = expression[2];
644
- if (cases === undefined) {
645
- if (!ignore) {
646
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases] -> undefined"));
647
- }
648
- }
649
- else {
650
- for (var _d = 0, cases_1 = cases; _d < cases_1.length; _d++) {
651
- var caseElement = cases_1[_d];
652
- if (caseElement === undefined) {
653
- if (ignore) {
654
- continue;
655
- }
656
- else {
657
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "] -> undefined"));
658
- }
659
- }
660
- var whenResult = resolveExpression(caseElement.when, paramsStart, ignore);
661
- if (!whenResult.ok) {
662
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][when] -> ").concat(whenResult.error));
663
- }
664
- if (whenResult.value.text === '') {
665
- if (ignore) {
666
- continue;
667
- }
668
- else {
669
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][when] -> neutral"));
670
- }
671
- }
672
- params.push.apply(params, whenResult.value.params);
673
- paramsStart += whenResult.value.params.length;
674
- var thenResult = resolveExpression(caseElement.then, paramsStart, ignore);
675
- if (!thenResult.ok) {
676
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][then] -> ").concat(thenResult.error));
677
- }
678
- if (thenResult.value.text === '') {
679
- if (ignore) {
680
- continue;
681
- }
682
- else {
683
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][then] -> neutral"));
684
- }
685
- }
686
- params.push.apply(params, thenResult.value.params);
687
- paramsStart += thenResult.value.params.length;
688
- if (tokens.length === 0) {
689
- tokens.push('CASE');
690
- }
691
- tokens.push("WHEN ".concat(whenResult.value.text, " THEN ").concat(thenResult.value.text));
692
- }
693
- }
694
- if (tokens.length === 0 && !ignore) {
695
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases] -> empty"));
696
- }
697
- if (otherwise === undefined) {
698
- if (tokens.length === 0) {
699
- return (0, never_catch_1.ok)(partialQuery());
700
- }
701
- else {
702
- tokens.push('END');
703
- }
704
- }
705
- else {
706
- var isOtherwiseNeutral = false;
707
- var otherwiseResult = resolveExpression(otherwise, paramsStart, ignore);
708
- if (!otherwiseResult.ok) {
709
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[otherwise] -> ").concat(otherwiseResult.error));
710
- }
711
- if (otherwiseResult.value.text === '') {
712
- if (ignore) {
713
- isOtherwiseNeutral = true;
714
- }
715
- else {
716
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[otherwise] -> neutral"));
717
- }
718
- }
719
- params.push.apply(params, otherwiseResult.value.params);
720
- paramsStart += otherwiseResult.value.params.length;
721
- if (tokens.length === 0) {
722
- if (isOtherwiseNeutral) {
723
- return (0, never_catch_1.ok)(partialQuery());
724
- }
725
- else {
726
- tokens.push(otherwiseResult.value.text);
727
- }
728
- }
729
- else {
730
- if (!isOtherwiseNeutral) {
731
- tokens.push('ELSE', otherwiseResult.value.text);
732
- }
733
- tokens.push('END');
734
- }
735
- }
736
- return (0, never_catch_1.ok)(partialQuery(tokens.join(' '), params));
737
- case 'col':
738
- case 'raw':
739
- return (0, never_catch_1.ok)(partialQuery("".concat(expression[1])));
740
- case 'qry':
741
- case 'exists':
742
- var subQueryDataResult = expression[1].getData(params);
743
- if (!subQueryDataResult.ok) {
744
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> ").concat(subQueryDataResult.error));
745
- }
746
- paramsStart += subQueryDataResult.value.params.length;
747
- switch (expression[0]) {
748
- case 'qry':
749
- return (0, never_catch_1.ok)(partialQuery("( ".concat(subQueryDataResult.value.sql, " )"), params));
750
- case 'exists':
751
- return (0, never_catch_1.ok)(partialQuery("EXISTS ( ".concat(subQueryDataResult.value.sql, " )"), params));
752
- }
753
- case '=':
754
- case '!=':
755
- case '>':
756
- case '>=':
757
- case '<':
758
- case '<=':
759
- case 'lk':
760
- case '@>':
761
- case '<@':
762
- case '?':
763
- case 'j-':
764
- e1Result = resolveExpression(expression[1], paramsStart, ignore);
765
- if (!e1Result.ok) {
766
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> ").concat(e1Result.error));
767
- }
768
- if (e1Result.value.text === '') {
769
- return ignore ? (0, never_catch_1.ok)(partialQuery())
770
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural"));
771
- }
772
- params.push.apply(params, e1Result.value.params);
773
- paramsStart += e1Result.value.params.length;
774
- e2Result = resolveExpression(expression[2], paramsStart, ignore);
775
- if (!e2Result.ok) {
776
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> ").concat(e2Result.error));
777
- }
778
- if (e2Result.value.text === '') {
779
- return ignore ? (0, never_catch_1.ok)(partialQuery())
780
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> netural"));
781
- }
782
- params.push.apply(params, e2Result.value.params);
783
- paramsStart += e2Result.value.params.length;
784
- switch (expression[0]) {
785
- case '=':
786
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " = ").concat(e2Result.value.text), params));
787
- case '!=':
788
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <> ").concat(e2Result.value.text), params));
789
- case '>':
790
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " > ").concat(e2Result.value.text), params));
791
- case '>=':
792
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " >= ").concat(e2Result.value.text), params));
793
- case '<':
794
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " < ").concat(e2Result.value.text), params));
795
- case '<=':
796
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <= ").concat(e2Result.value.text), params));
797
- case 'lk':
798
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ").concat(e2Result.value.text), params));
799
- case '@>':
800
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " @> ").concat(e2Result.value.text), params));
801
- case '<@':
802
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <@ ").concat(e2Result.value.text), params));
803
- case '?':
804
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ? ").concat(e2Result.value.text), params));
805
- case 'j-':
806
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " - ").concat(e2Result.value.text), params));
807
- }
808
- case 'in':
809
- case 'nin':
810
- case 'lka':
811
- case 'lks':
812
- case '?|':
813
- case '?&':
814
- case 'j-a':
815
- e1Result = resolveExpression(expression[1], paramsStart, ignore);
816
- if (!e1Result.ok) {
817
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> ").concat(e1Result.error));
818
- }
819
- if (e1Result.value.text === '') {
820
- return ignore ? (0, never_catch_1.ok)(partialQuery())
821
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural"));
822
- }
823
- params.push.apply(params, e1Result.value.params);
824
- paramsStart += e1Result.value.params.length;
825
- if (expression[2] === undefined) {
826
- return ignore ? (0, never_catch_1.ok)(partialQuery())
827
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> undefined"));
828
- }
829
- for (var _e = 0, _f = expression[2]; _e < _f.length; _e++) {
830
- var v2 = _f[_e];
831
- var v2Result = resolveExpression(v2, paramsStart, ignore);
832
- if (!v2Result.ok) {
833
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand][").concat(expression[2].indexOf(v2), "] -> ").concat(v2Result.error));
834
- }
835
- if (v2Result.value.text === '') {
836
- if (ignore) {
837
- continue;
838
- }
839
- else {
840
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand][").concat(expression[1].indexOf(v2), "] -> neutral"));
841
- }
842
- }
843
- params.push.apply(params, v2Result.value.params);
844
- paramsStart += v2Result.value.params.length;
845
- tokens.push(v2Result.value.text);
846
- }
847
- switch (tokens.length) {
848
- case 0:
849
- return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> empty"));
850
- case 1:
851
- switch (expression[0]) {
852
- case 'in':
853
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " = ").concat(tokens[0]), params));
854
- case 'nin':
855
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <> ").concat(tokens[0]), params));
856
- case 'lka':
857
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ").concat(tokens[0]), params));
858
- case 'lks':
859
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ").concat(tokens[0]), params));
860
- case '?|':
861
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ? ").concat(tokens[0]), params));
862
- case '?&':
863
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ? ").concat(tokens[0]), params));
864
- case 'j-a':
865
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " - ").concat(tokens[0]), params));
866
- }
867
- default:
868
- switch (expression[0]) {
869
- case 'in':
870
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " IN ( ").concat(tokens.join(', '), " )"), params));
871
- case 'nin':
872
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " NOT IN ( ").concat(tokens.join(', '), " )"), params));
873
- case 'lka':
874
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ALL( ARRAY[ ").concat(tokens.join(', '), " ] )"), params));
875
- case 'lks':
876
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE SOME( ARRAY[ ").concat(tokens.join(', '), " ] )"), params));
877
- case '?|':
878
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ?| ARRAY[ ").concat(tokens.join(', '), " ]"), params));
879
- case '?&':
880
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ?& ARRAY[ ").concat(tokens.join(', '), " ]"), params));
881
- case 'j-a':
882
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " - ARRAY[ ").concat(tokens.join(', '), " ]"), params));
883
- }
884
- }
885
- case 'bt':
886
- e1Result = resolveExpression(expression[1], paramsStart, ignore);
887
- if (!e1Result.ok) {
888
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> ").concat(e1Result.error));
889
- }
890
- if (e1Result.value.text === '') {
891
- return ignore ? (0, never_catch_1.ok)(partialQuery())
892
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural"));
893
- }
894
- params.push.apply(params, e1Result.value.params);
895
- paramsStart += e1Result.value.params.length;
896
- e2Result = resolveExpression(expression[2], paramsStart, ignore);
897
- if (!e2Result.ok) {
898
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> ").concat(e2Result.error));
899
- }
900
- if (e2Result.value.text === '') {
901
- return ignore ? (0, never_catch_1.ok)(partialQuery())
902
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> netural"));
903
- }
904
- params.push.apply(params, e2Result.value.params);
905
- paramsStart += e2Result.value.params.length;
906
- e3Result = resolveExpression(expression[3], paramsStart, ignore);
907
- if (!e3Result.ok) {
908
- return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[third operand] -> ").concat(e3Result.error));
909
- }
910
- if (e3Result.value.text === '') {
911
- return ignore ? (0, never_catch_1.ok)(partialQuery())
912
- : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[third operand] -> netural"));
913
- }
914
- params.push.apply(params, e3Result.value.params);
915
- paramsStart += e3Result.value.params.length;
916
- return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " BETWEEN ").concat(e2Result.value.text, " AND ").concat(e3Result.value.text), params));
917
- default:
918
- throw 'unexpected error. expect first element to be reserved key.';
919
- }
920
- };
921
- exports.resolveExpression = resolveExpression;
922
- var getTableDataOfJoinSelectColumn = function (tablesData, column) {
923
- var splitColumn = column.split('_');
924
- if (splitColumn.length < 2) {
925
- throw "no separator";
926
- }
927
- // in case alias or column key have "_". iterate on all of them until a match.
928
- var tableAliasUntilIndex = 1;
929
- while (true) {
930
- var tableAlias = splitColumn.slice(0, tableAliasUntilIndex).join('_');
931
- for (var _i = 0, tablesData_1 = tablesData; _i < tablesData_1.length; _i++) {
932
- var tableData = tablesData_1[_i];
933
- if (tableData.alias === tableAlias && tableData.table.columns[splitColumn.slice(tableAliasUntilIndex).join('_')] !== undefined) {
934
- return tableData;
935
- }
936
- }
937
- if (splitColumn.length > tableAliasUntilIndex + 1) {
938
- tableAliasUntilIndex++;
939
- }
940
- else {
941
- throw "column not found";
942
- }
943
- }
944
- };
945
- exports.getTableDataOfJoinSelectColumn = getTableDataOfJoinSelectColumn;
946
- var resolveColumn = function (table, column, full, alias) {
947
- var _a;
948
- if (full === void 0) { full = true; }
949
- var prefix;
950
- if (full) {
951
- if (alias !== undefined) {
952
- prefix = "\"".concat(alias, "\".");
953
- }
954
- else {
955
- prefix = "\"".concat(table.schema, "\".\"").concat(table.title, "\".");
956
- }
957
- }
958
- else {
959
- prefix = '';
960
- }
961
- return prefix + "\"".concat((_a = table.columns[column].title) !== null && _a !== void 0 ? _a : column, "\"");
962
- };
963
- exports.resolveColumn = resolveColumn;
964
- var partialQuery = function (text, params) {
965
- if (text === void 0) { text = ''; }
966
- if (params === void 0) { params = []; }
967
- return ({ text: text, params: params });
968
- };
969
- exports.partialQuery = partialQuery;
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.partialQuery = exports.resolveColumn = exports.getTableDataOfJoinSelectColumn = exports.resolveExpression = exports.resolveReturning = exports.resolveResult = exports.createQueryResult = exports.ReservedExpressionKeys = exports.createJoinSelectEntity = exports.createEntity = void 0;
27
+ var U_1 = __importDefault(require("./U"));
28
+ var decimal_js_1 = __importDefault(require("decimal.js"));
29
+ var context_1 = require("./context");
30
+ var never_catch_1 = require("never-catch");
31
+ var dictionary_1 = require("./dictionary");
32
+ // entity
33
+ var createEntity = function (table) { return ({
34
+ table: table,
35
+ context: (0, context_1.createContext)(table),
36
+ select: function (returning, where, options) {
37
+ var _this = this;
38
+ var _a, _b, _c;
39
+ var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false;
40
+ var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false;
41
+ var order = (_c = (options === null || options === void 0 ? void 0 : options.order)) !== null && _c !== void 0 ? _c : [];
42
+ var start = options === null || options === void 0 ? void 0 : options.start;
43
+ var step = options === null || options === void 0 ? void 0 : options.step;
44
+ var _returning = typeof returning === 'function' ? returning(this.context) : returning;
45
+ var createQuery = function (params) {
46
+ var tokens = ['SELECT'];
47
+ // select
48
+ var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
49
+ if (!resolvedReturning.ok) {
50
+ return (0, never_catch_1.err)("<select> -> ".concat(resolvedReturning.error));
51
+ }
52
+ params.push.apply(params, resolvedReturning.value.params);
53
+ tokens.push(resolvedReturning.value.text);
54
+ // from
55
+ tokens.push("FROM \"".concat(table.schema, "\".\"").concat(table.title, "\""));
56
+ // where
57
+ var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere);
58
+ if (!resolvedWhereResult.ok) {
59
+ return (0, never_catch_1.err)("<select>[where] -> ".concat(resolvedWhereResult.error));
60
+ }
61
+ if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
62
+ return (0, never_catch_1.err)("<select>[where] -> neutral");
63
+ }
64
+ params.push.apply(params, resolvedWhereResult.value.params);
65
+ tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'TRUE' : resolvedWhereResult.value.text);
66
+ // order
67
+ if (order.length !== 0) {
68
+ var ordersTextArray = [];
69
+ for (var _i = 0, order_1 = order; _i < order_1.length; _i++) {
70
+ var orderElement = order_1[_i];
71
+ var by = orderElement.by, direction = orderElement.direction;
72
+ ordersTextArray.push("".concat(resolveColumn(table, by, false), " ").concat((0, dictionary_1.toOrderDirection)(direction)));
73
+ }
74
+ tokens.push('ORDER BY', ordersTextArray.join(', '));
75
+ }
76
+ // pagination
77
+ if (start !== undefined) {
78
+ if (start < 0) {
79
+ return (0, never_catch_1.err)("<select>[start] -> invalid");
80
+ }
81
+ tokens.push('OFFSET', start.toString());
82
+ }
83
+ if (step !== undefined) {
84
+ if (step <= 0) {
85
+ return (0, never_catch_1.err)("<select>[step] -> invalid");
86
+ }
87
+ tokens.push('LIMIT', step.toString());
88
+ }
89
+ tokens.push(';');
90
+ var sql = tokens.join(' ');
91
+ return (0, never_catch_1.ok)({ sql: sql, params: params });
92
+ };
93
+ return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
94
+ },
95
+ insert: function (rows, returning, options) {
96
+ var _this = this;
97
+ var _a, _b, _c;
98
+ var nullableDefaultColumns = (_a = (options === null || options === void 0 ? void 0 : options.nullableDefaultColumns)) !== null && _a !== void 0 ? _a : [];
99
+ var ignoreInValues = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInValues)) !== null && _b !== void 0 ? _b : false;
100
+ var ignoreInReturning = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInValues)) !== null && _c !== void 0 ? _c : false;
101
+ var _returning = typeof returning === 'function' ? returning(this.context) : returning;
102
+ var createQuery = function (params) {
103
+ var tokens = ["INSERT INTO \"".concat(table.schema, "\".\"").concat(table.title, "\"")];
104
+ // columns
105
+ var insertingColumns = [];
106
+ var columnsTextArray = [];
107
+ for (var column in table.columns) {
108
+ if (nullableDefaultColumns.includes(column) || !table.columns[column].nullable) {
109
+ insertingColumns.push(column);
110
+ columnsTextArray.push(resolveColumn(table, column, false));
111
+ }
112
+ }
113
+ tokens.push("( ".concat(columnsTextArray.join(', '), " )"), 'VALUES');
114
+ // values
115
+ if (rows.length === 0) {
116
+ return (0, never_catch_1.err)('<insert>[values] -> empty');
117
+ }
118
+ var valuesTextArray = [];
119
+ var _rows = typeof rows === 'function' ? rows(_this.context) : rows;
120
+ for (var _i = 0, _rows_1 = _rows; _i < _rows_1.length; _i++) {
121
+ var _row = _rows_1[_i];
122
+ var rowTokens = [];
123
+ for (var _a = 0, insertingColumns_1 = insertingColumns; _a < insertingColumns_1.length; _a++) {
124
+ var insertingColumn = insertingColumns_1[_a];
125
+ if (_row[insertingColumn] === undefined) {
126
+ var column = table.columns[insertingColumn];
127
+ switch (column.default) {
128
+ case 'value':
129
+ rowTokens.push(U_1.default.stringify(column.value, true));
130
+ continue;
131
+ case true:
132
+ case 'auto-increment':
133
+ rowTokens.push('DEFAULT');
134
+ continue;
135
+ case 'created-at':
136
+ case 'updated-at':
137
+ rowTokens.push(U_1.default.stringify(new Date(), true));
138
+ continue;
139
+ }
140
+ if (column.nullable) {
141
+ rowTokens.push(U_1.default.stringify(null));
142
+ continue;
143
+ }
144
+ // never going to happen!
145
+ return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> no-value"));
146
+ }
147
+ else {
148
+ var resolvedExpressionResult = resolveExpression(_row[insertingColumn], params.length + 1, ignoreInValues);
149
+ if (!resolvedExpressionResult.ok) {
150
+ return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> ").concat(resolvedExpressionResult.error));
151
+ }
152
+ if (resolvedExpressionResult.value.text === '') {
153
+ return (0, never_catch_1.err)("<insert>[rows][".concat(_rows.indexOf(_row), "][").concat(insertingColumn, "] -> neutral"));
154
+ }
155
+ params.push.apply(params, resolvedExpressionResult.value.params);
156
+ rowTokens.push(resolvedExpressionResult.value.text);
157
+ }
158
+ }
159
+ valuesTextArray.push("( ".concat(rowTokens.join(', '), " )"));
160
+ }
161
+ tokens.push(valuesTextArray.join(', '));
162
+ // returning
163
+ if (_returning.length !== 0) {
164
+ var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
165
+ if (!resolvedReturning.ok) {
166
+ return (0, never_catch_1.err)("<insert> -> ".concat(resolvedReturning.error));
167
+ }
168
+ params.push.apply(params, resolvedReturning.value.params);
169
+ tokens.push('RETURNING', resolvedReturning.value.text);
170
+ }
171
+ tokens.push(';');
172
+ var sql = tokens.join(' ');
173
+ return (0, never_catch_1.ok)({ sql: sql, params: params });
174
+ };
175
+ return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
176
+ },
177
+ update: function (sets, where, returning, options) {
178
+ var _this = this;
179
+ var _a, _b, _c;
180
+ var ignoreInSets = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInSets)) !== null && _a !== void 0 ? _a : false;
181
+ var ignoreInWhere = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _b !== void 0 ? _b : false;
182
+ var ignoreInReturning = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _c !== void 0 ? _c : false;
183
+ var _returning = typeof returning === 'function' ? returning(this.context) : returning;
184
+ var createQuery = function (params) {
185
+ var tokens = ["UPDATE \"".concat(table.schema, "\".\"").concat(table.title, "\" SET")];
186
+ // set
187
+ var _set = typeof sets === 'function' ? sets(_this.context) : sets;
188
+ var setsTextArray = [];
189
+ var key;
190
+ for (key in _set) {
191
+ var setExpressionResult = resolveExpression(_set[key], params.length + 1, ignoreInSets);
192
+ if (!setExpressionResult.ok) {
193
+ return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> ").concat(setExpressionResult.error));
194
+ }
195
+ if (setExpressionResult.value.text === '') {
196
+ if (ignoreInSets) {
197
+ continue;
198
+ }
199
+ else {
200
+ return (0, never_catch_1.err)("<update>[sets][".concat(key, "] -> neutral"));
201
+ }
202
+ }
203
+ params.push.apply(params, setExpressionResult.value.params);
204
+ setsTextArray.push("".concat(resolveColumn(table, key, false), " = ").concat(setExpressionResult.value.text));
205
+ }
206
+ for (var column in table.columns) {
207
+ switch (table.columns[column].default) {
208
+ case 'updated-at':
209
+ setsTextArray.push("".concat(resolveColumn(table, column, false), " = ").concat(U_1.default.stringify(new Date(), true)));
210
+ break;
211
+ }
212
+ }
213
+ if (setsTextArray.length === 0) {
214
+ return (0, never_catch_1.err)('<update>[sets] -> empty');
215
+ }
216
+ tokens.push(setsTextArray.join(', '));
217
+ // where
218
+ var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere);
219
+ if (!resolvedWhereResult.ok) {
220
+ return (0, never_catch_1.err)("<update>[where] -> ".concat(resolvedWhereResult.error));
221
+ }
222
+ if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
223
+ return (0, never_catch_1.err)("<update>[where] -> neutral");
224
+ }
225
+ params.push.apply(params, resolvedWhereResult.value.params);
226
+ tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text);
227
+ // returning
228
+ if (_returning.length !== 0) {
229
+ var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
230
+ if (!resolvedReturning.ok) {
231
+ return (0, never_catch_1.err)("<update> -> ".concat(resolvedReturning.error));
232
+ }
233
+ params.push.apply(params, resolvedReturning.value.params);
234
+ tokens.push('RETURNING', resolvedReturning.value.text);
235
+ }
236
+ tokens.push(';');
237
+ var sql = tokens.join(' ');
238
+ return (0, never_catch_1.ok)({ sql: sql, params: params });
239
+ };
240
+ return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
241
+ },
242
+ delete: function (where, returning, options) {
243
+ var _this = this;
244
+ var _a, _b;
245
+ var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false;
246
+ var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false;
247
+ var _returning = typeof returning === 'function' ? returning(this.context) : returning;
248
+ var createQuery = function (params) {
249
+ var tokens = ["DELETE FROM \"".concat(table.schema, "\".\"").concat(table.title, "\"")];
250
+ // where
251
+ var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.context) : where, params.length + 1, ignoreInWhere);
252
+ if (!resolvedWhereResult.ok) {
253
+ return (0, never_catch_1.err)("<delete>[where] -> ".concat(resolvedWhereResult.error));
254
+ }
255
+ if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
256
+ return (0, never_catch_1.err)("<delete>[where] -> neutral");
257
+ }
258
+ params.push.apply(params, resolvedWhereResult.value.params);
259
+ tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text);
260
+ // returning
261
+ if (_returning.length !== 0) {
262
+ var resolvedReturning = resolveReturning(function (column) { return ({ type: table.columns[column].type, title: table.columns[column].title }); }, _returning, params.length + 1, ignoreInReturning);
263
+ if (!resolvedReturning.ok) {
264
+ return (0, never_catch_1.err)("<delete> -> ".concat(resolvedReturning.error));
265
+ }
266
+ params.push.apply(params, resolvedReturning.value.params);
267
+ tokens.push('RETURNING', resolvedReturning.value.text);
268
+ }
269
+ tokens.push(';');
270
+ var sql = tokens.join(' ');
271
+ return (0, never_catch_1.ok)({ sql: sql, params: params });
272
+ };
273
+ return createQueryResult(function (column) { return table.columns[column].type; }, createQuery, _returning);
274
+ },
275
+ join: function (mainAlias, joinType, joinTable, joinAlias, on) {
276
+ var _a;
277
+ return createJoinSelectEntity({ table: table, alias: mainAlias }, [{ table: joinTable, joinType: joinType, alias: joinAlias, on: on }], (_a = {}, _a[mainAlias] = (0, context_1.createContext)(table, mainAlias), _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a));
278
+ }
279
+ }); };
280
+ exports.createEntity = createEntity;
281
+ var createJoinSelectEntity = function (main, joinTables, contexts) { return ({
282
+ contexts: contexts,
283
+ select: function (returning, where, options) {
284
+ var _this = this;
285
+ var _a, _b, _c, _d;
286
+ var ignoreInWhere = (_a = (options === null || options === void 0 ? void 0 : options.ignoreInWhere)) !== null && _a !== void 0 ? _a : false;
287
+ var ignoreInReturning = (_b = (options === null || options === void 0 ? void 0 : options.ignoreInReturning)) !== null && _b !== void 0 ? _b : false;
288
+ var ignoreInJoin = (_c = (options === null || options === void 0 ? void 0 : options.ignoreInJoin)) !== null && _c !== void 0 ? _c : false;
289
+ var order = (_d = (options === null || options === void 0 ? void 0 : options.order)) !== null && _d !== void 0 ? _d : [];
290
+ var start = options === null || options === void 0 ? void 0 : options.start;
291
+ var step = options === null || options === void 0 ? void 0 : options.step;
292
+ var allTables = __spreadArray([main], joinTables, true);
293
+ var _returning = typeof returning === 'function' ? returning(this.contexts) : returning;
294
+ var createQuery = function (params) {
295
+ var tokens = ['SELECT'];
296
+ // select
297
+ var resolvedReturning = resolveReturning(function (column) {
298
+ var _a = getTableDataOfJoinSelectColumn(allTables, column), table = _a.table, alias = _a.alias;
299
+ var columnKey = column.substring((alias + '_').length);
300
+ return { type: table.columns[columnKey].type, title: table.columns[columnKey].title, alias: alias };
301
+ }, _returning, params.length + 1, ignoreInReturning);
302
+ if (!resolvedReturning.ok) {
303
+ return (0, never_catch_1.err)("<join-select>[columns] -> ".concat(resolvedReturning.error));
304
+ }
305
+ params.push.apply(params, resolvedReturning.value.params);
306
+ tokens.push(resolvedReturning.value.text, "FROM \"".concat(main.table.schema, "\".\"").concat(main.table.title, "\" \"").concat(main.alias, "\""));
307
+ // join
308
+ for (var _i = 0, joinTables_1 = joinTables; _i < joinTables_1.length; _i++) {
309
+ var joinTable = joinTables_1[_i];
310
+ var onExpressionResult = resolveExpression(typeof joinTable.on === 'function' ? joinTable.on(_this.contexts) : joinTable.on, params.length + 1, ignoreInJoin);
311
+ if (!onExpressionResult.ok) {
312
+ return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> ").concat(onExpressionResult.error));
313
+ }
314
+ if (onExpressionResult.value.text === '' && !ignoreInJoin) {
315
+ return (0, never_catch_1.err)("<join-select>[join][".concat(joinTables.indexOf(joinTable), "] -> neutral"));
316
+ }
317
+ params.push.apply(params, onExpressionResult.value.params);
318
+ tokens.push("".concat((0, dictionary_1.toJoinType)(joinTable.joinType), " \"").concat(joinTable.table.schema, "\".\"").concat(joinTable.table.title, "\" \"").concat(joinTable.alias, "\" ON ").concat(onExpressionResult.value.text));
319
+ }
320
+ // where
321
+ var resolvedWhereResult = resolveExpression(typeof where === 'function' ? where(_this.contexts) : where, params.length + 1, ignoreInWhere);
322
+ if (!resolvedWhereResult.ok) {
323
+ return (0, never_catch_1.err)("<join-select>[where] -> ".concat(resolvedWhereResult.error));
324
+ }
325
+ if (resolvedWhereResult.value.text === '' && !ignoreInWhere) {
326
+ return (0, never_catch_1.err)("<join-select>[where] -> neutral");
327
+ }
328
+ params.push.apply(params, resolvedWhereResult.value.params);
329
+ tokens.push('WHERE', resolvedWhereResult.value.text === '' ? 'FALSE' : resolvedWhereResult.value.text);
330
+ // order
331
+ if (order.length !== 0) {
332
+ var ordersTextArray = [];
333
+ for (var _a = 0, order_2 = order; _a < order_2.length; _a++) {
334
+ var orderElement = order_2[_a];
335
+ var by = orderElement.by, direction = orderElement.direction;
336
+ var _b = getTableDataOfJoinSelectColumn(allTables, by), alias = _b.alias, title = _b.table.title;
337
+ ordersTextArray.push("".concat(alias !== undefined ? "\"".concat(alias, "\".") : '', "\"").concat(title, "\" ").concat(direction));
338
+ }
339
+ tokens.push('ORDER BY', ordersTextArray.join(', '));
340
+ }
341
+ // pagination
342
+ if (start !== undefined) {
343
+ if (start < 0) {
344
+ return (0, never_catch_1.err)("<join-select>[start] -> invalid");
345
+ }
346
+ tokens.push('OFFSET', start.toString());
347
+ }
348
+ if (step !== undefined) {
349
+ if (step <= 0) {
350
+ return (0, never_catch_1.err)("<join-select>[step] -> invalid");
351
+ }
352
+ tokens.push('LIMIT', step.toString());
353
+ }
354
+ tokens.push(';');
355
+ var sql = tokens.join(' ');
356
+ return (0, never_catch_1.ok)({ sql: sql, params: params });
357
+ };
358
+ return createQueryResult(function (column) {
359
+ var _a = getTableDataOfJoinSelectColumn(allTables, column), alias = _a.alias, columns = _a.table.columns;
360
+ return columns[column.substring((alias + '_').length)].type;
361
+ }, createQuery, _returning);
362
+ },
363
+ join: function (joinType, joinTable, joinAlias, on) {
364
+ var _a;
365
+ joinTables.push({ table: joinTable, on: on, joinType: joinType, alias: joinAlias });
366
+ return createJoinSelectEntity(main, joinTables, __assign(__assign({}, contexts), (_a = {}, _a[joinAlias] = (0, context_1.createContext)(joinTable, joinAlias), _a)));
367
+ }
368
+ }); };
369
+ exports.createJoinSelectEntity = createJoinSelectEntity;
370
+ // utils
371
+ var ReservedExpressionKeys = ['val', '=n', '!=n', '=t', '=f', 'not', '+', '-', '*', '/', '||', 'and', 'or',
372
+ '**', 'fun', 'swt', 'col', 'raw', '=', '!=', '>', '>=', '<', '<=', 'lk', '@>', '<@', '?', 'j-', 'in', 'nin',
373
+ 'lka', 'lks', '?|', '?&', 'j-a', 'bt', 'qry', 'exists'];
374
+ exports.ReservedExpressionKeys = ReservedExpressionKeys;
375
+ var createQueryResult = function (getColumnType, createQuery, returning) {
376
+ var query = undefined;
377
+ return ({
378
+ getData: function (params) {
379
+ if (params === void 0) { params = []; }
380
+ if (query === undefined) {
381
+ var createQueryResult_1 = createQuery(params);
382
+ if (createQueryResult_1.ok) {
383
+ query = createQueryResult_1.value;
384
+ }
385
+ else {
386
+ return createQueryResult_1;
387
+ }
388
+ }
389
+ return (0, never_catch_1.ok)(query);
390
+ },
391
+ exec: function (client, mode, params) {
392
+ if (params === void 0) { params = []; }
393
+ if (query === undefined) {
394
+ var createQueryResult_2 = createQuery(params);
395
+ if (createQueryResult_2.ok) {
396
+ query = createQueryResult_2.value;
397
+ }
398
+ else {
399
+ return Promise.resolve(createQueryResult_2);
400
+ }
401
+ }
402
+ return client.query(query.sql, query.params)
403
+ .then(function (_a) {
404
+ var rows = _a.rows;
405
+ return resolveResult(getColumnType, returning, rows, mode);
406
+ })
407
+ .catch(function (e) { return (0, never_catch_1.err)(e); });
408
+ }
409
+ });
410
+ };
411
+ exports.createQueryResult = createQueryResult;
412
+ /*
413
+ * take getColumnType instead of table to support join-select too.
414
+ * this cause hard to call this function directly, but it is only solution I could come up with.
415
+ */
416
+ var resolveResult = function (getColumnType, columns, rows, mode) {
417
+ // check size in count and get mode
418
+ if (mode[0] === 'count') {
419
+ return rows.length === mode[1] ? (0, never_catch_1.ok)(undefined) : (0, never_catch_1.err)(false);
420
+ }
421
+ if (mode[0] === 'get' && rows.length !== (mode[1] === 'one' ? 1 : mode[1])) {
422
+ return (0, never_catch_1.err)(false);
423
+ }
424
+ // parse result
425
+ rows.forEach(function (_, i) {
426
+ columns.forEach(function (column) {
427
+ if (typeof column !== 'object') {
428
+ rows[i][column] = U_1.default.parse(rows[i][column], getColumnType(column));
429
+ }
430
+ });
431
+ });
432
+ // return first element for [get, one] mode and all for [get, number] and []
433
+ if (mode[0] === 'get' && mode[1] === 'one') {
434
+ return (0, never_catch_1.ok)(rows[0]);
435
+ }
436
+ else {
437
+ return (0, never_catch_1.ok)(rows);
438
+ }
439
+ };
440
+ exports.resolveResult = resolveResult;
441
+ /*
442
+ * take getColumnTypeTitleAlias instead of table to support join-select too.
443
+ * this cause hard to call this function directly, but it is only solution I could come up with.
444
+ */
445
+ var resolveReturning = function (getColumnTypeTitleAlias, columns, paramsStart, ignore) {
446
+ var tokens = [];
447
+ var params = [];
448
+ for (var _i = 0, columns_1 = columns; _i < columns_1.length; _i++) {
449
+ var column = columns_1[_i];
450
+ if (typeof column === 'object') {
451
+ var resolvedExpResult = resolveExpression(column.exp, paramsStart, ignore);
452
+ if (!resolvedExpResult.ok) {
453
+ return (0, never_catch_1.err)("<returning>[".concat(column.as, "] -> ").concat(resolvedExpResult.error));
454
+ }
455
+ if (resolvedExpResult.value.text === '') {
456
+ return (0, never_catch_1.err)("<returning>[".concat(column.as, "] -> neutral"));
457
+ }
458
+ params.push.apply(params, resolvedExpResult.value.params);
459
+ paramsStart += resolvedExpResult.value.params.length;
460
+ tokens.push("( ".concat(resolvedExpResult.value.text, " ) AS \"").concat(column.as, "\""));
461
+ }
462
+ else {
463
+ var _a = getColumnTypeTitleAlias(column), type = _a.type, title = _a.title, alias = _a.alias;
464
+ var customParse = void 0;
465
+ switch (type) {
466
+ case 'date':
467
+ case 'timestamp with time zone':
468
+ case 'timestamp without time zone':
469
+ customParse = '::TEXT';
470
+ break;
471
+ }
472
+ if (title === undefined) {
473
+ if (alias !== undefined) {
474
+ tokens.push("\"".concat(alias, "\".") +
475
+ "\"".concat(column.substring((alias + '_').length), "\"") +
476
+ (customParse !== null && customParse !== void 0 ? customParse : '') +
477
+ " AS \"".concat(column, "\""));
478
+ }
479
+ else {
480
+ tokens.push("\"".concat(column, "\"") +
481
+ "".concat(customParse !== undefined ? "".concat(customParse, " AS \"").concat(column, "\"") : ''));
482
+ }
483
+ }
484
+ else {
485
+ tokens.push((alias !== undefined ? "\"".concat(alias, "\".") : '') +
486
+ "\"".concat(title, "\"") +
487
+ (customParse !== null && customParse !== void 0 ? customParse : '') +
488
+ " AS \"".concat(column, "\""));
489
+ }
490
+ }
491
+ }
492
+ if (tokens.length === 0) {
493
+ return (0, never_catch_1.err)('<returning> -> empty');
494
+ }
495
+ return (0, never_catch_1.ok)(partialQuery(tokens.join(', '), params));
496
+ };
497
+ exports.resolveReturning = resolveReturning;
498
+ /*
499
+ ** currently all errors are handled with types and no dynamic check is required.
500
+ ** errors return only when ignore is false and an expression needs ignorance.
501
+ ** so, some errors written in function, but they will never be returned.
502
+ ** e.g.
503
+ ** if (e1Result.value.text === '') {
504
+ ** return ignore ? ok(partialQuery()) : err(`<${toDescription(expression[0])}> -> neutral`);
505
+ ** }
506
+ * if result is neutral then ignore is true, so error in this example will never be returned.
507
+ * but they exist in case new errors with dynamic check added.
508
+ */
509
+ var resolveExpression = function (expression, paramsStart, ignore) {
510
+ if (ignore === void 0) { ignore = false; }
511
+ // primitive expression
512
+ if (expression === undefined) {
513
+ return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)('undefined');
514
+ }
515
+ if (expression === null || typeof expression === 'boolean' || expression instanceof decimal_js_1.default
516
+ || expression instanceof Date || typeof expression === 'number' || typeof expression === 'bigint') {
517
+ return (0, never_catch_1.ok)(partialQuery("".concat(U_1.default.stringify(expression, true))));
518
+ }
519
+ if (typeof expression === 'string' || !(Array.isArray(expression) && ReservedExpressionKeys.includes(expression[0]))) {
520
+ return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), [U_1.default.stringify(expression, false)]));
521
+ }
522
+ // wrapped expression
523
+ var tokens = [];
524
+ var params = [];
525
+ var e1Result, e2Result, e3Result;
526
+ switch (expression[0]) {
527
+ case 'val':
528
+ if (expression[1] === undefined) {
529
+ return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)('val'), "> -> undefined"));
530
+ }
531
+ params.push(U_1.default.stringify(expression[1], false));
532
+ return (0, never_catch_1.ok)(partialQuery("$".concat(paramsStart++), params));
533
+ case '=n':
534
+ case '!=n':
535
+ case '=t':
536
+ case '=f':
537
+ case 'not':
538
+ e1Result = resolveExpression(expression[1], paramsStart, ignore);
539
+ if (!e1Result.ok) {
540
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> ").concat(e1Result.error));
541
+ }
542
+ if (e1Result.value.text === '') {
543
+ return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> neutral"));
544
+ }
545
+ params.push.apply(params, e1Result.value.params);
546
+ paramsStart += e1Result.value.params.length;
547
+ switch (expression[0]) {
548
+ case '=n':
549
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " IS NULL"), params));
550
+ case '!=n':
551
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " IS NOT NULL"), params));
552
+ case '=t':
553
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text), params));
554
+ case '=f':
555
+ case 'not':
556
+ return (0, never_catch_1.ok)(partialQuery("NOT ".concat(e1Result.value.text), params));
557
+ }
558
+ case '+':
559
+ case '-':
560
+ case '*':
561
+ case '/':
562
+ case '||':
563
+ case 'and':
564
+ case 'or':
565
+ case '**':
566
+ if (expression[1] === undefined) {
567
+ return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> undefined"));
568
+ }
569
+ for (var _i = 0, _a = expression[1]; _i < _a.length; _i++) {
570
+ var v1 = _a[_i];
571
+ var v1Result = resolveExpression(v1, paramsStart, ignore);
572
+ if (!v1Result.ok) {
573
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[").concat(expression[1].indexOf(v1), "] -> ").concat(v1Result.error));
574
+ }
575
+ if (v1Result.value.text === '') {
576
+ if (ignore) {
577
+ continue;
578
+ }
579
+ else {
580
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[").concat(expression[1].indexOf(v1), "] -> neutral"));
581
+ }
582
+ }
583
+ params.push.apply(params, v1Result.value.params);
584
+ paramsStart += v1Result.value.params.length;
585
+ tokens.push(v1Result.value.text);
586
+ }
587
+ switch (tokens.length) {
588
+ case 0:
589
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
590
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> no operands given"));
591
+ case 1:
592
+ return (0, never_catch_1.ok)(partialQuery(tokens[0], params));
593
+ default:
594
+ switch (expression[0]) {
595
+ case '+':
596
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' + '), " )"), params));
597
+ case '-':
598
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' - '), " )"), params));
599
+ case '*':
600
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' * '), " )"), params));
601
+ case '/':
602
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' / '), " )"), params));
603
+ case '||':
604
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' || '), " )"), params));
605
+ case 'and':
606
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' AND '), " )"), params));
607
+ case 'or':
608
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(tokens.join(' OR '), " )"), params));
609
+ case '**':
610
+ var tmp = tokens.pop();
611
+ tokens.splice(0, 0, 'a');
612
+ return (0, never_catch_1.ok)({
613
+ text: tokens.join(', power( ').substring(3) + ', ' + tmp + ' )'.repeat(tokens.length - 1),
614
+ params: params
615
+ });
616
+ }
617
+ }
618
+ case 'fun':
619
+ if (expression[1] === undefined) {
620
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
621
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[name] -> undefined"));
622
+ }
623
+ if (expression[2] === undefined) {
624
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
625
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters] -> undefined"));
626
+ }
627
+ for (var _b = 0, _c = expression[2]; _b < _c.length; _b++) {
628
+ var v2 = _c[_b];
629
+ var v2Result = resolveExpression(v2, paramsStart, ignore);
630
+ if (!v2Result.ok) {
631
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters][").concat(expression[2].indexOf(v2), "] -> ").concat(v2Result.error));
632
+ }
633
+ if (v2Result.value.text === '') {
634
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
635
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[parameters][").concat(expression[2].indexOf(v2), "] -> neutral"));
636
+ }
637
+ params.push.apply(params, v2Result.value.params);
638
+ paramsStart += v2Result.value.params.length;
639
+ tokens.push(v2Result.value.text);
640
+ }
641
+ return (0, never_catch_1.ok)(partialQuery("".concat(expression[1], "( ").concat(tokens.join(', '), " )").concat(expression[3]), params));
642
+ case 'swt':
643
+ var cases = expression[1];
644
+ var otherwise = expression[2];
645
+ if (cases === undefined) {
646
+ if (!ignore) {
647
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases] -> undefined"));
648
+ }
649
+ }
650
+ else {
651
+ for (var _d = 0, cases_1 = cases; _d < cases_1.length; _d++) {
652
+ var caseElement = cases_1[_d];
653
+ if (caseElement === undefined) {
654
+ if (ignore) {
655
+ continue;
656
+ }
657
+ else {
658
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "] -> undefined"));
659
+ }
660
+ }
661
+ var whenResult = resolveExpression(caseElement.when, paramsStart, ignore);
662
+ if (!whenResult.ok) {
663
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][when] -> ").concat(whenResult.error));
664
+ }
665
+ if (whenResult.value.text === '') {
666
+ if (ignore) {
667
+ continue;
668
+ }
669
+ else {
670
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][when] -> neutral"));
671
+ }
672
+ }
673
+ params.push.apply(params, whenResult.value.params);
674
+ paramsStart += whenResult.value.params.length;
675
+ var thenResult = resolveExpression(caseElement.then, paramsStart, ignore);
676
+ if (!thenResult.ok) {
677
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][then] -> ").concat(thenResult.error));
678
+ }
679
+ if (thenResult.value.text === '') {
680
+ if (ignore) {
681
+ continue;
682
+ }
683
+ else {
684
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases][").concat(cases.indexOf(caseElement), "][then] -> neutral"));
685
+ }
686
+ }
687
+ params.push.apply(params, thenResult.value.params);
688
+ paramsStart += thenResult.value.params.length;
689
+ if (tokens.length === 0) {
690
+ tokens.push('CASE');
691
+ }
692
+ tokens.push("WHEN ".concat(whenResult.value.text, " THEN ").concat(thenResult.value.text));
693
+ }
694
+ }
695
+ if (tokens.length === 0 && !ignore) {
696
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[cases] -> empty"));
697
+ }
698
+ if (otherwise === undefined) {
699
+ if (tokens.length === 0) {
700
+ return (0, never_catch_1.ok)(partialQuery());
701
+ }
702
+ else {
703
+ tokens.push('END');
704
+ }
705
+ }
706
+ else {
707
+ var isOtherwiseNeutral = false;
708
+ var otherwiseResult = resolveExpression(otherwise, paramsStart, ignore);
709
+ if (!otherwiseResult.ok) {
710
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[otherwise] -> ").concat(otherwiseResult.error));
711
+ }
712
+ if (otherwiseResult.value.text === '') {
713
+ if (ignore) {
714
+ isOtherwiseNeutral = true;
715
+ }
716
+ else {
717
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[otherwise] -> neutral"));
718
+ }
719
+ }
720
+ params.push.apply(params, otherwiseResult.value.params);
721
+ paramsStart += otherwiseResult.value.params.length;
722
+ if (tokens.length === 0) {
723
+ if (isOtherwiseNeutral) {
724
+ return (0, never_catch_1.ok)(partialQuery());
725
+ }
726
+ else {
727
+ tokens.push(otherwiseResult.value.text);
728
+ }
729
+ }
730
+ else {
731
+ if (!isOtherwiseNeutral) {
732
+ tokens.push('ELSE', otherwiseResult.value.text);
733
+ }
734
+ tokens.push('END');
735
+ }
736
+ }
737
+ return (0, never_catch_1.ok)(partialQuery(tokens.join(' '), params));
738
+ case 'col':
739
+ case 'raw':
740
+ return (0, never_catch_1.ok)(partialQuery("".concat(expression[1])));
741
+ case 'qry':
742
+ case 'exists':
743
+ var subQueryDataResult = expression[1].getData(params);
744
+ if (!subQueryDataResult.ok) {
745
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), "> -> ").concat(subQueryDataResult.error));
746
+ }
747
+ paramsStart += subQueryDataResult.value.params.length;
748
+ switch (expression[0]) {
749
+ case 'qry':
750
+ return (0, never_catch_1.ok)(partialQuery("( ".concat(subQueryDataResult.value.sql, " )"), params));
751
+ case 'exists':
752
+ return (0, never_catch_1.ok)(partialQuery("EXISTS ( ".concat(subQueryDataResult.value.sql, " )"), params));
753
+ }
754
+ case '=':
755
+ case '!=':
756
+ case '>':
757
+ case '>=':
758
+ case '<':
759
+ case '<=':
760
+ case 'lk':
761
+ case '@>':
762
+ case '<@':
763
+ case '?':
764
+ case 'j-':
765
+ e1Result = resolveExpression(expression[1], paramsStart, ignore);
766
+ if (!e1Result.ok) {
767
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> ").concat(e1Result.error));
768
+ }
769
+ if (e1Result.value.text === '') {
770
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
771
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural"));
772
+ }
773
+ params.push.apply(params, e1Result.value.params);
774
+ paramsStart += e1Result.value.params.length;
775
+ e2Result = resolveExpression(expression[2], paramsStart, ignore);
776
+ if (!e2Result.ok) {
777
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> ").concat(e2Result.error));
778
+ }
779
+ if (e2Result.value.text === '') {
780
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
781
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> netural"));
782
+ }
783
+ params.push.apply(params, e2Result.value.params);
784
+ paramsStart += e2Result.value.params.length;
785
+ switch (expression[0]) {
786
+ case '=':
787
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " = ").concat(e2Result.value.text), params));
788
+ case '!=':
789
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <> ").concat(e2Result.value.text), params));
790
+ case '>':
791
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " > ").concat(e2Result.value.text), params));
792
+ case '>=':
793
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " >= ").concat(e2Result.value.text), params));
794
+ case '<':
795
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " < ").concat(e2Result.value.text), params));
796
+ case '<=':
797
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <= ").concat(e2Result.value.text), params));
798
+ case 'lk':
799
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ").concat(e2Result.value.text), params));
800
+ case '@>':
801
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " @> ").concat(e2Result.value.text), params));
802
+ case '<@':
803
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <@ ").concat(e2Result.value.text), params));
804
+ case '?':
805
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ? ").concat(e2Result.value.text), params));
806
+ case 'j-':
807
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " - ").concat(e2Result.value.text), params));
808
+ }
809
+ case 'in':
810
+ case 'nin':
811
+ case 'lka':
812
+ case 'lks':
813
+ case '?|':
814
+ case '?&':
815
+ case 'j-a':
816
+ e1Result = resolveExpression(expression[1], paramsStart, ignore);
817
+ if (!e1Result.ok) {
818
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> ").concat(e1Result.error));
819
+ }
820
+ if (e1Result.value.text === '') {
821
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
822
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural"));
823
+ }
824
+ params.push.apply(params, e1Result.value.params);
825
+ paramsStart += e1Result.value.params.length;
826
+ if (expression[2] === undefined) {
827
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
828
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> undefined"));
829
+ }
830
+ for (var _e = 0, _f = expression[2]; _e < _f.length; _e++) {
831
+ var v2 = _f[_e];
832
+ var v2Result = resolveExpression(v2, paramsStart, ignore);
833
+ if (!v2Result.ok) {
834
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand][").concat(expression[2].indexOf(v2), "] -> ").concat(v2Result.error));
835
+ }
836
+ if (v2Result.value.text === '') {
837
+ if (ignore) {
838
+ continue;
839
+ }
840
+ else {
841
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand][").concat(expression[1].indexOf(v2), "] -> neutral"));
842
+ }
843
+ }
844
+ params.push.apply(params, v2Result.value.params);
845
+ paramsStart += v2Result.value.params.length;
846
+ tokens.push(v2Result.value.text);
847
+ }
848
+ switch (tokens.length) {
849
+ case 0:
850
+ return ignore ? (0, never_catch_1.ok)(partialQuery()) : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> empty"));
851
+ case 1:
852
+ switch (expression[0]) {
853
+ case 'in':
854
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " = ").concat(tokens[0]), params));
855
+ case 'nin':
856
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " <> ").concat(tokens[0]), params));
857
+ case 'lka':
858
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ").concat(tokens[0]), params));
859
+ case 'lks':
860
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ").concat(tokens[0]), params));
861
+ case '?|':
862
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ? ").concat(tokens[0]), params));
863
+ case '?&':
864
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ? ").concat(tokens[0]), params));
865
+ case 'j-a':
866
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " - ").concat(tokens[0]), params));
867
+ }
868
+ default:
869
+ switch (expression[0]) {
870
+ case 'in':
871
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " IN ( ").concat(tokens.join(', '), " )"), params));
872
+ case 'nin':
873
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " NOT IN ( ").concat(tokens.join(', '), " )"), params));
874
+ case 'lka':
875
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE ALL( ARRAY[ ").concat(tokens.join(', '), " ] )"), params));
876
+ case 'lks':
877
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " LIKE SOME( ARRAY[ ").concat(tokens.join(', '), " ] )"), params));
878
+ case '?|':
879
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ?| ARRAY[ ").concat(tokens.join(', '), " ]"), params));
880
+ case '?&':
881
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " ?& ARRAY[ ").concat(tokens.join(', '), " ]"), params));
882
+ case 'j-a':
883
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " - ARRAY[ ").concat(tokens.join(', '), " ]"), params));
884
+ }
885
+ }
886
+ case 'bt':
887
+ e1Result = resolveExpression(expression[1], paramsStart, ignore);
888
+ if (!e1Result.ok) {
889
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> ").concat(e1Result.error));
890
+ }
891
+ if (e1Result.value.text === '') {
892
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
893
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[first operand] -> netural"));
894
+ }
895
+ params.push.apply(params, e1Result.value.params);
896
+ paramsStart += e1Result.value.params.length;
897
+ e2Result = resolveExpression(expression[2], paramsStart, ignore);
898
+ if (!e2Result.ok) {
899
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> ").concat(e2Result.error));
900
+ }
901
+ if (e2Result.value.text === '') {
902
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
903
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[second operand] -> netural"));
904
+ }
905
+ params.push.apply(params, e2Result.value.params);
906
+ paramsStart += e2Result.value.params.length;
907
+ e3Result = resolveExpression(expression[3], paramsStart, ignore);
908
+ if (!e3Result.ok) {
909
+ return (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[third operand] -> ").concat(e3Result.error));
910
+ }
911
+ if (e3Result.value.text === '') {
912
+ return ignore ? (0, never_catch_1.ok)(partialQuery())
913
+ : (0, never_catch_1.err)("<".concat((0, dictionary_1.toReservedExpressionKeyDescription)(expression[0]), ">[third operand] -> netural"));
914
+ }
915
+ params.push.apply(params, e3Result.value.params);
916
+ paramsStart += e3Result.value.params.length;
917
+ return (0, never_catch_1.ok)(partialQuery("".concat(e1Result.value.text, " BETWEEN ").concat(e2Result.value.text, " AND ").concat(e3Result.value.text), params));
918
+ default:
919
+ throw 'unexpected error. expect first element to be reserved key.';
920
+ }
921
+ };
922
+ exports.resolveExpression = resolveExpression;
923
+ var getTableDataOfJoinSelectColumn = function (tablesData, column) {
924
+ var splitColumn = column.split('_');
925
+ if (splitColumn.length < 2) {
926
+ throw "no separator";
927
+ }
928
+ // in case alias or column key have "_". iterate on all of them until a match.
929
+ var tableAliasUntilIndex = 1;
930
+ while (true) {
931
+ var tableAlias = splitColumn.slice(0, tableAliasUntilIndex).join('_');
932
+ for (var _i = 0, tablesData_1 = tablesData; _i < tablesData_1.length; _i++) {
933
+ var tableData = tablesData_1[_i];
934
+ if (tableData.alias === tableAlias && tableData.table.columns[splitColumn.slice(tableAliasUntilIndex).join('_')] !== undefined) {
935
+ return tableData;
936
+ }
937
+ }
938
+ if (splitColumn.length > tableAliasUntilIndex + 1) {
939
+ tableAliasUntilIndex++;
940
+ }
941
+ else {
942
+ throw "column not found";
943
+ }
944
+ }
945
+ };
946
+ exports.getTableDataOfJoinSelectColumn = getTableDataOfJoinSelectColumn;
947
+ var resolveColumn = function (table, column, full, alias) {
948
+ var _a;
949
+ if (full === void 0) { full = true; }
950
+ var prefix;
951
+ if (full) {
952
+ if (alias !== undefined) {
953
+ prefix = "\"".concat(alias, "\".");
954
+ }
955
+ else {
956
+ prefix = "\"".concat(table.schema, "\".\"").concat(table.title, "\".");
957
+ }
958
+ }
959
+ else {
960
+ prefix = '';
961
+ }
962
+ return prefix + "\"".concat((_a = table.columns[column].title) !== null && _a !== void 0 ? _a : column, "\"");
963
+ };
964
+ exports.resolveColumn = resolveColumn;
965
+ var partialQuery = function (text, params) {
966
+ if (text === void 0) { text = ''; }
967
+ if (params === void 0) { params = []; }
968
+ return ({ text: text, params: params });
969
+ };
970
+ exports.partialQuery = partialQuery;
970
971
  //# sourceMappingURL=entity.js.map