@mrnafisia/type-query 1.0.46 → 1.0.47

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