@mrnafisia/type-query 1.0.0

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