pqb 0.7.13 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +53 -48
- package/dist/index.esm.js +103 -103
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +103 -103
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnType.test.ts +2 -2
- package/src/columnSchema/columnType.ts +9 -9
- package/src/columnSchema/columnTypes.ts +27 -13
- package/src/columnSchema/timestamps.test.ts +6 -6
- package/src/db.ts +1 -1
- package/src/query.ts +1 -1
- package/src/queryMethods/create.ts +6 -6
- package/src/queryMethods/for.ts +3 -3
- package/src/queryMethods/having.ts +1 -1
- package/src/queryMethods/join.ts +4 -4
- package/src/queryMethods/json.ts +1 -1
- package/src/queryMethods/queryMethods.ts +2 -2
- package/src/queryMethods/select.ts +3 -3
- package/src/queryMethods/update.ts +17 -17
- package/src/queryMethods/where.test.ts +1 -1
- package/src/queryMethods/where.ts +4 -4
- package/src/relations.ts +1 -1
- package/src/sql/aggregate.ts +2 -2
- package/src/sql/copy.ts +3 -3
- package/src/sql/delete.ts +5 -5
- package/src/sql/fromAndAs.ts +4 -4
- package/src/sql/having.ts +7 -7
- package/src/sql/insert.ts +5 -5
- package/src/sql/join.ts +16 -16
- package/src/sql/select.ts +6 -6
- package/src/sql/toSql.ts +24 -24
- package/src/sql/update.ts +4 -4
- package/src/sql/where.ts +18 -18
package/dist/index.js
CHANGED
|
@@ -225,24 +225,24 @@ const windowToSql = (window, values, quotedAs) => {
|
|
|
225
225
|
}
|
|
226
226
|
};
|
|
227
227
|
|
|
228
|
-
const processJoinItem = (ctx,
|
|
228
|
+
const processJoinItem = (ctx, table, args, quotedAs) => {
|
|
229
229
|
let target;
|
|
230
230
|
let conditions;
|
|
231
231
|
const [first] = args;
|
|
232
232
|
if (typeof first === "string") {
|
|
233
|
-
if (first in
|
|
233
|
+
if (first in table.relations) {
|
|
234
234
|
const {
|
|
235
235
|
key,
|
|
236
236
|
query: toQuery,
|
|
237
237
|
joinQuery
|
|
238
|
-
} =
|
|
239
|
-
const jq = joinQuery(
|
|
238
|
+
} = table.relations[first];
|
|
239
|
+
const jq = joinQuery(table, toQuery);
|
|
240
240
|
const { query } = jq;
|
|
241
|
-
const
|
|
242
|
-
target = quoteSchemaAndTable(query.schema,
|
|
241
|
+
const tableName = typeof query.from === "string" ? query.from : jq.table;
|
|
242
|
+
target = quoteSchemaAndTable(query.schema, tableName);
|
|
243
243
|
const as = query.as || key;
|
|
244
244
|
const joinAs = q(as);
|
|
245
|
-
if (as !==
|
|
245
|
+
if (as !== tableName) {
|
|
246
246
|
target += ` AS ${joinAs}`;
|
|
247
247
|
}
|
|
248
248
|
const queryData = {
|
|
@@ -251,7 +251,7 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
|
|
|
251
251
|
};
|
|
252
252
|
if (args[1]) {
|
|
253
253
|
const arg = args[1](
|
|
254
|
-
new ctx.onQueryBuilder(jq, jq.shape,
|
|
254
|
+
new ctx.onQueryBuilder(jq, jq.shape, table)
|
|
255
255
|
).query;
|
|
256
256
|
if (arg.and)
|
|
257
257
|
queryData.and.push(...arg.and);
|
|
@@ -261,7 +261,7 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
|
|
|
261
261
|
conditions = whereToSql(ctx, jq, queryData, joinAs);
|
|
262
262
|
} else {
|
|
263
263
|
target = q(first);
|
|
264
|
-
conditions = processArgs(args, ctx,
|
|
264
|
+
conditions = processArgs(args, ctx, table, first, target, quotedAs);
|
|
265
265
|
}
|
|
266
266
|
} else {
|
|
267
267
|
const query = first.query;
|
|
@@ -275,8 +275,8 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
|
|
|
275
275
|
target += ` AS ${quoted}`;
|
|
276
276
|
}
|
|
277
277
|
}
|
|
278
|
-
conditions = processArgs(args, ctx,
|
|
279
|
-
const whereSql = whereToSql(ctx,
|
|
278
|
+
conditions = processArgs(args, ctx, table, first, joinAs, quotedAs);
|
|
279
|
+
const whereSql = whereToSql(ctx, table, query, joinAs);
|
|
280
280
|
if (whereSql) {
|
|
281
281
|
if (conditions)
|
|
282
282
|
conditions += ` AND ${whereSql}`;
|
|
@@ -286,21 +286,21 @@ const processJoinItem = (ctx, model, args, quotedAs) => {
|
|
|
286
286
|
}
|
|
287
287
|
return { target, conditions };
|
|
288
288
|
};
|
|
289
|
-
const processArgs = (args, ctx,
|
|
289
|
+
const processArgs = (args, ctx, table, first, joinAs, quotedAs) => {
|
|
290
290
|
var _a;
|
|
291
291
|
if (args.length === 2) {
|
|
292
292
|
const arg = args[1];
|
|
293
293
|
if (typeof arg === "function") {
|
|
294
294
|
let shape;
|
|
295
295
|
if (typeof first === "string") {
|
|
296
|
-
shape = (_a =
|
|
296
|
+
shape = (_a = table.query.withShapes) == null ? void 0 : _a[first];
|
|
297
297
|
if (!shape) {
|
|
298
298
|
throw new Error("Cannot get shape of `with` statement");
|
|
299
299
|
}
|
|
300
300
|
} else {
|
|
301
301
|
shape = first.shape;
|
|
302
302
|
}
|
|
303
|
-
const jq = arg(new ctx.onQueryBuilder(first, shape,
|
|
303
|
+
const jq = arg(new ctx.onQueryBuilder(first, shape, table));
|
|
304
304
|
return whereToSql(ctx, jq, jq.query, joinAs);
|
|
305
305
|
} else {
|
|
306
306
|
return getObjectOrRawConditions(arg, ctx.values, quotedAs, joinAs);
|
|
@@ -335,11 +335,11 @@ const getObjectOrRawConditions = (data, values, quotedAs, joinAs) => {
|
|
|
335
335
|
return pairs.join(", ");
|
|
336
336
|
}
|
|
337
337
|
};
|
|
338
|
-
const pushJoinSql = (ctx,
|
|
338
|
+
const pushJoinSql = (ctx, table, query, quotedAs) => {
|
|
339
339
|
query.join.forEach((item) => {
|
|
340
340
|
const { target, conditions } = processJoinItem(
|
|
341
341
|
ctx,
|
|
342
|
-
|
|
342
|
+
table,
|
|
343
343
|
item.args,
|
|
344
344
|
quotedAs
|
|
345
345
|
);
|
|
@@ -349,41 +349,41 @@ const pushJoinSql = (ctx, model, query, quotedAs) => {
|
|
|
349
349
|
});
|
|
350
350
|
};
|
|
351
351
|
|
|
352
|
-
const pushWhereStatementSql = (ctx,
|
|
353
|
-
const res = whereToSql(ctx,
|
|
352
|
+
const pushWhereStatementSql = (ctx, table, query, quotedAs) => {
|
|
353
|
+
const res = whereToSql(ctx, table, query, quotedAs, false);
|
|
354
354
|
if (res) {
|
|
355
355
|
ctx.sql.push("WHERE", res);
|
|
356
356
|
}
|
|
357
357
|
};
|
|
358
|
-
const pushWhereToSql = (sql, ctx,
|
|
359
|
-
const res = whereToSql(ctx,
|
|
358
|
+
const pushWhereToSql = (sql, ctx, table, query, quotedAs, not) => {
|
|
359
|
+
const res = whereToSql(ctx, table, query, quotedAs, not);
|
|
360
360
|
if (res) {
|
|
361
361
|
sql.push(res);
|
|
362
362
|
}
|
|
363
363
|
};
|
|
364
|
-
const whereToSql = (ctx,
|
|
364
|
+
const whereToSql = (ctx, table, query, quotedAs, not) => {
|
|
365
365
|
if (query.or) {
|
|
366
366
|
const ors = query.and ? [query.and, ...query.or] : query.or;
|
|
367
|
-
return ors.map((and) => processAnds(and, ctx,
|
|
367
|
+
return ors.map((and) => processAnds(and, ctx, table, quotedAs, not)).join(" OR ");
|
|
368
368
|
} else if (query.and) {
|
|
369
|
-
return processAnds(query.and, ctx,
|
|
369
|
+
return processAnds(query.and, ctx, table, quotedAs, not);
|
|
370
370
|
} else {
|
|
371
371
|
return void 0;
|
|
372
372
|
}
|
|
373
373
|
};
|
|
374
|
-
const processAnds = (and, ctx,
|
|
374
|
+
const processAnds = (and, ctx, table, quotedAs, not) => {
|
|
375
375
|
const ands = [];
|
|
376
|
-
and.forEach((data) => processWhere(ands, ctx,
|
|
376
|
+
and.forEach((data) => processWhere(ands, ctx, table, data, quotedAs, not));
|
|
377
377
|
return ands.join(" AND ");
|
|
378
378
|
};
|
|
379
|
-
const processWhere = (ands, ctx,
|
|
379
|
+
const processWhere = (ands, ctx, table, data, quotedAs, not) => {
|
|
380
380
|
const prefix = not ? "NOT " : "";
|
|
381
381
|
if (typeof data === "function") {
|
|
382
|
-
const qb = data(new ctx.whereQueryBuilder(
|
|
382
|
+
const qb = data(new ctx.whereQueryBuilder(table, table.shape));
|
|
383
383
|
pushWhereToSql(ands, ctx, qb, qb.query, quotedAs, not);
|
|
384
384
|
return;
|
|
385
385
|
}
|
|
386
|
-
if ("prototype" in data || "
|
|
386
|
+
if ("prototype" in data || "__table" in data) {
|
|
387
387
|
const query = data;
|
|
388
388
|
const sql = whereToSql(
|
|
389
389
|
ctx,
|
|
@@ -404,15 +404,15 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
|
|
|
404
404
|
const value = data[key];
|
|
405
405
|
if (key === "AND") {
|
|
406
406
|
const arr = toArray(value);
|
|
407
|
-
ands.push(processAnds(arr, ctx,
|
|
407
|
+
ands.push(processAnds(arr, ctx, table, quotedAs, not));
|
|
408
408
|
} else if (key === "OR") {
|
|
409
409
|
const arr = value.map(toArray);
|
|
410
410
|
ands.push(
|
|
411
|
-
arr.map((and) => processAnds(and, ctx,
|
|
411
|
+
arr.map((and) => processAnds(and, ctx, table, quotedAs, not)).join(" OR ")
|
|
412
412
|
);
|
|
413
413
|
} else if (key === "NOT") {
|
|
414
414
|
const arr = toArray(value);
|
|
415
|
-
ands.push(processAnds(arr, ctx,
|
|
415
|
+
ands.push(processAnds(arr, ctx, table, quotedAs, !not));
|
|
416
416
|
} else if (key === "ON") {
|
|
417
417
|
if (Array.isArray(value)) {
|
|
418
418
|
const item = value;
|
|
@@ -421,7 +421,7 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
|
|
|
421
421
|
const rightColumn = quoteFullColumn(
|
|
422
422
|
item[2],
|
|
423
423
|
getQueryAs({
|
|
424
|
-
table:
|
|
424
|
+
table: table.table,
|
|
425
425
|
query: { as: quotedAs }
|
|
426
426
|
})
|
|
427
427
|
);
|
|
@@ -454,7 +454,7 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
|
|
|
454
454
|
joinItems.forEach((item) => {
|
|
455
455
|
const { target, conditions } = processJoinItem(
|
|
456
456
|
ctx,
|
|
457
|
-
|
|
457
|
+
table,
|
|
458
458
|
item,
|
|
459
459
|
quotedAs
|
|
460
460
|
);
|
|
@@ -471,7 +471,7 @@ const processWhere = (ands, ctx, model, data, quotedAs, not) => {
|
|
|
471
471
|
)}`
|
|
472
472
|
);
|
|
473
473
|
} else {
|
|
474
|
-
const column =
|
|
474
|
+
const column = table.shape[key];
|
|
475
475
|
if (!column) {
|
|
476
476
|
throw new Error(`Unknown column ${key} provided to condition`);
|
|
477
477
|
}
|
|
@@ -538,7 +538,7 @@ var __spreadValues$p = (a, b) => {
|
|
|
538
538
|
return a;
|
|
539
539
|
};
|
|
540
540
|
var __spreadProps$j = (a, b) => __defProps$j(a, __getOwnPropDescs$j(b));
|
|
541
|
-
const aggregateToSql = (ctx,
|
|
541
|
+
const aggregateToSql = (ctx, table, item, quotedAs) => {
|
|
542
542
|
var _a;
|
|
543
543
|
const sql = [`${item.function}(`];
|
|
544
544
|
ctx = __spreadProps$j(__spreadValues$p({}, ctx), { sql });
|
|
@@ -581,7 +581,7 @@ const aggregateToSql = (ctx, model, item, quotedAs) => {
|
|
|
581
581
|
if (options.filter || options.filterOr) {
|
|
582
582
|
const whereSql = whereToSql(
|
|
583
583
|
ctx,
|
|
584
|
-
|
|
584
|
+
table,
|
|
585
585
|
{
|
|
586
586
|
and: options.filter ? [options.filter] : void 0,
|
|
587
587
|
or: (_a = options.filterOr) == null ? void 0 : _a.map((item2) => [item2])
|
|
@@ -677,10 +677,10 @@ const jsonToSql = (item, values, quotedAs) => {
|
|
|
677
677
|
}
|
|
678
678
|
return "";
|
|
679
679
|
};
|
|
680
|
-
const pushSelectSql = (ctx,
|
|
681
|
-
ctx.sql.push(selectToSql(ctx,
|
|
680
|
+
const pushSelectSql = (ctx, table, query, quotedAs) => {
|
|
681
|
+
ctx.sql.push(selectToSql(ctx, table, query, quotedAs));
|
|
682
682
|
};
|
|
683
|
-
const selectToSql = (ctx,
|
|
683
|
+
const selectToSql = (ctx, table, query, quotedAs) => {
|
|
684
684
|
var _a;
|
|
685
685
|
if (query.select) {
|
|
686
686
|
const list = [];
|
|
@@ -717,13 +717,13 @@ const selectToSql = (ctx, model, query, quotedAs) => {
|
|
|
717
717
|
list.push(
|
|
718
718
|
`${item.function}(${selectToSql(
|
|
719
719
|
ctx,
|
|
720
|
-
|
|
720
|
+
table,
|
|
721
721
|
{ select: item.arguments },
|
|
722
722
|
quotedAs
|
|
723
723
|
)})${item.as ? ` AS ${q(item.as)}` : ""}`
|
|
724
724
|
);
|
|
725
725
|
} else {
|
|
726
|
-
list.push(aggregateToSql(ctx,
|
|
726
|
+
list.push(aggregateToSql(ctx, table, item, quotedAs));
|
|
727
727
|
}
|
|
728
728
|
}
|
|
729
729
|
});
|
|
@@ -752,7 +752,7 @@ const pushSubQuerySql = (query, as, values, list) => {
|
|
|
752
752
|
}
|
|
753
753
|
select.length = 0;
|
|
754
754
|
select[0] = { selectAs: { c: first } };
|
|
755
|
-
query = query._wrap(query.
|
|
755
|
+
query = query._wrap(query.__table.clone());
|
|
756
756
|
query._getOptional(raw(`COALESCE(json_agg("c"), '[]')`));
|
|
757
757
|
break;
|
|
758
758
|
}
|
|
@@ -781,12 +781,12 @@ const aggregateOptionNames = [
|
|
|
781
781
|
"filterOr",
|
|
782
782
|
"withinGroup"
|
|
783
783
|
];
|
|
784
|
-
const pushHavingSql = (ctx,
|
|
785
|
-
const conditions = havingToSql(ctx,
|
|
784
|
+
const pushHavingSql = (ctx, table, query, quotedAs) => {
|
|
785
|
+
const conditions = havingToSql(ctx, table, query, quotedAs);
|
|
786
786
|
if (conditions.length)
|
|
787
787
|
ctx.sql.push("HAVING", conditions);
|
|
788
788
|
};
|
|
789
|
-
const havingToSql = (ctx,
|
|
789
|
+
const havingToSql = (ctx, table, query, quotedAs) => {
|
|
790
790
|
const or = query.having && query.havingOr ? [query.having, ...query.havingOr] : query.having ? [query.having] : query.havingOr;
|
|
791
791
|
if (!(or == null ? void 0 : or.length))
|
|
792
792
|
return "";
|
|
@@ -794,7 +794,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
|
|
|
794
794
|
or.forEach((and) => {
|
|
795
795
|
const ands = [];
|
|
796
796
|
and.forEach((item) => {
|
|
797
|
-
if ("prototype" in item || "
|
|
797
|
+
if ("prototype" in item || "__table" in item) {
|
|
798
798
|
const query2 = item;
|
|
799
799
|
const sql = havingToSql(
|
|
800
800
|
ctx,
|
|
@@ -820,7 +820,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
|
|
|
820
820
|
if (!aggregateOptionNames.includes(
|
|
821
821
|
op
|
|
822
822
|
)) {
|
|
823
|
-
const operator =
|
|
823
|
+
const operator = table.shape[column].operators[op];
|
|
824
824
|
if (!operator) {
|
|
825
825
|
throw new Error(
|
|
826
826
|
`Unknown operator ${op} provided to condition`
|
|
@@ -828,7 +828,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
|
|
|
828
828
|
}
|
|
829
829
|
const expression = aggregateToSql(
|
|
830
830
|
ctx,
|
|
831
|
-
|
|
831
|
+
table,
|
|
832
832
|
{
|
|
833
833
|
function: key,
|
|
834
834
|
arg: column,
|
|
@@ -849,7 +849,7 @@ const havingToSql = (ctx, model, query, quotedAs) => {
|
|
|
849
849
|
ands.push(
|
|
850
850
|
`${aggregateToSql(
|
|
851
851
|
ctx,
|
|
852
|
-
|
|
852
|
+
table,
|
|
853
853
|
{
|
|
854
854
|
function: key,
|
|
855
855
|
arg: column,
|
|
@@ -915,17 +915,17 @@ const queryKeysOfNotSimpleQuery = [
|
|
|
915
915
|
"for"
|
|
916
916
|
];
|
|
917
917
|
|
|
918
|
-
const pushFromAndAs = (ctx,
|
|
918
|
+
const pushFromAndAs = (ctx, table, query, quotedAs) => {
|
|
919
919
|
ctx.sql.push("FROM");
|
|
920
920
|
if (query.fromOnly)
|
|
921
921
|
ctx.sql.push("ONLY");
|
|
922
|
-
const from = getFrom(
|
|
922
|
+
const from = getFrom(table, query, ctx.values);
|
|
923
923
|
ctx.sql.push(from);
|
|
924
924
|
if (query.as && quotedAs && quotedAs !== from) {
|
|
925
925
|
ctx.sql.push("AS", quotedAs);
|
|
926
926
|
}
|
|
927
927
|
};
|
|
928
|
-
const getFrom = (
|
|
928
|
+
const getFrom = (table, query, values) => {
|
|
929
929
|
if (query.from) {
|
|
930
930
|
if (typeof query.from === "object") {
|
|
931
931
|
if (isRaw(query.from)) {
|
|
@@ -944,7 +944,7 @@ const getFrom = (model, query, values) => {
|
|
|
944
944
|
}
|
|
945
945
|
return quoteSchemaAndTable(query.schema, query.from);
|
|
946
946
|
}
|
|
947
|
-
return quoteSchemaAndTable(query.schema,
|
|
947
|
+
return quoteSchemaAndTable(query.schema, table.table);
|
|
948
948
|
};
|
|
949
949
|
|
|
950
950
|
const pushQueryArray = (q, key, value) => {
|
|
@@ -974,7 +974,7 @@ const setQueryObjectValue = (q, object, key, value) => {
|
|
|
974
974
|
return q;
|
|
975
975
|
};
|
|
976
976
|
|
|
977
|
-
const pushInsertSql = (ctx,
|
|
977
|
+
const pushInsertSql = (ctx, table, query, quotedAs) => {
|
|
978
978
|
const quotedColumns = query.columns.map(q);
|
|
979
979
|
ctx.sql.push(`INSERT INTO ${quotedAs}(${quotedColumns.join(", ")})`);
|
|
980
980
|
if (query.fromQuery) {
|
|
@@ -1029,22 +1029,22 @@ const pushInsertSql = (ctx, model, query, quotedAs) => {
|
|
|
1029
1029
|
ctx.sql.push("DO UPDATE SET", set);
|
|
1030
1030
|
}
|
|
1031
1031
|
}
|
|
1032
|
-
pushWhereStatementSql(ctx,
|
|
1033
|
-
pushReturningSql(ctx,
|
|
1032
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
1033
|
+
pushReturningSql(ctx, table, query, quotedAs);
|
|
1034
1034
|
};
|
|
1035
1035
|
const encodeRow = (ctx, row) => {
|
|
1036
1036
|
return row.map(
|
|
1037
1037
|
(value) => value === void 0 ? "DEFAULT" : addValue(ctx.values, value)
|
|
1038
1038
|
).join(", ");
|
|
1039
1039
|
};
|
|
1040
|
-
const pushReturningSql = (ctx,
|
|
1040
|
+
const pushReturningSql = (ctx, table, query, quotedAs) => {
|
|
1041
1041
|
if (query.select) {
|
|
1042
|
-
ctx.sql.push(`RETURNING ${selectToSql(ctx,
|
|
1042
|
+
ctx.sql.push(`RETURNING ${selectToSql(ctx, table, query, quotedAs)}`);
|
|
1043
1043
|
}
|
|
1044
1044
|
};
|
|
1045
1045
|
|
|
1046
|
-
const pushUpdateSql = (ctx,
|
|
1047
|
-
const quotedTable = quoteSchemaAndTable(query.schema,
|
|
1046
|
+
const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
1047
|
+
const quotedTable = quoteSchemaAndTable(query.schema, table.table);
|
|
1048
1048
|
ctx.sql.push(`UPDATE ${quotedTable}`);
|
|
1049
1049
|
if (quotedTable !== quotedAs) {
|
|
1050
1050
|
ctx.sql.push(`AS ${quotedAs}`);
|
|
@@ -1053,8 +1053,8 @@ const pushUpdateSql = (ctx, model, query, quotedAs) => {
|
|
|
1053
1053
|
const set = [];
|
|
1054
1054
|
processData(ctx, set, query.updateData);
|
|
1055
1055
|
ctx.sql.push(set.join(", "));
|
|
1056
|
-
pushWhereStatementSql(ctx,
|
|
1057
|
-
pushReturningSql(ctx,
|
|
1056
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
1057
|
+
pushReturningSql(ctx, table, query, quotedAs);
|
|
1058
1058
|
};
|
|
1059
1059
|
const processData = (ctx, set, data) => {
|
|
1060
1060
|
let append;
|
|
@@ -1091,9 +1091,9 @@ const processValue = (values, key, value) => {
|
|
|
1091
1091
|
return addValue(values, value);
|
|
1092
1092
|
};
|
|
1093
1093
|
|
|
1094
|
-
const pushDeleteSql = (ctx,
|
|
1094
|
+
const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
1095
1095
|
var _a, _b, _c;
|
|
1096
|
-
const from = q(
|
|
1096
|
+
const from = q(table.table);
|
|
1097
1097
|
ctx.sql.push(`DELETE FROM ${from}`);
|
|
1098
1098
|
if (from !== quotedAs) {
|
|
1099
1099
|
ctx.sql.push(`AS ${quotedAs}`);
|
|
@@ -1101,12 +1101,12 @@ const pushDeleteSql = (ctx, model, query, quotedAs) => {
|
|
|
1101
1101
|
let conditions;
|
|
1102
1102
|
if ((_a = query.join) == null ? void 0 : _a.length) {
|
|
1103
1103
|
const items = query.join.map(
|
|
1104
|
-
(item) => processJoinItem(ctx,
|
|
1104
|
+
(item) => processJoinItem(ctx, table, item.args, quotedAs)
|
|
1105
1105
|
);
|
|
1106
1106
|
ctx.sql.push(`USING ${items.map((item) => item.target).join(", ")}`);
|
|
1107
1107
|
conditions = items.map((item) => item.conditions).filter(Boolean).join(" AND ");
|
|
1108
1108
|
}
|
|
1109
|
-
pushWhereStatementSql(ctx,
|
|
1109
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
1110
1110
|
if (conditions == null ? void 0 : conditions.length) {
|
|
1111
1111
|
if (((_b = query.and) == null ? void 0 : _b.length) || ((_c = query.or) == null ? void 0 : _c.length)) {
|
|
1112
1112
|
ctx.sql.push("AND", conditions);
|
|
@@ -1114,7 +1114,7 @@ const pushDeleteSql = (ctx, model, query, quotedAs) => {
|
|
|
1114
1114
|
ctx.sql.push("WHERE", conditions);
|
|
1115
1115
|
}
|
|
1116
1116
|
}
|
|
1117
|
-
pushReturningSql(ctx,
|
|
1117
|
+
pushReturningSql(ctx, table, query, quotedAs);
|
|
1118
1118
|
};
|
|
1119
1119
|
|
|
1120
1120
|
const pushTruncateSql = (ctx, table, query) => {
|
|
@@ -1137,13 +1137,13 @@ const pushColumnInfoSql = (ctx, table, query) => {
|
|
|
1137
1137
|
}
|
|
1138
1138
|
};
|
|
1139
1139
|
|
|
1140
|
-
const pushCopySql = (ctx,
|
|
1140
|
+
const pushCopySql = (ctx, table, query, quotedAs) => {
|
|
1141
1141
|
const { sql } = ctx;
|
|
1142
1142
|
const { copy } = query;
|
|
1143
1143
|
const columns = copy.columns ? `(${copy.columns.map(q).join(", ")})` : "";
|
|
1144
1144
|
const target = "from" in copy ? copy.from : copy.to;
|
|
1145
1145
|
sql.push(
|
|
1146
|
-
`COPY ${q(
|
|
1146
|
+
`COPY ${q(table.table)}${columns} ${"from" in copy ? "FROM" : "TO"} ${typeof target === "string" ? quote(target) : `PROGRAM ${quote(target.program)}`}`
|
|
1147
1147
|
);
|
|
1148
1148
|
if (Object.keys(copy).length > (copy.columns ? 2 : 1)) {
|
|
1149
1149
|
const options = [];
|
|
@@ -1173,19 +1173,19 @@ const pushCopySql = (ctx, model, query, quotedAs) => {
|
|
|
1173
1173
|
options.push(`ENCODING ${quote(copy.encoding)}`);
|
|
1174
1174
|
sql.push(`WITH (${options.join(", ")})`);
|
|
1175
1175
|
}
|
|
1176
|
-
pushWhereStatementSql(ctx,
|
|
1176
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
1177
1177
|
};
|
|
1178
1178
|
|
|
1179
1179
|
const toSqlCacheKey = Symbol("toSqlCache");
|
|
1180
|
-
const toSql = (
|
|
1181
|
-
return !(options == null ? void 0 : options.clearCache) &&
|
|
1180
|
+
const toSql = (table, options) => {
|
|
1181
|
+
return !(options == null ? void 0 : options.clearCache) && table.query[toSqlCacheKey] || (table.query[toSqlCacheKey] = makeSql(table, options));
|
|
1182
1182
|
};
|
|
1183
|
-
const makeSql = (
|
|
1184
|
-
const query =
|
|
1183
|
+
const makeSql = (table, { values = [] } = {}) => {
|
|
1184
|
+
const query = table.query;
|
|
1185
1185
|
const sql = [];
|
|
1186
1186
|
const ctx = {
|
|
1187
|
-
whereQueryBuilder:
|
|
1188
|
-
onQueryBuilder:
|
|
1187
|
+
whereQueryBuilder: table.whereQueryBuilder,
|
|
1188
|
+
onQueryBuilder: table.onQueryBuilder,
|
|
1189
1189
|
sql,
|
|
1190
1190
|
values
|
|
1191
1191
|
};
|
|
@@ -1194,56 +1194,56 @@ const makeSql = (model, { values = [] } = {}) => {
|
|
|
1194
1194
|
}
|
|
1195
1195
|
if (query.type) {
|
|
1196
1196
|
if (query.type === "truncate") {
|
|
1197
|
-
if (!
|
|
1197
|
+
if (!table.table)
|
|
1198
1198
|
throw new Error("Table is missing for truncate");
|
|
1199
|
-
pushTruncateSql(ctx,
|
|
1199
|
+
pushTruncateSql(ctx, table.table, query);
|
|
1200
1200
|
return { text: sql.join(" "), values };
|
|
1201
1201
|
}
|
|
1202
1202
|
if (query.type === "columnInfo") {
|
|
1203
|
-
if (!
|
|
1203
|
+
if (!table.table)
|
|
1204
1204
|
throw new Error("Table is missing for truncate");
|
|
1205
|
-
pushColumnInfoSql(ctx,
|
|
1205
|
+
pushColumnInfoSql(ctx, table.table, query);
|
|
1206
1206
|
return { text: sql.join(" "), values };
|
|
1207
1207
|
}
|
|
1208
|
-
if (!
|
|
1208
|
+
if (!table.table)
|
|
1209
1209
|
throw new Error(`Table is missing for ${query.type}`);
|
|
1210
|
-
const quotedAs2 = q(query.as ||
|
|
1210
|
+
const quotedAs2 = q(query.as || table.table);
|
|
1211
1211
|
if (query.type === "insert") {
|
|
1212
|
-
pushInsertSql(ctx,
|
|
1212
|
+
pushInsertSql(ctx, table, query, q(table.table));
|
|
1213
1213
|
return { text: sql.join(" "), values };
|
|
1214
1214
|
}
|
|
1215
1215
|
if (query.type === "update") {
|
|
1216
|
-
pushUpdateSql(ctx,
|
|
1216
|
+
pushUpdateSql(ctx, table, query, quotedAs2);
|
|
1217
1217
|
return { text: sql.join(" "), values };
|
|
1218
1218
|
}
|
|
1219
1219
|
if (query.type === "delete") {
|
|
1220
|
-
pushDeleteSql(ctx,
|
|
1220
|
+
pushDeleteSql(ctx, table, query, quotedAs2);
|
|
1221
1221
|
return { text: sql.join(" "), values };
|
|
1222
1222
|
}
|
|
1223
1223
|
if (query.type === "copy") {
|
|
1224
|
-
pushCopySql(ctx,
|
|
1224
|
+
pushCopySql(ctx, table, query, quotedAs2);
|
|
1225
1225
|
return { text: sql.join(" "), values };
|
|
1226
1226
|
}
|
|
1227
1227
|
}
|
|
1228
|
-
const quotedAs =
|
|
1228
|
+
const quotedAs = table.table && q(query.as || table.table);
|
|
1229
1229
|
sql.push("SELECT");
|
|
1230
1230
|
if (query.distinct) {
|
|
1231
1231
|
pushDistinctSql(ctx, query.distinct, quotedAs);
|
|
1232
1232
|
}
|
|
1233
|
-
pushSelectSql(ctx,
|
|
1234
|
-
if (
|
|
1235
|
-
pushFromAndAs(ctx,
|
|
1233
|
+
pushSelectSql(ctx, table, query, quotedAs);
|
|
1234
|
+
if (table.table || query.from) {
|
|
1235
|
+
pushFromAndAs(ctx, table, query, quotedAs);
|
|
1236
1236
|
}
|
|
1237
1237
|
if (query.join) {
|
|
1238
1238
|
pushJoinSql(
|
|
1239
1239
|
ctx,
|
|
1240
|
-
|
|
1240
|
+
table,
|
|
1241
1241
|
query,
|
|
1242
1242
|
quotedAs
|
|
1243
1243
|
);
|
|
1244
1244
|
}
|
|
1245
1245
|
if (query.and || query.or) {
|
|
1246
|
-
pushWhereStatementSql(ctx,
|
|
1246
|
+
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
1247
1247
|
}
|
|
1248
1248
|
if (query.group) {
|
|
1249
1249
|
const group = query.group.map(
|
|
@@ -1252,7 +1252,7 @@ const makeSql = (model, { values = [] } = {}) => {
|
|
|
1252
1252
|
sql.push(`GROUP BY ${group.join(", ")}`);
|
|
1253
1253
|
}
|
|
1254
1254
|
if (query.having || query.havingOr) {
|
|
1255
|
-
pushHavingSql(ctx,
|
|
1255
|
+
pushHavingSql(ctx, table, query, quotedAs);
|
|
1256
1256
|
}
|
|
1257
1257
|
if (query.window) {
|
|
1258
1258
|
const window = [];
|
|
@@ -4158,9 +4158,9 @@ class Delete {
|
|
|
4158
4158
|
|
|
4159
4159
|
const forQueryBuilder = (q, type, tableNames) => {
|
|
4160
4160
|
q.query.for = { type, tableNames };
|
|
4161
|
-
q.
|
|
4162
|
-
q.
|
|
4163
|
-
Object.assign(q.
|
|
4161
|
+
q.__table = Object.create(q.__table);
|
|
4162
|
+
q.__table.__table = q.__table;
|
|
4163
|
+
Object.assign(q.__table, {
|
|
4164
4164
|
noWait() {
|
|
4165
4165
|
return this.clone()._noWait();
|
|
4166
4166
|
},
|
|
@@ -4251,7 +4251,7 @@ var __spreadValues$5 = (a, b) => {
|
|
|
4251
4251
|
return a;
|
|
4252
4252
|
};
|
|
4253
4253
|
const processHavingArg = (arg) => {
|
|
4254
|
-
if ("
|
|
4254
|
+
if ("__table" in arg || isRaw(arg)) {
|
|
4255
4255
|
return arg;
|
|
4256
4256
|
} else {
|
|
4257
4257
|
const processed = __spreadValues$5({}, arg);
|
|
@@ -4698,13 +4698,13 @@ class WhereQueryBuilder extends Where {
|
|
|
4698
4698
|
this.withData = {};
|
|
4699
4699
|
this.table = typeof q === "object" ? q.table : q;
|
|
4700
4700
|
this.shape = shape;
|
|
4701
|
-
this.
|
|
4701
|
+
this.__table = this;
|
|
4702
4702
|
if (typeof q === "object" && q.query.as) {
|
|
4703
4703
|
this.query.as = q.query.as;
|
|
4704
4704
|
}
|
|
4705
4705
|
}
|
|
4706
4706
|
clone() {
|
|
4707
|
-
const cloned = Object.create(this.
|
|
4707
|
+
const cloned = Object.create(this.__table);
|
|
4708
4708
|
cloned.query = getClonedQueryData(this.query);
|
|
4709
4709
|
return cloned;
|
|
4710
4710
|
}
|
|
@@ -4845,7 +4845,7 @@ class Json {
|
|
|
4845
4845
|
return this.clone()._json();
|
|
4846
4846
|
}
|
|
4847
4847
|
_json() {
|
|
4848
|
-
const q = this._wrap(this.
|
|
4848
|
+
const q = this._wrap(this.__table.clone());
|
|
4849
4849
|
q._getOptional(
|
|
4850
4850
|
raw(
|
|
4851
4851
|
queryTypeWithLimitOne[this.query.returnType] ? `row_to_json("t".*)` : `COALESCE(json_agg(row_to_json("t".*)), '[]')`
|
|
@@ -5251,7 +5251,7 @@ class Update {
|
|
|
5251
5251
|
ctx.resultAll = await handleResult(q, queryResult);
|
|
5252
5252
|
if (ctx.updateLater) {
|
|
5253
5253
|
await Promise.all(ctx.updateLaterPromises);
|
|
5254
|
-
const t = this.
|
|
5254
|
+
const t = this.__table.clone().transacting(q);
|
|
5255
5255
|
const keys = this.primaryKeys;
|
|
5256
5256
|
t._whereIn(
|
|
5257
5257
|
keys,
|
|
@@ -5500,7 +5500,7 @@ class QueryMethods {
|
|
|
5500
5500
|
return this;
|
|
5501
5501
|
}
|
|
5502
5502
|
clone() {
|
|
5503
|
-
const cloned = Object.create(this.
|
|
5503
|
+
const cloned = Object.create(this.__table);
|
|
5504
5504
|
cloned.query = getClonedQueryData(this.query);
|
|
5505
5505
|
return cloned;
|
|
5506
5506
|
}
|
|
@@ -5684,7 +5684,7 @@ class Db {
|
|
|
5684
5684
|
this.whereQueryBuilder = WhereQueryBuilder;
|
|
5685
5685
|
this.onQueryBuilder = OnQueryBuilder;
|
|
5686
5686
|
var _a, _b;
|
|
5687
|
-
this.
|
|
5687
|
+
this.__table = this;
|
|
5688
5688
|
const logger = options.logger || console;
|
|
5689
5689
|
this.query = {
|
|
5690
5690
|
adapter,
|