pqb 0.31.7 → 0.31.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +23 -6
- package/dist/index.js +320 -199
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +318 -200
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -836,6 +836,12 @@ const columnCode = (type, t, code, migration, data = type.data, skip) => {
|
|
|
836
836
|
return code.length === 1 && typeof code[0] === "string" ? code[0] : code;
|
|
837
837
|
};
|
|
838
838
|
|
|
839
|
+
const getSqlText = (sql) => {
|
|
840
|
+
if ("text" in sql)
|
|
841
|
+
return sql.text;
|
|
842
|
+
throw new Error(`Batch SQL is not supported in this query`);
|
|
843
|
+
};
|
|
844
|
+
|
|
839
845
|
var __defProp$f = Object.defineProperty;
|
|
840
846
|
var __defProps$7 = Object.defineProperties;
|
|
841
847
|
var __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors;
|
|
@@ -889,7 +895,7 @@ const quoteValue$1 = (arg, ctx, quotedAs, jsonArray) => {
|
|
|
889
895
|
return arg.toSQL(ctx, quotedAs);
|
|
890
896
|
}
|
|
891
897
|
if ("toSQL" in arg) {
|
|
892
|
-
return `(${arg.toSQL({ values: ctx.values })
|
|
898
|
+
return `(${getSqlText(arg.toSQL({ values: ctx.values }))})`;
|
|
893
899
|
}
|
|
894
900
|
}
|
|
895
901
|
return orchidCore.addValue(ctx.values, arg);
|
|
@@ -2072,7 +2078,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2072
2078
|
if (query2.q.expr) {
|
|
2073
2079
|
const q = joinSubQuery(table, query2);
|
|
2074
2080
|
q.q.select = [query2.q.expr];
|
|
2075
|
-
ands.push(`(${makeSQL(q, ctx)
|
|
2081
|
+
ands.push(`(${getSqlText(makeSQL(q, ctx))})`);
|
|
2076
2082
|
} else {
|
|
2077
2083
|
pushWhereToSql(
|
|
2078
2084
|
ands,
|
|
@@ -2225,7 +2231,9 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2225
2231
|
}
|
|
2226
2232
|
}
|
|
2227
2233
|
if (value instanceof ctx.queryBuilder.constructor) {
|
|
2228
|
-
ands.push(
|
|
2234
|
+
ands.push(
|
|
2235
|
+
`${quotedColumn} = (${getSqlText(value.toSQL(ctx))})`
|
|
2236
|
+
);
|
|
2229
2237
|
} else {
|
|
2230
2238
|
for (const op in value) {
|
|
2231
2239
|
const operator = column.operators[op];
|
|
@@ -2272,8 +2280,7 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
|
|
|
2272
2280
|
} else if (orchidCore.isExpression(arg.values)) {
|
|
2273
2281
|
value = arg.values.toSQL(ctx, quotedAs);
|
|
2274
2282
|
} else {
|
|
2275
|
-
|
|
2276
|
-
value = `(${sql.text})`;
|
|
2283
|
+
value = `(${getSqlText(makeSQL(arg.values, ctx))})`;
|
|
2277
2284
|
}
|
|
2278
2285
|
const columnsSql = arg.columns.map((column) => columnToSql(ctx, query, query.shape, column, quotedAs)).join(", ");
|
|
2279
2286
|
ands.push(`${multiple ? `(${columnsSql})` : columnsSql} IN ${value}`);
|
|
@@ -2402,9 +2409,11 @@ const subJoinToSql = (ctx, jq, innerAs, outerAs, cloned) => {
|
|
|
2402
2409
|
jq = jq.clone();
|
|
2403
2410
|
jq.q.select = [new RawSQL(`${innerAs}.*`)];
|
|
2404
2411
|
}
|
|
2405
|
-
return `(${
|
|
2406
|
-
|
|
2407
|
-
|
|
2412
|
+
return `(${getSqlText(
|
|
2413
|
+
jq.toSQL({
|
|
2414
|
+
values: ctx.values
|
|
2415
|
+
})
|
|
2416
|
+
)}) ${outerAs || innerAs}`;
|
|
2408
2417
|
};
|
|
2409
2418
|
const processArgs = (args, ctx, query, joinAs, joinShape, quotedAs) => {
|
|
2410
2419
|
return args.length === 1 ? getObjectOrRawConditions(ctx, query, args[0], quotedAs, joinAs, joinShape) : getConditionsFor3Or4LengthItem(
|
|
@@ -2461,7 +2470,7 @@ const pushJoinSql = (ctx, table, query, quotedAs) => {
|
|
|
2461
2470
|
const { aliasValue } = ctx;
|
|
2462
2471
|
ctx.aliasValue = true;
|
|
2463
2472
|
const as = item[2];
|
|
2464
|
-
sql = `${item[0]} LATERAL (${q.toSQL(ctx)
|
|
2473
|
+
sql = `${item[0]} LATERAL (${getSqlText(q.toSQL(ctx))}) "${((_a = query.joinOverrides) == null ? void 0 : _a[as]) || as}" ON true`;
|
|
2465
2474
|
ctx.aliasValue = aliasValue;
|
|
2466
2475
|
} else {
|
|
2467
2476
|
const { target, on = "true" } = processJoinItem(
|
|
@@ -2987,6 +2996,104 @@ const defaultSchemaConfig = {
|
|
|
2987
2996
|
timestamp: (precision) => new TimestampTZColumn(defaultSchemaConfig, precision)
|
|
2988
2997
|
};
|
|
2989
2998
|
|
|
2999
|
+
const commitSql$1 = {
|
|
3000
|
+
text: "COMMIT"
|
|
3001
|
+
};
|
|
3002
|
+
const rollbackSql$1 = {
|
|
3003
|
+
text: "ROLLBACK"
|
|
3004
|
+
};
|
|
3005
|
+
class Transaction {
|
|
3006
|
+
async transaction(cbOrOptions, cb) {
|
|
3007
|
+
let options;
|
|
3008
|
+
let fn;
|
|
3009
|
+
if (typeof cbOrOptions === "function") {
|
|
3010
|
+
options = orchidCore.emptyObject;
|
|
3011
|
+
fn = cbOrOptions;
|
|
3012
|
+
} else {
|
|
3013
|
+
options = typeof cbOrOptions === "object" ? cbOrOptions : { level: cbOrOptions };
|
|
3014
|
+
fn = cb;
|
|
3015
|
+
}
|
|
3016
|
+
const sql = {
|
|
3017
|
+
values: orchidCore.emptyArray
|
|
3018
|
+
};
|
|
3019
|
+
const log = this.q.log;
|
|
3020
|
+
let logData;
|
|
3021
|
+
let trx = this.internal.transactionStorage.getStore();
|
|
3022
|
+
const transactionId = trx ? trx.transactionId + 1 : 0;
|
|
3023
|
+
const callback = (adapter) => {
|
|
3024
|
+
if (log)
|
|
3025
|
+
log.afterQuery(sql, logData);
|
|
3026
|
+
if (log)
|
|
3027
|
+
logData = log.beforeQuery(commitSql$1);
|
|
3028
|
+
if (trx) {
|
|
3029
|
+
trx.transactionId = transactionId;
|
|
3030
|
+
return fn();
|
|
3031
|
+
}
|
|
3032
|
+
trx = {
|
|
3033
|
+
adapter,
|
|
3034
|
+
transactionId
|
|
3035
|
+
};
|
|
3036
|
+
return this.internal.transactionStorage.run(trx, fn);
|
|
3037
|
+
};
|
|
3038
|
+
if (!trx) {
|
|
3039
|
+
sql.text = `BEGIN${options.level ? ` ISOLATION LEVEL ${options.level}` : ""}${options.readOnly !== void 0 ? ` READ ${options.readOnly ? "ONLY" : "WRITE"}` : ""}${options.deferrable !== void 0 ? ` ${options.deferrable ? "" : "NOT "}DEFERRABLE` : ""}`;
|
|
3040
|
+
if (log)
|
|
3041
|
+
logData = log.beforeQuery(sql);
|
|
3042
|
+
try {
|
|
3043
|
+
const result = await this.q.adapter.transaction(sql, callback);
|
|
3044
|
+
if (log)
|
|
3045
|
+
log.afterQuery(commitSql$1, logData);
|
|
3046
|
+
const { afterCommit } = trx;
|
|
3047
|
+
if (afterCommit) {
|
|
3048
|
+
const promises = [];
|
|
3049
|
+
for (let i = 0, len = afterCommit.length; i < len; i += 2) {
|
|
3050
|
+
const q = afterCommit[i];
|
|
3051
|
+
const result2 = afterCommit[i + 1];
|
|
3052
|
+
for (const fn2 of afterCommit[i + 2]) {
|
|
3053
|
+
promises.push(fn2(result2, q));
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
3056
|
+
await Promise.all(promises);
|
|
3057
|
+
}
|
|
3058
|
+
return result;
|
|
3059
|
+
} catch (err) {
|
|
3060
|
+
if (log)
|
|
3061
|
+
log.afterQuery(rollbackSql$1, logData);
|
|
3062
|
+
throw err;
|
|
3063
|
+
}
|
|
3064
|
+
} else {
|
|
3065
|
+
try {
|
|
3066
|
+
sql.text = `SAVEPOINT "${transactionId}"`;
|
|
3067
|
+
if (log)
|
|
3068
|
+
logData = log.beforeQuery(sql);
|
|
3069
|
+
const { adapter } = trx;
|
|
3070
|
+
await adapter.query(sql);
|
|
3071
|
+
let result;
|
|
3072
|
+
try {
|
|
3073
|
+
result = await callback(adapter);
|
|
3074
|
+
} catch (err) {
|
|
3075
|
+
sql.text = `ROLLBACK TO SAVEPOINT "${transactionId}"`;
|
|
3076
|
+
if (log)
|
|
3077
|
+
logData = log.beforeQuery(sql);
|
|
3078
|
+
await adapter.query(sql);
|
|
3079
|
+
if (log)
|
|
3080
|
+
log.afterQuery(sql, logData);
|
|
3081
|
+
throw err;
|
|
3082
|
+
}
|
|
3083
|
+
sql.text = `RELEASE SAVEPOINT "${transactionId}"`;
|
|
3084
|
+
if (log)
|
|
3085
|
+
logData = log.beforeQuery(sql);
|
|
3086
|
+
await adapter.query(sql);
|
|
3087
|
+
if (log)
|
|
3088
|
+
log.afterQuery(sql, logData);
|
|
3089
|
+
return result;
|
|
3090
|
+
} finally {
|
|
3091
|
+
trx.transactionId = transactionId - 1;
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
|
|
2990
3097
|
const queryMethodByReturnType = {
|
|
2991
3098
|
all: "query",
|
|
2992
3099
|
rows: "arrays",
|
|
@@ -3093,6 +3200,7 @@ let nameI = 0;
|
|
|
3093
3200
|
const callAfterHook = function(cb) {
|
|
3094
3201
|
return cb(this[0], this[1]);
|
|
3095
3202
|
};
|
|
3203
|
+
const beginSql = { text: "BEGIN" };
|
|
3096
3204
|
const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks, resolve, reject) => {
|
|
3097
3205
|
var _a;
|
|
3098
3206
|
const { q: query } = q;
|
|
@@ -3110,23 +3218,64 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
3110
3218
|
}
|
|
3111
3219
|
sql = q.toSQL();
|
|
3112
3220
|
const { hookSelect } = sql;
|
|
3113
|
-
if (query.autoPreparedStatements) {
|
|
3114
|
-
sql.name = queriesNames[sql.text] || (queriesNames[sql.text] = (nameI++).toString(36));
|
|
3115
|
-
}
|
|
3116
|
-
if (query.log) {
|
|
3117
|
-
logData = query.log.beforeQuery(sql);
|
|
3118
|
-
}
|
|
3119
3221
|
const { returnType = "all" } = query;
|
|
3120
3222
|
const returns = hookSelect ? "all" : returnType;
|
|
3121
|
-
|
|
3122
|
-
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3223
|
+
let result;
|
|
3224
|
+
let queryResult;
|
|
3225
|
+
if ("text" in sql) {
|
|
3226
|
+
if (query.autoPreparedStatements) {
|
|
3227
|
+
sql.name = queriesNames[sql.text] || (queriesNames[sql.text] = (nameI++).toString(36));
|
|
3228
|
+
}
|
|
3229
|
+
if (query.log) {
|
|
3230
|
+
logData = query.log.beforeQuery(sql);
|
|
3231
|
+
}
|
|
3232
|
+
queryResult = await adapter[hookSelect ? "query" : queryMethodByReturnType[returnType]](sql);
|
|
3233
|
+
if (query.patchResult) {
|
|
3234
|
+
await query.patchResult(q, queryResult);
|
|
3235
|
+
}
|
|
3236
|
+
if (query.log) {
|
|
3237
|
+
query.log.afterQuery(sql, logData);
|
|
3238
|
+
sql = void 0;
|
|
3239
|
+
}
|
|
3240
|
+
result = query.handleResult(q, returns, queryResult);
|
|
3241
|
+
} else {
|
|
3242
|
+
const queryMethod = hookSelect ? "query" : queryMethodByReturnType[returnType];
|
|
3243
|
+
if (!trx) {
|
|
3244
|
+
if (query.log)
|
|
3245
|
+
logData = query.log.beforeQuery(beginSql);
|
|
3246
|
+
await adapter.arrays(beginSql);
|
|
3247
|
+
if (query.log)
|
|
3248
|
+
query.log.afterQuery(beginSql, logData);
|
|
3249
|
+
}
|
|
3250
|
+
for (const item of sql.batch) {
|
|
3251
|
+
sql = item;
|
|
3252
|
+
if (query.log) {
|
|
3253
|
+
logData = query.log.beforeQuery(sql);
|
|
3254
|
+
}
|
|
3255
|
+
const result2 = await adapter[queryMethod](sql);
|
|
3256
|
+
if (queryResult) {
|
|
3257
|
+
queryResult.rowCount += result2.rowCount;
|
|
3258
|
+
queryResult.rows.push(...result2.rows);
|
|
3259
|
+
} else {
|
|
3260
|
+
queryResult = result2;
|
|
3261
|
+
}
|
|
3262
|
+
if (query.log) {
|
|
3263
|
+
query.log.afterQuery(sql, logData);
|
|
3264
|
+
sql = void 0;
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
3267
|
+
if (!trx) {
|
|
3268
|
+
if (query.log)
|
|
3269
|
+
logData = query.log.beforeQuery(commitSql$1);
|
|
3270
|
+
await adapter.arrays(commitSql$1);
|
|
3271
|
+
if (query.log)
|
|
3272
|
+
query.log.afterQuery(commitSql$1, logData);
|
|
3273
|
+
}
|
|
3274
|
+
if (query.patchResult) {
|
|
3275
|
+
await query.patchResult(q, queryResult);
|
|
3276
|
+
}
|
|
3277
|
+
result = query.handleResult(q, returns, queryResult);
|
|
3128
3278
|
}
|
|
3129
|
-
let result = query.handleResult(q, returns, queryResult);
|
|
3130
3279
|
if (afterHooks || afterCommitHooks || query.after) {
|
|
3131
3280
|
if (queryResult.rowCount) {
|
|
3132
3281
|
if (afterHooks || query.after) {
|
|
@@ -4029,7 +4178,7 @@ const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
|
|
|
4029
4178
|
`${coalesce(
|
|
4030
4179
|
ctx,
|
|
4031
4180
|
query,
|
|
4032
|
-
`(${makeSQL(query, ctx)
|
|
4181
|
+
`(${getSqlText(makeSQL(query, ctx))})`,
|
|
4033
4182
|
quotedAs
|
|
4034
4183
|
)} "${as}"`
|
|
4035
4184
|
);
|
|
@@ -4112,7 +4261,7 @@ const pushWithSql = (ctx, items) => {
|
|
|
4112
4261
|
var _a;
|
|
4113
4262
|
let inner;
|
|
4114
4263
|
if (item.q) {
|
|
4115
|
-
inner = makeSQL(item.q, ctx)
|
|
4264
|
+
inner = getSqlText(makeSQL(item.q, ctx));
|
|
4116
4265
|
} else {
|
|
4117
4266
|
inner = item.s.toSQL(ctx, `"${item.n}"`);
|
|
4118
4267
|
}
|
|
@@ -4206,9 +4355,9 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
4206
4355
|
} else {
|
|
4207
4356
|
only = from.q.only;
|
|
4208
4357
|
if (!from.table) {
|
|
4209
|
-
sql = `(${makeSQL(from, ctx)
|
|
4358
|
+
sql = `(${getSqlText(makeSQL(from, ctx))})`;
|
|
4210
4359
|
} else if (!checkIfASimpleQuery(from)) {
|
|
4211
|
-
sql = `(${makeSQL(from, ctx)
|
|
4360
|
+
sql = `(${getSqlText(makeSQL(from, ctx))})`;
|
|
4212
4361
|
} else {
|
|
4213
4362
|
sql = quoteSchemaAndTable(from.q.schema, from.table);
|
|
4214
4363
|
}
|
|
@@ -4276,8 +4425,10 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
4276
4425
|
}
|
|
4277
4426
|
};
|
|
4278
4427
|
|
|
4428
|
+
const MAX_BINDING_PARAMS = 65536;
|
|
4429
|
+
|
|
4279
4430
|
const quotedColumns = [];
|
|
4280
|
-
const
|
|
4431
|
+
const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
4281
4432
|
var _a, _b, _c, _d;
|
|
4282
4433
|
const { columns, shape } = query;
|
|
4283
4434
|
quotedColumns.length = columns.length;
|
|
@@ -4304,59 +4455,11 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4304
4455
|
values = [[void 0]];
|
|
4305
4456
|
}
|
|
4306
4457
|
}
|
|
4307
|
-
ctx.sql.push(
|
|
4458
|
+
ctx.sql.push(
|
|
4459
|
+
`INSERT INTO ${quotedAs}(${quotedColumns.join(", ")})`,
|
|
4460
|
+
null
|
|
4461
|
+
);
|
|
4308
4462
|
const QueryClass = ctx.queryBuilder.constructor;
|
|
4309
|
-
if (query.kind === "object") {
|
|
4310
|
-
let sql = "";
|
|
4311
|
-
for (let i = 0; i < values.length; i++) {
|
|
4312
|
-
if (i)
|
|
4313
|
-
sql += ", ";
|
|
4314
|
-
sql += `(${encodeRow(
|
|
4315
|
-
ctx,
|
|
4316
|
-
q,
|
|
4317
|
-
QueryClass,
|
|
4318
|
-
values[i],
|
|
4319
|
-
runtimeDefaults,
|
|
4320
|
-
quotedAs
|
|
4321
|
-
)})`;
|
|
4322
|
-
}
|
|
4323
|
-
ctx.sql.push(`VALUES ${sql}`);
|
|
4324
|
-
} else if (query.kind === "raw") {
|
|
4325
|
-
if (orchidCore.isExpression(values)) {
|
|
4326
|
-
let valuesSql = values.toSQL(ctx, quotedAs);
|
|
4327
|
-
if (runtimeDefaults) {
|
|
4328
|
-
valuesSql += `, ${runtimeDefaults.map((fn) => orchidCore.addValue(ctx.values, fn())).join(", ")}`;
|
|
4329
|
-
}
|
|
4330
|
-
ctx.sql.push(`VALUES (${valuesSql})`);
|
|
4331
|
-
} else {
|
|
4332
|
-
let sql;
|
|
4333
|
-
if (runtimeDefaults) {
|
|
4334
|
-
const { values: v } = ctx;
|
|
4335
|
-
sql = values.map(
|
|
4336
|
-
(raw) => (
|
|
4337
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
4338
|
-
`(${raw.toSQL(ctx, quotedAs)}, ${runtimeDefaults.map((fn) => orchidCore.addValue(v, fn())).join(", ")})`
|
|
4339
|
-
)
|
|
4340
|
-
).join(", ");
|
|
4341
|
-
} else {
|
|
4342
|
-
sql = values.map((raw) => `(${raw.toSQL(ctx, quotedAs)})`).join(", ");
|
|
4343
|
-
}
|
|
4344
|
-
ctx.sql.push(`VALUES ${sql}`);
|
|
4345
|
-
}
|
|
4346
|
-
} else {
|
|
4347
|
-
const { from, values: v } = values;
|
|
4348
|
-
const q2 = from.clone();
|
|
4349
|
-
if (v) {
|
|
4350
|
-
pushQueryValue(
|
|
4351
|
-
q2,
|
|
4352
|
-
"select",
|
|
4353
|
-
new RawSQL(
|
|
4354
|
-
encodeRow(ctx, q2, QueryClass, v[0], runtimeDefaults, quotedAs)
|
|
4355
|
-
)
|
|
4356
|
-
);
|
|
4357
|
-
}
|
|
4358
|
-
ctx.sql.push(makeSQL(q2, { values: ctx.values }).text);
|
|
4359
|
-
}
|
|
4360
4463
|
if (query.onConflict) {
|
|
4361
4464
|
ctx.sql.push("ON CONFLICT");
|
|
4362
4465
|
const { target } = query.onConflict;
|
|
@@ -4422,7 +4525,111 @@ const pushInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
4422
4525
|
}
|
|
4423
4526
|
}
|
|
4424
4527
|
pushWhereStatementSql(ctx, q, query, quotedAs);
|
|
4425
|
-
|
|
4528
|
+
const hookSelect = pushReturningSql(
|
|
4529
|
+
ctx,
|
|
4530
|
+
q,
|
|
4531
|
+
query,
|
|
4532
|
+
quotedAs,
|
|
4533
|
+
query.afterCreateSelect
|
|
4534
|
+
);
|
|
4535
|
+
if (query.kind === "object") {
|
|
4536
|
+
const valuesSql = [];
|
|
4537
|
+
let ctxValues = ctx.values;
|
|
4538
|
+
const restValuesLen = ctxValues.length;
|
|
4539
|
+
let currentValuesLen = restValuesLen;
|
|
4540
|
+
let batch;
|
|
4541
|
+
for (let i = 0; i < values.length; i++) {
|
|
4542
|
+
const encodedRow = `(${encodeRow(
|
|
4543
|
+
ctx,
|
|
4544
|
+
ctxValues,
|
|
4545
|
+
q,
|
|
4546
|
+
QueryClass,
|
|
4547
|
+
values[i],
|
|
4548
|
+
runtimeDefaults,
|
|
4549
|
+
quotedAs
|
|
4550
|
+
)})`;
|
|
4551
|
+
if (ctxValues.length > MAX_BINDING_PARAMS) {
|
|
4552
|
+
if (ctxValues.length - currentValuesLen > MAX_BINDING_PARAMS) {
|
|
4553
|
+
throw new Error(
|
|
4554
|
+
`Too many parameters for a single insert row, max is ${MAX_BINDING_PARAMS}`
|
|
4555
|
+
);
|
|
4556
|
+
}
|
|
4557
|
+
ctx.sql[1] = `VALUES ${valuesSql.join(",")}`;
|
|
4558
|
+
ctxValues.length = currentValuesLen;
|
|
4559
|
+
batch = orchidCore.pushOrNewArray(batch, {
|
|
4560
|
+
text: ctx.sql.join(" "),
|
|
4561
|
+
values: ctxValues
|
|
4562
|
+
});
|
|
4563
|
+
ctxValues = ctx.values = [];
|
|
4564
|
+
valuesSql.length = 0;
|
|
4565
|
+
i--;
|
|
4566
|
+
} else {
|
|
4567
|
+
currentValuesLen = ctxValues.length;
|
|
4568
|
+
valuesSql.push(encodedRow);
|
|
4569
|
+
}
|
|
4570
|
+
}
|
|
4571
|
+
if (batch) {
|
|
4572
|
+
ctx.sql[1] = `VALUES ${valuesSql.join(",")}`;
|
|
4573
|
+
batch.push({
|
|
4574
|
+
text: ctx.sql.join(" "),
|
|
4575
|
+
values: ctxValues
|
|
4576
|
+
});
|
|
4577
|
+
return {
|
|
4578
|
+
hookSelect,
|
|
4579
|
+
batch
|
|
4580
|
+
};
|
|
4581
|
+
} else {
|
|
4582
|
+
ctx.sql[1] = `VALUES ${valuesSql.join(", ")}`;
|
|
4583
|
+
}
|
|
4584
|
+
} else if (query.kind === "raw") {
|
|
4585
|
+
if (orchidCore.isExpression(values)) {
|
|
4586
|
+
let valuesSql = values.toSQL(ctx, quotedAs);
|
|
4587
|
+
if (runtimeDefaults) {
|
|
4588
|
+
valuesSql += `, ${runtimeDefaults.map((fn) => orchidCore.addValue(ctx.values, fn())).join(", ")}`;
|
|
4589
|
+
}
|
|
4590
|
+
ctx.sql[1] = `VALUES (${valuesSql})`;
|
|
4591
|
+
} else {
|
|
4592
|
+
let sql;
|
|
4593
|
+
if (runtimeDefaults) {
|
|
4594
|
+
const { values: v } = ctx;
|
|
4595
|
+
sql = values.map(
|
|
4596
|
+
(raw) => (
|
|
4597
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
4598
|
+
`(${raw.toSQL(ctx, quotedAs)}, ${runtimeDefaults.map((fn) => orchidCore.addValue(v, fn())).join(", ")})`
|
|
4599
|
+
)
|
|
4600
|
+
).join(", ");
|
|
4601
|
+
} else {
|
|
4602
|
+
sql = values.map((raw) => `(${raw.toSQL(ctx, quotedAs)})`).join(", ");
|
|
4603
|
+
}
|
|
4604
|
+
ctx.sql[1] = `VALUES ${sql}`;
|
|
4605
|
+
}
|
|
4606
|
+
} else {
|
|
4607
|
+
const { from, values: v } = values;
|
|
4608
|
+
const q2 = from.clone();
|
|
4609
|
+
if (v) {
|
|
4610
|
+
pushQueryValue(
|
|
4611
|
+
q2,
|
|
4612
|
+
"select",
|
|
4613
|
+
new RawSQL(
|
|
4614
|
+
encodeRow(
|
|
4615
|
+
ctx,
|
|
4616
|
+
ctx.values,
|
|
4617
|
+
q2,
|
|
4618
|
+
QueryClass,
|
|
4619
|
+
v[0],
|
|
4620
|
+
runtimeDefaults,
|
|
4621
|
+
quotedAs
|
|
4622
|
+
)
|
|
4623
|
+
)
|
|
4624
|
+
);
|
|
4625
|
+
}
|
|
4626
|
+
ctx.sql[1] = getSqlText(makeSQL(q2, { values: ctx.values }));
|
|
4627
|
+
}
|
|
4628
|
+
return {
|
|
4629
|
+
hookSelect,
|
|
4630
|
+
text: ctx.sql.join(" "),
|
|
4631
|
+
values: ctx.values
|
|
4632
|
+
};
|
|
4426
4633
|
};
|
|
4427
4634
|
const mergeColumnsSql = (columns, quotedColumns2, target, except) => {
|
|
4428
4635
|
const notExcluded = [];
|
|
@@ -4441,20 +4648,20 @@ const mergeColumnsSql = (columns, quotedColumns2, target, except) => {
|
|
|
4441
4648
|
}
|
|
4442
4649
|
return notExcluded.length ? `DO UPDATE SET ${notExcluded.map((column) => `${column} = excluded.${column}`).join(", ")}` : "DO NOTHING";
|
|
4443
4650
|
};
|
|
4444
|
-
const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
|
|
4651
|
+
const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs) => {
|
|
4445
4652
|
const arr = row.map((value) => {
|
|
4446
4653
|
if (value && typeof value === "object") {
|
|
4447
4654
|
if (value instanceof orchidCore.Expression) {
|
|
4448
4655
|
return value.toSQL(ctx, quotedAs);
|
|
4449
4656
|
} else if (value instanceof QueryClass) {
|
|
4450
|
-
return `(${joinSubQuery(q, value).toSQL(ctx)
|
|
4657
|
+
return `(${getSqlText(joinSubQuery(q, value).toSQL(ctx))})`;
|
|
4451
4658
|
}
|
|
4452
4659
|
}
|
|
4453
|
-
return value === void 0 ? "DEFAULT" : orchidCore.addValue(
|
|
4660
|
+
return value === void 0 ? "DEFAULT" : orchidCore.addValue(values, value);
|
|
4454
4661
|
});
|
|
4455
4662
|
if (runtimeDefaults) {
|
|
4456
4663
|
for (const fn of runtimeDefaults) {
|
|
4457
|
-
arr.push(orchidCore.addValue(
|
|
4664
|
+
arr.push(orchidCore.addValue(values, fn()));
|
|
4458
4665
|
}
|
|
4459
4666
|
}
|
|
4460
4667
|
return arr.join(", ");
|
|
@@ -4568,7 +4775,9 @@ const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
|
4568
4775
|
} else if (orchidCore.isExpression(value)) {
|
|
4569
4776
|
return value.toSQL(ctx, quotedAs);
|
|
4570
4777
|
} else if (value instanceof QueryClass) {
|
|
4571
|
-
return `(${
|
|
4778
|
+
return `(${getSqlText(
|
|
4779
|
+
joinSubQuery(table, value).toSQL(ctx)
|
|
4780
|
+
)})`;
|
|
4572
4781
|
} else if ("op" in value && "arg" in value) {
|
|
4573
4782
|
return `"${table.q.shape[key].data.name || key}" ${value.op} ${orchidCore.addValue(ctx.values, value.arg)}`;
|
|
4574
4783
|
}
|
|
@@ -4595,7 +4804,7 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
|
4595
4804
|
ctx.aliasValue = true;
|
|
4596
4805
|
const as = item[2];
|
|
4597
4806
|
targets.push(
|
|
4598
|
-
`LATERAL (${q.toSQL(ctx)
|
|
4807
|
+
`LATERAL (${getSqlText(q.toSQL(ctx))}) "${((_b = query.joinOverrides) == null ? void 0 : _b[as]) || as}"`
|
|
4599
4808
|
);
|
|
4600
4809
|
ctx.aliasValue = aliasValue;
|
|
4601
4810
|
} else {
|
|
@@ -4772,11 +4981,7 @@ const makeSQL = (table, options) => {
|
|
|
4772
4981
|
}
|
|
4773
4982
|
const quotedAs2 = `"${query.as || tableName}"`;
|
|
4774
4983
|
if (query.type === "insert") {
|
|
4775
|
-
return {
|
|
4776
|
-
hookSelect: pushInsertSql(ctx, table, query, `"${tableName}"`),
|
|
4777
|
-
text: sql.join(" "),
|
|
4778
|
-
values
|
|
4779
|
-
};
|
|
4984
|
+
return makeInsertSql(ctx, table, query, `"${tableName}"`);
|
|
4780
4985
|
}
|
|
4781
4986
|
if (query.type === "update") {
|
|
4782
4987
|
return {
|
|
@@ -4799,9 +5004,9 @@ const makeSQL = (table, options) => {
|
|
|
4799
5004
|
}
|
|
4800
5005
|
const quotedAs = (query.as || table.table) && `"${query.as || table.table}"`;
|
|
4801
5006
|
if (query.union) {
|
|
4802
|
-
sql.push(`(${makeSQL(query.union.b, { values })
|
|
5007
|
+
sql.push(`(${getSqlText(makeSQL(query.union.b, { values }))})`);
|
|
4803
5008
|
for (const u of query.union.u) {
|
|
4804
|
-
const itemSql = orchidCore.isExpression(u.a) ? u.a.toSQL(ctx, quotedAs) : makeSQL(u.a, { values })
|
|
5009
|
+
const itemSql = orchidCore.isExpression(u.a) ? u.a.toSQL(ctx, quotedAs) : getSqlText(makeSQL(u.a, { values }));
|
|
4805
5010
|
sql.push(`${u.k} (${itemSql})`);
|
|
4806
5011
|
}
|
|
4807
5012
|
} else {
|
|
@@ -5291,8 +5496,8 @@ for (const key in types.builtins) {
|
|
|
5291
5496
|
delete defaultTypeParsers[id];
|
|
5292
5497
|
});
|
|
5293
5498
|
const returnArg = (arg) => arg;
|
|
5294
|
-
const rollbackSql
|
|
5295
|
-
const commitSql
|
|
5499
|
+
const rollbackSql = { text: "ROLLBACK" };
|
|
5500
|
+
const commitSql = { text: "COMMIT" };
|
|
5296
5501
|
class Adapter {
|
|
5297
5502
|
constructor(_a) {
|
|
5298
5503
|
var _b = _a, { types: types2 = defaultTypeParsers } = _b, config = __objRest$1(_b, ["types"]);
|
|
@@ -5334,7 +5539,7 @@ class Adapter {
|
|
|
5334
5539
|
arrays(query, types2) {
|
|
5335
5540
|
return performQuery$1(this, query, types2, "array");
|
|
5336
5541
|
}
|
|
5337
|
-
async transaction(begin, cb, end = commitSql
|
|
5542
|
+
async transaction(begin, cb, end = commitSql) {
|
|
5338
5543
|
const client = await this.connect();
|
|
5339
5544
|
try {
|
|
5340
5545
|
await setSearchPath(client, this.schema);
|
|
@@ -5343,7 +5548,7 @@ class Adapter {
|
|
|
5343
5548
|
try {
|
|
5344
5549
|
result = await cb(new TransactionAdapter(this, client, this.types));
|
|
5345
5550
|
} catch (err) {
|
|
5346
|
-
await performQueryOnClient(client, rollbackSql
|
|
5551
|
+
await performQueryOnClient(client, rollbackSql, this.types);
|
|
5347
5552
|
throw err;
|
|
5348
5553
|
}
|
|
5349
5554
|
await performQueryOnClient(client, end, this.types);
|
|
@@ -6449,6 +6654,19 @@ class Create {
|
|
|
6449
6654
|
* const createdCount = await db.table.insertMany([data, data, data]);
|
|
6450
6655
|
* ```
|
|
6451
6656
|
*
|
|
6657
|
+
* Because of a limitation of Postgres protocol, queries having more than **65536** are going to fail in runtime.
|
|
6658
|
+
* To solve this seamlessly, OrchidORM will automatically batch such queries, and wrap them into a transaction, unless they are already in a transaction.
|
|
6659
|
+
*
|
|
6660
|
+
* ```ts
|
|
6661
|
+
* // OK: executes 2 inserts wrapped into a transaction
|
|
6662
|
+
* await db.table.createMany(
|
|
6663
|
+
* Array.from({ length: 65536 }, () => ({ text: 'text' })),
|
|
6664
|
+
* );
|
|
6665
|
+
* ```
|
|
6666
|
+
*
|
|
6667
|
+
* However, this only works in the case shown above. This **won't** work if you're using the `createMany` in `with` statement,
|
|
6668
|
+
* or if the insert is used as a sub-query in other query part.
|
|
6669
|
+
*
|
|
6452
6670
|
* @param data - array of records data, may have values, raw SQL, queries, relation operations
|
|
6453
6671
|
*/
|
|
6454
6672
|
createMany(data) {
|
|
@@ -9830,106 +10048,6 @@ class Update {
|
|
|
9830
10048
|
}
|
|
9831
10049
|
}
|
|
9832
10050
|
|
|
9833
|
-
const commitSql = {
|
|
9834
|
-
text: "COMMIT",
|
|
9835
|
-
values: orchidCore.emptyArray
|
|
9836
|
-
};
|
|
9837
|
-
const rollbackSql = {
|
|
9838
|
-
text: "ROLLBACK",
|
|
9839
|
-
values: orchidCore.emptyArray
|
|
9840
|
-
};
|
|
9841
|
-
class Transaction {
|
|
9842
|
-
async transaction(cbOrOptions, cb) {
|
|
9843
|
-
let options;
|
|
9844
|
-
let fn;
|
|
9845
|
-
if (typeof cbOrOptions === "function") {
|
|
9846
|
-
options = orchidCore.emptyObject;
|
|
9847
|
-
fn = cbOrOptions;
|
|
9848
|
-
} else {
|
|
9849
|
-
options = typeof cbOrOptions === "object" ? cbOrOptions : { level: cbOrOptions };
|
|
9850
|
-
fn = cb;
|
|
9851
|
-
}
|
|
9852
|
-
const sql = {
|
|
9853
|
-
values: orchidCore.emptyArray
|
|
9854
|
-
};
|
|
9855
|
-
const log = this.q.log;
|
|
9856
|
-
let logData;
|
|
9857
|
-
let trx = this.internal.transactionStorage.getStore();
|
|
9858
|
-
const transactionId = trx ? trx.transactionId + 1 : 0;
|
|
9859
|
-
const callback = (adapter) => {
|
|
9860
|
-
if (log)
|
|
9861
|
-
log.afterQuery(sql, logData);
|
|
9862
|
-
if (log)
|
|
9863
|
-
logData = log.beforeQuery(commitSql);
|
|
9864
|
-
if (trx) {
|
|
9865
|
-
trx.transactionId = transactionId;
|
|
9866
|
-
return fn();
|
|
9867
|
-
}
|
|
9868
|
-
trx = {
|
|
9869
|
-
adapter,
|
|
9870
|
-
transactionId
|
|
9871
|
-
};
|
|
9872
|
-
return this.internal.transactionStorage.run(trx, fn);
|
|
9873
|
-
};
|
|
9874
|
-
if (!trx) {
|
|
9875
|
-
sql.text = `BEGIN${options.level ? ` ISOLATION LEVEL ${options.level}` : ""}${options.readOnly !== void 0 ? ` READ ${options.readOnly ? "ONLY" : "WRITE"}` : ""}${options.deferrable !== void 0 ? ` ${options.deferrable ? "" : "NOT "}DEFERRABLE` : ""}`;
|
|
9876
|
-
if (log)
|
|
9877
|
-
logData = log.beforeQuery(sql);
|
|
9878
|
-
try {
|
|
9879
|
-
const result = await this.q.adapter.transaction(sql, callback);
|
|
9880
|
-
if (log)
|
|
9881
|
-
log.afterQuery(commitSql, logData);
|
|
9882
|
-
const { afterCommit } = trx;
|
|
9883
|
-
if (afterCommit) {
|
|
9884
|
-
const promises = [];
|
|
9885
|
-
for (let i = 0, len = afterCommit.length; i < len; i += 2) {
|
|
9886
|
-
const q = afterCommit[i];
|
|
9887
|
-
const result2 = afterCommit[i + 1];
|
|
9888
|
-
for (const fn2 of afterCommit[i + 2]) {
|
|
9889
|
-
promises.push(fn2(result2, q));
|
|
9890
|
-
}
|
|
9891
|
-
}
|
|
9892
|
-
await Promise.all(promises);
|
|
9893
|
-
}
|
|
9894
|
-
return result;
|
|
9895
|
-
} catch (err) {
|
|
9896
|
-
if (log)
|
|
9897
|
-
log.afterQuery(rollbackSql, logData);
|
|
9898
|
-
throw err;
|
|
9899
|
-
}
|
|
9900
|
-
} else {
|
|
9901
|
-
try {
|
|
9902
|
-
sql.text = `SAVEPOINT "${transactionId}"`;
|
|
9903
|
-
if (log)
|
|
9904
|
-
logData = log.beforeQuery(sql);
|
|
9905
|
-
const { adapter } = trx;
|
|
9906
|
-
await adapter.query(sql);
|
|
9907
|
-
let result;
|
|
9908
|
-
try {
|
|
9909
|
-
result = await callback(adapter);
|
|
9910
|
-
} catch (err) {
|
|
9911
|
-
sql.text = `ROLLBACK TO SAVEPOINT "${transactionId}"`;
|
|
9912
|
-
if (log)
|
|
9913
|
-
logData = log.beforeQuery(sql);
|
|
9914
|
-
await adapter.query(sql);
|
|
9915
|
-
if (log)
|
|
9916
|
-
log.afterQuery(sql, logData);
|
|
9917
|
-
throw err;
|
|
9918
|
-
}
|
|
9919
|
-
sql.text = `RELEASE SAVEPOINT "${transactionId}"`;
|
|
9920
|
-
if (log)
|
|
9921
|
-
logData = log.beforeQuery(sql);
|
|
9922
|
-
await adapter.query(sql);
|
|
9923
|
-
if (log)
|
|
9924
|
-
log.afterQuery(sql, logData);
|
|
9925
|
-
return result;
|
|
9926
|
-
} finally {
|
|
9927
|
-
trx.transactionId = transactionId - 1;
|
|
9928
|
-
}
|
|
9929
|
-
}
|
|
9930
|
-
}
|
|
9931
|
-
}
|
|
9932
|
-
|
|
9933
10051
|
var __defProp$2 = Object.defineProperty;
|
|
9934
10052
|
var __defProps = Object.defineProperties;
|
|
9935
10053
|
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
@@ -10604,7 +10722,7 @@ class OrExpression extends orchidCore.Expression {
|
|
|
10604
10722
|
if (sql)
|
|
10605
10723
|
res.push(sql);
|
|
10606
10724
|
} else {
|
|
10607
|
-
res.push(`(${arg.toSQL(ctx)
|
|
10725
|
+
res.push(`(${getSqlText(arg.toSQL(ctx))})`);
|
|
10608
10726
|
}
|
|
10609
10727
|
}
|
|
10610
10728
|
}
|
|
@@ -12190,6 +12308,7 @@ exports.columnCode = columnCode;
|
|
|
12190
12308
|
exports.columnForeignKeysToCode = columnForeignKeysToCode;
|
|
12191
12309
|
exports.columnIndexesToCode = columnIndexesToCode;
|
|
12192
12310
|
exports.columnsShapeToCode = columnsShapeToCode;
|
|
12311
|
+
exports.commitSql = commitSql$1;
|
|
12193
12312
|
exports.constraintInnerToCode = constraintInnerToCode;
|
|
12194
12313
|
exports.constraintToCode = constraintToCode;
|
|
12195
12314
|
exports.copyTableData = copyTableData;
|
|
@@ -12204,6 +12323,7 @@ exports.getColumnTypes = getColumnTypes;
|
|
|
12204
12323
|
exports.getPrimaryKeys = getPrimaryKeys;
|
|
12205
12324
|
exports.getQueryAs = getQueryAs;
|
|
12206
12325
|
exports.getShapeFromSelect = getShapeFromSelect;
|
|
12326
|
+
exports.getSqlText = getSqlText;
|
|
12207
12327
|
exports.handleResult = handleResult;
|
|
12208
12328
|
exports.identityToCode = identityToCode;
|
|
12209
12329
|
exports.indexInnerToCode = indexInnerToCode;
|
|
@@ -12243,6 +12363,7 @@ exports.quoteString = quoteString;
|
|
|
12243
12363
|
exports.raw = raw;
|
|
12244
12364
|
exports.referencesArgsToCode = referencesArgsToCode;
|
|
12245
12365
|
exports.resolveSubQueryCallback = resolveSubQueryCallback;
|
|
12366
|
+
exports.rollbackSql = rollbackSql$1;
|
|
12246
12367
|
exports.saveSearchAlias = saveSearchAlias;
|
|
12247
12368
|
exports.setParserForSelectedString = setParserForSelectedString;
|
|
12248
12369
|
exports.setQueryObjectValue = setQueryObjectValue;
|