effect-qb 0.16.0 → 4.0.0-beta.66
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/README.md +7 -0
- package/dist/mysql.js +1858 -715
- package/dist/postgres/metadata.js +2036 -172
- package/dist/postgres.js +8011 -6849
- package/dist/sqlite.js +8433 -0
- package/package.json +7 -4
- package/src/internal/column-state.d.ts +3 -3
- package/src/internal/column-state.ts +3 -3
- package/src/internal/column.ts +8 -8
- package/src/internal/derived-table.ts +29 -3
- package/src/internal/dialect.ts +3 -1
- package/src/internal/dsl-mutation-runtime.ts +173 -4
- package/src/internal/dsl-plan-runtime.ts +165 -20
- package/src/internal/dsl-query-runtime.ts +60 -6
- package/src/internal/dsl-transaction-ddl-runtime.ts +72 -2
- package/src/internal/executor.ts +100 -43
- package/src/internal/expression-ast.ts +3 -2
- package/src/internal/grouping-key.ts +141 -1
- package/src/internal/implication-runtime.ts +2 -1
- package/src/internal/json/types.ts +155 -40
- package/src/internal/predicate/context.ts +14 -1
- package/src/internal/predicate/key.ts +19 -2
- package/src/internal/predicate/runtime.ts +27 -3
- package/src/internal/query.d.ts +1 -1
- package/src/internal/query.ts +253 -31
- package/src/internal/renderer.ts +35 -2
- package/src/internal/runtime/driver-value-mapping.ts +60 -2
- package/src/internal/runtime/normalize.ts +62 -38
- package/src/internal/runtime/schema.ts +32 -40
- package/src/internal/runtime/value.ts +159 -39
- package/src/internal/scalar.d.ts +1 -1
- package/src/internal/scalar.ts +1 -1
- package/src/internal/schema-derivation.d.ts +12 -61
- package/src/internal/schema-derivation.ts +95 -43
- package/src/internal/table-options.ts +108 -1
- package/src/internal/table.d.ts +29 -22
- package/src/internal/table.ts +260 -53
- package/src/mysql/column.ts +24 -8
- package/src/mysql/datatypes/index.ts +21 -0
- package/src/mysql/errors/catalog.ts +5 -5
- package/src/mysql/errors/normalize.ts +2 -2
- package/src/mysql/executor.ts +4 -4
- package/src/mysql/function/temporal.ts +1 -1
- package/src/mysql/internal/dsl.ts +759 -235
- package/src/mysql/internal/renderer.ts +2 -1
- package/src/mysql/internal/sql-expression-renderer.ts +486 -130
- package/src/mysql/query.ts +9 -2
- package/src/mysql/table.ts +64 -35
- package/src/postgres/column.ts +14 -12
- package/src/postgres/errors/normalize.ts +2 -2
- package/src/postgres/executor.ts +52 -9
- package/src/postgres/function/core.ts +19 -1
- package/src/postgres/function/temporal.ts +1 -1
- package/src/postgres/internal/dsl.ts +705 -256
- package/src/postgres/internal/renderer.ts +2 -1
- package/src/postgres/internal/schema-ddl.ts +2 -1
- package/src/postgres/internal/schema-model.ts +6 -3
- package/src/postgres/internal/sql-expression-renderer.ts +420 -91
- package/src/postgres/json.ts +57 -17
- package/src/postgres/query.ts +9 -2
- package/src/postgres/schema-management.ts +92 -6
- package/src/postgres/schema.ts +1 -1
- package/src/postgres/table.ts +203 -75
- package/src/sqlite/column.ts +128 -0
- package/src/sqlite/datatypes/index.ts +79 -0
- package/src/sqlite/datatypes/spec.ts +98 -0
- package/src/sqlite/errors/catalog.ts +103 -0
- package/src/sqlite/errors/fields.ts +19 -0
- package/src/sqlite/errors/index.ts +19 -0
- package/src/sqlite/errors/normalize.ts +229 -0
- package/src/sqlite/errors/requirements.ts +71 -0
- package/src/sqlite/errors/types.ts +29 -0
- package/src/sqlite/executor.ts +227 -0
- package/src/sqlite/function/aggregate.ts +2 -0
- package/src/sqlite/function/core.ts +2 -0
- package/src/sqlite/function/index.ts +19 -0
- package/src/sqlite/function/string.ts +2 -0
- package/src/sqlite/function/temporal.ts +100 -0
- package/src/sqlite/function/window.ts +2 -0
- package/src/sqlite/internal/dialect.ts +37 -0
- package/src/sqlite/internal/dsl.ts +6927 -0
- package/src/sqlite/internal/renderer.ts +47 -0
- package/src/sqlite/internal/sql-expression-renderer.ts +1821 -0
- package/src/sqlite/json.ts +2 -0
- package/src/sqlite/query.ts +196 -0
- package/src/sqlite/renderer.ts +24 -0
- package/src/sqlite/table.ts +175 -0
- package/src/sqlite.ts +22 -0
package/dist/mysql.js
CHANGED
|
@@ -311,20 +311,119 @@ var references = (target) => foreignKey(target);
|
|
|
311
311
|
|
|
312
312
|
// src/internal/runtime/value.ts
|
|
313
313
|
import * as Schema3 from "effect/Schema";
|
|
314
|
-
var brandString = (
|
|
315
|
-
var
|
|
316
|
-
var
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
314
|
+
var brandString = (pattern, brand5) => Schema3.String.pipe(Schema3.check(Schema3.isPattern(pattern)), Schema3.brand(brand5));
|
|
315
|
+
var localDatePattern = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
316
|
+
var isValidLocalDateString = (value) => {
|
|
317
|
+
const match = localDatePattern.exec(value);
|
|
318
|
+
if (match === null) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
const year = Number(match[1]);
|
|
322
|
+
const month = Number(match[2]);
|
|
323
|
+
const day = Number(match[3]);
|
|
324
|
+
const parsed = new Date(Date.UTC(year, month - 1, day));
|
|
325
|
+
parsed.setUTCFullYear(year);
|
|
326
|
+
return parsed.getUTCFullYear() === year && parsed.getUTCMonth() === month - 1 && parsed.getUTCDate() === day;
|
|
327
|
+
};
|
|
328
|
+
var localTimePattern = /^(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?$/;
|
|
329
|
+
var isValidLocalTimeString = (value) => {
|
|
330
|
+
const match = localTimePattern.exec(value);
|
|
331
|
+
if (match === null) {
|
|
332
|
+
return false;
|
|
333
|
+
}
|
|
334
|
+
const hour = Number(match[1]);
|
|
335
|
+
const minute = Number(match[2]);
|
|
336
|
+
const second = Number(match[3]);
|
|
337
|
+
return hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59 && second >= 0 && second <= 59;
|
|
338
|
+
};
|
|
339
|
+
var offsetPattern = /^(?:Z|[+-](\d{2}):(\d{2}))$/;
|
|
340
|
+
var isValidOffset = (value) => {
|
|
341
|
+
const match = offsetPattern.exec(value);
|
|
342
|
+
if (match === null) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
if (value === "Z") {
|
|
346
|
+
return true;
|
|
347
|
+
}
|
|
348
|
+
const hour = Number(match[1]);
|
|
349
|
+
const minute = Number(match[2]);
|
|
350
|
+
return hour >= 0 && hour <= 23 && minute >= 0 && minute <= 59;
|
|
351
|
+
};
|
|
352
|
+
var offsetTimePattern = /^(\d{2}:\d{2}:\d{2}(?:\.\d+)?)(Z|[+-]\d{2}:\d{2})$/;
|
|
353
|
+
var isValidOffsetTimeString = (value) => {
|
|
354
|
+
const match = offsetTimePattern.exec(value);
|
|
355
|
+
return match !== null && isValidLocalTimeString(match[1]) && isValidOffset(match[2]);
|
|
356
|
+
};
|
|
357
|
+
var localDateTimePattern = /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?)$/;
|
|
358
|
+
var isValidLocalDateTimeString = (value) => {
|
|
359
|
+
const match = localDateTimePattern.exec(value);
|
|
360
|
+
return match !== null && isValidLocalDateString(match[1]) && isValidLocalTimeString(match[2]);
|
|
361
|
+
};
|
|
362
|
+
var instantPattern = /^(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?)(Z|[+-]\d{2}:\d{2})$/;
|
|
363
|
+
var isValidInstantString = (value) => {
|
|
364
|
+
const match = instantPattern.exec(value);
|
|
365
|
+
return match !== null && isValidLocalDateString(match[1]) && isValidLocalTimeString(match[2]) && isValidOffset(match[3]);
|
|
366
|
+
};
|
|
367
|
+
var LocalDateStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(localDatePattern)), Schema3.check(Schema3.makeFilter((value) => isValidLocalDateString(value))), Schema3.brand("LocalDateString"));
|
|
368
|
+
var LocalTimeStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(localTimePattern)), Schema3.check(Schema3.makeFilter((value) => isValidLocalTimeString(value))), Schema3.brand("LocalTimeString"));
|
|
369
|
+
var OffsetTimeStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(offsetTimePattern)), Schema3.check(Schema3.makeFilter((value) => isValidOffsetTimeString(value))), Schema3.brand("OffsetTimeString"));
|
|
370
|
+
var LocalDateTimeStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(localDateTimePattern)), Schema3.check(Schema3.makeFilter((value) => isValidLocalDateTimeString(value))), Schema3.brand("LocalDateTimeString"));
|
|
371
|
+
var InstantStringSchema = Schema3.String.pipe(Schema3.check(Schema3.isPattern(instantPattern)), Schema3.check(Schema3.makeFilter((value) => isValidInstantString(value))), Schema3.brand("InstantString"));
|
|
320
372
|
var YearStringSchema = brandString(/^\d{4}$/, "YearString");
|
|
321
|
-
var
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
373
|
+
var canonicalizeBigIntString = (input) => {
|
|
374
|
+
const trimmed = input.trim();
|
|
375
|
+
if (!/^-?\d+$/.test(trimmed)) {
|
|
376
|
+
throw new Error("Expected an integer-like bigint value");
|
|
377
|
+
}
|
|
378
|
+
return BigInt(trimmed).toString();
|
|
379
|
+
};
|
|
380
|
+
var isCanonicalBigIntString = (value) => {
|
|
381
|
+
try {
|
|
382
|
+
return canonicalizeBigIntString(value) === value;
|
|
383
|
+
} catch {
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
var canonicalizeDecimalString = (input) => {
|
|
388
|
+
const trimmed = input.trim();
|
|
389
|
+
const match = /^([+-]?)(\d+)(?:\.(\d+))?$/.exec(trimmed);
|
|
390
|
+
if (match === null) {
|
|
391
|
+
throw new Error("Expected a decimal string");
|
|
392
|
+
}
|
|
393
|
+
const sign = match[1] === "-" ? "-" : "";
|
|
394
|
+
const integer = match[2].replace(/^0+(?=\d)/, "") || "0";
|
|
395
|
+
const fraction = (match[3] ?? "").replace(/0+$/, "");
|
|
396
|
+
if (fraction.length === 0) {
|
|
397
|
+
if (integer === "0") {
|
|
398
|
+
return "0";
|
|
399
|
+
}
|
|
400
|
+
return `${sign}${integer}`;
|
|
401
|
+
}
|
|
402
|
+
return `${sign}${integer}.${fraction}`;
|
|
403
|
+
};
|
|
404
|
+
var isCanonicalDecimalString = (value) => {
|
|
405
|
+
try {
|
|
406
|
+
return canonicalizeDecimalString(value) === value;
|
|
407
|
+
} catch {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
};
|
|
411
|
+
var BigIntStringSchema = Schema3.String.pipe(Schema3.check(Schema3.makeFilter((value) => isCanonicalBigIntString(value))), Schema3.brand("BigIntString"));
|
|
412
|
+
var DecimalStringSchema = Schema3.String.pipe(Schema3.check(Schema3.makeFilter((value) => isCanonicalDecimalString(value))), Schema3.brand("DecimalString"));
|
|
413
|
+
var JsonValueSchema = Schema3.suspend(() => Schema3.Union([
|
|
414
|
+
Schema3.String,
|
|
415
|
+
Schema3.Number.check(Schema3.isFinite()),
|
|
416
|
+
Schema3.Boolean,
|
|
417
|
+
Schema3.Null,
|
|
418
|
+
Schema3.Array(JsonValueSchema),
|
|
419
|
+
Schema3.Record(Schema3.String, JsonValueSchema)
|
|
420
|
+
]));
|
|
421
|
+
var JsonPrimitiveSchema = Schema3.Union([
|
|
422
|
+
Schema3.String,
|
|
423
|
+
Schema3.Number.check(Schema3.isFinite()),
|
|
424
|
+
Schema3.Boolean,
|
|
425
|
+
Schema3.Null
|
|
426
|
+
]);
|
|
328
427
|
|
|
329
428
|
// src/mysql/datatypes/index.ts
|
|
330
429
|
var exports_datatypes = {};
|
|
@@ -542,6 +641,12 @@ var mysqlDatatypeModule = {
|
|
|
542
641
|
for (const kind of Object.keys(mysqlDatatypeKinds)) {
|
|
543
642
|
mysqlDatatypeModule[kind] = () => withMetadata(kind);
|
|
544
643
|
}
|
|
644
|
+
mysqlDatatypeModule.json = () => ({
|
|
645
|
+
...withMetadata("json"),
|
|
646
|
+
driverValueMapping: {
|
|
647
|
+
toDriver: (value) => value !== null && typeof value === "object" ? JSON.stringify(value) : value
|
|
648
|
+
}
|
|
649
|
+
});
|
|
545
650
|
var mysqlDatatypes = mysqlDatatypeModule;
|
|
546
651
|
|
|
547
652
|
// src/mysql/column.ts
|
|
@@ -575,7 +680,7 @@ var custom = (schema2, dbType) => makeColumnDefinition(schema2, {
|
|
|
575
680
|
ddlType: undefined,
|
|
576
681
|
identity: undefined
|
|
577
682
|
});
|
|
578
|
-
var uuid = () => primitive(Schema4.
|
|
683
|
+
var uuid = () => primitive(Schema4.String.check(Schema4.isUUID()), mysqlDatatypes.uuid());
|
|
579
684
|
var text = () => primitive(Schema4.String, mysqlDatatypes.text());
|
|
580
685
|
var int = () => primitive(Schema4.Int, mysqlDatatypes.int());
|
|
581
686
|
var number = (options) => makeColumnDefinition(DecimalStringSchema, {
|
|
@@ -52238,10 +52343,8 @@ var mysqlErrorCatalogByNumber = {
|
|
|
52238
52343
|
"MY-015152": [mysqlErrorCatalogBySymbol["ER_AUDIT_LOG_JSON_INVALID_FILTER_CANNOT_BE_USED"]],
|
|
52239
52344
|
"MY-015153": [mysqlErrorCatalogBySymbol["ER_WARN_AUDIT_LOG_FILTER_RECOVERY_LOGGING_DISABLED_LOG"]]
|
|
52240
52345
|
};
|
|
52241
|
-
var
|
|
52242
|
-
var
|
|
52243
|
-
var isMysqlErrorSymbol = (value) => mysqlSymbolPattern.test(value);
|
|
52244
|
-
var isMysqlErrorNumber = (value) => mysqlNumberPattern.test(value);
|
|
52346
|
+
var isMysqlErrorSymbol = (value) => (value in mysqlErrorCatalogBySymbol);
|
|
52347
|
+
var isMysqlErrorNumber = (value) => (value in mysqlErrorCatalogByNumber);
|
|
52245
52348
|
var getMysqlErrorDescriptor = (symbol) => mysqlErrorCatalogBySymbol[symbol];
|
|
52246
52349
|
var findMysqlErrorDescriptorsByNumber = (value) => mysqlErrorCatalogByNumber[value];
|
|
52247
52350
|
var findMysqlErrorDescriptorsByNumberLoose = (value) => mysqlErrorCatalogByNumber[String(value)];
|
|
@@ -109613,8 +109716,8 @@ var asNumber = (value) => {
|
|
|
109613
109716
|
if (typeof value === "number" && Number.isFinite(value)) {
|
|
109614
109717
|
return value;
|
|
109615
109718
|
}
|
|
109616
|
-
if (typeof value === "string" && value.trim()
|
|
109617
|
-
const parsed = Number(value);
|
|
109719
|
+
if (typeof value === "string" && /^[+-]?\d+$/.test(value.trim())) {
|
|
109720
|
+
const parsed = Number(value.trim());
|
|
109618
109721
|
return Number.isFinite(parsed) ? parsed : undefined;
|
|
109619
109722
|
}
|
|
109620
109723
|
return;
|
|
@@ -109797,6 +109900,27 @@ var TypeId3 = Symbol.for("effect-qb/Plan");
|
|
|
109797
109900
|
import { pipeArguments as pipeArguments2 } from "effect/Pipeable";
|
|
109798
109901
|
|
|
109799
109902
|
// src/internal/table-options.ts
|
|
109903
|
+
var referentialActionError = "Foreign key action must be noAction, restrict, cascade, setNull, or setDefault";
|
|
109904
|
+
var renderReferentialAction = (action) => {
|
|
109905
|
+
switch (action) {
|
|
109906
|
+
case "noAction":
|
|
109907
|
+
return "no action";
|
|
109908
|
+
case "restrict":
|
|
109909
|
+
return "restrict";
|
|
109910
|
+
case "cascade":
|
|
109911
|
+
return "cascade";
|
|
109912
|
+
case "setNull":
|
|
109913
|
+
return "set null";
|
|
109914
|
+
case "setDefault":
|
|
109915
|
+
return "set default";
|
|
109916
|
+
}
|
|
109917
|
+
throw new Error(referentialActionError);
|
|
109918
|
+
};
|
|
109919
|
+
var validateReferentialAction = (action) => {
|
|
109920
|
+
if (action !== undefined) {
|
|
109921
|
+
renderReferentialAction(action);
|
|
109922
|
+
}
|
|
109923
|
+
};
|
|
109800
109924
|
var normalizeColumnList = (columns) => {
|
|
109801
109925
|
const normalized = Array.isArray(columns) ? [...columns] : [columns];
|
|
109802
109926
|
if (normalized.length === 0) {
|
|
@@ -109824,6 +109948,8 @@ var collectInlineOptions = (fields2) => {
|
|
|
109824
109948
|
});
|
|
109825
109949
|
}
|
|
109826
109950
|
if (column.metadata.references) {
|
|
109951
|
+
validateReferentialAction(column.metadata.references.onUpdate);
|
|
109952
|
+
validateReferentialAction(column.metadata.references.onDelete);
|
|
109827
109953
|
const local = [columnName];
|
|
109828
109954
|
options.push({
|
|
109829
109955
|
kind: "foreignKey",
|
|
@@ -109900,6 +110026,8 @@ var validateOptions = (tableName, fields2, options) => {
|
|
|
109900
110026
|
}
|
|
109901
110027
|
}
|
|
109902
110028
|
if (option.kind === "foreignKey") {
|
|
110029
|
+
validateReferentialAction(option.onUpdate);
|
|
110030
|
+
validateReferentialAction(option.onDelete);
|
|
109903
110031
|
const reference = option.references();
|
|
109904
110032
|
if (reference.columns.length !== columns.length) {
|
|
109905
110033
|
throw new Error(`Foreign key on table '${tableName}' must reference the same number of columns`);
|
|
@@ -109924,7 +110052,7 @@ var validateOptions = (tableName, fields2, options) => {
|
|
|
109924
110052
|
throw new Error(`Unknown index key column '${key.column}' on table '${tableName}'`);
|
|
109925
110053
|
}
|
|
109926
110054
|
}
|
|
109927
|
-
if (
|
|
110055
|
+
if (columns.length === 0 && (option.keys === undefined || option.keys.length === 0)) {
|
|
109928
110056
|
throw new Error(`Index on table '${tableName}' requires at least one column or key`);
|
|
109929
110057
|
}
|
|
109930
110058
|
}
|
|
@@ -109943,12 +110071,7 @@ var validateOptions = (tableName, fields2, options) => {
|
|
|
109943
110071
|
};
|
|
109944
110072
|
|
|
109945
110073
|
// src/internal/schema-derivation.ts
|
|
109946
|
-
import * as VariantSchema from "@effect/experimental/VariantSchema";
|
|
109947
110074
|
import * as Schema5 from "effect/Schema";
|
|
109948
|
-
var TableSchema = VariantSchema.make({
|
|
109949
|
-
variants: ["select", "insert", "update"],
|
|
109950
|
-
defaultVariant: "select"
|
|
109951
|
-
});
|
|
109952
110075
|
var maybeBrandSchema = (column, tableName, columnName) => column.metadata.brand === true ? Schema5.brand(`${tableName}.${columnName}`)(column.schema) : column.schema;
|
|
109953
110076
|
var selectSchema = (column, tableName, columnName) => column.metadata.nullable ? Schema5.NullOr(maybeBrandSchema(column, tableName, columnName)) : maybeBrandSchema(column, tableName, columnName);
|
|
109954
110077
|
var insertSchema = (column, tableName, columnName) => {
|
|
@@ -109965,42 +110088,37 @@ var updateSchema = (column, tableName, columnName, isPrimaryKey) => {
|
|
|
109965
110088
|
const base = column.metadata.nullable ? Schema5.NullOr(maybeBrandSchema(column, tableName, columnName)) : maybeBrandSchema(column, tableName, columnName);
|
|
109966
110089
|
return Schema5.optional(base);
|
|
109967
110090
|
};
|
|
109968
|
-
var
|
|
110091
|
+
var fieldSchemaForVariant = (variant, column, tableName, columnName, primaryKeySet) => {
|
|
110092
|
+
switch (variant) {
|
|
110093
|
+
case "select":
|
|
110094
|
+
return selectSchema(column, tableName, columnName);
|
|
110095
|
+
case "insert":
|
|
110096
|
+
return insertSchema(column, tableName, columnName);
|
|
110097
|
+
case "update":
|
|
110098
|
+
return updateSchema(column, tableName, columnName, primaryKeySet.has(columnName));
|
|
110099
|
+
}
|
|
110100
|
+
};
|
|
110101
|
+
var deriveSchema = (variant, tableName, fields2, primaryKeyColumns) => {
|
|
109969
110102
|
const primaryKeySet = new Set(primaryKeyColumns);
|
|
109970
|
-
const
|
|
110103
|
+
const structFields = {};
|
|
109971
110104
|
for (const [key, column] of Object.entries(fields2)) {
|
|
109972
|
-
const
|
|
109973
|
-
|
|
109974
|
-
|
|
109975
|
-
update: undefined
|
|
109976
|
-
};
|
|
109977
|
-
const insert = insertSchema(column, tableName, key);
|
|
109978
|
-
const update = updateSchema(column, tableName, key, primaryKeySet.has(key));
|
|
109979
|
-
if (insert !== undefined) {
|
|
109980
|
-
config.insert = insert;
|
|
109981
|
-
} else {
|
|
109982
|
-
delete config.insert;
|
|
110105
|
+
const schema3 = fieldSchemaForVariant(variant, column, tableName, key, primaryKeySet);
|
|
110106
|
+
if (schema3 !== undefined) {
|
|
110107
|
+
structFields[key] = schema3;
|
|
109983
110108
|
}
|
|
109984
|
-
if (update !== undefined) {
|
|
109985
|
-
config.update = update;
|
|
109986
|
-
} else {
|
|
109987
|
-
delete config.update;
|
|
109988
|
-
}
|
|
109989
|
-
variants[key] = TableSchema.Field(config);
|
|
109990
110109
|
}
|
|
109991
|
-
|
|
109992
|
-
return {
|
|
109993
|
-
select: TableSchema.extract(struct, "select"),
|
|
109994
|
-
insert: TableSchema.extract(struct, "insert"),
|
|
109995
|
-
update: TableSchema.extract(struct, "update")
|
|
109996
|
-
};
|
|
110110
|
+
return Schema5.Struct(structFields);
|
|
109997
110111
|
};
|
|
110112
|
+
var deriveSelectSchema = (tableName, fields2, primaryKeyColumns) => deriveSchema("select", tableName, fields2, primaryKeyColumns);
|
|
110113
|
+
var deriveInsertSchema = (tableName, fields2, primaryKeyColumns) => deriveSchema("insert", tableName, fields2, primaryKeyColumns);
|
|
110114
|
+
var deriveUpdateSchema = (tableName, fields2, primaryKeyColumns) => deriveSchema("update", tableName, fields2, primaryKeyColumns);
|
|
109998
110115
|
|
|
109999
110116
|
// src/internal/table.ts
|
|
110000
110117
|
var TypeId4 = Symbol.for("effect-qb/Table");
|
|
110001
110118
|
var OptionsSymbol = Symbol.for("effect-qb/Table/normalizedOptions");
|
|
110002
110119
|
var options = Symbol.for("effect-qb/Table/declaredOptions");
|
|
110003
110120
|
var CacheSymbol = Symbol.for("effect-qb/Table/cache");
|
|
110121
|
+
var SchemaCacheSymbol = Symbol.for("effect-qb/Table/schemaCache");
|
|
110004
110122
|
var DeclaredOptionsSymbol = Symbol.for("effect-qb/Table/factoryDeclaredOptions");
|
|
110005
110123
|
var TableProto = {
|
|
110006
110124
|
pipe() {
|
|
@@ -110023,14 +110141,86 @@ var buildArtifacts = (name, fields2, declaredOptions, schemaName) => {
|
|
|
110023
110141
|
validateOptions(name, fields2, declaredOptions);
|
|
110024
110142
|
const primaryKey3 = resolvePrimaryKeyColumns(fields2, declaredOptions);
|
|
110025
110143
|
const columns = Object.fromEntries(Object.entries(fields2).map(([key, column]) => [key, bindColumn(name, key, column, name, schemaName)]));
|
|
110026
|
-
const schemas = deriveSchemas(name, fields2, primaryKey3);
|
|
110027
110144
|
return {
|
|
110028
110145
|
columns,
|
|
110029
|
-
schemas,
|
|
110030
110146
|
normalizedOptions,
|
|
110031
110147
|
primaryKey: primaryKey3
|
|
110032
110148
|
};
|
|
110033
110149
|
};
|
|
110150
|
+
var getSchemaCache = (table) => {
|
|
110151
|
+
const target = table;
|
|
110152
|
+
if (target[SchemaCacheSymbol] !== undefined) {
|
|
110153
|
+
return target[SchemaCacheSymbol];
|
|
110154
|
+
}
|
|
110155
|
+
const cache = {};
|
|
110156
|
+
Object.defineProperty(table, SchemaCacheSymbol, {
|
|
110157
|
+
configurable: true,
|
|
110158
|
+
value: cache
|
|
110159
|
+
});
|
|
110160
|
+
return cache;
|
|
110161
|
+
};
|
|
110162
|
+
var deriveTableSchema = (table, variant) => {
|
|
110163
|
+
const state = table[TypeId4];
|
|
110164
|
+
switch (variant) {
|
|
110165
|
+
case "select":
|
|
110166
|
+
return deriveSelectSchema(state.name, state.fields, state.primaryKey);
|
|
110167
|
+
case "insert":
|
|
110168
|
+
return deriveInsertSchema(state.name, state.fields, state.primaryKey);
|
|
110169
|
+
case "update":
|
|
110170
|
+
return deriveUpdateSchema(state.name, state.fields, state.primaryKey);
|
|
110171
|
+
}
|
|
110172
|
+
};
|
|
110173
|
+
var schemaFor = (table, variant) => {
|
|
110174
|
+
const cache = getSchemaCache(table);
|
|
110175
|
+
const cached = cache[variant];
|
|
110176
|
+
if (cached !== undefined) {
|
|
110177
|
+
return cached;
|
|
110178
|
+
}
|
|
110179
|
+
const schema3 = deriveTableSchema(table, variant);
|
|
110180
|
+
cache[variant] = schema3;
|
|
110181
|
+
return schema3;
|
|
110182
|
+
};
|
|
110183
|
+
function selectSchema2(table) {
|
|
110184
|
+
return schemaFor(table, "select");
|
|
110185
|
+
}
|
|
110186
|
+
function insertSchema2(table) {
|
|
110187
|
+
return schemaFor(table, "insert");
|
|
110188
|
+
}
|
|
110189
|
+
function updateSchema2(table) {
|
|
110190
|
+
return schemaFor(table, "update");
|
|
110191
|
+
}
|
|
110192
|
+
var schemasFor = (table) => {
|
|
110193
|
+
const cache = getSchemaCache(table);
|
|
110194
|
+
if (cache.schemas !== undefined) {
|
|
110195
|
+
return cache.schemas;
|
|
110196
|
+
}
|
|
110197
|
+
const schemas = {};
|
|
110198
|
+
Object.defineProperties(schemas, {
|
|
110199
|
+
select: {
|
|
110200
|
+
enumerable: true,
|
|
110201
|
+
get: () => selectSchema2(table)
|
|
110202
|
+
},
|
|
110203
|
+
insert: {
|
|
110204
|
+
enumerable: true,
|
|
110205
|
+
get: () => insertSchema2(table)
|
|
110206
|
+
},
|
|
110207
|
+
update: {
|
|
110208
|
+
enumerable: true,
|
|
110209
|
+
get: () => updateSchema2(table)
|
|
110210
|
+
}
|
|
110211
|
+
});
|
|
110212
|
+
cache.schemas = schemas;
|
|
110213
|
+
return schemas;
|
|
110214
|
+
};
|
|
110215
|
+
var defineSchemasGetter = (table) => {
|
|
110216
|
+
Object.defineProperty(table, "schemas", {
|
|
110217
|
+
configurable: true,
|
|
110218
|
+
enumerable: true,
|
|
110219
|
+
get() {
|
|
110220
|
+
return schemasFor(table);
|
|
110221
|
+
}
|
|
110222
|
+
});
|
|
110223
|
+
};
|
|
110034
110224
|
var makeTable = (name, fields2, declaredOptions, baseName = name, kind = "schema", schemaName, schemaMode = "default") => {
|
|
110035
110225
|
const resolvedSchemaName = schemaMode === "explicit" ? schemaName : "public";
|
|
110036
110226
|
const artifacts = buildArtifacts(name, fields2, declaredOptions, resolvedSchemaName);
|
|
@@ -110038,7 +110228,7 @@ var makeTable = (name, fields2, declaredOptions, baseName = name, kind = "schema
|
|
|
110038
110228
|
const table = attachPipe2(Object.create(TableProto));
|
|
110039
110229
|
table.name = name;
|
|
110040
110230
|
table.columns = artifacts.columns;
|
|
110041
|
-
table
|
|
110231
|
+
defineSchemasGetter(table);
|
|
110042
110232
|
table[TypeId4] = {
|
|
110043
110233
|
name,
|
|
110044
110234
|
baseName,
|
|
@@ -110111,7 +110301,6 @@ var ensureClassArtifacts = (self) => {
|
|
|
110111
110301
|
const table = applyDeclaredOptions(makeTable(state.name, state.fields, [], state.name, "schema", state.schemaName, state.schemaName === undefined || state.schemaName === "public" ? "default" : "explicit"), classOptions);
|
|
110112
110302
|
const artifacts = {
|
|
110113
110303
|
columns: table.columns,
|
|
110114
|
-
schemas: table.schemas,
|
|
110115
110304
|
normalizedOptions: table[OptionsSymbol],
|
|
110116
110305
|
primaryKey: table[TypeId4].primaryKey
|
|
110117
110306
|
};
|
|
@@ -110134,21 +110323,24 @@ var makeOption = (option) => {
|
|
|
110134
110323
|
return builder;
|
|
110135
110324
|
};
|
|
110136
110325
|
var option = (spec) => makeOption(spec);
|
|
110137
|
-
function
|
|
110326
|
+
function make(name, fields2, schemaName) {
|
|
110138
110327
|
const resolvedSchemaName = arguments.length >= 3 ? schemaName : "public";
|
|
110139
110328
|
return makeTable(name, fields2, [], name, "schema", resolvedSchemaName, arguments.length >= 3 ? "explicit" : "default");
|
|
110140
110329
|
}
|
|
110141
|
-
var schema3 = (schemaName) =>
|
|
110142
|
-
schemaName,
|
|
110143
|
-
|
|
110144
|
-
|
|
110330
|
+
var schema3 = (schemaName) => {
|
|
110331
|
+
const table = (name, fields2, ...options2) => applyDeclaredOptions(makeTable(name, fields2, [], name, "schema", schemaName, "explicit"), options2);
|
|
110332
|
+
return {
|
|
110333
|
+
schemaName,
|
|
110334
|
+
table
|
|
110335
|
+
};
|
|
110336
|
+
};
|
|
110145
110337
|
var alias = (table, aliasName) => {
|
|
110146
110338
|
const state = table[TypeId4];
|
|
110147
110339
|
const columns = Object.fromEntries(Object.entries(state.fields).map(([key, column]) => [key, bindColumn(aliasName, key, column, state.baseName, state.schemaName)]));
|
|
110148
110340
|
const aliased = attachPipe2(Object.create(TableProto));
|
|
110149
110341
|
aliased.name = aliasName;
|
|
110150
110342
|
aliased.columns = columns;
|
|
110151
|
-
aliased
|
|
110343
|
+
defineSchemasGetter(aliased);
|
|
110152
110344
|
aliased[TypeId4] = {
|
|
110153
110345
|
name: aliasName,
|
|
110154
110346
|
baseName: state.baseName,
|
|
@@ -110189,7 +110381,7 @@ function Class(name, schemaName) {
|
|
|
110189
110381
|
return ensureClassArtifacts(this).columns;
|
|
110190
110382
|
}
|
|
110191
110383
|
static get schemas() {
|
|
110192
|
-
return
|
|
110384
|
+
return schemasFor(this);
|
|
110193
110385
|
}
|
|
110194
110386
|
static get [TypeId4]() {
|
|
110195
110387
|
const declaredOptions = extractDeclaredOptions(this[options]);
|
|
@@ -110259,12 +110451,315 @@ var foreignKey2 = (columns, target, referencedColumns) => makeOption({
|
|
|
110259
110451
|
knownColumns: Object.keys(target()[TypeId4].fields)
|
|
110260
110452
|
})
|
|
110261
110453
|
});
|
|
110262
|
-
var
|
|
110454
|
+
var check2 = (name, predicate) => makeOption({
|
|
110263
110455
|
kind: "check",
|
|
110264
110456
|
name,
|
|
110265
110457
|
predicate
|
|
110266
110458
|
});
|
|
110267
110459
|
|
|
110460
|
+
// src/internal/runtime/normalize.ts
|
|
110461
|
+
var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
110462
|
+
var isPlainRecord = (value) => {
|
|
110463
|
+
if (!isRecord2(value)) {
|
|
110464
|
+
return false;
|
|
110465
|
+
}
|
|
110466
|
+
const prototype = Object.getPrototypeOf(value);
|
|
110467
|
+
return prototype === Object.prototype || prototype === null;
|
|
110468
|
+
};
|
|
110469
|
+
var pad = (value, width = 2) => value.toString().padStart(width, "0");
|
|
110470
|
+
var formatLocalDate = (value) => `${pad(value.getUTCFullYear(), 4)}-${pad(value.getUTCMonth() + 1)}-${pad(value.getUTCDate())}`;
|
|
110471
|
+
var formatLocalTime = (value) => {
|
|
110472
|
+
const milliseconds = value.getUTCMilliseconds();
|
|
110473
|
+
const base = `${pad(value.getUTCHours())}:${pad(value.getUTCMinutes())}:${pad(value.getUTCSeconds())}`;
|
|
110474
|
+
return milliseconds === 0 ? base : `${base}.${pad(milliseconds, 3)}`;
|
|
110475
|
+
};
|
|
110476
|
+
var formatLocalDateTime = (value) => {
|
|
110477
|
+
const milliseconds = value.getUTCMilliseconds();
|
|
110478
|
+
const base = `${formatLocalDate(value)}T${pad(value.getUTCHours())}:${pad(value.getUTCMinutes())}:${pad(value.getUTCSeconds())}`;
|
|
110479
|
+
return milliseconds === 0 ? base : `${base}.${pad(milliseconds, 3)}`;
|
|
110480
|
+
};
|
|
110481
|
+
var runtimeTagOfBaseDbType = (dbType) => {
|
|
110482
|
+
return dbType.runtime;
|
|
110483
|
+
};
|
|
110484
|
+
var expectString = (value, label) => {
|
|
110485
|
+
if (typeof value === "string") {
|
|
110486
|
+
return value;
|
|
110487
|
+
}
|
|
110488
|
+
throw new Error(`Expected ${label} as string`);
|
|
110489
|
+
};
|
|
110490
|
+
var finiteNumberStringPattern = /^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:[eE][+-]?\d+)?$/;
|
|
110491
|
+
var normalizeNumber = (value) => {
|
|
110492
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
110493
|
+
return value;
|
|
110494
|
+
}
|
|
110495
|
+
if (typeof value === "string") {
|
|
110496
|
+
const trimmed = value.trim();
|
|
110497
|
+
const parsed = finiteNumberStringPattern.test(trimmed) ? Number(trimmed) : Number.NaN;
|
|
110498
|
+
if (Number.isFinite(parsed)) {
|
|
110499
|
+
return parsed;
|
|
110500
|
+
}
|
|
110501
|
+
}
|
|
110502
|
+
if (typeof value === "bigint" && Number.isSafeInteger(Number(value))) {
|
|
110503
|
+
return Number(value);
|
|
110504
|
+
}
|
|
110505
|
+
throw new Error("Expected a finite numeric value");
|
|
110506
|
+
};
|
|
110507
|
+
var normalizeBoolean = (value) => {
|
|
110508
|
+
if (typeof value === "boolean") {
|
|
110509
|
+
return value;
|
|
110510
|
+
}
|
|
110511
|
+
if (typeof value === "number") {
|
|
110512
|
+
if (value === 1) {
|
|
110513
|
+
return true;
|
|
110514
|
+
}
|
|
110515
|
+
if (value === 0) {
|
|
110516
|
+
return false;
|
|
110517
|
+
}
|
|
110518
|
+
}
|
|
110519
|
+
if (typeof value === "string") {
|
|
110520
|
+
const normalized = value.trim().toLowerCase();
|
|
110521
|
+
if (normalized === "true" || normalized === "t" || normalized === "1") {
|
|
110522
|
+
return true;
|
|
110523
|
+
}
|
|
110524
|
+
if (normalized === "false" || normalized === "f" || normalized === "0") {
|
|
110525
|
+
return false;
|
|
110526
|
+
}
|
|
110527
|
+
}
|
|
110528
|
+
throw new Error("Expected a boolean-like value");
|
|
110529
|
+
};
|
|
110530
|
+
var normalizeBigIntString = (value) => {
|
|
110531
|
+
if (typeof value === "bigint") {
|
|
110532
|
+
return value.toString();
|
|
110533
|
+
}
|
|
110534
|
+
if (typeof value === "number" && Number.isSafeInteger(value)) {
|
|
110535
|
+
return BigInt(value).toString();
|
|
110536
|
+
}
|
|
110537
|
+
if (typeof value === "string") {
|
|
110538
|
+
return canonicalizeBigIntString(value);
|
|
110539
|
+
}
|
|
110540
|
+
throw new Error("Expected an integer-like bigint value");
|
|
110541
|
+
};
|
|
110542
|
+
var normalizeDecimalString = (value) => {
|
|
110543
|
+
if (typeof value === "string") {
|
|
110544
|
+
return canonicalizeDecimalString(value);
|
|
110545
|
+
}
|
|
110546
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
110547
|
+
const rendered = String(value);
|
|
110548
|
+
if (/[eE]/.test(rendered)) {
|
|
110549
|
+
throw new Error("Scientific notation is not a supported decimal runtime");
|
|
110550
|
+
}
|
|
110551
|
+
return canonicalizeDecimalString(rendered);
|
|
110552
|
+
}
|
|
110553
|
+
throw new Error("Expected a decimal-like value");
|
|
110554
|
+
};
|
|
110555
|
+
var normalizeLocalDate = (value) => {
|
|
110556
|
+
if (value instanceof Date) {
|
|
110557
|
+
return formatLocalDate(value);
|
|
110558
|
+
}
|
|
110559
|
+
const raw = expectString(value, "local date").trim();
|
|
110560
|
+
if (isValidLocalDateString(raw)) {
|
|
110561
|
+
return raw;
|
|
110562
|
+
}
|
|
110563
|
+
const canonicalInstant = raw.replace(" ", "T").replace(/z$/, "Z");
|
|
110564
|
+
if (isValidInstantString(canonicalInstant)) {
|
|
110565
|
+
const parsed = new Date(canonicalInstant);
|
|
110566
|
+
if (!Number.isNaN(parsed.getTime())) {
|
|
110567
|
+
return formatLocalDate(parsed);
|
|
110568
|
+
}
|
|
110569
|
+
}
|
|
110570
|
+
throw new Error("Expected a local-date value");
|
|
110571
|
+
};
|
|
110572
|
+
var normalizeLocalTime = (value) => {
|
|
110573
|
+
if (value instanceof Date) {
|
|
110574
|
+
return formatLocalTime(value);
|
|
110575
|
+
}
|
|
110576
|
+
const raw = expectString(value, "local time").trim();
|
|
110577
|
+
if (isValidLocalTimeString(raw)) {
|
|
110578
|
+
return raw;
|
|
110579
|
+
}
|
|
110580
|
+
throw new Error("Expected a local-time value");
|
|
110581
|
+
};
|
|
110582
|
+
var normalizeOffsetTime = (value) => {
|
|
110583
|
+
if (value instanceof Date) {
|
|
110584
|
+
return `${formatLocalTime(value)}Z`;
|
|
110585
|
+
}
|
|
110586
|
+
const raw = expectString(value, "offset time").trim();
|
|
110587
|
+
if (isValidOffsetTimeString(raw)) {
|
|
110588
|
+
return raw;
|
|
110589
|
+
}
|
|
110590
|
+
throw new Error("Expected an offset-time value");
|
|
110591
|
+
};
|
|
110592
|
+
var normalizeLocalDateTime = (value) => {
|
|
110593
|
+
if (value instanceof Date) {
|
|
110594
|
+
return formatLocalDateTime(value);
|
|
110595
|
+
}
|
|
110596
|
+
const raw = expectString(value, "local datetime").trim();
|
|
110597
|
+
const canonicalLocalDateTime = raw.replace(" ", "T");
|
|
110598
|
+
if (isValidLocalDateTimeString(canonicalLocalDateTime)) {
|
|
110599
|
+
return canonicalLocalDateTime;
|
|
110600
|
+
}
|
|
110601
|
+
const canonicalInstant = raw.replace(" ", "T").replace(/z$/, "Z");
|
|
110602
|
+
if (isValidInstantString(canonicalInstant)) {
|
|
110603
|
+
const parsed = new Date(canonicalInstant);
|
|
110604
|
+
if (!Number.isNaN(parsed.getTime())) {
|
|
110605
|
+
return formatLocalDateTime(parsed);
|
|
110606
|
+
}
|
|
110607
|
+
}
|
|
110608
|
+
throw new Error("Expected a local-datetime value");
|
|
110609
|
+
};
|
|
110610
|
+
var normalizeInstant = (value) => {
|
|
110611
|
+
if (value instanceof Date) {
|
|
110612
|
+
return value.toISOString();
|
|
110613
|
+
}
|
|
110614
|
+
const raw = expectString(value, "instant").trim();
|
|
110615
|
+
if (!/[zZ]|[+-]\d{2}:\d{2}$/.test(raw)) {
|
|
110616
|
+
throw new Error("Instant values require a timezone offset");
|
|
110617
|
+
}
|
|
110618
|
+
const canonicalInstant = raw.replace(" ", "T").replace(/z$/, "Z");
|
|
110619
|
+
if (!isValidInstantString(canonicalInstant)) {
|
|
110620
|
+
throw new Error("Expected an ISO instant value");
|
|
110621
|
+
}
|
|
110622
|
+
const parsed = new Date(canonicalInstant);
|
|
110623
|
+
if (Number.isNaN(parsed.getTime())) {
|
|
110624
|
+
throw new Error("Expected an ISO instant value");
|
|
110625
|
+
}
|
|
110626
|
+
return parsed.toISOString();
|
|
110627
|
+
};
|
|
110628
|
+
var normalizeYear = (value) => {
|
|
110629
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 9999) {
|
|
110630
|
+
return pad(value, 4);
|
|
110631
|
+
}
|
|
110632
|
+
const raw = expectString(value, "year").trim();
|
|
110633
|
+
if (/^\d{4}$/.test(raw)) {
|
|
110634
|
+
return raw;
|
|
110635
|
+
}
|
|
110636
|
+
throw new Error("Expected a four-digit year");
|
|
110637
|
+
};
|
|
110638
|
+
var normalizeBytes = (value) => {
|
|
110639
|
+
if (value instanceof Uint8Array) {
|
|
110640
|
+
return new Uint8Array(value);
|
|
110641
|
+
}
|
|
110642
|
+
const BufferConstructor = globalThis.Buffer;
|
|
110643
|
+
if (BufferConstructor !== undefined && value instanceof BufferConstructor) {
|
|
110644
|
+
return new Uint8Array(value);
|
|
110645
|
+
}
|
|
110646
|
+
throw new Error("Expected a byte array value");
|
|
110647
|
+
};
|
|
110648
|
+
var isJsonValue = (value) => {
|
|
110649
|
+
if (value === null) {
|
|
110650
|
+
return true;
|
|
110651
|
+
}
|
|
110652
|
+
switch (typeof value) {
|
|
110653
|
+
case "string":
|
|
110654
|
+
case "boolean":
|
|
110655
|
+
return true;
|
|
110656
|
+
case "number":
|
|
110657
|
+
return Number.isFinite(value);
|
|
110658
|
+
case "object":
|
|
110659
|
+
if (Array.isArray(value)) {
|
|
110660
|
+
return value.every(isJsonValue);
|
|
110661
|
+
}
|
|
110662
|
+
return isPlainRecord(value) && Object.values(value).every(isJsonValue);
|
|
110663
|
+
default:
|
|
110664
|
+
return false;
|
|
110665
|
+
}
|
|
110666
|
+
};
|
|
110667
|
+
var normalizeJson = (value) => {
|
|
110668
|
+
if (typeof value === "string") {
|
|
110669
|
+
try {
|
|
110670
|
+
const parsed = JSON.parse(value);
|
|
110671
|
+
if (isJsonValue(parsed)) {
|
|
110672
|
+
return parsed;
|
|
110673
|
+
}
|
|
110674
|
+
throw new Error("Parsed JSON value is not a valid JSON runtime");
|
|
110675
|
+
} catch (error) {
|
|
110676
|
+
if (error instanceof SyntaxError) {
|
|
110677
|
+
return value;
|
|
110678
|
+
}
|
|
110679
|
+
throw error;
|
|
110680
|
+
}
|
|
110681
|
+
}
|
|
110682
|
+
if (isJsonValue(value)) {
|
|
110683
|
+
return value;
|
|
110684
|
+
}
|
|
110685
|
+
throw new Error("Expected a JSON value");
|
|
110686
|
+
};
|
|
110687
|
+
var normalizeDbValue = (dbType, value) => {
|
|
110688
|
+
if (value === null) {
|
|
110689
|
+
return null;
|
|
110690
|
+
}
|
|
110691
|
+
if ("base" in dbType) {
|
|
110692
|
+
return normalizeDbValue(dbType.base, value);
|
|
110693
|
+
}
|
|
110694
|
+
if ("element" in dbType) {
|
|
110695
|
+
if (!Array.isArray(value)) {
|
|
110696
|
+
throw new Error("Expected an array value");
|
|
110697
|
+
}
|
|
110698
|
+
return value.map((entry) => normalizeDbValue(dbType.element, entry));
|
|
110699
|
+
}
|
|
110700
|
+
if ("fields" in dbType) {
|
|
110701
|
+
if (!isRecord2(value)) {
|
|
110702
|
+
throw new Error("Expected a record value");
|
|
110703
|
+
}
|
|
110704
|
+
const normalized = {};
|
|
110705
|
+
for (const [key, fieldDbType] of Object.entries(dbType.fields)) {
|
|
110706
|
+
if (key in value) {
|
|
110707
|
+
normalized[key] = normalizeDbValue(fieldDbType, value[key]);
|
|
110708
|
+
}
|
|
110709
|
+
}
|
|
110710
|
+
return normalized;
|
|
110711
|
+
}
|
|
110712
|
+
if ("variant" in dbType && dbType.variant === "json") {
|
|
110713
|
+
return normalizeJson(value);
|
|
110714
|
+
}
|
|
110715
|
+
if ("variant" in dbType && (dbType.variant === "enum" || dbType.variant === "set")) {
|
|
110716
|
+
return expectString(value, "text");
|
|
110717
|
+
}
|
|
110718
|
+
switch (runtimeTagOfBaseDbType(dbType)) {
|
|
110719
|
+
case "string":
|
|
110720
|
+
return expectString(value, "text");
|
|
110721
|
+
case "number":
|
|
110722
|
+
return normalizeNumber(value);
|
|
110723
|
+
case "bigintString":
|
|
110724
|
+
return normalizeBigIntString(value);
|
|
110725
|
+
case "boolean":
|
|
110726
|
+
return normalizeBoolean(value);
|
|
110727
|
+
case "json":
|
|
110728
|
+
return normalizeJson(value);
|
|
110729
|
+
case "localDate":
|
|
110730
|
+
return normalizeLocalDate(value);
|
|
110731
|
+
case "localTime":
|
|
110732
|
+
return normalizeLocalTime(value);
|
|
110733
|
+
case "offsetTime":
|
|
110734
|
+
return normalizeOffsetTime(value);
|
|
110735
|
+
case "localDateTime":
|
|
110736
|
+
return normalizeLocalDateTime(value);
|
|
110737
|
+
case "instant":
|
|
110738
|
+
return normalizeInstant(value);
|
|
110739
|
+
case "year":
|
|
110740
|
+
return normalizeYear(value);
|
|
110741
|
+
case "decimalString":
|
|
110742
|
+
return normalizeDecimalString(value);
|
|
110743
|
+
case "bytes":
|
|
110744
|
+
return normalizeBytes(value);
|
|
110745
|
+
case "array":
|
|
110746
|
+
if (!Array.isArray(value)) {
|
|
110747
|
+
throw new Error("Expected an array value");
|
|
110748
|
+
}
|
|
110749
|
+
return value;
|
|
110750
|
+
case "record":
|
|
110751
|
+
if (!isRecord2(value)) {
|
|
110752
|
+
throw new Error("Expected a record value");
|
|
110753
|
+
}
|
|
110754
|
+
return value;
|
|
110755
|
+
case "null":
|
|
110756
|
+
return null;
|
|
110757
|
+
case "unknown":
|
|
110758
|
+
case undefined:
|
|
110759
|
+
return value;
|
|
110760
|
+
}
|
|
110761
|
+
};
|
|
110762
|
+
|
|
110268
110763
|
// src/internal/query.ts
|
|
110269
110764
|
import { pipeArguments as pipeArguments3 } from "effect/Pipeable";
|
|
110270
110765
|
|
|
@@ -110301,7 +110796,18 @@ var cloneContext = (context) => ({
|
|
|
110301
110796
|
unknown: context.unknown
|
|
110302
110797
|
});
|
|
110303
110798
|
var freezeContext = (context) => context;
|
|
110304
|
-
var
|
|
110799
|
+
var columnPredicateKey = (tableName, columnName) => JSON.stringify([tableName, columnName]);
|
|
110800
|
+
var columnPredicateKeyParts = (key) => {
|
|
110801
|
+
const jsonSeparator = key.indexOf("#json:");
|
|
110802
|
+
const columnKey = jsonSeparator === -1 ? key : key.slice(0, jsonSeparator);
|
|
110803
|
+
try {
|
|
110804
|
+
const parsed = JSON.parse(columnKey);
|
|
110805
|
+
return Array.isArray(parsed) && parsed.length === 2 && typeof parsed[0] === "string" && typeof parsed[1] === "string" ? [parsed[0], parsed[1]] : undefined;
|
|
110806
|
+
} catch {
|
|
110807
|
+
return;
|
|
110808
|
+
}
|
|
110809
|
+
};
|
|
110810
|
+
var sourceNameOfKey = (key) => columnPredicateKeyParts(key)?.[0] ?? key.split(".", 1)[0] ?? key;
|
|
110305
110811
|
var addSourceName = (context, key) => {
|
|
110306
110812
|
context.sourceNames.add(sourceNameOfKey(key));
|
|
110307
110813
|
};
|
|
@@ -110551,9 +111057,10 @@ var analyzeFormula = (formula) => freezeContext(analyzeStack(emptyContext(), [{
|
|
|
110551
111057
|
var astOf = (value) => value[TypeId2];
|
|
110552
111058
|
var columnKeyOfExpression = (value) => {
|
|
110553
111059
|
const ast = astOf(value);
|
|
110554
|
-
return ast.kind === "column" ?
|
|
111060
|
+
return ast.kind === "column" ? columnPredicateKey(ast.tableName, ast.columnName) : undefined;
|
|
110555
111061
|
};
|
|
110556
111062
|
var sameDbType = (left, right) => left.dialect === right.dialect && left.kind === right.kind;
|
|
111063
|
+
var escapeJsonPathPredicateKeySegment = (value) => value.replaceAll("\\", "\\\\").replaceAll(".", "\\.");
|
|
110557
111064
|
var jsonPathPredicateKeyOfExpression = (value) => {
|
|
110558
111065
|
const ast = astOf(value);
|
|
110559
111066
|
switch (ast.kind) {
|
|
@@ -110577,7 +111084,7 @@ var jsonPathPredicateKeyOfExpression = (value) => {
|
|
|
110577
111084
|
return;
|
|
110578
111085
|
}
|
|
110579
111086
|
const baseKey = columnKeyOfExpression(jsonAst.base);
|
|
110580
|
-
return baseKey === undefined ? undefined : `${baseKey}#json:${path.join(".")}`;
|
|
111087
|
+
return baseKey === undefined ? undefined : `${baseKey}#json:${path.map(escapeJsonPathPredicateKeySegment).join(".")}`;
|
|
110581
111088
|
}
|
|
110582
111089
|
default:
|
|
110583
111090
|
return;
|
|
@@ -110993,7 +111500,7 @@ var collectPresenceWitnesses = (selection, output) => {
|
|
|
110993
111500
|
const expression = selection;
|
|
110994
111501
|
const ast = expression[TypeId2];
|
|
110995
111502
|
if (ast.kind === "column" && expression[TypeId].nullability === "never") {
|
|
110996
|
-
output.add(
|
|
111503
|
+
output.add(columnPredicateKey(ast.tableName, ast.columnName));
|
|
110997
111504
|
}
|
|
110998
111505
|
return;
|
|
110999
111506
|
}
|
|
@@ -111142,6 +111649,20 @@ var path = (...segments) => ({
|
|
|
111142
111649
|
});
|
|
111143
111650
|
|
|
111144
111651
|
// src/internal/grouping-key.ts
|
|
111652
|
+
var subqueryPlanIds = new WeakMap;
|
|
111653
|
+
var nextSubqueryPlanId = 0;
|
|
111654
|
+
var subqueryPlanGroupingKey = (plan) => {
|
|
111655
|
+
if (plan === null || typeof plan !== "object") {
|
|
111656
|
+
return "unknown";
|
|
111657
|
+
}
|
|
111658
|
+
const existing = subqueryPlanIds.get(plan);
|
|
111659
|
+
if (existing !== undefined) {
|
|
111660
|
+
return existing;
|
|
111661
|
+
}
|
|
111662
|
+
const next = `${nextSubqueryPlanId++}`;
|
|
111663
|
+
subqueryPlanIds.set(plan, next);
|
|
111664
|
+
return next;
|
|
111665
|
+
};
|
|
111145
111666
|
var literalGroupingKey = (value) => {
|
|
111146
111667
|
if (value instanceof Date) {
|
|
111147
111668
|
return `date:${value.toISOString()}`;
|
|
@@ -111160,15 +111681,62 @@ var literalGroupingKey = (value) => {
|
|
|
111160
111681
|
return `literal:${JSON.stringify(value)}`;
|
|
111161
111682
|
}
|
|
111162
111683
|
};
|
|
111684
|
+
var isExpression = (value) => value !== null && typeof value === "object" && (TypeId in value);
|
|
111685
|
+
var expressionGroupingKey = (value) => isExpression(value) ? groupingKeyOfExpression(value) : "missing";
|
|
111686
|
+
var escapeGroupingText = (value) => value.replace(/\\/g, "\\\\").replace(/,/g, "\\,").replace(/\|/g, "\\|").replace(/=/g, "\\=").replace(/>/g, "\\>");
|
|
111687
|
+
var jsonSegmentGroupingKey = (segment) => {
|
|
111688
|
+
if (segment !== null && typeof segment === "object" && "kind" in segment) {
|
|
111689
|
+
switch (segment.kind) {
|
|
111690
|
+
case "key":
|
|
111691
|
+
return `key:${escapeGroupingText(segment.key)}`;
|
|
111692
|
+
case "index":
|
|
111693
|
+
return `index:${segment.index}`;
|
|
111694
|
+
case "wildcard":
|
|
111695
|
+
return "wildcard";
|
|
111696
|
+
case "slice": {
|
|
111697
|
+
const slice2 = segment;
|
|
111698
|
+
return `slice:${slice2.start ?? ""}:${slice2.end ?? ""}`;
|
|
111699
|
+
}
|
|
111700
|
+
case "descend":
|
|
111701
|
+
return "descend";
|
|
111702
|
+
}
|
|
111703
|
+
}
|
|
111704
|
+
if (typeof segment === "string") {
|
|
111705
|
+
return `key:${escapeGroupingText(segment)}`;
|
|
111706
|
+
}
|
|
111707
|
+
if (typeof segment === "number") {
|
|
111708
|
+
return `index:${segment}`;
|
|
111709
|
+
}
|
|
111710
|
+
return "unknown";
|
|
111711
|
+
};
|
|
111712
|
+
var jsonPathGroupingKey = (segments) => (segments ?? []).map(jsonSegmentGroupingKey).join(",");
|
|
111713
|
+
var isJsonPath = (value) => value !== null && typeof value === "object" && (TypeId6 in value);
|
|
111714
|
+
var jsonOpaquePathGroupingKey = (value) => {
|
|
111715
|
+
if (isJsonPath(value)) {
|
|
111716
|
+
return `jsonpath:${jsonPathGroupingKey(value.segments)}`;
|
|
111717
|
+
}
|
|
111718
|
+
if (typeof value === "string") {
|
|
111719
|
+
return `jsonpath:${escapeGroupingText(value)}`;
|
|
111720
|
+
}
|
|
111721
|
+
if (isExpression(value)) {
|
|
111722
|
+
return `jsonpath:${groupingKeyOfExpression(value)}`;
|
|
111723
|
+
}
|
|
111724
|
+
return "jsonpath:unknown";
|
|
111725
|
+
};
|
|
111726
|
+
var jsonEntryGroupingKey = (entry) => `${escapeGroupingText(entry.key)}=>${groupingKeyOfExpression(entry.value)}`;
|
|
111163
111727
|
var groupingKeyOfExpression = (expression) => {
|
|
111164
111728
|
const ast = expression[TypeId2];
|
|
111165
111729
|
switch (ast.kind) {
|
|
111166
111730
|
case "column":
|
|
111167
|
-
return `column:${ast.tableName
|
|
111731
|
+
return `column:${columnPredicateKey(ast.tableName, ast.columnName)}`;
|
|
111168
111732
|
case "literal":
|
|
111169
111733
|
return `literal:${literalGroupingKey(ast.value)}`;
|
|
111170
111734
|
case "cast":
|
|
111171
111735
|
return `cast(${groupingKeyOfExpression(ast.value)} as ${ast.target.dialect}:${ast.target.kind})`;
|
|
111736
|
+
case "collate":
|
|
111737
|
+
return `collate(${groupingKeyOfExpression(ast.value)},${ast.collation.map(escapeGroupingText).join(",")})`;
|
|
111738
|
+
case "function":
|
|
111739
|
+
return `function(${escapeGroupingText(ast.name)},${ast.args.map(groupingKeyOfExpression).join(",")})`;
|
|
111172
111740
|
case "isNull":
|
|
111173
111741
|
case "isNotNull":
|
|
111174
111742
|
case "not":
|
|
@@ -111186,8 +111754,15 @@ var groupingKeyOfExpression = (expression) => {
|
|
|
111186
111754
|
case "gte":
|
|
111187
111755
|
case "like":
|
|
111188
111756
|
case "ilike":
|
|
111757
|
+
case "regexMatch":
|
|
111758
|
+
case "regexIMatch":
|
|
111759
|
+
case "regexNotMatch":
|
|
111760
|
+
case "regexNotIMatch":
|
|
111189
111761
|
case "isDistinctFrom":
|
|
111190
111762
|
case "isNotDistinctFrom":
|
|
111763
|
+
case "contains":
|
|
111764
|
+
case "containedBy":
|
|
111765
|
+
case "overlaps":
|
|
111191
111766
|
return `${ast.kind}(${groupingKeyOfExpression(ast.left)},${groupingKeyOfExpression(ast.right)})`;
|
|
111192
111767
|
case "and":
|
|
111193
111768
|
case "or":
|
|
@@ -111199,6 +111774,54 @@ var groupingKeyOfExpression = (expression) => {
|
|
|
111199
111774
|
return `${ast.kind}(${ast.values.map(groupingKeyOfExpression).join(",")})`;
|
|
111200
111775
|
case "case":
|
|
111201
111776
|
return `case(${ast.branches.map((branch) => `when:${groupingKeyOfExpression(branch.when)}=>${groupingKeyOfExpression(branch.then)}`).join("|")};else:${groupingKeyOfExpression(ast.else)})`;
|
|
111777
|
+
case "exists":
|
|
111778
|
+
return `exists(${subqueryPlanGroupingKey(ast.plan)})`;
|
|
111779
|
+
case "scalarSubquery":
|
|
111780
|
+
return `scalarSubquery(${subqueryPlanGroupingKey(ast.plan)})`;
|
|
111781
|
+
case "inSubquery":
|
|
111782
|
+
return `inSubquery(${groupingKeyOfExpression(ast.left)},${subqueryPlanGroupingKey(ast.plan)})`;
|
|
111783
|
+
case "comparisonAny":
|
|
111784
|
+
case "comparisonAll":
|
|
111785
|
+
return `${ast.kind}(${ast.operator},${groupingKeyOfExpression(ast.left)},${subqueryPlanGroupingKey(ast.plan)})`;
|
|
111786
|
+
case "jsonGet":
|
|
111787
|
+
case "jsonPath":
|
|
111788
|
+
case "jsonAccess":
|
|
111789
|
+
case "jsonTraverse":
|
|
111790
|
+
case "jsonGetText":
|
|
111791
|
+
case "jsonPathText":
|
|
111792
|
+
case "jsonAccessText":
|
|
111793
|
+
case "jsonTraverseText":
|
|
111794
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.base)},${jsonPathGroupingKey(ast.segments)})`;
|
|
111795
|
+
case "jsonHasKey":
|
|
111796
|
+
case "jsonKeyExists":
|
|
111797
|
+
case "jsonHasAnyKeys":
|
|
111798
|
+
case "jsonHasAllKeys":
|
|
111799
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.base)},${(ast.keys ?? []).map(escapeGroupingText).join(",")})`;
|
|
111800
|
+
case "jsonConcat":
|
|
111801
|
+
case "jsonMerge":
|
|
111802
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.left)},${expressionGroupingKey(ast.right)},)`;
|
|
111803
|
+
case "jsonDelete":
|
|
111804
|
+
case "jsonDeletePath":
|
|
111805
|
+
case "jsonRemove":
|
|
111806
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.base)},${expressionGroupingKey(undefined)},${jsonPathGroupingKey(ast.segments)})`;
|
|
111807
|
+
case "jsonSet":
|
|
111808
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.base)},${expressionGroupingKey(ast.newValue)},${jsonPathGroupingKey(ast.segments)})`;
|
|
111809
|
+
case "jsonInsert":
|
|
111810
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.base)},${expressionGroupingKey(ast.insert)},${jsonPathGroupingKey(ast.segments)})`;
|
|
111811
|
+
case "jsonPathExists":
|
|
111812
|
+
case "jsonPathMatch":
|
|
111813
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.base)},${jsonOpaquePathGroupingKey(ast.query)})`;
|
|
111814
|
+
case "jsonBuildObject":
|
|
111815
|
+
return `json(${ast.kind},${(ast.entries ?? []).map(jsonEntryGroupingKey).join("|")})`;
|
|
111816
|
+
case "jsonBuildArray":
|
|
111817
|
+
return `json(${ast.kind},${(ast.values ?? []).map(groupingKeyOfExpression).join(",")})`;
|
|
111818
|
+
case "jsonToJson":
|
|
111819
|
+
case "jsonToJsonb":
|
|
111820
|
+
case "jsonTypeOf":
|
|
111821
|
+
case "jsonLength":
|
|
111822
|
+
case "jsonKeys":
|
|
111823
|
+
case "jsonStripNulls":
|
|
111824
|
+
return `json(${ast.kind},${expressionGroupingKey(ast.value)})`;
|
|
111202
111825
|
default:
|
|
111203
111826
|
throw new Error("Unsupported expression for grouping key generation");
|
|
111204
111827
|
}
|
|
@@ -111216,8 +111839,112 @@ var dedupeGroupedExpressions = (values) => {
|
|
|
111216
111839
|
};
|
|
111217
111840
|
|
|
111218
111841
|
// src/internal/dsl-mutation-runtime.ts
|
|
111842
|
+
var expectInsertSourceKind = (source) => {
|
|
111843
|
+
if (source !== undefined && source.kind !== "values" && source.kind !== "query" && source.kind !== "unnest") {
|
|
111844
|
+
throw new Error("Unsupported insert source kind");
|
|
111845
|
+
}
|
|
111846
|
+
return source;
|
|
111847
|
+
};
|
|
111848
|
+
var expectConflictClause = (conflict) => {
|
|
111849
|
+
if (conflict === undefined) {
|
|
111850
|
+
return conflict;
|
|
111851
|
+
}
|
|
111852
|
+
if (conflict.kind !== "conflict") {
|
|
111853
|
+
throw new Error("Unsupported conflict clause kind");
|
|
111854
|
+
}
|
|
111855
|
+
if (conflict.action !== "doNothing" && conflict.action !== "doUpdate") {
|
|
111856
|
+
throw new Error("Unsupported conflict action");
|
|
111857
|
+
}
|
|
111858
|
+
if (conflict.target !== undefined && conflict.target.kind !== "columns" && conflict.target.kind !== "constraint") {
|
|
111859
|
+
throw new Error("Unsupported conflict target kind");
|
|
111860
|
+
}
|
|
111861
|
+
return conflict;
|
|
111862
|
+
};
|
|
111219
111863
|
var makeDslMutationRuntime = (ctx) => {
|
|
111864
|
+
const aliasedSourceKinds = new Set(["derived", "cte", "lateral", "values", "unnest", "tableFunction"]);
|
|
111865
|
+
const isRecord3 = (value) => typeof value === "object" && value !== null;
|
|
111866
|
+
const isTableTarget = (target) => typeof target === "object" && target !== null && (TypeId4 in target) && (TypeId3 in target);
|
|
111867
|
+
const hasColumnRecord = (value) => isRecord3(value.columns);
|
|
111868
|
+
const isAliasedSource = (source) => {
|
|
111869
|
+
if (!isRecord3(source)) {
|
|
111870
|
+
return false;
|
|
111871
|
+
}
|
|
111872
|
+
if (isTableTarget(source)) {
|
|
111873
|
+
return true;
|
|
111874
|
+
}
|
|
111875
|
+
if (!("kind" in source) || !("name" in source) || !("baseName" in source)) {
|
|
111876
|
+
return false;
|
|
111877
|
+
}
|
|
111878
|
+
if (typeof source.kind !== "string" || !aliasedSourceKinds.has(source.kind)) {
|
|
111879
|
+
return false;
|
|
111880
|
+
}
|
|
111881
|
+
if (typeof source.name !== "string" || typeof source.baseName !== "string") {
|
|
111882
|
+
return false;
|
|
111883
|
+
}
|
|
111884
|
+
switch (source.kind) {
|
|
111885
|
+
case "derived":
|
|
111886
|
+
case "cte":
|
|
111887
|
+
case "lateral":
|
|
111888
|
+
return isRecord3(source.plan) && TypeId3 in source.plan && hasColumnRecord(source);
|
|
111889
|
+
case "values":
|
|
111890
|
+
return Array.isArray(source.rows) && hasColumnRecord(source);
|
|
111891
|
+
case "unnest":
|
|
111892
|
+
return isRecord3(source.arrays) && hasColumnRecord(source);
|
|
111893
|
+
case "tableFunction":
|
|
111894
|
+
return typeof source.functionName === "string" && Array.isArray(source.args) && hasColumnRecord(source);
|
|
111895
|
+
}
|
|
111896
|
+
return false;
|
|
111897
|
+
};
|
|
111898
|
+
const assertMutationTarget = (target, apiName) => {
|
|
111899
|
+
if (!isTableTarget(target)) {
|
|
111900
|
+
throw new Error(`${apiName}(...) requires table targets`);
|
|
111901
|
+
}
|
|
111902
|
+
};
|
|
111903
|
+
const assertAliasedSource = (source, apiName) => {
|
|
111904
|
+
if (!isAliasedSource(source)) {
|
|
111905
|
+
throw new Error(`${apiName}(...) requires an aliased source`);
|
|
111906
|
+
}
|
|
111907
|
+
};
|
|
111908
|
+
const assertMutationTargets = (target, apiName, options2 = {}) => {
|
|
111909
|
+
const targets = Array.isArray(target) ? target : [target];
|
|
111910
|
+
if (targets.length === 0) {
|
|
111911
|
+
throw new Error(`${apiName}(...) requires at least one table target`);
|
|
111912
|
+
}
|
|
111913
|
+
if (Array.isArray(target) && targets.length === 1) {
|
|
111914
|
+
throw new Error(`${apiName}(...) requires a table target, not a single-element target tuple`);
|
|
111915
|
+
}
|
|
111916
|
+
for (const entry of targets) {
|
|
111917
|
+
assertMutationTarget(entry, apiName);
|
|
111918
|
+
}
|
|
111919
|
+
if (targets.length > 1 && options2.allowMultiple !== true) {
|
|
111920
|
+
throw new Error(`${apiName}(...) requires a single table target`);
|
|
111921
|
+
}
|
|
111922
|
+
if (targets.length > 1 && ctx.profile.dialect !== "mysql" && ctx.profile.dialect !== "sqlite") {
|
|
111923
|
+
throw new Error(`${apiName}(...) only supports multiple mutation targets for mysql`);
|
|
111924
|
+
}
|
|
111925
|
+
};
|
|
111926
|
+
const assertUniqueTargetNames = (targets) => {
|
|
111927
|
+
const seen = new Set;
|
|
111928
|
+
for (const target of targets) {
|
|
111929
|
+
if (seen.has(target.tableName)) {
|
|
111930
|
+
throw new Error(`mutation target source names must be unique: ${target.tableName}`);
|
|
111931
|
+
}
|
|
111932
|
+
seen.add(target.tableName);
|
|
111933
|
+
}
|
|
111934
|
+
};
|
|
111935
|
+
const assertInsertSelectSource = (sourcePlan, selection) => {
|
|
111936
|
+
const statement = ctx.getQueryState(sourcePlan).statement;
|
|
111937
|
+
if (statement !== "select" && statement !== "set") {
|
|
111938
|
+
throw new Error("insert sources only accept select-like query plans");
|
|
111939
|
+
}
|
|
111940
|
+
for (const value of Object.values(selection)) {
|
|
111941
|
+
if (value === null || typeof value !== "object" || !(TypeId in value)) {
|
|
111942
|
+
throw new Error("insert sources require a flat selection object");
|
|
111943
|
+
}
|
|
111944
|
+
}
|
|
111945
|
+
};
|
|
111220
111946
|
const insert = (target, values) => {
|
|
111947
|
+
assertMutationTargets(target, "insert");
|
|
111221
111948
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
111222
111949
|
const assignments = values === undefined ? [] : ctx.buildMutationAssignments(target, values);
|
|
111223
111950
|
const required = assignments.flatMap((entry) => Object.keys(entry.value[TypeId].dependencies));
|
|
@@ -111293,10 +112020,11 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111293
112020
|
}
|
|
111294
112021
|
const sourcePlan = source;
|
|
111295
112022
|
const selection = sourcePlan[TypeId3].selection;
|
|
112023
|
+
assertInsertSelectSource(sourcePlan, selection);
|
|
111296
112024
|
const columns = ctx.normalizeInsertSelectColumns(selection);
|
|
111297
112025
|
return ctx.makePlan({
|
|
111298
112026
|
selection: current.selection,
|
|
111299
|
-
required: ctx.currentRequiredList(sourcePlan[TypeId3].required)
|
|
112027
|
+
required: ctx.currentRequiredList(sourcePlan[TypeId3].required),
|
|
111300
112028
|
available: current.available,
|
|
111301
112029
|
dialect: current.dialect
|
|
111302
112030
|
}, {
|
|
@@ -111313,14 +112041,22 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111313
112041
|
const current = plan[TypeId3];
|
|
111314
112042
|
const currentAst = ctx.getAst(plan);
|
|
111315
112043
|
const currentQuery = ctx.getQueryState(plan);
|
|
112044
|
+
if (currentQuery.statement !== "insert") {
|
|
112045
|
+
throw new Error(`onConflict(...) is not supported for ${currentQuery.statement} statements`);
|
|
112046
|
+
}
|
|
111316
112047
|
const insertTarget = currentAst.into.source;
|
|
111317
112048
|
const conflictTarget = ctx.buildConflictTarget(insertTarget, target);
|
|
111318
112049
|
const updateAssignments = options2.update ? ctx.buildMutationAssignments(insertTarget, options2.update) : [];
|
|
112050
|
+
if (options2.update !== undefined && updateAssignments.length === 0) {
|
|
112051
|
+
throw new Error("conflict update assignments require at least one assignment");
|
|
112052
|
+
}
|
|
111319
112053
|
const updateWhere = options2.where === undefined ? undefined : ctx.toDialectExpression(options2.where);
|
|
112054
|
+
const targetWhere = conflictTarget.kind === "columns" ? conflictTarget.where : undefined;
|
|
111320
112055
|
const required = [
|
|
111321
112056
|
...ctx.currentRequiredList(current.required),
|
|
111322
112057
|
...updateAssignments.flatMap((entry) => Object.keys(entry.value[TypeId].dependencies)),
|
|
111323
|
-
...updateWhere ? Object.keys(updateWhere[TypeId].dependencies) : []
|
|
112058
|
+
...updateWhere ? Object.keys(updateWhere[TypeId].dependencies) : [],
|
|
112059
|
+
...targetWhere ? Object.keys(targetWhere[TypeId].dependencies) : []
|
|
111324
112060
|
].filter((name, index4, list) => !(name in current.available) && list.indexOf(name) === index4);
|
|
111325
112061
|
return ctx.makePlan({
|
|
111326
112062
|
selection: current.selection,
|
|
@@ -111339,7 +112075,9 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111339
112075
|
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, currentQuery.insertSource);
|
|
111340
112076
|
};
|
|
111341
112077
|
const update = (target, values) => {
|
|
112078
|
+
assertMutationTargets(target, "update", { allowMultiple: true });
|
|
111342
112079
|
const targets = ctx.mutationTargetClauses(target);
|
|
112080
|
+
assertUniqueTargetNames(targets);
|
|
111343
112081
|
const primaryTarget = targets[0];
|
|
111344
112082
|
const assignments = ctx.buildMutationAssignments(target, values);
|
|
111345
112083
|
const targetNames = new Set(targets.map((entry) => entry.tableName));
|
|
@@ -111363,9 +112101,13 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111363
112101
|
}, undefined, "write", "update");
|
|
111364
112102
|
};
|
|
111365
112103
|
const upsert = (target, values, conflictColumns, updateValues) => {
|
|
112104
|
+
assertMutationTargets(target, "upsert");
|
|
111366
112105
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
111367
112106
|
const assignments = ctx.buildMutationAssignments(target, values);
|
|
111368
112107
|
const updateAssignments = updateValues ? ctx.buildMutationAssignments(target, updateValues) : [];
|
|
112108
|
+
if (updateValues !== undefined && updateAssignments.length === 0) {
|
|
112109
|
+
throw new Error("upsert update assignments require at least one assignment");
|
|
112110
|
+
}
|
|
111369
112111
|
const required = [
|
|
111370
112112
|
...assignments.flatMap((entry) => Object.keys(entry.value[TypeId].dependencies)),
|
|
111371
112113
|
...updateAssignments.flatMap((entry) => Object.keys(entry.value[TypeId].dependencies))
|
|
@@ -111395,7 +112137,7 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111395
112137
|
kind: "conflict",
|
|
111396
112138
|
target: {
|
|
111397
112139
|
kind: "columns",
|
|
111398
|
-
columns: ctx.
|
|
112140
|
+
columns: ctx.normalizeConflictColumns(target, conflictColumns)
|
|
111399
112141
|
},
|
|
111400
112142
|
action: updateAssignments.length > 0 ? "doUpdate" : "doNothing",
|
|
111401
112143
|
values: updateAssignments.length > 0 ? updateAssignments : undefined
|
|
@@ -111408,7 +112150,9 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111408
112150
|
}, undefined, "write", "insert", target, "ready");
|
|
111409
112151
|
};
|
|
111410
112152
|
const delete_ = (target) => {
|
|
112153
|
+
assertMutationTargets(target, "delete", { allowMultiple: true });
|
|
111411
112154
|
const targets = ctx.mutationTargetClauses(target);
|
|
112155
|
+
assertUniqueTargetNames(targets);
|
|
111412
112156
|
const primaryTarget = targets[0];
|
|
111413
112157
|
return ctx.makePlan({
|
|
111414
112158
|
selection: {},
|
|
@@ -111428,6 +112172,7 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111428
112172
|
}, undefined, "write", "delete");
|
|
111429
112173
|
};
|
|
111430
112174
|
const truncate = (target, options2 = {}) => {
|
|
112175
|
+
assertMutationTargets(target, "truncate");
|
|
111431
112176
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
111432
112177
|
return ctx.makePlan({
|
|
111433
112178
|
selection: {},
|
|
@@ -111456,8 +112201,13 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111456
112201
|
}, undefined, "write", "truncate");
|
|
111457
112202
|
};
|
|
111458
112203
|
const merge = (target, source, on, options2 = {}) => {
|
|
112204
|
+
assertMutationTargets(target, "merge");
|
|
112205
|
+
assertAliasedSource(source, "merge");
|
|
111459
112206
|
const { sourceName: targetName, sourceBaseName: targetBaseName } = ctx.targetSourceDetails(target);
|
|
111460
112207
|
const { sourceName: usingName, sourceBaseName: usingBaseName } = ctx.sourceDetails(source);
|
|
112208
|
+
if (targetName === usingName) {
|
|
112209
|
+
throw new Error(`merge(...) source name must differ from target source name: ${targetName}`);
|
|
112210
|
+
}
|
|
111461
112211
|
const onExpression = ctx.toDialectExpression(on);
|
|
111462
112212
|
const matched = options2.whenMatched;
|
|
111463
112213
|
const notMatched = options2.whenNotMatched;
|
|
@@ -111543,8 +112293,98 @@ var makeDslMutationRuntime = (ctx) => {
|
|
|
111543
112293
|
};
|
|
111544
112294
|
|
|
111545
112295
|
// src/internal/dsl-plan-runtime.ts
|
|
112296
|
+
var renderSelectLockMode = (mode) => {
|
|
112297
|
+
switch (mode) {
|
|
112298
|
+
case "update":
|
|
112299
|
+
return "for update";
|
|
112300
|
+
case "share":
|
|
112301
|
+
return "for share";
|
|
112302
|
+
}
|
|
112303
|
+
throw new Error("lock(...) mode must be update or share for select statements");
|
|
112304
|
+
};
|
|
112305
|
+
var renderMysqlMutationLockMode = (mode, statement) => {
|
|
112306
|
+
switch (mode) {
|
|
112307
|
+
case "lowPriority":
|
|
112308
|
+
return " low_priority";
|
|
112309
|
+
case "ignore":
|
|
112310
|
+
return " ignore";
|
|
112311
|
+
case "quick":
|
|
112312
|
+
if (statement === "delete") {
|
|
112313
|
+
return " quick";
|
|
112314
|
+
}
|
|
112315
|
+
break;
|
|
112316
|
+
}
|
|
112317
|
+
throw new Error(statement === "update" ? "lock(...) mode must be lowPriority or ignore for update statements" : "lock(...) mode must be lowPriority, quick, or ignore for delete statements");
|
|
112318
|
+
};
|
|
111546
112319
|
var makeDslPlanRuntime = (ctx) => {
|
|
112320
|
+
const aliasedSourceKinds = new Set(["derived", "cte", "lateral", "values", "unnest", "tableFunction"]);
|
|
112321
|
+
const isRecord3 = (value) => typeof value === "object" && value !== null;
|
|
112322
|
+
const isPlan = (value) => isRecord3(value) && (TypeId3 in value);
|
|
112323
|
+
const hasColumnRecord = (value) => isRecord3(value.columns);
|
|
112324
|
+
const sourceRequiredList = (source) => typeof source === "object" && source !== null && ("required" in source) ? ctx.currentRequiredList(source.required) : [];
|
|
112325
|
+
const isAliasedSource = (source) => {
|
|
112326
|
+
if (!isRecord3(source)) {
|
|
112327
|
+
return false;
|
|
112328
|
+
}
|
|
112329
|
+
if (TypeId4 in source) {
|
|
112330
|
+
return true;
|
|
112331
|
+
}
|
|
112332
|
+
if (!("kind" in source) || !("name" in source) || !("baseName" in source)) {
|
|
112333
|
+
return false;
|
|
112334
|
+
}
|
|
112335
|
+
if (typeof source.kind !== "string" || !aliasedSourceKinds.has(source.kind)) {
|
|
112336
|
+
return false;
|
|
112337
|
+
}
|
|
112338
|
+
if (typeof source.name !== "string" || typeof source.baseName !== "string") {
|
|
112339
|
+
return false;
|
|
112340
|
+
}
|
|
112341
|
+
switch (source.kind) {
|
|
112342
|
+
case "derived":
|
|
112343
|
+
case "cte":
|
|
112344
|
+
case "lateral":
|
|
112345
|
+
return isPlan(source.plan) && hasColumnRecord(source);
|
|
112346
|
+
case "values":
|
|
112347
|
+
return Array.isArray(source.rows) && hasColumnRecord(source);
|
|
112348
|
+
case "unnest":
|
|
112349
|
+
return isRecord3(source.arrays) && hasColumnRecord(source);
|
|
112350
|
+
case "tableFunction":
|
|
112351
|
+
return typeof source.functionName === "string" && Array.isArray(source.args) && hasColumnRecord(source);
|
|
112352
|
+
}
|
|
112353
|
+
return false;
|
|
112354
|
+
};
|
|
112355
|
+
const assertAliasedSource = (source, message) => {
|
|
112356
|
+
if (!isAliasedSource(source)) {
|
|
112357
|
+
throw new Error(message);
|
|
112358
|
+
}
|
|
112359
|
+
};
|
|
112360
|
+
const assertPlanComplete = (plan) => {
|
|
112361
|
+
const required = ctx.currentRequiredList(plan[TypeId3].required);
|
|
112362
|
+
if (required.length > 0) {
|
|
112363
|
+
throw new Error(`query references sources that are not yet in scope: ${required.join(", ")}`);
|
|
112364
|
+
}
|
|
112365
|
+
};
|
|
112366
|
+
const assertSourceNameAvailable = (available, sourceName) => {
|
|
112367
|
+
if (sourceName in available) {
|
|
112368
|
+
throw new Error(`query source name is already in scope: ${sourceName}`);
|
|
112369
|
+
}
|
|
112370
|
+
};
|
|
112371
|
+
const assertSelectHasBaseSourceForJoin = (statement, available) => {
|
|
112372
|
+
if (statement === "select" && Object.keys(available).length === 0) {
|
|
112373
|
+
throw new Error("select joins require a from(...) source before joining");
|
|
112374
|
+
}
|
|
112375
|
+
};
|
|
112376
|
+
const supportsJoinSources = (statement) => statement === "select" || statement === "update" || statement === "delete";
|
|
112377
|
+
const assertSetOperandStatement = (plan) => {
|
|
112378
|
+
const statement = ctx.getQueryState(plan).statement;
|
|
112379
|
+
if (statement !== "select" && statement !== "set") {
|
|
112380
|
+
throw new Error("set operator operands only accept select-like query plans");
|
|
112381
|
+
}
|
|
112382
|
+
};
|
|
111547
112383
|
const buildSetOperation = (kind, all, left, right) => {
|
|
112384
|
+
assertSetOperandStatement(left);
|
|
112385
|
+
assertSetOperandStatement(right);
|
|
112386
|
+
assertPlanComplete(left);
|
|
112387
|
+
assertPlanComplete(right);
|
|
111548
112388
|
const leftState = left[TypeId3];
|
|
111549
112389
|
const leftAst = ctx.getAst(left);
|
|
111550
112390
|
const basePlan = leftAst.kind === "set" ? leftAst.setBase ?? left : left;
|
|
@@ -111599,25 +112439,29 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111599
112439
|
if (currentQuery.statement === "insert") {
|
|
111600
112440
|
return ctx.attachInsertSource(plan, source);
|
|
111601
112441
|
}
|
|
111602
|
-
|
|
111603
|
-
|
|
112442
|
+
assertAliasedSource(source, "from(...) requires an aliased source in select/update statements");
|
|
112443
|
+
if (currentQuery.statement === "select" && currentAst.from !== undefined) {
|
|
112444
|
+
throw new Error("select statements accept only one from(...) source; use joins for additional sources");
|
|
111604
112445
|
}
|
|
111605
112446
|
const sourceLike = source;
|
|
111606
112447
|
const { sourceName, sourceBaseName } = ctx.sourceDetails(sourceLike);
|
|
111607
112448
|
const presenceWitnesses = ctx.presenceWitnessesOfSourceLike(sourceLike);
|
|
112449
|
+
const sourceRequired = sourceRequiredList(sourceLike);
|
|
112450
|
+
assertSourceNameAvailable(current.available, sourceName);
|
|
111608
112451
|
if (currentQuery.statement === "select") {
|
|
112452
|
+
const nextAvailable = {
|
|
112453
|
+
[sourceName]: {
|
|
112454
|
+
name: sourceName,
|
|
112455
|
+
mode: "required",
|
|
112456
|
+
baseName: sourceBaseName,
|
|
112457
|
+
_presentFormula: ctx.trueFormula(),
|
|
112458
|
+
_presenceWitnesses: presenceWitnesses
|
|
112459
|
+
}
|
|
112460
|
+
};
|
|
111609
112461
|
return ctx.makePlan({
|
|
111610
112462
|
selection: current.selection,
|
|
111611
|
-
required: ctx.currentRequiredList(current.required).filter((name) => name
|
|
111612
|
-
available:
|
|
111613
|
-
[sourceName]: {
|
|
111614
|
-
name: sourceName,
|
|
111615
|
-
mode: "required",
|
|
111616
|
-
baseName: sourceBaseName,
|
|
111617
|
-
_presentFormula: ctx.trueFormula(),
|
|
111618
|
-
_presenceWitnesses: presenceWitnesses
|
|
111619
|
-
}
|
|
111620
|
-
},
|
|
112463
|
+
required: [...ctx.currentRequiredList(current.required), ...sourceRequired].filter((name, index4, values) => !(name in nextAvailable) && values.indexOf(name) === index4),
|
|
112464
|
+
available: nextAvailable,
|
|
111621
112465
|
dialect: current.dialect
|
|
111622
112466
|
}, {
|
|
111623
112467
|
...currentAst,
|
|
@@ -111642,7 +112486,7 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111642
112486
|
};
|
|
111643
112487
|
return ctx.makePlan({
|
|
111644
112488
|
selection: current.selection,
|
|
111645
|
-
required: ctx.currentRequiredList(current.required).filter((name) => !(name in nextAvailable)),
|
|
112489
|
+
required: [...ctx.currentRequiredList(current.required), ...sourceRequired].filter((name, index4, values) => !(name in nextAvailable) && values.indexOf(name) === index4),
|
|
111646
112490
|
available: nextAvailable,
|
|
111647
112491
|
dialect: current.dialect
|
|
111648
112492
|
}, {
|
|
@@ -111683,8 +112527,16 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111683
112527
|
const current = plan[TypeId3];
|
|
111684
112528
|
const currentAst = ctx.getAst(plan);
|
|
111685
112529
|
const currentQuery = ctx.getQueryState(plan);
|
|
112530
|
+
if (supportsJoinSources(currentQuery.statement)) {
|
|
112531
|
+
assertAliasedSource(table, "join(...) requires an aliased source in select/update/delete statements");
|
|
112532
|
+
assertSelectHasBaseSourceForJoin(currentQuery.statement, current.available);
|
|
112533
|
+
}
|
|
111686
112534
|
const { sourceName, sourceBaseName } = ctx.sourceDetails(table);
|
|
111687
112535
|
const presenceWitnesses = ctx.presenceWitnessesOfSourceLike(table);
|
|
112536
|
+
const sourceRequired = sourceRequiredList(table);
|
|
112537
|
+
if (supportsJoinSources(currentQuery.statement)) {
|
|
112538
|
+
assertSourceNameAvailable(current.available, sourceName);
|
|
112539
|
+
}
|
|
111688
112540
|
const nextAvailable = {
|
|
111689
112541
|
...current.available,
|
|
111690
112542
|
[sourceName]: {
|
|
@@ -111697,7 +112549,7 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111697
112549
|
};
|
|
111698
112550
|
return ctx.makePlan({
|
|
111699
112551
|
selection: current.selection,
|
|
111700
|
-
required: ctx.currentRequiredList(current.required).filter((name) => !(name in nextAvailable)),
|
|
112552
|
+
required: [...ctx.currentRequiredList(current.required), ...sourceRequired].filter((name, index4, values) => !(name in nextAvailable) && values.indexOf(name) === index4),
|
|
111701
112553
|
available: nextAvailable,
|
|
111702
112554
|
dialect: current.dialect ?? table[TypeId3]?.dialect ?? table.dialect
|
|
111703
112555
|
}, {
|
|
@@ -111716,8 +112568,16 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111716
112568
|
const currentQuery = ctx.getQueryState(plan);
|
|
111717
112569
|
const onExpression = ctx.toDialectExpression(on);
|
|
111718
112570
|
const onFormula = ctx.formulaOfExpressionRuntime(onExpression);
|
|
112571
|
+
if (supportsJoinSources(currentQuery.statement)) {
|
|
112572
|
+
assertAliasedSource(table, "join(...) requires an aliased source in select/update/delete statements");
|
|
112573
|
+
assertSelectHasBaseSourceForJoin(currentQuery.statement, current.available);
|
|
112574
|
+
}
|
|
111719
112575
|
const { sourceName, sourceBaseName } = ctx.sourceDetails(table);
|
|
111720
112576
|
const presenceWitnesses = ctx.presenceWitnessesOfSourceLike(table);
|
|
112577
|
+
const sourceRequired = sourceRequiredList(table);
|
|
112578
|
+
if (supportsJoinSources(currentQuery.statement)) {
|
|
112579
|
+
assertSourceNameAvailable(current.available, sourceName);
|
|
112580
|
+
}
|
|
111721
112581
|
const baseAvailable = kind === "right" || kind === "full" ? Object.fromEntries(Object.entries(current.available).map(([name, source]) => [name, {
|
|
111722
112582
|
name: source.name,
|
|
111723
112583
|
mode: "optional",
|
|
@@ -111737,7 +112597,7 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111737
112597
|
};
|
|
111738
112598
|
return ctx.makePlan({
|
|
111739
112599
|
selection: current.selection,
|
|
111740
|
-
required: [...ctx.currentRequiredList(current.required), ...ctx.extractRequiredFromDialectInputRuntime(on)].filter((name, index4, values) => !(name in nextAvailable) && values.indexOf(name) === index4),
|
|
112600
|
+
required: [...ctx.currentRequiredList(current.required), ...sourceRequired, ...ctx.extractRequiredFromDialectInputRuntime(on)].filter((name, index4, values) => !(name in nextAvailable) && values.indexOf(name) === index4),
|
|
111741
112601
|
available: nextAvailable,
|
|
111742
112602
|
dialect: current.dialect ?? table.dialect ?? onExpression[TypeId].dialect
|
|
111743
112603
|
}, {
|
|
@@ -111752,6 +112612,9 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111752
112612
|
}, kind === "inner" ? ctx.assumeFormulaTrue(currentQuery.assumptions, onFormula) : currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement);
|
|
111753
112613
|
};
|
|
111754
112614
|
const orderBy = (value, direction = "asc") => (plan) => {
|
|
112615
|
+
if (direction !== "asc" && direction !== "desc") {
|
|
112616
|
+
throw new Error("orderBy(...) direction must be asc or desc");
|
|
112617
|
+
}
|
|
111755
112618
|
const current = plan[TypeId3];
|
|
111756
112619
|
const currentAst = ctx.getAst(plan);
|
|
111757
112620
|
const currentQuery = ctx.getQueryState(plan);
|
|
@@ -111775,6 +112638,15 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111775
112638
|
const current = plan[TypeId3];
|
|
111776
112639
|
const currentAst = ctx.getAst(plan);
|
|
111777
112640
|
const currentQuery = ctx.getQueryState(plan);
|
|
112641
|
+
if (currentQuery.statement === "select") {
|
|
112642
|
+
renderSelectLockMode(mode);
|
|
112643
|
+
}
|
|
112644
|
+
if (ctx.profile.dialect === "mysql" && currentQuery.statement === "update") {
|
|
112645
|
+
renderMysqlMutationLockMode(mode, "update");
|
|
112646
|
+
}
|
|
112647
|
+
if (ctx.profile.dialect === "mysql" && currentQuery.statement === "delete") {
|
|
112648
|
+
renderMysqlMutationLockMode(mode, "delete");
|
|
112649
|
+
}
|
|
111778
112650
|
return ctx.makePlan({
|
|
111779
112651
|
selection: current.selection,
|
|
111780
112652
|
required: current.required,
|
|
@@ -111851,18 +112723,96 @@ var makeDslPlanRuntime = (ctx) => {
|
|
|
111851
112723
|
};
|
|
111852
112724
|
};
|
|
111853
112725
|
|
|
112726
|
+
// src/internal/projection-alias.ts
|
|
112727
|
+
var TypeId7 = Symbol.for("effect-qb/ProjectionAlias");
|
|
112728
|
+
|
|
112729
|
+
// src/internal/projections.ts
|
|
112730
|
+
var aliasFromPath = (path2) => path2.join("__");
|
|
112731
|
+
var isExpression2 = (value) => typeof value === "object" && value !== null && (TypeId in value);
|
|
112732
|
+
var projectionAliasOf = (expression) => (TypeId7 in expression) ? expression[TypeId7].alias : undefined;
|
|
112733
|
+
var pathKeyOf = (path2) => JSON.stringify(path2);
|
|
112734
|
+
var formatProjectionPath = (path2) => path2.join(".");
|
|
112735
|
+
var isPrefixPath = (left, right) => left.length < right.length && left.every((segment, index4) => segment === right[index4]);
|
|
112736
|
+
var flattenSelection = (selection, path2 = []) => {
|
|
112737
|
+
const fields2 = [];
|
|
112738
|
+
for (const [key2, value] of Object.entries(selection)) {
|
|
112739
|
+
const nextPath = [...path2, key2];
|
|
112740
|
+
if (isExpression2(value)) {
|
|
112741
|
+
fields2.push({
|
|
112742
|
+
path: nextPath,
|
|
112743
|
+
expression: value,
|
|
112744
|
+
alias: projectionAliasOf(value) ?? aliasFromPath(nextPath)
|
|
112745
|
+
});
|
|
112746
|
+
continue;
|
|
112747
|
+
}
|
|
112748
|
+
fields2.push(...flattenSelection(value, nextPath));
|
|
112749
|
+
}
|
|
112750
|
+
return fields2;
|
|
112751
|
+
};
|
|
112752
|
+
var validateProjections = (projections) => {
|
|
112753
|
+
const seen = new Set;
|
|
112754
|
+
const pathKeys = new Set;
|
|
112755
|
+
for (const projection of projections) {
|
|
112756
|
+
if (seen.has(projection.alias)) {
|
|
112757
|
+
throw new Error(`Duplicate projection alias: ${projection.alias}`);
|
|
112758
|
+
}
|
|
112759
|
+
seen.add(projection.alias);
|
|
112760
|
+
const pathKey = pathKeyOf(projection.path);
|
|
112761
|
+
if (pathKeys.has(pathKey)) {
|
|
112762
|
+
throw new Error(`Duplicate projection path: ${formatProjectionPath(projection.path)}`);
|
|
112763
|
+
}
|
|
112764
|
+
pathKeys.add(pathKey);
|
|
112765
|
+
}
|
|
112766
|
+
for (let index4 = 0;index4 < projections.length; index4++) {
|
|
112767
|
+
const current = projections[index4];
|
|
112768
|
+
for (let compareIndex = index4 + 1;compareIndex < projections.length; compareIndex++) {
|
|
112769
|
+
const other = projections[compareIndex];
|
|
112770
|
+
if (isPrefixPath(current.path, other.path) || isPrefixPath(other.path, current.path)) {
|
|
112771
|
+
throw new Error(`Conflicting projection paths: ${formatProjectionPath(current.path)} conflicts with ${formatProjectionPath(other.path)}`);
|
|
112772
|
+
}
|
|
112773
|
+
}
|
|
112774
|
+
}
|
|
112775
|
+
};
|
|
112776
|
+
|
|
111854
112777
|
// src/internal/dsl-query-runtime.ts
|
|
111855
112778
|
var makeDslQueryRuntime = (ctx) => {
|
|
112779
|
+
const assertSelectionObject = (apiName, selection) => {
|
|
112780
|
+
if (selection === null || typeof selection !== "object" || Array.isArray(selection) || TypeId in selection) {
|
|
112781
|
+
throw new Error(`${apiName}(...) expects a projection object`);
|
|
112782
|
+
}
|
|
112783
|
+
};
|
|
112784
|
+
const assertSelectionTree = (apiName, selection) => {
|
|
112785
|
+
const visit = (value, isRoot) => {
|
|
112786
|
+
if (value !== null && typeof value === "object" && TypeId in value) {
|
|
112787
|
+
return;
|
|
112788
|
+
}
|
|
112789
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
112790
|
+
throw new Error(`${apiName}(...) selection leaves must be expressions`);
|
|
112791
|
+
}
|
|
112792
|
+
const nested = Object.values(value);
|
|
112793
|
+
if (!isRoot && nested.length === 0) {
|
|
112794
|
+
throw new Error(`${apiName}(...) projection objects cannot contain empty nested selections`);
|
|
112795
|
+
}
|
|
112796
|
+
for (const item of nested) {
|
|
112797
|
+
visit(item, false);
|
|
112798
|
+
}
|
|
112799
|
+
};
|
|
112800
|
+
visit(selection, true);
|
|
112801
|
+
};
|
|
111856
112802
|
const values = (rows) => {
|
|
111857
112803
|
if (rows.length === 0) {
|
|
111858
112804
|
throw new Error("values(...) requires at least one row");
|
|
111859
112805
|
}
|
|
111860
112806
|
const normalizedRows = rows.map((row) => ctx.normalizeValuesRow(row));
|
|
111861
112807
|
const columnNames = Object.keys(normalizedRows[0]);
|
|
112808
|
+
if (columnNames.length === 0) {
|
|
112809
|
+
throw new Error("values(...) rows must specify at least one column");
|
|
112810
|
+
}
|
|
112811
|
+
const columnNameSet = new Set(columnNames);
|
|
111862
112812
|
for (const row of normalizedRows) {
|
|
111863
112813
|
const rowKeys = Object.keys(row);
|
|
111864
|
-
if (rowKeys.length !== columnNames.length || !rowKeys.every((key2
|
|
111865
|
-
throw new Error("values(...) rows must project the same columns
|
|
112814
|
+
if (rowKeys.length !== columnNames.length || !rowKeys.every((key2) => columnNameSet.has(key2))) {
|
|
112815
|
+
throw new Error("values(...) rows must project the same columns");
|
|
111866
112816
|
}
|
|
111867
112817
|
}
|
|
111868
112818
|
return Object.assign(Object.create(ctx.ValuesInputProto), {
|
|
@@ -111921,21 +112871,28 @@ var makeDslQueryRuntime = (ctx) => {
|
|
|
111921
112871
|
};
|
|
111922
112872
|
return Object.assign(source, columns);
|
|
111923
112873
|
};
|
|
111924
|
-
const select = (selection) =>
|
|
111925
|
-
selection
|
|
111926
|
-
|
|
111927
|
-
|
|
111928
|
-
|
|
111929
|
-
|
|
111930
|
-
|
|
111931
|
-
|
|
111932
|
-
|
|
111933
|
-
|
|
111934
|
-
|
|
111935
|
-
|
|
111936
|
-
|
|
111937
|
-
|
|
112874
|
+
const select = (selection = {}) => {
|
|
112875
|
+
assertSelectionObject("select", selection);
|
|
112876
|
+
assertSelectionTree("select", selection);
|
|
112877
|
+
return ctx.makePlan({
|
|
112878
|
+
selection,
|
|
112879
|
+
required: ctx.extractRequiredRuntime(selection),
|
|
112880
|
+
available: {},
|
|
112881
|
+
dialect: ctx.profile.dialect
|
|
112882
|
+
}, {
|
|
112883
|
+
kind: "select",
|
|
112884
|
+
select: selection,
|
|
112885
|
+
where: [],
|
|
112886
|
+
having: [],
|
|
112887
|
+
joins: [],
|
|
112888
|
+
groupBy: [],
|
|
112889
|
+
orderBy: []
|
|
112890
|
+
}, undefined, "read", "select");
|
|
112891
|
+
};
|
|
111938
112892
|
const groupBy = (...values2) => (plan) => {
|
|
112893
|
+
if (values2.length === 0) {
|
|
112894
|
+
throw new Error("groupBy(...) requires at least one expression");
|
|
112895
|
+
}
|
|
111939
112896
|
const current = plan[TypeId3];
|
|
111940
112897
|
const currentAst = ctx.getAst(plan);
|
|
111941
112898
|
const currentQuery = ctx.getQueryState(plan);
|
|
@@ -111950,19 +112907,29 @@ var makeDslQueryRuntime = (ctx) => {
|
|
|
111950
112907
|
groupBy: ctx.dedupeGroupedExpressions([...currentAst.groupBy, ...values2])
|
|
111951
112908
|
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement);
|
|
111952
112909
|
};
|
|
111953
|
-
const returning = (selection) =>
|
|
111954
|
-
|
|
111955
|
-
|
|
111956
|
-
|
|
111957
|
-
|
|
111958
|
-
|
|
111959
|
-
|
|
111960
|
-
|
|
111961
|
-
|
|
111962
|
-
|
|
111963
|
-
|
|
111964
|
-
|
|
111965
|
-
|
|
112910
|
+
const returning = (selection) => {
|
|
112911
|
+
assertSelectionObject("returning", selection);
|
|
112912
|
+
assertSelectionTree("returning", selection);
|
|
112913
|
+
if (flattenSelection(selection).length === 0) {
|
|
112914
|
+
throw new Error("returning(...) requires at least one selected expression");
|
|
112915
|
+
}
|
|
112916
|
+
return (plan) => {
|
|
112917
|
+
const current = plan[TypeId3];
|
|
112918
|
+
const currentAst = ctx.getAst(plan);
|
|
112919
|
+
const currentQuery = ctx.getQueryState(plan);
|
|
112920
|
+
if (currentQuery.statement !== "insert" && currentQuery.statement !== "update" && currentQuery.statement !== "delete") {
|
|
112921
|
+
throw new Error(`returning(...) is not supported for ${currentQuery.statement} statements`);
|
|
112922
|
+
}
|
|
112923
|
+
return ctx.makePlan({
|
|
112924
|
+
selection,
|
|
112925
|
+
required: [...ctx.currentRequiredList(current.required), ...ctx.extractRequiredRuntime(selection)].filter((name, index4, list) => !(name in current.available) && list.indexOf(name) === index4),
|
|
112926
|
+
available: current.available,
|
|
112927
|
+
dialect: current.dialect
|
|
112928
|
+
}, {
|
|
112929
|
+
...currentAst,
|
|
112930
|
+
select: selection
|
|
112931
|
+
}, currentQuery.assumptions, currentQuery.capabilities, currentQuery.statement, currentQuery.target, currentQuery.insertSource);
|
|
112932
|
+
};
|
|
111966
112933
|
};
|
|
111967
112934
|
return {
|
|
111968
112935
|
values,
|
|
@@ -111975,26 +112942,71 @@ var makeDslQueryRuntime = (ctx) => {
|
|
|
111975
112942
|
};
|
|
111976
112943
|
|
|
111977
112944
|
// src/internal/dsl-transaction-ddl-runtime.ts
|
|
112945
|
+
var allowedIsolationLevels = new Set(["read committed", "repeatable read", "serializable"]);
|
|
112946
|
+
var renderTransactionIsolationLevel = (isolationLevel) => {
|
|
112947
|
+
if (isolationLevel === undefined) {
|
|
112948
|
+
return "";
|
|
112949
|
+
}
|
|
112950
|
+
if (typeof isolationLevel !== "string" || !allowedIsolationLevels.has(isolationLevel)) {
|
|
112951
|
+
throw new Error("Unsupported transaction isolation level");
|
|
112952
|
+
}
|
|
112953
|
+
return `isolation level ${isolationLevel}`;
|
|
112954
|
+
};
|
|
112955
|
+
var expectDdlClauseKind = (ddl, kind) => {
|
|
112956
|
+
if (ddl === undefined || ddl.kind !== kind) {
|
|
112957
|
+
throw new Error("Unsupported DDL statement kind");
|
|
112958
|
+
}
|
|
112959
|
+
return ddl;
|
|
112960
|
+
};
|
|
112961
|
+
var expectTruncateClause = (truncate) => {
|
|
112962
|
+
if (truncate === undefined || truncate.kind !== "truncate") {
|
|
112963
|
+
throw new Error("Unsupported truncate statement kind");
|
|
112964
|
+
}
|
|
112965
|
+
return truncate;
|
|
112966
|
+
};
|
|
112967
|
+
var validateIsolationLevel = (isolationLevel) => {
|
|
112968
|
+
renderTransactionIsolationLevel(isolationLevel);
|
|
112969
|
+
};
|
|
111978
112970
|
var makeDslTransactionDdlRuntime = (ctx) => {
|
|
111979
|
-
const
|
|
111980
|
-
|
|
111981
|
-
|
|
111982
|
-
|
|
111983
|
-
|
|
111984
|
-
}
|
|
111985
|
-
|
|
111986
|
-
|
|
111987
|
-
|
|
112971
|
+
const isRecord3 = (value) => typeof value === "object" && value !== null;
|
|
112972
|
+
const assertTableTarget = (target, apiName) => {
|
|
112973
|
+
if (!isRecord3(target) || !(TypeId4 in target) || !(TypeId3 in target)) {
|
|
112974
|
+
throw new Error(`${apiName}(...) requires a table target`);
|
|
112975
|
+
}
|
|
112976
|
+
};
|
|
112977
|
+
const validateIndexColumns = (target, columns) => {
|
|
112978
|
+
const fields2 = target[TypeId4]?.fields;
|
|
112979
|
+
if (fields2 === undefined) {
|
|
112980
|
+
return;
|
|
112981
|
+
}
|
|
112982
|
+
for (const columnName of columns) {
|
|
112983
|
+
if (!(columnName in fields2)) {
|
|
112984
|
+
throw new Error(`effect-qb: unknown index column '${columnName}'`);
|
|
112985
|
+
}
|
|
112986
|
+
}
|
|
112987
|
+
};
|
|
112988
|
+
const transaction = (options2 = {}) => {
|
|
112989
|
+
validateIsolationLevel(options2.isolationLevel);
|
|
112990
|
+
return ctx.makePlan({
|
|
112991
|
+
selection: {},
|
|
112992
|
+
required: [],
|
|
112993
|
+
available: {},
|
|
112994
|
+
dialect: ctx.profile.dialect
|
|
112995
|
+
}, {
|
|
111988
112996
|
kind: "transaction",
|
|
111989
|
-
|
|
111990
|
-
|
|
111991
|
-
|
|
111992
|
-
|
|
111993
|
-
|
|
111994
|
-
|
|
111995
|
-
|
|
111996
|
-
|
|
111997
|
-
|
|
112997
|
+
select: {},
|
|
112998
|
+
transaction: {
|
|
112999
|
+
kind: "transaction",
|
|
113000
|
+
isolationLevel: options2.isolationLevel,
|
|
113001
|
+
readOnly: options2.readOnly
|
|
113002
|
+
},
|
|
113003
|
+
where: [],
|
|
113004
|
+
having: [],
|
|
113005
|
+
joins: [],
|
|
113006
|
+
groupBy: [],
|
|
113007
|
+
orderBy: []
|
|
113008
|
+
}, undefined, "transaction", "transaction");
|
|
113009
|
+
};
|
|
111998
113010
|
const commit = () => ctx.makePlan({
|
|
111999
113011
|
selection: {},
|
|
112000
113012
|
required: [],
|
|
@@ -112084,6 +113096,7 @@ var makeDslTransactionDdlRuntime = (ctx) => {
|
|
|
112084
113096
|
orderBy: []
|
|
112085
113097
|
}, undefined, "transaction", "releaseSavepoint");
|
|
112086
113098
|
const createTable = (target, options2 = {}) => {
|
|
113099
|
+
assertTableTarget(target, "createTable");
|
|
112087
113100
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
112088
113101
|
return ctx.makePlan({
|
|
112089
113102
|
selection: {},
|
|
@@ -112111,6 +113124,7 @@ var makeDslTransactionDdlRuntime = (ctx) => {
|
|
|
112111
113124
|
}, undefined, "ddl", "createTable");
|
|
112112
113125
|
};
|
|
112113
113126
|
const dropTable = (target, options2 = {}) => {
|
|
113127
|
+
assertTableTarget(target, "dropTable");
|
|
112114
113128
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
112115
113129
|
return ctx.makePlan({
|
|
112116
113130
|
selection: {},
|
|
@@ -112138,7 +113152,9 @@ var makeDslTransactionDdlRuntime = (ctx) => {
|
|
|
112138
113152
|
}, undefined, "ddl", "dropTable");
|
|
112139
113153
|
};
|
|
112140
113154
|
const createIndex = (target, columns, options2 = {}) => {
|
|
113155
|
+
assertTableTarget(target, "createIndex");
|
|
112141
113156
|
const normalizedColumns = ctx.normalizeColumnList(columns);
|
|
113157
|
+
validateIndexColumns(target, normalizedColumns);
|
|
112142
113158
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
112143
113159
|
return ctx.makePlan({
|
|
112144
113160
|
selection: {},
|
|
@@ -112169,7 +113185,9 @@ var makeDslTransactionDdlRuntime = (ctx) => {
|
|
|
112169
113185
|
}, undefined, "ddl", "createIndex");
|
|
112170
113186
|
};
|
|
112171
113187
|
const dropIndex = (target, columns, options2 = {}) => {
|
|
113188
|
+
assertTableTarget(target, "dropIndex");
|
|
112172
113189
|
const normalizedColumns = ctx.normalizeColumnList(columns);
|
|
113190
|
+
validateIndexColumns(target, normalizedColumns);
|
|
112173
113191
|
const { sourceName, sourceBaseName } = ctx.targetSourceDetails(target);
|
|
112174
113192
|
return ctx.makePlan({
|
|
112175
113193
|
selection: {},
|
|
@@ -112213,59 +113231,6 @@ var makeDslTransactionDdlRuntime = (ctx) => {
|
|
|
112213
113231
|
|
|
112214
113232
|
// src/internal/derived-table.ts
|
|
112215
113233
|
import { pipeArguments as pipeArguments4 } from "effect/Pipeable";
|
|
112216
|
-
|
|
112217
|
-
// src/internal/projection-alias.ts
|
|
112218
|
-
var TypeId7 = Symbol.for("effect-qb/ProjectionAlias");
|
|
112219
|
-
|
|
112220
|
-
// src/internal/projections.ts
|
|
112221
|
-
var aliasFromPath = (path2) => path2.join("__");
|
|
112222
|
-
var isExpression = (value) => typeof value === "object" && value !== null && (TypeId in value);
|
|
112223
|
-
var projectionAliasOf = (expression) => (TypeId7 in expression) ? expression[TypeId7].alias : undefined;
|
|
112224
|
-
var pathKeyOf = (path2) => JSON.stringify(path2);
|
|
112225
|
-
var formatProjectionPath = (path2) => path2.join(".");
|
|
112226
|
-
var isPrefixPath = (left, right) => left.length < right.length && left.every((segment, index4) => segment === right[index4]);
|
|
112227
|
-
var flattenSelection = (selection, path2 = []) => {
|
|
112228
|
-
const fields2 = [];
|
|
112229
|
-
for (const [key2, value] of Object.entries(selection)) {
|
|
112230
|
-
const nextPath = [...path2, key2];
|
|
112231
|
-
if (isExpression(value)) {
|
|
112232
|
-
fields2.push({
|
|
112233
|
-
path: nextPath,
|
|
112234
|
-
expression: value,
|
|
112235
|
-
alias: projectionAliasOf(value) ?? aliasFromPath(nextPath)
|
|
112236
|
-
});
|
|
112237
|
-
continue;
|
|
112238
|
-
}
|
|
112239
|
-
fields2.push(...flattenSelection(value, nextPath));
|
|
112240
|
-
}
|
|
112241
|
-
return fields2;
|
|
112242
|
-
};
|
|
112243
|
-
var validateProjections = (projections) => {
|
|
112244
|
-
const seen = new Set;
|
|
112245
|
-
const pathKeys = new Set;
|
|
112246
|
-
for (const projection of projections) {
|
|
112247
|
-
if (seen.has(projection.alias)) {
|
|
112248
|
-
throw new Error(`Duplicate projection alias: ${projection.alias}`);
|
|
112249
|
-
}
|
|
112250
|
-
seen.add(projection.alias);
|
|
112251
|
-
const pathKey = pathKeyOf(projection.path);
|
|
112252
|
-
if (pathKeys.has(pathKey)) {
|
|
112253
|
-
throw new Error(`Duplicate projection path: ${formatProjectionPath(projection.path)}`);
|
|
112254
|
-
}
|
|
112255
|
-
pathKeys.add(pathKey);
|
|
112256
|
-
}
|
|
112257
|
-
for (let index4 = 0;index4 < projections.length; index4++) {
|
|
112258
|
-
const current = projections[index4];
|
|
112259
|
-
for (let compareIndex = index4 + 1;compareIndex < projections.length; compareIndex++) {
|
|
112260
|
-
const other = projections[compareIndex];
|
|
112261
|
-
if (isPrefixPath(current.path, other.path) || isPrefixPath(other.path, current.path)) {
|
|
112262
|
-
throw new Error(`Conflicting projection paths: ${formatProjectionPath(current.path)} conflicts with ${formatProjectionPath(other.path)}`);
|
|
112263
|
-
}
|
|
112264
|
-
}
|
|
112265
|
-
}
|
|
112266
|
-
};
|
|
112267
|
-
|
|
112268
|
-
// src/internal/derived-table.ts
|
|
112269
113234
|
var DerivedProto = {
|
|
112270
113235
|
pipe() {
|
|
112271
113236
|
return pipeArguments4(this, arguments);
|
|
@@ -112297,10 +113262,24 @@ var setPath = (target, path2, value) => {
|
|
|
112297
113262
|
current[path2[path2.length - 1]] = value;
|
|
112298
113263
|
};
|
|
112299
113264
|
var pathAlias = (path2) => path2.join("__");
|
|
113265
|
+
var assertSourceComplete = (plan) => {
|
|
113266
|
+
const required = currentRequiredList(plan[TypeId3].required);
|
|
113267
|
+
if (required.length > 0) {
|
|
113268
|
+
throw new Error(`query references sources that are not yet in scope: ${required.join(", ")}`);
|
|
113269
|
+
}
|
|
113270
|
+
};
|
|
113271
|
+
var assertInlineSourceStatement = (plan) => {
|
|
113272
|
+
const statement = getQueryState(plan).statement;
|
|
113273
|
+
if (statement !== "select" && statement !== "set") {
|
|
113274
|
+
throw new Error("inline derived sources only accept select-like query plans");
|
|
113275
|
+
}
|
|
113276
|
+
};
|
|
112300
113277
|
var reboundedColumns = (plan, alias2) => {
|
|
112301
113278
|
const ast = getAst(plan);
|
|
112302
113279
|
const selection = {};
|
|
112303
|
-
|
|
113280
|
+
const projections = flattenSelection(ast.select);
|
|
113281
|
+
validateProjections(projections);
|
|
113282
|
+
for (const projection of projections) {
|
|
112304
113283
|
const expectedAlias = pathAlias(projection.path);
|
|
112305
113284
|
if (projection.alias !== expectedAlias) {
|
|
112306
113285
|
throw new Error(`Derived subqueries currently require path-based output aliases; expected '${expectedAlias}' for path '${projection.path.join(".")}'`);
|
|
@@ -112325,6 +113304,8 @@ var reboundedColumns = (plan, alias2) => {
|
|
|
112325
113304
|
return selection;
|
|
112326
113305
|
};
|
|
112327
113306
|
var makeDerivedSource = (plan, alias2) => {
|
|
113307
|
+
assertInlineSourceStatement(plan);
|
|
113308
|
+
assertSourceComplete(plan);
|
|
112328
113309
|
const columns = reboundedColumns(plan, alias2);
|
|
112329
113310
|
const derived = attachPipe3(Object.create(DerivedProto));
|
|
112330
113311
|
Object.assign(derived, columns);
|
|
@@ -112338,6 +113319,7 @@ var makeDerivedSource = (plan, alias2) => {
|
|
|
112338
113319
|
return derived;
|
|
112339
113320
|
};
|
|
112340
113321
|
var makeCteSource = (plan, alias2, recursive = false) => {
|
|
113322
|
+
assertSourceComplete(plan);
|
|
112341
113323
|
const columns = reboundedColumns(plan, alias2);
|
|
112342
113324
|
const cte = attachPipe3(Object.create(DerivedProto));
|
|
112343
113325
|
Object.assign(cte, columns);
|
|
@@ -112352,6 +113334,7 @@ var makeCteSource = (plan, alias2, recursive = false) => {
|
|
|
112352
113334
|
return cte;
|
|
112353
113335
|
};
|
|
112354
113336
|
var makeLateralSource = (plan, alias2) => {
|
|
113337
|
+
assertInlineSourceStatement(plan);
|
|
112355
113338
|
const columns = reboundedColumns(plan, alias2);
|
|
112356
113339
|
const lateral = attachPipe3(Object.create(DerivedProto));
|
|
112357
113340
|
Object.assign(lateral, columns);
|
|
@@ -112360,7 +113343,7 @@ var makeLateralSource = (plan, alias2) => {
|
|
|
112360
113343
|
lateral.baseName = alias2;
|
|
112361
113344
|
lateral.dialect = plan[TypeId3].dialect;
|
|
112362
113345
|
lateral.plan = plan;
|
|
112363
|
-
lateral.required =
|
|
113346
|
+
lateral.required = currentRequiredList(plan[TypeId3].required);
|
|
112364
113347
|
lateral.columns = columns;
|
|
112365
113348
|
return lateral;
|
|
112366
113349
|
};
|
|
@@ -112504,10 +113487,16 @@ var extractRequiredFromDialectInputRuntime = (value) => {
|
|
|
112504
113487
|
};
|
|
112505
113488
|
var normalizeWindowSpec = (spec) => {
|
|
112506
113489
|
const partitionBy = [...spec?.partitionBy ?? []];
|
|
112507
|
-
const orderBy = (spec?.orderBy ?? []).map((term) =>
|
|
112508
|
-
|
|
112509
|
-
|
|
112510
|
-
|
|
113490
|
+
const orderBy = (spec?.orderBy ?? []).map((term) => {
|
|
113491
|
+
const direction = term.direction ?? "asc";
|
|
113492
|
+
if (direction !== "asc" && direction !== "desc") {
|
|
113493
|
+
throw new Error("window order direction must be asc or desc");
|
|
113494
|
+
}
|
|
113495
|
+
return {
|
|
113496
|
+
value: term.value,
|
|
113497
|
+
direction
|
|
113498
|
+
};
|
|
113499
|
+
});
|
|
112511
113500
|
return {
|
|
112512
113501
|
partitionBy,
|
|
112513
113502
|
orderBy
|
|
@@ -113485,21 +114474,52 @@ var excluded = (value) => {
|
|
|
113485
114474
|
});
|
|
113486
114475
|
};
|
|
113487
114476
|
var toMutationValueExpression = (value, column2) => {
|
|
114477
|
+
const columnState = column2[TypeId];
|
|
114478
|
+
const normalizeMutationValue = (candidate) => {
|
|
114479
|
+
if (candidate === null && columnState.nullability !== "never") {
|
|
114480
|
+
return null;
|
|
114481
|
+
}
|
|
114482
|
+
const runtimeSchemaAccepts = columnState.runtimeSchema !== undefined && Schema6.is(columnState.runtimeSchema)(candidate);
|
|
114483
|
+
if (runtimeSchemaAccepts) {
|
|
114484
|
+
return candidate;
|
|
114485
|
+
}
|
|
114486
|
+
const normalized = normalizeDbValue(columnState.dbType, candidate);
|
|
114487
|
+
return columnState.runtimeSchema === undefined ? normalized : Schema6.decodeUnknownSync(columnState.runtimeSchema)(normalized);
|
|
114488
|
+
};
|
|
113488
114489
|
if (value !== null && typeof value === "object" && TypeId in value) {
|
|
114490
|
+
const expression = value;
|
|
114491
|
+
const ast = expression[TypeId2];
|
|
114492
|
+
if (ast.kind === "literal") {
|
|
114493
|
+
const normalizedValue2 = normalizeMutationValue(ast.value);
|
|
114494
|
+
return makeExpression({
|
|
114495
|
+
runtime: normalizedValue2,
|
|
114496
|
+
dbType: columnState.dbType,
|
|
114497
|
+
runtimeSchema: columnState.runtimeSchema,
|
|
114498
|
+
driverValueMapping: columnState.driverValueMapping,
|
|
114499
|
+
nullability: normalizedValue2 === null ? "always" : "never",
|
|
114500
|
+
dialect: columnState.dialect,
|
|
114501
|
+
kind: "scalar",
|
|
114502
|
+
dependencies: {}
|
|
114503
|
+
}, {
|
|
114504
|
+
kind: "literal",
|
|
114505
|
+
value: normalizedValue2
|
|
114506
|
+
});
|
|
114507
|
+
}
|
|
113489
114508
|
return retargetLiteralExpression(value, column2);
|
|
113490
114509
|
}
|
|
114510
|
+
const normalizedValue = normalizeMutationValue(value);
|
|
113491
114511
|
return makeExpression({
|
|
113492
|
-
runtime:
|
|
113493
|
-
dbType:
|
|
113494
|
-
runtimeSchema:
|
|
113495
|
-
driverValueMapping:
|
|
113496
|
-
nullability:
|
|
113497
|
-
dialect:
|
|
114512
|
+
runtime: normalizedValue,
|
|
114513
|
+
dbType: columnState.dbType,
|
|
114514
|
+
runtimeSchema: columnState.runtimeSchema,
|
|
114515
|
+
driverValueMapping: columnState.driverValueMapping,
|
|
114516
|
+
nullability: normalizedValue === null ? "always" : "never",
|
|
114517
|
+
dialect: columnState.dialect,
|
|
113498
114518
|
kind: "scalar",
|
|
113499
114519
|
dependencies: {}
|
|
113500
114520
|
}, {
|
|
113501
114521
|
kind: "literal",
|
|
113502
|
-
value
|
|
114522
|
+
value: normalizedValue
|
|
113503
114523
|
});
|
|
113504
114524
|
};
|
|
113505
114525
|
var renderQuantifiedComparisonAst = (left, plan, operator, quantifier) => ({
|
|
@@ -113562,7 +114582,12 @@ var makeAliasedValuesSource = (rows, selection, alias2) => {
|
|
|
113562
114582
|
return Object.assign(source, columns);
|
|
113563
114583
|
};
|
|
113564
114584
|
var normalizeValuesRow = (row) => Object.fromEntries(Object.entries(row).map(([key2, value]) => [key2, toDialectExpression(value)]));
|
|
113565
|
-
var normalizeUnnestColumns = (columns) => Object.fromEntries(Object.entries(columns).map(([key2, values]) =>
|
|
114585
|
+
var normalizeUnnestColumns = (columns) => Object.fromEntries(Object.entries(columns).map(([key2, values]) => {
|
|
114586
|
+
if (!Array.isArray(values)) {
|
|
114587
|
+
throw new Error("unnest(...) expects every value to be an array");
|
|
114588
|
+
}
|
|
114589
|
+
return [key2, values.map((value) => toDialectExpression(value))];
|
|
114590
|
+
}));
|
|
113566
114591
|
var normalizeMutationTargets = (target) => Array.isArray(target) ? target : [target];
|
|
113567
114592
|
var mutationTargetClauses = (target) => normalizeMutationTargets(target).map((table) => {
|
|
113568
114593
|
const { sourceName, sourceBaseName } = targetSourceDetails(table);
|
|
@@ -113584,13 +114609,20 @@ var mutationAvailableSources = (target, mode = "required") => Object.fromEntries
|
|
|
113584
114609
|
}
|
|
113585
114610
|
];
|
|
113586
114611
|
}));
|
|
114612
|
+
var getMutationColumn = (columns, columnName) => {
|
|
114613
|
+
const column2 = columns[columnName];
|
|
114614
|
+
if (column2 === undefined || column2 === null || typeof column2 !== "object" || !(TypeId in column2)) {
|
|
114615
|
+
throw new Error("effect-qb: unknown mutation column");
|
|
114616
|
+
}
|
|
114617
|
+
return column2;
|
|
114618
|
+
};
|
|
113587
114619
|
var buildMutationAssignments = (target, values) => {
|
|
113588
114620
|
const targets = normalizeMutationTargets(target);
|
|
113589
114621
|
if (targets.length === 1 && !Array.isArray(target)) {
|
|
113590
114622
|
const columns = target;
|
|
113591
114623
|
return Object.entries(values).map(([columnName, value]) => ({
|
|
113592
114624
|
columnName,
|
|
113593
|
-
value: toMutationValueExpression(value, columns
|
|
114625
|
+
value: toMutationValueExpression(value, getMutationColumn(columns, columnName))
|
|
113594
114626
|
}));
|
|
113595
114627
|
}
|
|
113596
114628
|
const valueMap = values;
|
|
@@ -113601,7 +114633,7 @@ var buildMutationAssignments = (target, values) => {
|
|
|
113601
114633
|
return Object.entries(scopedValues).map(([columnName, value]) => ({
|
|
113602
114634
|
tableName: targetName,
|
|
113603
114635
|
columnName,
|
|
113604
|
-
value: toMutationValueExpression(value, columns
|
|
114636
|
+
value: toMutationValueExpression(value, getMutationColumn(columns, columnName))
|
|
113605
114637
|
}));
|
|
113606
114638
|
});
|
|
113607
114639
|
};
|
|
@@ -113664,25 +114696,22 @@ var normalizeInsertUnnestValues = (target, values) => {
|
|
|
113664
114696
|
values: normalized
|
|
113665
114697
|
};
|
|
113666
114698
|
};
|
|
114699
|
+
var normalizeConflictColumns = (target, columnsInput) => {
|
|
114700
|
+
const columns = normalizeColumnList(columnsInput);
|
|
114701
|
+
const knownColumns = new Set(Object.keys(target[TypeId4].fields));
|
|
114702
|
+
if (columns.some((columnName) => !knownColumns.has(columnName))) {
|
|
114703
|
+
throw new Error("effect-qb: unknown conflict target column");
|
|
114704
|
+
}
|
|
114705
|
+
return columns;
|
|
114706
|
+
};
|
|
113667
114707
|
var buildConflictTarget = (target, input) => {
|
|
113668
|
-
if (Array.isArray(input)) {
|
|
114708
|
+
if (typeof input === "string" || Array.isArray(input)) {
|
|
113669
114709
|
return {
|
|
113670
114710
|
kind: "columns",
|
|
113671
|
-
columns:
|
|
113672
|
-
};
|
|
113673
|
-
}
|
|
113674
|
-
if (!Array.isArray(input) && "constraint" in input) {
|
|
113675
|
-
return {
|
|
113676
|
-
kind: "constraint",
|
|
113677
|
-
name: input.constraint
|
|
114711
|
+
columns: normalizeConflictColumns(target, input)
|
|
113678
114712
|
};
|
|
113679
114713
|
}
|
|
113680
|
-
|
|
113681
|
-
return {
|
|
113682
|
-
kind: "columns",
|
|
113683
|
-
columns: normalizeColumnList(columnTarget.columns),
|
|
113684
|
-
where: columnTarget.where === undefined ? undefined : toDialectExpression(columnTarget.where)
|
|
113685
|
-
};
|
|
114714
|
+
throw new Error("Unsupported mysql conflict target");
|
|
113686
114715
|
};
|
|
113687
114716
|
var defaultIndexName = (tableName, columns, unique4) => `${tableName}_${columns.join("_")}_${unique4 ? "uniq" : "idx"}`;
|
|
113688
114717
|
function as(valueOrAlias, alias2) {
|
|
@@ -113795,6 +114824,7 @@ var distinctOn = {
|
|
|
113795
114824
|
__effect_qb_hint__: "Use postgres.Query.distinctOn(...) or regular distinct()/grouping logic"
|
|
113796
114825
|
};
|
|
113797
114826
|
var mutationRuntime = makeDslMutationRuntime({
|
|
114827
|
+
profile,
|
|
113798
114828
|
makePlan,
|
|
113799
114829
|
getAst,
|
|
113800
114830
|
getQueryState,
|
|
@@ -113807,7 +114837,7 @@ var mutationRuntime = makeDslMutationRuntime({
|
|
|
113807
114837
|
buildConflictTarget,
|
|
113808
114838
|
mutationTargetClauses,
|
|
113809
114839
|
mutationAvailableSources,
|
|
113810
|
-
|
|
114840
|
+
normalizeConflictColumns,
|
|
113811
114841
|
targetSourceDetails,
|
|
113812
114842
|
sourceDetails
|
|
113813
114843
|
});
|
|
@@ -113818,7 +114848,7 @@ var update = (target, values2) => mutationRuntime.update(target, values2);
|
|
|
113818
114848
|
var upsert = (target, values2, conflictColumns, updateValues) => mutationRuntime.upsert(target, values2, conflictColumns, updateValues);
|
|
113819
114849
|
var delete_ = (target) => mutationRuntime.delete_(target);
|
|
113820
114850
|
var truncate = (target, options2 = {}) => mutationRuntime.truncate(target, options2);
|
|
113821
|
-
var merge = (target, source, on, options2
|
|
114851
|
+
var merge = (target, source, on, options2) => mutationRuntime.merge(target, source, on, options2);
|
|
113822
114852
|
var {
|
|
113823
114853
|
transaction,
|
|
113824
114854
|
commit,
|
|
@@ -113911,317 +114941,25 @@ var exports_executor = {};
|
|
|
113911
114941
|
__export(exports_executor, {
|
|
113912
114942
|
withTransaction: () => withTransaction2,
|
|
113913
114943
|
withSavepoint: () => withSavepoint2,
|
|
113914
|
-
make: () =>
|
|
114944
|
+
make: () => make5,
|
|
113915
114945
|
driver: () => driver2,
|
|
113916
114946
|
custom: () => custom3
|
|
113917
114947
|
});
|
|
113918
114948
|
import * as Effect2 from "effect/Effect";
|
|
113919
|
-
import * as SqlClient3 from "
|
|
114949
|
+
import * as SqlClient3 from "effect/unstable/sql/SqlClient";
|
|
113920
114950
|
import * as Stream2 from "effect/Stream";
|
|
113921
114951
|
|
|
113922
114952
|
// src/internal/executor.ts
|
|
113923
114953
|
import * as Chunk from "effect/Chunk";
|
|
113924
114954
|
import * as Effect from "effect/Effect";
|
|
114955
|
+
import * as Exit from "effect/Exit";
|
|
113925
114956
|
import * as Option from "effect/Option";
|
|
113926
114957
|
import * as Schema9 from "effect/Schema";
|
|
113927
|
-
import * as SqlClient from "
|
|
114958
|
+
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
|
113928
114959
|
import * as Stream from "effect/Stream";
|
|
113929
114960
|
|
|
113930
114961
|
// src/internal/runtime/driver-value-mapping.ts
|
|
113931
114962
|
import * as Schema7 from "effect/Schema";
|
|
113932
|
-
|
|
113933
|
-
// src/internal/runtime/normalize.ts
|
|
113934
|
-
var isRecord2 = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
113935
|
-
var pad = (value, width = 2) => value.toString().padStart(width, "0");
|
|
113936
|
-
var formatLocalDate = (value) => `${value.getUTCFullYear()}-${pad(value.getUTCMonth() + 1)}-${pad(value.getUTCDate())}`;
|
|
113937
|
-
var formatLocalTime = (value) => {
|
|
113938
|
-
const milliseconds = value.getUTCMilliseconds();
|
|
113939
|
-
const base = `${pad(value.getUTCHours())}:${pad(value.getUTCMinutes())}:${pad(value.getUTCSeconds())}`;
|
|
113940
|
-
return milliseconds === 0 ? base : `${base}.${pad(milliseconds, 3)}`;
|
|
113941
|
-
};
|
|
113942
|
-
var formatLocalDateTime = (value) => {
|
|
113943
|
-
const milliseconds = value.getUTCMilliseconds();
|
|
113944
|
-
const base = `${formatLocalDate(value)}T${pad(value.getUTCHours())}:${pad(value.getUTCMinutes())}:${pad(value.getUTCSeconds())}`;
|
|
113945
|
-
return milliseconds === 0 ? base : `${base}.${pad(milliseconds, 3)}`;
|
|
113946
|
-
};
|
|
113947
|
-
var runtimeTagOfBaseDbType = (dbType) => {
|
|
113948
|
-
return dbType.runtime;
|
|
113949
|
-
};
|
|
113950
|
-
var expectString = (value, label) => {
|
|
113951
|
-
if (typeof value === "string") {
|
|
113952
|
-
return value;
|
|
113953
|
-
}
|
|
113954
|
-
throw new Error(`Expected ${label} as string`);
|
|
113955
|
-
};
|
|
113956
|
-
var normalizeNumber = (value) => {
|
|
113957
|
-
if (typeof value === "number" && Number.isFinite(value)) {
|
|
113958
|
-
return value;
|
|
113959
|
-
}
|
|
113960
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
113961
|
-
const parsed = Number(value);
|
|
113962
|
-
if (Number.isFinite(parsed)) {
|
|
113963
|
-
return parsed;
|
|
113964
|
-
}
|
|
113965
|
-
}
|
|
113966
|
-
if (typeof value === "bigint" && Number.isSafeInteger(Number(value))) {
|
|
113967
|
-
return Number(value);
|
|
113968
|
-
}
|
|
113969
|
-
throw new Error("Expected a finite numeric value");
|
|
113970
|
-
};
|
|
113971
|
-
var normalizeBoolean = (value) => {
|
|
113972
|
-
if (typeof value === "boolean") {
|
|
113973
|
-
return value;
|
|
113974
|
-
}
|
|
113975
|
-
if (typeof value === "number") {
|
|
113976
|
-
if (value === 1) {
|
|
113977
|
-
return true;
|
|
113978
|
-
}
|
|
113979
|
-
if (value === 0) {
|
|
113980
|
-
return false;
|
|
113981
|
-
}
|
|
113982
|
-
}
|
|
113983
|
-
if (typeof value === "string") {
|
|
113984
|
-
const normalized = value.trim().toLowerCase();
|
|
113985
|
-
if (normalized === "true" || normalized === "t" || normalized === "1") {
|
|
113986
|
-
return true;
|
|
113987
|
-
}
|
|
113988
|
-
if (normalized === "false" || normalized === "f" || normalized === "0") {
|
|
113989
|
-
return false;
|
|
113990
|
-
}
|
|
113991
|
-
}
|
|
113992
|
-
throw new Error("Expected a boolean-like value");
|
|
113993
|
-
};
|
|
113994
|
-
var normalizeBigIntString = (value) => {
|
|
113995
|
-
if (typeof value === "bigint") {
|
|
113996
|
-
return value.toString();
|
|
113997
|
-
}
|
|
113998
|
-
if (typeof value === "number" && Number.isSafeInteger(value)) {
|
|
113999
|
-
return BigInt(value).toString();
|
|
114000
|
-
}
|
|
114001
|
-
if (typeof value === "string" && /^-?\d+$/.test(value.trim())) {
|
|
114002
|
-
return BigInt(value.trim()).toString();
|
|
114003
|
-
}
|
|
114004
|
-
throw new Error("Expected an integer-like bigint value");
|
|
114005
|
-
};
|
|
114006
|
-
var canonicalizeDecimalString = (input) => {
|
|
114007
|
-
const trimmed = input.trim();
|
|
114008
|
-
const match2 = /^([+-]?)(\d+)(?:\.(\d+))?$/.exec(trimmed);
|
|
114009
|
-
if (match2 === null) {
|
|
114010
|
-
throw new Error("Expected a decimal string");
|
|
114011
|
-
}
|
|
114012
|
-
const sign = match2[1] === "-" ? "-" : "";
|
|
114013
|
-
const integer = match2[2].replace(/^0+(?=\d)/, "") || "0";
|
|
114014
|
-
const fraction = (match2[3] ?? "").replace(/0+$/, "");
|
|
114015
|
-
if (fraction.length === 0) {
|
|
114016
|
-
return `${sign}${integer}`;
|
|
114017
|
-
}
|
|
114018
|
-
return `${sign}${integer}.${fraction}`;
|
|
114019
|
-
};
|
|
114020
|
-
var normalizeDecimalString = (value) => {
|
|
114021
|
-
if (typeof value === "string") {
|
|
114022
|
-
return canonicalizeDecimalString(value);
|
|
114023
|
-
}
|
|
114024
|
-
if (typeof value === "number" && Number.isFinite(value)) {
|
|
114025
|
-
const rendered = String(value);
|
|
114026
|
-
if (/[eE]/.test(rendered)) {
|
|
114027
|
-
throw new Error("Scientific notation is not a supported decimal runtime");
|
|
114028
|
-
}
|
|
114029
|
-
return canonicalizeDecimalString(rendered);
|
|
114030
|
-
}
|
|
114031
|
-
throw new Error("Expected a decimal-like value");
|
|
114032
|
-
};
|
|
114033
|
-
var normalizeLocalDate = (value) => {
|
|
114034
|
-
if (value instanceof Date) {
|
|
114035
|
-
return formatLocalDate(value);
|
|
114036
|
-
}
|
|
114037
|
-
const raw = expectString(value, "local date").trim();
|
|
114038
|
-
if (/^\d{4}-\d{2}-\d{2}$/.test(raw)) {
|
|
114039
|
-
return raw;
|
|
114040
|
-
}
|
|
114041
|
-
const parsed = new Date(raw);
|
|
114042
|
-
if (!Number.isNaN(parsed.getTime())) {
|
|
114043
|
-
return formatLocalDate(parsed);
|
|
114044
|
-
}
|
|
114045
|
-
throw new Error("Expected a local-date value");
|
|
114046
|
-
};
|
|
114047
|
-
var normalizeLocalTime = (value) => {
|
|
114048
|
-
if (value instanceof Date) {
|
|
114049
|
-
return formatLocalTime(value);
|
|
114050
|
-
}
|
|
114051
|
-
const raw = expectString(value, "local time").trim();
|
|
114052
|
-
if (/^\d{2}:\d{2}:\d{2}(?:\.\d+)?$/.test(raw)) {
|
|
114053
|
-
return raw;
|
|
114054
|
-
}
|
|
114055
|
-
throw new Error("Expected a local-time value");
|
|
114056
|
-
};
|
|
114057
|
-
var normalizeOffsetTime = (value) => {
|
|
114058
|
-
if (value instanceof Date) {
|
|
114059
|
-
return `${formatLocalTime(value)}Z`;
|
|
114060
|
-
}
|
|
114061
|
-
const raw = expectString(value, "offset time").trim();
|
|
114062
|
-
if (/^\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.test(raw)) {
|
|
114063
|
-
return raw;
|
|
114064
|
-
}
|
|
114065
|
-
throw new Error("Expected an offset-time value");
|
|
114066
|
-
};
|
|
114067
|
-
var normalizeLocalDateTime = (value) => {
|
|
114068
|
-
if (value instanceof Date) {
|
|
114069
|
-
return formatLocalDateTime(value);
|
|
114070
|
-
}
|
|
114071
|
-
const raw = expectString(value, "local datetime").trim();
|
|
114072
|
-
if (/^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d+)?$/.test(raw)) {
|
|
114073
|
-
return raw.replace(" ", "T");
|
|
114074
|
-
}
|
|
114075
|
-
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.test(raw)) {
|
|
114076
|
-
const parsed = new Date(raw);
|
|
114077
|
-
if (!Number.isNaN(parsed.getTime())) {
|
|
114078
|
-
return formatLocalDateTime(parsed);
|
|
114079
|
-
}
|
|
114080
|
-
}
|
|
114081
|
-
throw new Error("Expected a local-datetime value");
|
|
114082
|
-
};
|
|
114083
|
-
var normalizeInstant = (value) => {
|
|
114084
|
-
if (value instanceof Date) {
|
|
114085
|
-
return value.toISOString();
|
|
114086
|
-
}
|
|
114087
|
-
const raw = expectString(value, "instant").trim();
|
|
114088
|
-
if (!/[zZ]|[+-]\d{2}:\d{2}$/.test(raw)) {
|
|
114089
|
-
throw new Error("Instant values require a timezone offset");
|
|
114090
|
-
}
|
|
114091
|
-
const parsed = new Date(raw);
|
|
114092
|
-
if (Number.isNaN(parsed.getTime())) {
|
|
114093
|
-
throw new Error("Expected an ISO instant value");
|
|
114094
|
-
}
|
|
114095
|
-
return parsed.toISOString();
|
|
114096
|
-
};
|
|
114097
|
-
var normalizeYear = (value) => {
|
|
114098
|
-
if (typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 9999) {
|
|
114099
|
-
return pad(value, 4);
|
|
114100
|
-
}
|
|
114101
|
-
const raw = expectString(value, "year").trim();
|
|
114102
|
-
if (/^\d{4}$/.test(raw)) {
|
|
114103
|
-
return raw;
|
|
114104
|
-
}
|
|
114105
|
-
throw new Error("Expected a four-digit year");
|
|
114106
|
-
};
|
|
114107
|
-
var normalizeBytes = (value) => {
|
|
114108
|
-
if (value instanceof Uint8Array) {
|
|
114109
|
-
return new Uint8Array(value);
|
|
114110
|
-
}
|
|
114111
|
-
const BufferConstructor = globalThis.Buffer;
|
|
114112
|
-
if (BufferConstructor !== undefined && value instanceof BufferConstructor) {
|
|
114113
|
-
return new Uint8Array(value);
|
|
114114
|
-
}
|
|
114115
|
-
throw new Error("Expected a byte array value");
|
|
114116
|
-
};
|
|
114117
|
-
var isJsonValue = (value) => {
|
|
114118
|
-
if (value === null) {
|
|
114119
|
-
return true;
|
|
114120
|
-
}
|
|
114121
|
-
switch (typeof value) {
|
|
114122
|
-
case "string":
|
|
114123
|
-
case "number":
|
|
114124
|
-
case "boolean":
|
|
114125
|
-
return true;
|
|
114126
|
-
case "object":
|
|
114127
|
-
if (Array.isArray(value)) {
|
|
114128
|
-
return value.every(isJsonValue);
|
|
114129
|
-
}
|
|
114130
|
-
return isRecord2(value) && Object.values(value).every(isJsonValue);
|
|
114131
|
-
default:
|
|
114132
|
-
return false;
|
|
114133
|
-
}
|
|
114134
|
-
};
|
|
114135
|
-
var normalizeJson = (value) => {
|
|
114136
|
-
if (typeof value === "string") {
|
|
114137
|
-
const parsed = JSON.parse(value);
|
|
114138
|
-
if (isJsonValue(parsed)) {
|
|
114139
|
-
return parsed;
|
|
114140
|
-
}
|
|
114141
|
-
throw new Error("Parsed JSON value is not a valid JSON runtime");
|
|
114142
|
-
}
|
|
114143
|
-
if (isJsonValue(value)) {
|
|
114144
|
-
return value;
|
|
114145
|
-
}
|
|
114146
|
-
throw new Error("Expected a JSON value");
|
|
114147
|
-
};
|
|
114148
|
-
var normalizeDbValue = (dbType, value) => {
|
|
114149
|
-
if (value === null) {
|
|
114150
|
-
return null;
|
|
114151
|
-
}
|
|
114152
|
-
if ("base" in dbType) {
|
|
114153
|
-
return normalizeDbValue(dbType.base, value);
|
|
114154
|
-
}
|
|
114155
|
-
if ("element" in dbType) {
|
|
114156
|
-
if (!Array.isArray(value)) {
|
|
114157
|
-
throw new Error("Expected an array value");
|
|
114158
|
-
}
|
|
114159
|
-
return value.map((entry) => normalizeDbValue(dbType.element, entry));
|
|
114160
|
-
}
|
|
114161
|
-
if ("fields" in dbType) {
|
|
114162
|
-
if (!isRecord2(value)) {
|
|
114163
|
-
throw new Error("Expected a record value");
|
|
114164
|
-
}
|
|
114165
|
-
const normalized = {};
|
|
114166
|
-
for (const [key2, fieldDbType] of Object.entries(dbType.fields)) {
|
|
114167
|
-
if (key2 in value) {
|
|
114168
|
-
normalized[key2] = normalizeDbValue(fieldDbType, value[key2]);
|
|
114169
|
-
}
|
|
114170
|
-
}
|
|
114171
|
-
return normalized;
|
|
114172
|
-
}
|
|
114173
|
-
if ("variant" in dbType && dbType.variant === "json") {
|
|
114174
|
-
return normalizeJson(value);
|
|
114175
|
-
}
|
|
114176
|
-
if ("variant" in dbType && (dbType.variant === "enum" || dbType.variant === "set")) {
|
|
114177
|
-
return expectString(value, "text");
|
|
114178
|
-
}
|
|
114179
|
-
switch (runtimeTagOfBaseDbType(dbType)) {
|
|
114180
|
-
case "string":
|
|
114181
|
-
return expectString(value, "text");
|
|
114182
|
-
case "number":
|
|
114183
|
-
return normalizeNumber(value);
|
|
114184
|
-
case "bigintString":
|
|
114185
|
-
return normalizeBigIntString(value);
|
|
114186
|
-
case "boolean":
|
|
114187
|
-
return normalizeBoolean(value);
|
|
114188
|
-
case "json":
|
|
114189
|
-
return normalizeJson(value);
|
|
114190
|
-
case "localDate":
|
|
114191
|
-
return normalizeLocalDate(value);
|
|
114192
|
-
case "localTime":
|
|
114193
|
-
return normalizeLocalTime(value);
|
|
114194
|
-
case "offsetTime":
|
|
114195
|
-
return normalizeOffsetTime(value);
|
|
114196
|
-
case "localDateTime":
|
|
114197
|
-
return normalizeLocalDateTime(value);
|
|
114198
|
-
case "instant":
|
|
114199
|
-
return normalizeInstant(value);
|
|
114200
|
-
case "year":
|
|
114201
|
-
return normalizeYear(value);
|
|
114202
|
-
case "decimalString":
|
|
114203
|
-
return normalizeDecimalString(value);
|
|
114204
|
-
case "bytes":
|
|
114205
|
-
return normalizeBytes(value);
|
|
114206
|
-
case "array":
|
|
114207
|
-
if (!Array.isArray(value)) {
|
|
114208
|
-
throw new Error("Expected an array value");
|
|
114209
|
-
}
|
|
114210
|
-
return value;
|
|
114211
|
-
case "record":
|
|
114212
|
-
if (!isRecord2(value)) {
|
|
114213
|
-
throw new Error("Expected a record value");
|
|
114214
|
-
}
|
|
114215
|
-
return value;
|
|
114216
|
-
case "null":
|
|
114217
|
-
return null;
|
|
114218
|
-
case "unknown":
|
|
114219
|
-
case undefined:
|
|
114220
|
-
return value;
|
|
114221
|
-
}
|
|
114222
|
-
};
|
|
114223
|
-
|
|
114224
|
-
// src/internal/runtime/driver-value-mapping.ts
|
|
114225
114963
|
var runtimeTagOfDbType = (dbType) => {
|
|
114226
114964
|
if (dbType === undefined) {
|
|
114227
114965
|
return;
|
|
@@ -114273,6 +115011,20 @@ var findMapping = (context, key2) => {
|
|
|
114273
115011
|
}
|
|
114274
115012
|
return;
|
|
114275
115013
|
};
|
|
115014
|
+
var isJsonDbType = (dbType) => {
|
|
115015
|
+
if (dbType === undefined) {
|
|
115016
|
+
return false;
|
|
115017
|
+
}
|
|
115018
|
+
if ("base" in dbType) {
|
|
115019
|
+
return isJsonDbType(dbType.base);
|
|
115020
|
+
}
|
|
115021
|
+
if (!("variant" in dbType)) {
|
|
115022
|
+
return false;
|
|
115023
|
+
}
|
|
115024
|
+
const variant = dbType.variant;
|
|
115025
|
+
return variant === "json" || variant === "jsonb";
|
|
115026
|
+
};
|
|
115027
|
+
var schemaAccepts = (schema4, value) => schema4 !== undefined && Schema7.is(schema4)(value);
|
|
114276
115028
|
var encodeWithSchema = (schema4, value) => {
|
|
114277
115029
|
if (schema4 === undefined) {
|
|
114278
115030
|
return { value, encoded: false };
|
|
@@ -114285,10 +115037,35 @@ var encodeWithSchema = (schema4, value) => {
|
|
|
114285
115037
|
encoded: true
|
|
114286
115038
|
};
|
|
114287
115039
|
};
|
|
115040
|
+
var normalizeJsonDriverString = (value, context) => {
|
|
115041
|
+
if (!isJsonDbType(context.dbType) || context.runtimeSchema === undefined) {
|
|
115042
|
+
return;
|
|
115043
|
+
}
|
|
115044
|
+
try {
|
|
115045
|
+
const parsed = JSON.parse(value);
|
|
115046
|
+
if (value.trimStart().startsWith('"') && schemaAccepts(context.runtimeSchema, parsed)) {
|
|
115047
|
+
return parsed;
|
|
115048
|
+
}
|
|
115049
|
+
if (schemaAccepts(context.runtimeSchema, value) && !schemaAccepts(context.runtimeSchema, parsed)) {
|
|
115050
|
+
return value;
|
|
115051
|
+
}
|
|
115052
|
+
} catch (error) {
|
|
115053
|
+
if (error instanceof SyntaxError && schemaAccepts(context.runtimeSchema, value)) {
|
|
115054
|
+
return value;
|
|
115055
|
+
}
|
|
115056
|
+
if (!(error instanceof SyntaxError)) {
|
|
115057
|
+
throw error;
|
|
115058
|
+
}
|
|
115059
|
+
}
|
|
115060
|
+
return;
|
|
115061
|
+
};
|
|
114288
115062
|
var toDriverValue = (value, context) => {
|
|
114289
115063
|
if (value === null) {
|
|
114290
115064
|
return null;
|
|
114291
115065
|
}
|
|
115066
|
+
if (value instanceof Date && Number.isNaN(value.getTime())) {
|
|
115067
|
+
throw new Error("Expected a valid Date value");
|
|
115068
|
+
}
|
|
114292
115069
|
const dbType = context.dbType;
|
|
114293
115070
|
const encoded = encodeWithSchema(context.runtimeSchema, value);
|
|
114294
115071
|
let current = encoded.value;
|
|
@@ -114296,6 +115073,9 @@ var toDriverValue = (value, context) => {
|
|
|
114296
115073
|
if (custom3 !== undefined && dbType !== undefined) {
|
|
114297
115074
|
return custom3(current, dbType);
|
|
114298
115075
|
}
|
|
115076
|
+
if (encoded.encoded && typeof current === "string" && isJsonDbType(dbType)) {
|
|
115077
|
+
return current;
|
|
115078
|
+
}
|
|
114299
115079
|
return dbType === undefined || !encoded.encoded ? current : normalizeDbValue(dbType, current);
|
|
114300
115080
|
};
|
|
114301
115081
|
var fromDriverValue = (value, context) => {
|
|
@@ -114307,6 +115087,12 @@ var fromDriverValue = (value, context) => {
|
|
|
114307
115087
|
if (custom3 !== undefined && dbType !== undefined) {
|
|
114308
115088
|
return custom3(value, dbType);
|
|
114309
115089
|
}
|
|
115090
|
+
if (typeof value === "string") {
|
|
115091
|
+
const normalizedJsonString = normalizeJsonDriverString(value, context);
|
|
115092
|
+
if (normalizedJsonString !== undefined) {
|
|
115093
|
+
return normalizedJsonString;
|
|
115094
|
+
}
|
|
115095
|
+
}
|
|
114310
115096
|
return dbType === undefined ? value : normalizeDbValue(dbType, value);
|
|
114311
115097
|
};
|
|
114312
115098
|
var textCast = (sql) => `(${sql})::text`;
|
|
@@ -114346,12 +115132,13 @@ var renderJsonSelectSql = (sql, context) => {
|
|
|
114346
115132
|
import * as Schema8 from "effect/Schema";
|
|
114347
115133
|
import * as SchemaAST from "effect/SchemaAST";
|
|
114348
115134
|
var schemaCache = new WeakMap;
|
|
115135
|
+
var FiniteNumberSchema = Schema8.Number.check(Schema8.isFinite());
|
|
114349
115136
|
var runtimeSchemaForTag = (tag) => {
|
|
114350
115137
|
switch (tag) {
|
|
114351
115138
|
case "string":
|
|
114352
115139
|
return Schema8.String;
|
|
114353
115140
|
case "number":
|
|
114354
|
-
return
|
|
115141
|
+
return FiniteNumberSchema;
|
|
114355
115142
|
case "bigintString":
|
|
114356
115143
|
return BigIntStringSchema;
|
|
114357
115144
|
case "boolean":
|
|
@@ -114373,14 +115160,11 @@ var runtimeSchemaForTag = (tag) => {
|
|
|
114373
115160
|
case "decimalString":
|
|
114374
115161
|
return DecimalStringSchema;
|
|
114375
115162
|
case "bytes":
|
|
114376
|
-
return Schema8.
|
|
115163
|
+
return Schema8.Uint8Array;
|
|
114377
115164
|
case "array":
|
|
114378
115165
|
return Schema8.Array(Schema8.Unknown);
|
|
114379
115166
|
case "record":
|
|
114380
|
-
return Schema8.Record(
|
|
114381
|
-
key: Schema8.String,
|
|
114382
|
-
value: Schema8.Unknown
|
|
114383
|
-
});
|
|
115167
|
+
return Schema8.Record(Schema8.String, Schema8.Unknown);
|
|
114384
115168
|
case "null":
|
|
114385
115169
|
return Schema8.Null;
|
|
114386
115170
|
case "unknown":
|
|
@@ -114418,22 +115202,18 @@ var unionAst = (asts) => {
|
|
|
114418
115202
|
if (asts.length === 1) {
|
|
114419
115203
|
return asts[0];
|
|
114420
115204
|
}
|
|
114421
|
-
return SchemaAST.Union
|
|
115205
|
+
return new SchemaAST.Union(asts, "anyOf");
|
|
114422
115206
|
};
|
|
114423
115207
|
var propertyAstOf = (ast, key2) => {
|
|
114424
115208
|
switch (ast._tag) {
|
|
114425
|
-
case "Transformation":
|
|
114426
|
-
return propertyAstOf(SchemaAST.typeAST(ast), key2);
|
|
114427
|
-
case "Refinement":
|
|
114428
|
-
return propertyAstOf(ast.from, key2);
|
|
114429
115209
|
case "Suspend":
|
|
114430
|
-
return propertyAstOf(ast.
|
|
114431
|
-
case "
|
|
115210
|
+
return propertyAstOf(ast.thunk(), key2);
|
|
115211
|
+
case "Objects": {
|
|
114432
115212
|
const property = ast.propertySignatures.find((entry) => entry.name === key2);
|
|
114433
115213
|
if (property !== undefined) {
|
|
114434
115214
|
return property.type;
|
|
114435
115215
|
}
|
|
114436
|
-
const index4 = ast.indexSignatures.find((entry) => entry.parameter._tag === "
|
|
115216
|
+
const index4 = ast.indexSignatures.find((entry) => entry.parameter._tag === "String");
|
|
114437
115217
|
return index4?.type;
|
|
114438
115218
|
}
|
|
114439
115219
|
case "Union": {
|
|
@@ -114449,21 +115229,17 @@ var propertyAstOf = (ast, key2) => {
|
|
|
114449
115229
|
};
|
|
114450
115230
|
var numberAstOf = (ast, index4) => {
|
|
114451
115231
|
switch (ast._tag) {
|
|
114452
|
-
case "Transformation":
|
|
114453
|
-
return numberAstOf(SchemaAST.typeAST(ast), index4);
|
|
114454
|
-
case "Refinement":
|
|
114455
|
-
return numberAstOf(ast.from, index4);
|
|
114456
115232
|
case "Suspend":
|
|
114457
|
-
return numberAstOf(ast.
|
|
114458
|
-
case "
|
|
115233
|
+
return numberAstOf(ast.thunk(), index4);
|
|
115234
|
+
case "Arrays": {
|
|
114459
115235
|
const element = ast.elements[index4];
|
|
114460
115236
|
if (element !== undefined) {
|
|
114461
|
-
return element
|
|
115237
|
+
return element;
|
|
114462
115238
|
}
|
|
114463
115239
|
if (ast.rest.length === 0) {
|
|
114464
115240
|
return;
|
|
114465
115241
|
}
|
|
114466
|
-
return unionAst(ast.rest
|
|
115242
|
+
return unionAst(ast.rest);
|
|
114467
115243
|
}
|
|
114468
115244
|
case "Union": {
|
|
114469
115245
|
const values2 = ast.types.flatMap((member) => {
|
|
@@ -114478,7 +115254,7 @@ var numberAstOf = (ast, index4) => {
|
|
|
114478
115254
|
};
|
|
114479
115255
|
var exactJsonSegments = (segments) => segments.every((segment) => segment.kind === "key" || segment.kind === "index");
|
|
114480
115256
|
var schemaAstAtExactJsonPath = (schema4, segments) => {
|
|
114481
|
-
let current =
|
|
115257
|
+
let current = schema4.ast;
|
|
114482
115258
|
for (const segment of segments) {
|
|
114483
115259
|
if (segment.kind === "key") {
|
|
114484
115260
|
const property = propertyAstOf(current, segment.key);
|
|
@@ -114508,28 +115284,28 @@ var unionSchemas = (schemas) => {
|
|
|
114508
115284
|
if (resolved.length === 1) {
|
|
114509
115285
|
return resolved[0];
|
|
114510
115286
|
}
|
|
114511
|
-
return Schema8.Union(
|
|
115287
|
+
return Schema8.Union(resolved);
|
|
114512
115288
|
};
|
|
114513
115289
|
var firstSelectedExpression = (plan) => {
|
|
114514
115290
|
const selection = getAst(plan).select;
|
|
114515
115291
|
return flattenSelection(selection)[0]?.expression;
|
|
114516
115292
|
};
|
|
114517
115293
|
var isJsonCompatibleAst = (ast) => {
|
|
115294
|
+
ast = SchemaAST.toType(ast);
|
|
114518
115295
|
switch (ast._tag) {
|
|
114519
|
-
case "
|
|
114520
|
-
case "
|
|
114521
|
-
case "
|
|
114522
|
-
case "
|
|
114523
|
-
case "
|
|
115296
|
+
case "String":
|
|
115297
|
+
case "Number":
|
|
115298
|
+
case "Boolean":
|
|
115299
|
+
case "Null":
|
|
115300
|
+
case "Arrays":
|
|
115301
|
+
case "Objects":
|
|
114524
115302
|
return true;
|
|
114525
115303
|
case "Literal":
|
|
114526
|
-
return
|
|
115304
|
+
return typeof ast.literal === "string" || typeof ast.literal === "number" || typeof ast.literal === "boolean";
|
|
114527
115305
|
case "Union":
|
|
114528
115306
|
return ast.types.every(isJsonCompatibleAst);
|
|
114529
|
-
case "Transformation":
|
|
114530
|
-
return isJsonCompatibleAst(SchemaAST.typeAST(ast));
|
|
114531
115307
|
case "Suspend":
|
|
114532
|
-
return isJsonCompatibleAst(ast.
|
|
115308
|
+
return isJsonCompatibleAst(ast.thunk());
|
|
114533
115309
|
default:
|
|
114534
115310
|
return false;
|
|
114535
115311
|
}
|
|
@@ -114538,14 +115314,14 @@ var jsonCompatibleSchema = (schema4) => {
|
|
|
114538
115314
|
if (schema4 === undefined) {
|
|
114539
115315
|
return;
|
|
114540
115316
|
}
|
|
114541
|
-
const ast = SchemaAST.
|
|
115317
|
+
const ast = SchemaAST.toType(schema4.ast);
|
|
114542
115318
|
return isJsonCompatibleAst(ast) ? schema4 : JsonValueSchema;
|
|
114543
115319
|
};
|
|
114544
115320
|
var buildStructSchema = (entries, context) => {
|
|
114545
115321
|
const fields2 = Object.fromEntries(entries.map((entry) => [entry.key, expressionRuntimeSchema(entry.value, context) ?? JsonValueSchema]));
|
|
114546
115322
|
return Schema8.Struct(fields2);
|
|
114547
115323
|
};
|
|
114548
|
-
var buildTupleSchema = (values2, context) => Schema8.Tuple(
|
|
115324
|
+
var buildTupleSchema = (values2, context) => Schema8.Tuple(values2.map((value) => expressionRuntimeSchema(value, context) ?? JsonValueSchema));
|
|
114549
115325
|
var deriveCaseSchema = (ast, context) => {
|
|
114550
115326
|
if (context === undefined) {
|
|
114551
115327
|
return unionSchemas([
|
|
@@ -114636,7 +115412,7 @@ var deriveRuntimeSchema = (expression, context) => {
|
|
|
114636
115412
|
return Schema8.String;
|
|
114637
115413
|
case "count":
|
|
114638
115414
|
case "jsonLength":
|
|
114639
|
-
return
|
|
115415
|
+
return FiniteNumberSchema;
|
|
114640
115416
|
case "max":
|
|
114641
115417
|
case "min":
|
|
114642
115418
|
return expressionRuntimeSchema(ast.value, context);
|
|
@@ -114649,7 +115425,7 @@ var deriveRuntimeSchema = (expression, context) => {
|
|
|
114649
115425
|
return selection === undefined ? undefined : expressionRuntimeSchema(selection, context);
|
|
114650
115426
|
}
|
|
114651
115427
|
case "window":
|
|
114652
|
-
return ast.function === "over" && ast.value !== undefined ? expressionRuntimeSchema(ast.value, context) :
|
|
115428
|
+
return ast.function === "over" && ast.value !== undefined ? expressionRuntimeSchema(ast.value, context) : FiniteNumberSchema;
|
|
114653
115429
|
case "jsonGet":
|
|
114654
115430
|
case "jsonPath":
|
|
114655
115431
|
case "jsonAccess":
|
|
@@ -114742,24 +115518,31 @@ var hasWriteCapability = (plan) => {
|
|
|
114742
115518
|
}
|
|
114743
115519
|
return false;
|
|
114744
115520
|
};
|
|
114745
|
-
var makeRowDecodeError = (rendered, projection, expression, raw, stage, cause, normalized) =>
|
|
114746
|
-
|
|
114747
|
-
|
|
114748
|
-
|
|
114749
|
-
|
|
114750
|
-
|
|
114751
|
-
|
|
114752
|
-
|
|
114753
|
-
|
|
114754
|
-
|
|
114755
|
-
|
|
114756
|
-
|
|
114757
|
-
|
|
114758
|
-
|
|
114759
|
-
|
|
114760
|
-
|
|
114761
|
-
|
|
114762
|
-
|
|
115521
|
+
var makeRowDecodeError = (rendered, projection, expression, raw, stage, cause, normalized) => {
|
|
115522
|
+
const schemaError = Schema9.isSchemaError(cause) ? {
|
|
115523
|
+
message: cause.message,
|
|
115524
|
+
issue: cause.issue
|
|
115525
|
+
} : undefined;
|
|
115526
|
+
return {
|
|
115527
|
+
_tag: "RowDecodeError",
|
|
115528
|
+
message: stage === "normalize" ? `Failed to normalize projection '${projection.alias}'` : `Failed to decode projection '${projection.alias}' against its runtime schema`,
|
|
115529
|
+
dialect: rendered.dialect,
|
|
115530
|
+
query: {
|
|
115531
|
+
sql: rendered.sql,
|
|
115532
|
+
params: rendered.params
|
|
115533
|
+
},
|
|
115534
|
+
projection: {
|
|
115535
|
+
alias: projection.alias,
|
|
115536
|
+
path: projection.path
|
|
115537
|
+
},
|
|
115538
|
+
dbType: expression[TypeId].dbType,
|
|
115539
|
+
raw,
|
|
115540
|
+
normalized,
|
|
115541
|
+
stage,
|
|
115542
|
+
cause,
|
|
115543
|
+
...schemaError === undefined ? {} : { schemaError }
|
|
115544
|
+
};
|
|
115545
|
+
};
|
|
114763
115546
|
var hasOptionalSourceDependency = (expression, scope) => {
|
|
114764
115547
|
const state = expression[TypeId];
|
|
114765
115548
|
return Object.keys(state.dependencies).some((sourceName) => !scope.absentSourceNames.has(sourceName) && scope.sourceModes.get(sourceName) === "optional");
|
|
@@ -114771,7 +115554,7 @@ var effectiveRuntimeNullability = (expression, scope) => {
|
|
|
114771
115554
|
return "always";
|
|
114772
115555
|
}
|
|
114773
115556
|
if (ast.kind === "column") {
|
|
114774
|
-
const key2 =
|
|
115557
|
+
const key2 = columnPredicateKey(ast.tableName, ast.columnName);
|
|
114775
115558
|
if (scope.absentSourceNames.has(ast.tableName) || scope.nullKeys.has(key2)) {
|
|
114776
115559
|
return "always";
|
|
114777
115560
|
}
|
|
@@ -114784,6 +115567,13 @@ var effectiveRuntimeNullability = (expression, scope) => {
|
|
|
114784
115567
|
}
|
|
114785
115568
|
return hasOptionalSourceDependency(expression, scope) ? "maybe" : nullability;
|
|
114786
115569
|
};
|
|
115570
|
+
var dbTypeAllowsTopLevelJsonNull = (dbType) => {
|
|
115571
|
+
if ("base" in dbType) {
|
|
115572
|
+
return dbTypeAllowsTopLevelJsonNull(dbType.base);
|
|
115573
|
+
}
|
|
115574
|
+
return "variant" in dbType && dbType.variant === "json" || dbType.runtime === "json";
|
|
115575
|
+
};
|
|
115576
|
+
var schemaAcceptsNull = (schema4) => schema4 !== undefined && Schema9.is(schema4)(null);
|
|
114787
115577
|
var decodeProjectionValue = (rendered, projection, expression, raw, scope, driverMode, valueMappings) => {
|
|
114788
115578
|
let normalized = raw;
|
|
114789
115579
|
if (driverMode === "raw") {
|
|
@@ -114795,13 +115585,17 @@ var decodeProjectionValue = (rendered, projection, expression, raw, scope, drive
|
|
|
114795
115585
|
driverValueMapping: expression[TypeId].driverValueMapping,
|
|
114796
115586
|
valueMappings
|
|
114797
115587
|
});
|
|
114798
|
-
} catch (
|
|
114799
|
-
throw makeRowDecodeError(rendered, projection, expression, raw, "normalize",
|
|
115588
|
+
} catch (cause2) {
|
|
115589
|
+
throw makeRowDecodeError(rendered, projection, expression, raw, "normalize", cause2);
|
|
114800
115590
|
}
|
|
114801
115591
|
}
|
|
114802
115592
|
const nullability = effectiveRuntimeNullability(expression, scope);
|
|
115593
|
+
const schema4 = expressionRuntimeSchema(expression, { assumptions: scope.assumptions });
|
|
114803
115594
|
if (normalized === null) {
|
|
114804
115595
|
if (nullability === "never") {
|
|
115596
|
+
if (dbTypeAllowsTopLevelJsonNull(expression[TypeId].dbType) && schemaAcceptsNull(schema4)) {
|
|
115597
|
+
return null;
|
|
115598
|
+
}
|
|
114805
115599
|
throw makeRowDecodeError(rendered, projection, expression, raw, "schema", new Error("Received null for a non-null projection"), normalized);
|
|
114806
115600
|
}
|
|
114807
115601
|
return null;
|
|
@@ -114809,49 +115603,51 @@ var decodeProjectionValue = (rendered, projection, expression, raw, scope, drive
|
|
|
114809
115603
|
if (nullability === "always") {
|
|
114810
115604
|
throw makeRowDecodeError(rendered, projection, expression, raw, "schema", new Error("Received non-null for an always-null projection"), normalized);
|
|
114811
115605
|
}
|
|
114812
|
-
|
|
115606
|
+
if (dbTypeAllowsTopLevelJsonNull(expression[TypeId].dbType) && !isJsonValue(normalized)) {
|
|
115607
|
+
throw makeRowDecodeError(rendered, projection, expression, raw, "schema", new Error("Expected a JSON value"), normalized);
|
|
115608
|
+
}
|
|
114813
115609
|
if (schema4 === undefined) {
|
|
114814
115610
|
return normalized;
|
|
114815
115611
|
}
|
|
114816
115612
|
if (Schema9.is(schema4)(normalized)) {
|
|
114817
115613
|
return normalized;
|
|
114818
115614
|
}
|
|
114819
|
-
|
|
114820
|
-
|
|
114821
|
-
|
|
114822
|
-
throw makeRowDecodeError(rendered, projection, expression, raw, "schema", cause, normalized);
|
|
115615
|
+
const decoded = Schema9.decodeUnknownExit(schema4)(normalized);
|
|
115616
|
+
if (Exit.isSuccess(decoded)) {
|
|
115617
|
+
return decoded.value;
|
|
114823
115618
|
}
|
|
115619
|
+
const cause = Option.match(Exit.findErrorOption(decoded), {
|
|
115620
|
+
onNone: () => decoded.cause,
|
|
115621
|
+
onSome: (schemaError) => schemaError
|
|
115622
|
+
});
|
|
115623
|
+
throw makeRowDecodeError(rendered, projection, expression, raw, "schema", cause, normalized);
|
|
114824
115624
|
};
|
|
114825
115625
|
var makeRowDecoder = (rendered, plan, options2 = {}) => {
|
|
114826
115626
|
const projections = flattenSelection(getAst(plan).select);
|
|
114827
|
-
const
|
|
115627
|
+
const byPath = new Map(projections.map((projection) => [JSON.stringify(projection.path), projection.expression]));
|
|
114828
115628
|
const driverMode = options2.driverMode ?? "raw";
|
|
114829
115629
|
const valueMappings = options2.valueMappings ?? rendered.valueMappings;
|
|
114830
115630
|
const scope = resolveImplicationScope(plan[TypeId3].available, getQueryState(plan).assumptions);
|
|
114831
115631
|
return (row) => {
|
|
114832
115632
|
const decoded = {};
|
|
114833
115633
|
for (const projection of rendered.projections) {
|
|
114834
|
-
|
|
114835
|
-
continue;
|
|
114836
|
-
}
|
|
114837
|
-
const expression = byAlias.get(projection.alias);
|
|
115634
|
+
const expression = byPath.get(JSON.stringify(projection.path));
|
|
114838
115635
|
if (expression === undefined) {
|
|
114839
|
-
|
|
115636
|
+
throw new Error(`Rendered projection path '${projection.path.join(".")}' does not exist in the query selection`);
|
|
115637
|
+
}
|
|
115638
|
+
if (!(projection.alias in row)) {
|
|
115639
|
+
throw makeRowDecodeError(rendered, projection, expression, undefined, "schema", new Error(`Missing required projection alias '${projection.alias}'`));
|
|
114840
115640
|
}
|
|
114841
115641
|
setPath2(decoded, projection.path, decodeProjectionValue(rendered, projection, expression, row[projection.alias], scope, driverMode, valueMappings));
|
|
114842
115642
|
}
|
|
114843
115643
|
return decoded;
|
|
114844
115644
|
};
|
|
114845
115645
|
};
|
|
114846
|
-
var decodeChunk = (rendered, plan, rows, options2 = {}) => {
|
|
114847
|
-
const decodeRow = makeRowDecoder(rendered, plan, options2);
|
|
114848
|
-
return Chunk.unsafeFromArray(Chunk.toReadonlyArray(rows).map((row) => decodeRow(row)));
|
|
114849
|
-
};
|
|
114850
115646
|
var decodeRows = (rendered, plan, rows, options2 = {}) => {
|
|
114851
115647
|
const decodeRow = makeRowDecoder(rendered, plan, options2);
|
|
114852
115648
|
return rows.map((row) => decodeRow(row));
|
|
114853
115649
|
};
|
|
114854
|
-
var
|
|
115650
|
+
var make3 = (dialect, execute) => ({
|
|
114855
115651
|
dialect,
|
|
114856
115652
|
execute(plan) {
|
|
114857
115653
|
return execute(plan);
|
|
@@ -114874,7 +115670,7 @@ function driver(dialect, executeOrHandlers) {
|
|
|
114874
115670
|
}
|
|
114875
115671
|
};
|
|
114876
115672
|
}
|
|
114877
|
-
var streamFromSqlClient = (query) => Stream.
|
|
115673
|
+
var streamFromSqlClient = (query) => Stream.unwrap(Effect.flatMap(SqlClient.SqlClient, (sql) => Effect.flatMap(Effect.serviceOption(sql.transactionService), Option.match({
|
|
114878
115674
|
onNone: () => sql.reserve,
|
|
114879
115675
|
onSome: ([connection]) => Effect.succeed(connection)
|
|
114880
115676
|
})).pipe(Effect.map((connection) => connection.executeStream(query.sql, [...query.params], undefined)))));
|
|
@@ -114883,16 +115679,42 @@ var withSavepoint = (effect) => Effect.flatMap(SqlClient.SqlClient, (sql) => sql
|
|
|
114883
115679
|
|
|
114884
115680
|
// src/internal/renderer.ts
|
|
114885
115681
|
var TypeId8 = Symbol.for("effect-qb/Renderer");
|
|
114886
|
-
|
|
115682
|
+
var projectionPathKey = (path2) => JSON.stringify(path2);
|
|
115683
|
+
var formatProjectionPath2 = (path2) => path2.join(".");
|
|
115684
|
+
var validateProjectionPathsMatchSelection = (plan, projections) => {
|
|
115685
|
+
const expected = flattenSelection(getAst(plan).select);
|
|
115686
|
+
const expectedPaths = new Set(expected.map((projection) => projectionPathKey(projection.path)));
|
|
115687
|
+
const actualPaths = new Set(projections.map((projection) => projectionPathKey(projection.path)));
|
|
115688
|
+
for (const projection of projections) {
|
|
115689
|
+
if (!expectedPaths.has(projectionPathKey(projection.path))) {
|
|
115690
|
+
throw new Error(`Projection path ${formatProjectionPath2(projection.path)} does not exist in the query selection`);
|
|
115691
|
+
}
|
|
115692
|
+
}
|
|
115693
|
+
for (const projection of expected) {
|
|
115694
|
+
if (!actualPaths.has(projectionPathKey(projection.path))) {
|
|
115695
|
+
throw new Error(`Projection path ${formatProjectionPath2(projection.path)} is missing from rendered projections`);
|
|
115696
|
+
}
|
|
115697
|
+
}
|
|
115698
|
+
};
|
|
115699
|
+
function make4(dialect, render) {
|
|
114887
115700
|
if (typeof render !== "function") {
|
|
114888
115701
|
throw new Error(`Renderer.make requires an explicit render implementation for dialect: ${dialect}`);
|
|
114889
115702
|
}
|
|
114890
115703
|
return {
|
|
114891
115704
|
dialect,
|
|
114892
115705
|
render(plan) {
|
|
115706
|
+
const required = currentRequiredList(plan[TypeId3].required);
|
|
115707
|
+
if (required.length > 0) {
|
|
115708
|
+
throw new Error(`query references sources that are not yet in scope: ${required.join(", ")}`);
|
|
115709
|
+
}
|
|
115710
|
+
const planDialect = plan[TypeId3].dialect;
|
|
115711
|
+
if (planDialect !== dialect) {
|
|
115712
|
+
throw new Error("effect-qb: plan dialect is not compatible with the target renderer or executor");
|
|
115713
|
+
}
|
|
114893
115714
|
const rendered = render(plan);
|
|
114894
115715
|
const projections = rendered.projections ?? [];
|
|
114895
115716
|
validateProjections(projections);
|
|
115717
|
+
validateProjectionPathsMatchSelection(plan, projections);
|
|
114896
115718
|
return {
|
|
114897
115719
|
sql: rendered.sql,
|
|
114898
115720
|
params: rendered.params ?? [],
|
|
@@ -114938,16 +115760,19 @@ var mysqlDialect = {
|
|
|
114938
115760
|
}
|
|
114939
115761
|
};
|
|
114940
115762
|
|
|
115763
|
+
// src/mysql/internal/sql-expression-renderer.ts
|
|
115764
|
+
import * as Schema10 from "effect/Schema";
|
|
115765
|
+
|
|
114941
115766
|
// src/internal/aggregation-validation.ts
|
|
114942
|
-
var
|
|
115767
|
+
var isExpression3 = (value) => typeof value === "object" && value !== null && (TypeId in value);
|
|
114943
115768
|
var selectionHasAggregate = (selection) => {
|
|
114944
|
-
if (
|
|
115769
|
+
if (isExpression3(selection)) {
|
|
114945
115770
|
return selection[TypeId].kind === "aggregate";
|
|
114946
115771
|
}
|
|
114947
115772
|
return Object.values(selection).some((value) => selectionHasAggregate(value));
|
|
114948
115773
|
};
|
|
114949
115774
|
var isGroupedSelectionValid = (selection, groupedExpressions) => {
|
|
114950
|
-
if (
|
|
115775
|
+
if (isExpression3(selection)) {
|
|
114951
115776
|
const aggregation = selection[TypeId].kind;
|
|
114952
115777
|
if (aggregation === "aggregate") {
|
|
114953
115778
|
return true;
|
|
@@ -115052,7 +115877,46 @@ var renderCastType = (dialect, dbType) => {
|
|
|
115052
115877
|
return dbType.kind;
|
|
115053
115878
|
}
|
|
115054
115879
|
};
|
|
115055
|
-
var
|
|
115880
|
+
var renderMysqlDdlString = (value) => `'${value.replaceAll("'", "''")}'`;
|
|
115881
|
+
var renderMysqlDdlBytes = (value) => `x'${Array.from(value, (byte) => byte.toString(16).padStart(2, "0")).join("")}'`;
|
|
115882
|
+
var renderMysqlDdlLiteral = (value, state, context = {}) => {
|
|
115883
|
+
const driverValue = toDriverValue(value, {
|
|
115884
|
+
dialect: "mysql",
|
|
115885
|
+
valueMappings: state.valueMappings,
|
|
115886
|
+
...context
|
|
115887
|
+
});
|
|
115888
|
+
if (driverValue === null) {
|
|
115889
|
+
return "null";
|
|
115890
|
+
}
|
|
115891
|
+
switch (typeof driverValue) {
|
|
115892
|
+
case "boolean":
|
|
115893
|
+
return driverValue ? "true" : "false";
|
|
115894
|
+
case "number":
|
|
115895
|
+
if (!Number.isFinite(driverValue)) {
|
|
115896
|
+
throw new Error("Expected a finite numeric value");
|
|
115897
|
+
}
|
|
115898
|
+
return String(driverValue);
|
|
115899
|
+
case "bigint":
|
|
115900
|
+
return driverValue.toString();
|
|
115901
|
+
case "string":
|
|
115902
|
+
return renderMysqlDdlString(driverValue);
|
|
115903
|
+
case "object":
|
|
115904
|
+
if (driverValue instanceof Uint8Array) {
|
|
115905
|
+
return renderMysqlDdlBytes(driverValue);
|
|
115906
|
+
}
|
|
115907
|
+
break;
|
|
115908
|
+
}
|
|
115909
|
+
throw new Error("Unsupported mysql DDL literal value");
|
|
115910
|
+
};
|
|
115911
|
+
var renderDdlExpression = (expression, state, dialect) => {
|
|
115912
|
+
if (isSchemaExpression(expression)) {
|
|
115913
|
+
return render(expression);
|
|
115914
|
+
}
|
|
115915
|
+
return renderExpression(expression, state, {
|
|
115916
|
+
...dialect,
|
|
115917
|
+
renderLiteral: renderMysqlDdlLiteral
|
|
115918
|
+
});
|
|
115919
|
+
};
|
|
115056
115920
|
var renderMysqlMutationLimit = (expression, state, dialect) => {
|
|
115057
115921
|
const ast = expression[TypeId2];
|
|
115058
115922
|
if (ast.kind === "literal" && typeof ast.value === "number" && Number.isInteger(ast.value) && ast.value >= 0) {
|
|
@@ -115087,30 +115951,52 @@ var renderCreateTableSql = (targetSource, state, dialect, ifNotExists) => {
|
|
|
115087
115951
|
definitions.push(`${option2.name ? `constraint ${dialect.quoteIdentifier(option2.name)} ` : ""}primary key (${option2.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")})${option2.deferrable ? ` deferrable${option2.initiallyDeferred ? " initially deferred" : ""}` : ""}`);
|
|
115088
115952
|
break;
|
|
115089
115953
|
case "unique":
|
|
115954
|
+
if (option2.nullsNotDistinct || option2.deferrable || option2.initiallyDeferred) {
|
|
115955
|
+
throw new Error("Unsupported mysql unique constraint options");
|
|
115956
|
+
}
|
|
115090
115957
|
definitions.push(`${option2.name ? `constraint ${dialect.quoteIdentifier(option2.name)} ` : ""}unique${option2.nullsNotDistinct ? " nulls not distinct" : ""} (${option2.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")})${option2.deferrable ? ` deferrable${option2.initiallyDeferred ? " initially deferred" : ""}` : ""}`);
|
|
115091
115958
|
break;
|
|
115092
115959
|
case "foreignKey": {
|
|
115093
115960
|
const reference = option2.references();
|
|
115094
|
-
definitions.push(`${option2.name ? `constraint ${dialect.quoteIdentifier(option2.name)} ` : ""}foreign key (${option2.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")}) references ${dialect.renderTableReference(reference.tableName, reference.tableName, reference.schemaName)} (${reference.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")})${option2.onDelete ? ` on delete ${option2.onDelete
|
|
115961
|
+
definitions.push(`${option2.name ? `constraint ${dialect.quoteIdentifier(option2.name)} ` : ""}foreign key (${option2.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")}) references ${dialect.renderTableReference(reference.tableName, reference.tableName, reference.schemaName)} (${reference.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")})${option2.onDelete !== undefined ? ` on delete ${renderReferentialAction(option2.onDelete)}` : ""}${option2.onUpdate !== undefined ? ` on update ${renderReferentialAction(option2.onUpdate)}` : ""}${option2.deferrable ? ` deferrable${option2.initiallyDeferred ? " initially deferred" : ""}` : ""}`);
|
|
115095
115962
|
break;
|
|
115096
115963
|
}
|
|
115097
115964
|
case "check":
|
|
115098
|
-
definitions.push(`constraint ${dialect.quoteIdentifier(option2.name)} check (${renderDdlExpression(option2.predicate, state, dialect)})${option2.noInherit ? " no inherit" : ""}`);
|
|
115965
|
+
definitions.push(`constraint ${dialect.quoteIdentifier(option2.name)} check (${renderDdlExpression(option2.predicate, { ...state, rowLocalColumns: true }, dialect)})${option2.noInherit ? " no inherit" : ""}`);
|
|
115099
115966
|
break;
|
|
115100
115967
|
case "index":
|
|
115101
115968
|
break;
|
|
115969
|
+
default:
|
|
115970
|
+
throw new Error("Unsupported table option kind");
|
|
115102
115971
|
}
|
|
115103
115972
|
}
|
|
115104
115973
|
return `create table${ifNotExists ? " if not exists" : ""} ${renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect)} (${definitions.join(", ")})`;
|
|
115105
115974
|
};
|
|
115106
115975
|
var renderCreateIndexSql = (targetSource, ddl, state, dialect) => {
|
|
115976
|
+
if (ddl.ifNotExists) {
|
|
115977
|
+
throw new Error("Unsupported mysql create index options");
|
|
115978
|
+
}
|
|
115107
115979
|
const maybeIfNotExists = dialect.name === "postgres" && ddl.ifNotExists ? " if not exists" : "";
|
|
115108
115980
|
return `create${ddl.unique ? " unique" : ""} index${maybeIfNotExists} ${dialect.quoteIdentifier(ddl.name)} on ${renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect)} (${ddl.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")})`;
|
|
115109
115981
|
};
|
|
115110
|
-
var renderDropIndexSql = (targetSource, ddl, state, dialect) =>
|
|
115111
|
-
|
|
115112
|
-
|
|
115113
|
-
|
|
115982
|
+
var renderDropIndexSql = (targetSource, ddl, state, dialect) => {
|
|
115983
|
+
if (ddl.ifExists) {
|
|
115984
|
+
throw new Error("Unsupported mysql drop index options");
|
|
115985
|
+
}
|
|
115986
|
+
return dialect.name === "postgres" ? `drop index${ddl.ifExists ? " if exists" : ""} ${dialect.quoteIdentifier(ddl.name)}` : `drop index ${dialect.quoteIdentifier(ddl.name)} on ${renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect)}`;
|
|
115987
|
+
};
|
|
115988
|
+
var isExpression4 = (value) => value !== null && typeof value === "object" && (TypeId in value);
|
|
115989
|
+
var isJsonDbType2 = (dbType) => {
|
|
115990
|
+
if (dbType.kind === "jsonb" || dbType.kind === "json") {
|
|
115991
|
+
return true;
|
|
115992
|
+
}
|
|
115993
|
+
if (!("variant" in dbType)) {
|
|
115994
|
+
return false;
|
|
115995
|
+
}
|
|
115996
|
+
const variant = dbType.variant;
|
|
115997
|
+
return variant === "json" || variant === "jsonb";
|
|
115998
|
+
};
|
|
115999
|
+
var isJsonExpression = (value) => isExpression4(value) && isJsonDbType2(value[TypeId].dbType);
|
|
115114
116000
|
var unsupportedJsonFeature = (dialect, feature) => {
|
|
115115
116001
|
const error = new Error(`Unsupported JSON feature for ${dialect.name}: ${feature}`);
|
|
115116
116002
|
Object.assign(error, {
|
|
@@ -115153,15 +116039,16 @@ var extractJsonPathSegments = (node) => {
|
|
|
115153
116039
|
};
|
|
115154
116040
|
var extractJsonValue = (node) => node.newValue ?? node.insert ?? node.right;
|
|
115155
116041
|
var renderJsonPathSegment = (segment) => {
|
|
116042
|
+
const renderKey = (value) => /^[A-Za-z_][A-Za-z0-9_]*$/.test(value) ? `.${value}` : `.${JSON.stringify(value)}`;
|
|
115156
116043
|
if (typeof segment === "string") {
|
|
115157
|
-
return
|
|
116044
|
+
return renderKey(segment);
|
|
115158
116045
|
}
|
|
115159
116046
|
if (typeof segment === "number") {
|
|
115160
116047
|
return `[${segment}]`;
|
|
115161
116048
|
}
|
|
115162
116049
|
switch (segment.kind) {
|
|
115163
116050
|
case "key":
|
|
115164
|
-
return
|
|
116051
|
+
return renderKey(segment.key);
|
|
115165
116052
|
case "index":
|
|
115166
116053
|
return `[${segment.index}]`;
|
|
115167
116054
|
case "wildcard":
|
|
@@ -115174,14 +116061,45 @@ var renderJsonPathSegment = (segment) => {
|
|
|
115174
116061
|
throw new Error("Unsupported JSON path segment");
|
|
115175
116062
|
}
|
|
115176
116063
|
};
|
|
115177
|
-
var
|
|
116064
|
+
var renderMySqlJsonIndex = (index4) => index4 >= 0 ? String(index4) : index4 === -1 ? "last" : `last-${Math.abs(index4) - 1}`;
|
|
116065
|
+
var renderMySqlJsonPathSegment = (segment) => {
|
|
116066
|
+
if (typeof segment === "number") {
|
|
116067
|
+
return `[${renderMySqlJsonIndex(segment)}]`;
|
|
116068
|
+
}
|
|
116069
|
+
if (typeof segment === "object" && segment !== null && segment.kind === "index") {
|
|
116070
|
+
return `[${renderMySqlJsonIndex(segment.index)}]`;
|
|
116071
|
+
}
|
|
116072
|
+
if (typeof segment === "object" && segment !== null && segment.kind === "slice") {
|
|
116073
|
+
return `[${renderMySqlJsonIndex(segment.start ?? 0)} to ${segment.end === undefined ? "last" : renderMySqlJsonIndex(segment.end)}]`;
|
|
116074
|
+
}
|
|
116075
|
+
if (typeof segment === "object" && segment !== null && segment.kind === "descend") {
|
|
116076
|
+
return "**";
|
|
116077
|
+
}
|
|
116078
|
+
return renderJsonPathSegment(segment);
|
|
116079
|
+
};
|
|
116080
|
+
var renderJsonPathStringLiteral = (segments, renderSegment = renderJsonPathSegment) => {
|
|
115178
116081
|
let path2 = "$";
|
|
115179
116082
|
for (const segment of segments) {
|
|
115180
|
-
path2 +=
|
|
116083
|
+
path2 += renderSegment(segment);
|
|
115181
116084
|
}
|
|
115182
116085
|
return path2;
|
|
115183
116086
|
};
|
|
115184
|
-
var renderMySqlJsonPath = (segments, state, dialect) => dialect.renderLiteral(renderJsonPathStringLiteral(segments), state);
|
|
116087
|
+
var renderMySqlJsonPath = (segments, state, dialect) => dialect.renderLiteral(renderJsonPathStringLiteral(segments, renderMySqlJsonPathSegment), state);
|
|
116088
|
+
var isJsonArrayIndexSegment = (segment) => typeof segment === "number" || typeof segment === "object" && segment !== null && segment.kind === "index";
|
|
116089
|
+
var renderMySqlJsonInsertPath = (segments, insertAfter, state, dialect) => {
|
|
116090
|
+
if (!insertAfter || segments.length === 0) {
|
|
116091
|
+
return renderMySqlJsonPath(segments, state, dialect);
|
|
116092
|
+
}
|
|
116093
|
+
const last = segments[segments.length - 1];
|
|
116094
|
+
const nextSegments = segments.slice(0, -1);
|
|
116095
|
+
if (typeof last === "number") {
|
|
116096
|
+
return renderMySqlJsonPath([...nextSegments, last + 1], state, dialect);
|
|
116097
|
+
}
|
|
116098
|
+
if (typeof last === "object" && last !== null && last.kind === "index") {
|
|
116099
|
+
return renderMySqlJsonPath([...nextSegments, { ...last, index: last.index + 1 }], state, dialect);
|
|
116100
|
+
}
|
|
116101
|
+
return renderMySqlJsonPath(segments, state, dialect);
|
|
116102
|
+
};
|
|
115185
116103
|
var renderPostgresJsonPathArray = (segments, state, dialect) => `array[${segments.map((segment) => {
|
|
115186
116104
|
if (typeof segment === "string") {
|
|
115187
116105
|
return dialect.renderLiteral(segment, state);
|
|
@@ -115210,7 +116128,7 @@ var renderPostgresJsonAccessStep = (segment, textMode, state, dialect) => {
|
|
|
115210
116128
|
}
|
|
115211
116129
|
};
|
|
115212
116130
|
var renderPostgresJsonValue = (value, state, dialect) => {
|
|
115213
|
-
if (!
|
|
116131
|
+
if (!isExpression4(value)) {
|
|
115214
116132
|
throw new Error("Expected a JSON expression");
|
|
115215
116133
|
}
|
|
115216
116134
|
const rendered = renderExpression(value, state, dialect);
|
|
@@ -115223,23 +116141,48 @@ var expressionDriverContext = (expression, state, dialect) => ({
|
|
|
115223
116141
|
runtimeSchema: expression[TypeId].runtimeSchema,
|
|
115224
116142
|
driverValueMapping: expression[TypeId].driverValueMapping
|
|
115225
116143
|
});
|
|
115226
|
-
var
|
|
115227
|
-
|
|
115228
|
-
|
|
115229
|
-
|
|
115230
|
-
|
|
115231
|
-
|
|
115232
|
-
|
|
115233
|
-
}
|
|
116144
|
+
var renderMySqlStructuredJsonLiteral = (expression, state) => {
|
|
116145
|
+
const ast = expression[TypeId2];
|
|
116146
|
+
if (ast.kind !== "literal" || ast.value === null || typeof ast.value !== "object") {
|
|
116147
|
+
return;
|
|
116148
|
+
}
|
|
116149
|
+
state.params.push(JSON.stringify(ast.value));
|
|
116150
|
+
return "cast(? as json)";
|
|
116151
|
+
};
|
|
116152
|
+
var renderJsonInputExpression = (expression, state, dialect) => {
|
|
116153
|
+
if (dialect.name === "mysql") {
|
|
116154
|
+
const jsonLiteral = renderMySqlStructuredJsonLiteral(expression, state);
|
|
116155
|
+
if (jsonLiteral !== undefined) {
|
|
116156
|
+
return jsonLiteral;
|
|
116157
|
+
}
|
|
116158
|
+
}
|
|
116159
|
+
return renderJsonSelectSql(renderExpression(expression, state, dialect), expressionDriverContext(expression, state, dialect));
|
|
116160
|
+
};
|
|
116161
|
+
var encodeArrayValues = (values2, column2, state, dialect) => values2.map((value) => {
|
|
116162
|
+
if (value === null && column2.metadata.nullable) {
|
|
116163
|
+
return null;
|
|
116164
|
+
}
|
|
116165
|
+
const runtimeSchemaAccepts = column2.schema !== undefined && Schema10.is(column2.schema)(value);
|
|
116166
|
+
const normalizedValue = runtimeSchemaAccepts ? value : normalizeDbValue(column2.metadata.dbType, value);
|
|
116167
|
+
const encodedValue = column2.schema === undefined || runtimeSchemaAccepts ? normalizedValue : Schema10.decodeUnknownSync(column2.schema)(normalizedValue);
|
|
116168
|
+
return toDriverValue(encodedValue, {
|
|
116169
|
+
dialect: dialect.name,
|
|
116170
|
+
valueMappings: state.valueMappings,
|
|
116171
|
+
dbType: column2.metadata.dbType,
|
|
116172
|
+
runtimeSchema: column2.schema,
|
|
116173
|
+
driverValueMapping: column2.metadata.driverValueMapping
|
|
116174
|
+
});
|
|
116175
|
+
});
|
|
115234
116176
|
var renderPostgresJsonKind = (value) => value[TypeId].dbType.kind === "jsonb" ? "jsonb" : "json";
|
|
115235
116177
|
var renderJsonOpaquePath = (value, state, dialect) => {
|
|
115236
116178
|
if (isJsonPathValue2(value)) {
|
|
115237
|
-
|
|
116179
|
+
const renderSegment = dialect.name === "mysql" ? renderMySqlJsonPathSegment : renderJsonPathSegment;
|
|
116180
|
+
return dialect.renderLiteral(renderJsonPathStringLiteral(value.segments, renderSegment), state);
|
|
115238
116181
|
}
|
|
115239
116182
|
if (typeof value === "string") {
|
|
115240
116183
|
return dialect.renderLiteral(value, state);
|
|
115241
116184
|
}
|
|
115242
|
-
if (
|
|
116185
|
+
if (isExpression4(value)) {
|
|
115243
116186
|
return renderExpression(value, state, dialect);
|
|
115244
116187
|
}
|
|
115245
116188
|
throw new Error("Unsupported SQL/JSON path input");
|
|
@@ -115257,7 +116200,7 @@ var renderFunctionCall = (name, args, state, dialect) => {
|
|
|
115257
116200
|
if (source === undefined) {
|
|
115258
116201
|
throw new Error("Unsupported SQL extract expression");
|
|
115259
116202
|
}
|
|
115260
|
-
const fieldRuntime =
|
|
116203
|
+
const fieldRuntime = isExpression4(field) && field[TypeId].dbType.kind === "text" && typeof field[TypeId].runtime === "string" ? field[TypeId].runtime : undefined;
|
|
115261
116204
|
const renderedField = fieldRuntime ?? renderExpression(field, state, dialect);
|
|
115262
116205
|
return `extract(${renderedField} from ${renderExpression(source, state, dialect)})`;
|
|
115263
116206
|
}
|
|
@@ -115295,7 +116238,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115295
116238
|
case "jsonPathText":
|
|
115296
116239
|
case "jsonAccessText":
|
|
115297
116240
|
case "jsonTraverseText": {
|
|
115298
|
-
if (!
|
|
116241
|
+
if (!isExpression4(base) || segments.length === 0) {
|
|
115299
116242
|
return;
|
|
115300
116243
|
}
|
|
115301
116244
|
const baseSql = renderExpression(base, state, dialect);
|
|
@@ -115318,7 +116261,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115318
116261
|
case "jsonKeyExists":
|
|
115319
116262
|
case "jsonHasAnyKeys":
|
|
115320
116263
|
case "jsonHasAllKeys": {
|
|
115321
|
-
if (!
|
|
116264
|
+
if (!isExpression4(base)) {
|
|
115322
116265
|
return;
|
|
115323
116266
|
}
|
|
115324
116267
|
const baseSql = dialect.name === "postgres" ? renderPostgresJsonValue(base, state, dialect) : renderExpression(base, state, dialect);
|
|
@@ -115337,21 +116280,22 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115337
116280
|
}
|
|
115338
116281
|
if (dialect.name === "mysql") {
|
|
115339
116282
|
const mode = kind === "jsonHasAllKeys" ? "all" : "one";
|
|
116283
|
+
const modeSql = dialect.renderLiteral(mode, state);
|
|
115340
116284
|
const paths = keys.map((segment) => renderMySqlJsonPath([segment], state, dialect)).join(", ");
|
|
115341
|
-
return `json_contains_path(${baseSql}, ${
|
|
116285
|
+
return `json_contains_path(${baseSql}, ${modeSql}, ${paths})`;
|
|
115342
116286
|
}
|
|
115343
116287
|
return;
|
|
115344
116288
|
}
|
|
115345
116289
|
case "jsonConcat":
|
|
115346
116290
|
case "jsonMerge": {
|
|
115347
|
-
if (!
|
|
116291
|
+
if (!isExpression4(ast.left) || !isExpression4(ast.right)) {
|
|
115348
116292
|
return;
|
|
115349
116293
|
}
|
|
115350
116294
|
if (dialect.name === "postgres") {
|
|
115351
116295
|
return `(${renderPostgresJsonValue(ast.left, state, dialect)} || ${renderPostgresJsonValue(ast.right, state, dialect)})`;
|
|
115352
116296
|
}
|
|
115353
116297
|
if (dialect.name === "mysql") {
|
|
115354
|
-
return `json_merge_preserve(${
|
|
116298
|
+
return `json_merge_preserve(${renderJsonInputExpression(ast.left, state, dialect)}, ${renderJsonInputExpression(ast.right, state, dialect)})`;
|
|
115355
116299
|
}
|
|
115356
116300
|
return;
|
|
115357
116301
|
}
|
|
@@ -115381,29 +116325,29 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115381
116325
|
return;
|
|
115382
116326
|
}
|
|
115383
116327
|
case "jsonToJson":
|
|
115384
|
-
if (!
|
|
116328
|
+
if (!isExpression4(base)) {
|
|
115385
116329
|
return;
|
|
115386
116330
|
}
|
|
115387
116331
|
if (dialect.name === "postgres") {
|
|
115388
116332
|
return `to_json(${renderJsonInputExpression(base, state, dialect)})`;
|
|
115389
116333
|
}
|
|
115390
116334
|
if (dialect.name === "mysql") {
|
|
115391
|
-
return `cast(${renderExpression(base, state, dialect)} as json)`;
|
|
116335
|
+
return renderMySqlStructuredJsonLiteral(base, state) ?? `cast(${renderExpression(base, state, dialect)} as json)`;
|
|
115392
116336
|
}
|
|
115393
116337
|
return;
|
|
115394
116338
|
case "jsonToJsonb":
|
|
115395
|
-
if (!
|
|
116339
|
+
if (!isExpression4(base)) {
|
|
115396
116340
|
return;
|
|
115397
116341
|
}
|
|
115398
116342
|
if (dialect.name === "postgres") {
|
|
115399
116343
|
return `to_jsonb(${renderJsonInputExpression(base, state, dialect)})`;
|
|
115400
116344
|
}
|
|
115401
116345
|
if (dialect.name === "mysql") {
|
|
115402
|
-
return `cast(${renderExpression(base, state, dialect)} as json)`;
|
|
116346
|
+
return renderMySqlStructuredJsonLiteral(base, state) ?? `cast(${renderExpression(base, state, dialect)} as json)`;
|
|
115403
116347
|
}
|
|
115404
116348
|
return;
|
|
115405
116349
|
case "jsonTypeOf":
|
|
115406
|
-
if (!
|
|
116350
|
+
if (!isExpression4(base)) {
|
|
115407
116351
|
return;
|
|
115408
116352
|
}
|
|
115409
116353
|
if (dialect.name === "postgres") {
|
|
@@ -115415,7 +116359,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115415
116359
|
}
|
|
115416
116360
|
return;
|
|
115417
116361
|
case "jsonLength":
|
|
115418
|
-
if (!
|
|
116362
|
+
if (!isExpression4(base)) {
|
|
115419
116363
|
return;
|
|
115420
116364
|
}
|
|
115421
116365
|
if (dialect.name === "postgres") {
|
|
@@ -115430,7 +116374,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115430
116374
|
}
|
|
115431
116375
|
return;
|
|
115432
116376
|
case "jsonKeys":
|
|
115433
|
-
if (!
|
|
116377
|
+
if (!isExpression4(base)) {
|
|
115434
116378
|
return;
|
|
115435
116379
|
}
|
|
115436
116380
|
if (dialect.name === "postgres") {
|
|
@@ -115444,7 +116388,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115444
116388
|
}
|
|
115445
116389
|
return;
|
|
115446
116390
|
case "jsonStripNulls":
|
|
115447
|
-
if (!
|
|
116391
|
+
if (!isExpression4(base)) {
|
|
115448
116392
|
return;
|
|
115449
116393
|
}
|
|
115450
116394
|
if (dialect.name === "postgres") {
|
|
@@ -115455,7 +116399,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115455
116399
|
case "jsonDelete":
|
|
115456
116400
|
case "jsonDeletePath":
|
|
115457
116401
|
case "jsonRemove": {
|
|
115458
|
-
if (!
|
|
116402
|
+
if (!isExpression4(base) || segments.length === 0) {
|
|
115459
116403
|
return;
|
|
115460
116404
|
}
|
|
115461
116405
|
if (dialect.name === "postgres") {
|
|
@@ -115467,17 +116411,17 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115467
116411
|
return `(${baseSql} #- ${renderPostgresJsonPathArray(segments, state, dialect)})`;
|
|
115468
116412
|
}
|
|
115469
116413
|
if (dialect.name === "mysql") {
|
|
115470
|
-
return `json_remove(${renderExpression(base, state, dialect)}, ${
|
|
116414
|
+
return `json_remove(${renderExpression(base, state, dialect)}, ${renderMySqlJsonPath(segments, state, dialect)})`;
|
|
115471
116415
|
}
|
|
115472
116416
|
return;
|
|
115473
116417
|
}
|
|
115474
116418
|
case "jsonSet":
|
|
115475
116419
|
case "jsonInsert": {
|
|
115476
|
-
if (!
|
|
116420
|
+
if (!isExpression4(base) || segments.length === 0) {
|
|
115477
116421
|
return;
|
|
115478
116422
|
}
|
|
115479
116423
|
const nextValue = extractJsonValue(ast);
|
|
115480
|
-
if (!
|
|
116424
|
+
if (!isExpression4(nextValue)) {
|
|
115481
116425
|
return;
|
|
115482
116426
|
}
|
|
115483
116427
|
const createMissing = ast.createMissing === true;
|
|
@@ -115488,13 +116432,21 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115488
116432
|
return `${functionName}(${renderPostgresJsonValue(base, state, dialect)}, ${renderPostgresJsonPathArray(segments, state, dialect)}, ${renderPostgresJsonValue(nextValue, state, dialect)}${extra})`;
|
|
115489
116433
|
}
|
|
115490
116434
|
if (dialect.name === "mysql") {
|
|
115491
|
-
const
|
|
115492
|
-
|
|
116435
|
+
const renderedBase = renderExpression(base, state, dialect);
|
|
116436
|
+
if (kind === "jsonInsert" && isJsonArrayIndexSegment(segments[segments.length - 1])) {
|
|
116437
|
+
const renderedPath2 = renderMySqlJsonInsertPath(segments, insertAfter, state, dialect);
|
|
116438
|
+
const renderedValue2 = renderJsonInputExpression(nextValue, state, dialect);
|
|
116439
|
+
return `json_array_insert(${renderedBase}, ${renderedPath2}, ${renderedValue2})`;
|
|
116440
|
+
}
|
|
116441
|
+
const functionName = kind === "jsonInsert" ? "json_insert" : createMissing ? "json_set" : "json_replace";
|
|
116442
|
+
const renderedPath = renderMySqlJsonPath(segments, state, dialect);
|
|
116443
|
+
const renderedValue = renderJsonInputExpression(nextValue, state, dialect);
|
|
116444
|
+
return `${functionName}(${renderedBase}, ${renderedPath}, ${renderedValue})`;
|
|
115493
116445
|
}
|
|
115494
116446
|
return;
|
|
115495
116447
|
}
|
|
115496
116448
|
case "jsonPathExists": {
|
|
115497
|
-
if (!
|
|
116449
|
+
if (!isExpression4(base)) {
|
|
115498
116450
|
return;
|
|
115499
116451
|
}
|
|
115500
116452
|
const path2 = ast.path ?? ast.query ?? ast.right;
|
|
@@ -115510,7 +116462,7 @@ var renderJsonExpression = (expression, ast, state, dialect) => {
|
|
|
115510
116462
|
return;
|
|
115511
116463
|
}
|
|
115512
116464
|
case "jsonPathMatch": {
|
|
115513
|
-
if (!
|
|
116465
|
+
if (!isExpression4(base)) {
|
|
115514
116466
|
return;
|
|
115515
116467
|
}
|
|
115516
116468
|
const path2 = ast.path ?? ast.query ?? ast.right;
|
|
@@ -115541,23 +116493,15 @@ var renderMysqlMutationLock = (lock2, statement) => {
|
|
|
115541
116493
|
if (!lock2) {
|
|
115542
116494
|
return "";
|
|
115543
116495
|
}
|
|
115544
|
-
|
|
115545
|
-
case "lowPriority":
|
|
115546
|
-
return " low_priority";
|
|
115547
|
-
case "ignore":
|
|
115548
|
-
return " ignore";
|
|
115549
|
-
case "quick":
|
|
115550
|
-
return statement === "delete" ? " quick" : "";
|
|
115551
|
-
default:
|
|
115552
|
-
return "";
|
|
115553
|
-
}
|
|
116496
|
+
return renderMysqlMutationLockMode(lock2.mode, statement);
|
|
115554
116497
|
};
|
|
115555
116498
|
var renderTransactionClause = (clause, dialect) => {
|
|
115556
116499
|
switch (clause.kind) {
|
|
115557
116500
|
case "transaction": {
|
|
115558
116501
|
const modes = [];
|
|
115559
|
-
|
|
115560
|
-
|
|
116502
|
+
const isolationLevel = renderTransactionIsolationLevel(clause.isolationLevel);
|
|
116503
|
+
if (isolationLevel) {
|
|
116504
|
+
modes.push(isolationLevel);
|
|
115561
116505
|
}
|
|
115562
116506
|
if (clause.readOnly === true) {
|
|
115563
116507
|
modes.push("read only");
|
|
@@ -115575,13 +116519,16 @@ var renderTransactionClause = (clause, dialect) => {
|
|
|
115575
116519
|
case "releaseSavepoint":
|
|
115576
116520
|
return `release savepoint ${dialect.quoteIdentifier(clause.name)}`;
|
|
115577
116521
|
}
|
|
115578
|
-
|
|
116522
|
+
throw new Error("Unsupported transaction statement kind");
|
|
115579
116523
|
};
|
|
115580
116524
|
var renderSelectionList = (selection, state, dialect, validateAggregation) => {
|
|
115581
116525
|
if (validateAggregation) {
|
|
115582
116526
|
validateAggregationSelection(selection, []);
|
|
115583
116527
|
}
|
|
115584
116528
|
const flattened = flattenSelection(selection);
|
|
116529
|
+
if (dialect.name === "mysql" && flattened.length === 0) {
|
|
116530
|
+
throw new Error("mysql select statements require at least one selected expression");
|
|
116531
|
+
}
|
|
115585
116532
|
const projections = selectionProjections(selection);
|
|
115586
116533
|
const sql = flattened.map(({ expression, alias: alias2 }) => `${renderSelectSql(renderExpression(expression, state, dialect), expressionDriverContext(expression, state, dialect))} as ${dialect.quoteIdentifier(alias2)}`).join(", ");
|
|
115587
116534
|
return {
|
|
@@ -115589,7 +116536,84 @@ var renderSelectionList = (selection, state, dialect, validateAggregation) => {
|
|
|
115589
116536
|
projections
|
|
115590
116537
|
};
|
|
115591
116538
|
};
|
|
115592
|
-
var
|
|
116539
|
+
var nestedRenderState = (state) => ({
|
|
116540
|
+
params: state.params,
|
|
116541
|
+
valueMappings: state.valueMappings,
|
|
116542
|
+
ctes: [],
|
|
116543
|
+
cteNames: new Set(state.cteNames),
|
|
116544
|
+
cteSources: new Map(state.cteSources)
|
|
116545
|
+
});
|
|
116546
|
+
var assertMatchingSetProjections = (left, right) => {
|
|
116547
|
+
const leftKeys = left.map((projection) => JSON.stringify(projection.path));
|
|
116548
|
+
const rightKeys = right.map((projection) => JSON.stringify(projection.path));
|
|
116549
|
+
if (leftKeys.length !== rightKeys.length || leftKeys.some((key2, index4) => key2 !== rightKeys[index4])) {
|
|
116550
|
+
throw new Error("set operator operands must have matching result rows");
|
|
116551
|
+
}
|
|
116552
|
+
};
|
|
116553
|
+
var assertNoGroupedMutationClauses = (ast, statement) => {
|
|
116554
|
+
if (ast.groupBy.length > 0) {
|
|
116555
|
+
throw new Error(`groupBy(...) is not supported for ${statement} statements`);
|
|
116556
|
+
}
|
|
116557
|
+
if (ast.having.length > 0) {
|
|
116558
|
+
throw new Error(`having(...) is not supported for ${statement} statements`);
|
|
116559
|
+
}
|
|
116560
|
+
};
|
|
116561
|
+
var assertNoInsertQueryClauses = (ast) => {
|
|
116562
|
+
if (ast.where.length > 0) {
|
|
116563
|
+
throw new Error("where(...) is not supported for insert statements");
|
|
116564
|
+
}
|
|
116565
|
+
if (ast.joins.length > 0) {
|
|
116566
|
+
throw new Error("join(...) is not supported for insert statements");
|
|
116567
|
+
}
|
|
116568
|
+
if (ast.orderBy.length > 0) {
|
|
116569
|
+
throw new Error("orderBy(...) is not supported for insert statements");
|
|
116570
|
+
}
|
|
116571
|
+
if (ast.limit) {
|
|
116572
|
+
throw new Error("limit(...) is not supported for insert statements");
|
|
116573
|
+
}
|
|
116574
|
+
if (ast.offset) {
|
|
116575
|
+
throw new Error("offset(...) is not supported for insert statements");
|
|
116576
|
+
}
|
|
116577
|
+
if (ast.lock) {
|
|
116578
|
+
throw new Error("lock(...) is not supported for insert statements");
|
|
116579
|
+
}
|
|
116580
|
+
};
|
|
116581
|
+
var assertNoStatementQueryClauses = (ast, statement, options2 = {}) => {
|
|
116582
|
+
if (ast.distinct) {
|
|
116583
|
+
throw new Error(`distinct(...) is not supported for ${statement} statements`);
|
|
116584
|
+
}
|
|
116585
|
+
if (ast.where.length > 0) {
|
|
116586
|
+
throw new Error(`where(...) is not supported for ${statement} statements`);
|
|
116587
|
+
}
|
|
116588
|
+
if ((ast.fromSources?.length ?? 0) > 0 || ast.from) {
|
|
116589
|
+
throw new Error(`from(...) is not supported for ${statement} statements`);
|
|
116590
|
+
}
|
|
116591
|
+
if (ast.joins.length > 0) {
|
|
116592
|
+
throw new Error(`join(...) is not supported for ${statement} statements`);
|
|
116593
|
+
}
|
|
116594
|
+
if (ast.groupBy.length > 0) {
|
|
116595
|
+
throw new Error(`groupBy(...) is not supported for ${statement} statements`);
|
|
116596
|
+
}
|
|
116597
|
+
if (ast.having.length > 0) {
|
|
116598
|
+
throw new Error(`having(...) is not supported for ${statement} statements`);
|
|
116599
|
+
}
|
|
116600
|
+
if (ast.orderBy.length > 0) {
|
|
116601
|
+
throw new Error(`orderBy(...) is not supported for ${statement} statements`);
|
|
116602
|
+
}
|
|
116603
|
+
if (ast.limit) {
|
|
116604
|
+
throw new Error(`limit(...) is not supported for ${statement} statements`);
|
|
116605
|
+
}
|
|
116606
|
+
if (ast.offset) {
|
|
116607
|
+
throw new Error(`offset(...) is not supported for ${statement} statements`);
|
|
116608
|
+
}
|
|
116609
|
+
if (ast.lock) {
|
|
116610
|
+
throw new Error(`lock(...) is not supported for ${statement} statements`);
|
|
116611
|
+
}
|
|
116612
|
+
if (options2.allowSelection !== true && Object.keys(ast.select).length > 0) {
|
|
116613
|
+
throw new Error(`returning(...) is not supported for ${statement} statements`);
|
|
116614
|
+
}
|
|
116615
|
+
};
|
|
116616
|
+
var renderQueryAst = (ast, state, dialect, options2 = {}) => {
|
|
115593
116617
|
let sql = "";
|
|
115594
116618
|
let projections = [];
|
|
115595
116619
|
switch (ast.kind) {
|
|
@@ -115597,13 +116621,17 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115597
116621
|
validateAggregationSelection(ast.select, ast.groupBy);
|
|
115598
116622
|
const rendered = renderSelectionList(ast.select, state, dialect, false);
|
|
115599
116623
|
projections = rendered.projections;
|
|
116624
|
+
const selectList = rendered.sql.length > 0 ? ` ${rendered.sql}` : "";
|
|
115600
116625
|
const clauses = [
|
|
115601
|
-
ast.distinctOn && ast.distinctOn.length > 0 ? `select distinct on (${ast.distinctOn.map((value) => renderExpression(value, state, dialect)).join(", ")})
|
|
116626
|
+
ast.distinctOn && ast.distinctOn.length > 0 ? `select distinct on (${ast.distinctOn.map((value) => renderExpression(value, state, dialect)).join(", ")})${selectList}` : `select${ast.distinct ? " distinct" : ""}${selectList}`
|
|
115602
116627
|
];
|
|
115603
116628
|
if (ast.from) {
|
|
115604
116629
|
clauses.push(`from ${renderSourceReference(ast.from.source, ast.from.tableName, ast.from.baseTableName, state, dialect)}`);
|
|
115605
116630
|
}
|
|
115606
116631
|
for (const join2 of ast.joins) {
|
|
116632
|
+
if (dialect.name === "mysql" && join2.kind === "full") {
|
|
116633
|
+
throw new Error("Unsupported mysql full join");
|
|
116634
|
+
}
|
|
115607
116635
|
const source = renderSourceReference(join2.source, join2.tableName, join2.baseTableName, state, dialect);
|
|
115608
116636
|
clauses.push(join2.kind === "cross" ? `cross join ${source}` : `${join2.kind} join ${source} on ${renderExpression(join2.on, state, dialect)}`);
|
|
115609
116637
|
}
|
|
@@ -115626,19 +116654,25 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115626
116654
|
clauses.push(`offset ${renderExpression(ast.offset, state, dialect)}`);
|
|
115627
116655
|
}
|
|
115628
116656
|
if (ast.lock) {
|
|
115629
|
-
|
|
116657
|
+
if (ast.lock.nowait && ast.lock.skipLocked) {
|
|
116658
|
+
throw new Error("lock(...) cannot specify both nowait and skipLocked");
|
|
116659
|
+
}
|
|
116660
|
+
clauses.push(`${renderSelectLockMode(ast.lock.mode)}${ast.lock.nowait ? " nowait" : ""}${ast.lock.skipLocked ? " skip locked" : ""}`);
|
|
115630
116661
|
}
|
|
115631
116662
|
sql = clauses.join(" ");
|
|
115632
116663
|
break;
|
|
115633
116664
|
}
|
|
115634
116665
|
case "set": {
|
|
115635
116666
|
const setAst = ast;
|
|
116667
|
+
assertNoStatementQueryClauses(setAst, "set", { allowSelection: true });
|
|
115636
116668
|
const base = renderQueryAst(getAst(setAst.setBase), state, dialect);
|
|
115637
116669
|
projections = selectionProjections(setAst.select);
|
|
116670
|
+
assertMatchingSetProjections(projections, base.projections);
|
|
115638
116671
|
sql = [
|
|
115639
116672
|
`(${base.sql})`,
|
|
115640
116673
|
...(setAst.setOperations ?? []).map((entry) => {
|
|
115641
116674
|
const rendered = renderQueryAst(getAst(entry.query), state, dialect);
|
|
116675
|
+
assertMatchingSetProjections(projections, rendered.projections);
|
|
115642
116676
|
return `${entry.kind}${entry.all ? " all" : ""} (${rendered.sql})`;
|
|
115643
116677
|
})
|
|
115644
116678
|
].join(" ");
|
|
@@ -115646,28 +116680,40 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115646
116680
|
}
|
|
115647
116681
|
case "insert": {
|
|
115648
116682
|
const insertAst = ast;
|
|
116683
|
+
if (insertAst.distinct) {
|
|
116684
|
+
throw new Error("distinct(...) is not supported for insert statements");
|
|
116685
|
+
}
|
|
116686
|
+
assertNoGroupedMutationClauses(insertAst, "insert");
|
|
116687
|
+
assertNoInsertQueryClauses(insertAst);
|
|
115649
116688
|
const targetSource = insertAst.into;
|
|
115650
116689
|
const target = renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect);
|
|
116690
|
+
const insertSource = expectInsertSourceKind(insertAst.insertSource);
|
|
116691
|
+
const conflict = expectConflictClause(insertAst.conflict);
|
|
115651
116692
|
sql = `insert into ${target}`;
|
|
115652
|
-
if (
|
|
115653
|
-
const columns =
|
|
115654
|
-
const rows =
|
|
116693
|
+
if (insertSource?.kind === "values") {
|
|
116694
|
+
const columns = insertSource.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ");
|
|
116695
|
+
const rows = insertSource.rows.map((row) => `(${row.values.map((entry) => renderExpression(entry.value, state, dialect)).join(", ")})`).join(", ");
|
|
115655
116696
|
sql += ` (${columns}) values ${rows}`;
|
|
115656
|
-
} else if (
|
|
115657
|
-
const columns =
|
|
115658
|
-
const renderedQuery = renderQueryAst(getAst(
|
|
116697
|
+
} else if (insertSource?.kind === "query") {
|
|
116698
|
+
const columns = insertSource.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ");
|
|
116699
|
+
const renderedQuery = renderQueryAst(getAst(insertSource.query), state, dialect);
|
|
115659
116700
|
sql += ` (${columns}) ${renderedQuery.sql}`;
|
|
115660
|
-
} else if (
|
|
115661
|
-
const
|
|
115662
|
-
const columns = unnestSource.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ");
|
|
116701
|
+
} else if (insertSource?.kind === "unnest") {
|
|
116702
|
+
const columns = insertSource.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ");
|
|
115663
116703
|
if (dialect.name === "postgres") {
|
|
115664
116704
|
const table = targetSource.source;
|
|
115665
116705
|
const fields2 = table[TypeId4].fields;
|
|
115666
|
-
const rendered =
|
|
116706
|
+
const rendered = insertSource.values.map((entry) => `cast(${dialect.renderLiteral(encodeArrayValues(entry.values, fields2[entry.columnName], state, dialect), state)} as ${renderCastType(dialect, fields2[entry.columnName].metadata.dbType)}[])`).join(", ");
|
|
115667
116707
|
sql += ` (${columns}) select * from unnest(${rendered})`;
|
|
115668
116708
|
} else {
|
|
115669
|
-
const
|
|
115670
|
-
const
|
|
116709
|
+
const table = targetSource.source;
|
|
116710
|
+
const fields2 = table[TypeId4].fields;
|
|
116711
|
+
const encodedValues = insertSource.values.map((entry) => ({
|
|
116712
|
+
columnName: entry.columnName,
|
|
116713
|
+
values: encodeArrayValues(entry.values, fields2[entry.columnName], state, dialect)
|
|
116714
|
+
}));
|
|
116715
|
+
const rowCount = encodedValues[0]?.values.length ?? 0;
|
|
116716
|
+
const rows = Array.from({ length: rowCount }, (_, index4) => `(${encodedValues.map((entry) => dialect.renderLiteral(entry.values[index4], state)).join(", ")})`).join(", ");
|
|
115671
116717
|
sql += ` (${columns}) values ${rows}`;
|
|
115672
116718
|
}
|
|
115673
116719
|
} else {
|
|
@@ -115676,22 +116722,29 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115676
116722
|
if ((insertAst.values ?? []).length > 0) {
|
|
115677
116723
|
sql += ` (${columns}) values (${values2})`;
|
|
115678
116724
|
} else {
|
|
115679
|
-
sql += "
|
|
116725
|
+
sql += " () values ()";
|
|
115680
116726
|
}
|
|
115681
116727
|
}
|
|
115682
|
-
if (
|
|
115683
|
-
|
|
116728
|
+
if (conflict) {
|
|
116729
|
+
if (conflict.where) {
|
|
116730
|
+
throw new Error("Unsupported mysql conflict action predicates");
|
|
116731
|
+
}
|
|
116732
|
+
const updateValues = (conflict.values ?? []).map((entry) => `${dialect.quoteIdentifier(entry.columnName)} = ${renderExpression(entry.value, state, dialect)}`).join(", ");
|
|
115684
116733
|
if (dialect.name === "postgres") {
|
|
115685
|
-
const targetSql =
|
|
116734
|
+
const targetSql = conflict.target?.kind === "constraint" ? ` on conflict on constraint ${dialect.quoteIdentifier(conflict.target.name)}` : conflict.target?.kind === "columns" ? ` on conflict (${conflict.target.columns.map((column2) => dialect.quoteIdentifier(column2)).join(", ")})${conflict.target.where ? ` where ${renderExpression(conflict.target.where, state, dialect)}` : ""}` : " on conflict";
|
|
115686
116735
|
sql += targetSql;
|
|
115687
|
-
sql +=
|
|
115688
|
-
} else if (
|
|
116736
|
+
sql += conflict.action === "doNothing" ? " do nothing" : ` do update set ${updateValues}${conflict.where ? ` where ${renderExpression(conflict.where, state, dialect)}` : ""}`;
|
|
116737
|
+
} else if (conflict.action === "doNothing") {
|
|
115689
116738
|
sql = sql.replace(/^insert/, "insert ignore");
|
|
115690
116739
|
} else {
|
|
115691
116740
|
sql += ` on duplicate key update ${updateValues}`;
|
|
115692
116741
|
}
|
|
115693
116742
|
}
|
|
115694
|
-
const
|
|
116743
|
+
const hasReturning = Object.keys(insertAst.select).length > 0;
|
|
116744
|
+
const returning2 = hasReturning ? renderSelectionList(insertAst.select, state, dialect, false) : { sql: "", projections: [] };
|
|
116745
|
+
if (dialect.name === "mysql" && returning2.sql.length > 0) {
|
|
116746
|
+
throw new Error("Unsupported mysql returning");
|
|
116747
|
+
}
|
|
115695
116748
|
projections = returning2.projections;
|
|
115696
116749
|
if (returning2.sql.length > 0) {
|
|
115697
116750
|
sql += ` returning ${returning2.sql}`;
|
|
@@ -115700,15 +116753,27 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115700
116753
|
}
|
|
115701
116754
|
case "update": {
|
|
115702
116755
|
const updateAst = ast;
|
|
116756
|
+
if (updateAst.distinct) {
|
|
116757
|
+
throw new Error("distinct(...) is not supported for update statements");
|
|
116758
|
+
}
|
|
116759
|
+
assertNoGroupedMutationClauses(updateAst, "update");
|
|
116760
|
+
if (updateAst.offset) {
|
|
116761
|
+
throw new Error("offset(...) is not supported for update statements");
|
|
116762
|
+
}
|
|
115703
116763
|
const targetSource = updateAst.target;
|
|
115704
116764
|
const target = renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect);
|
|
115705
116765
|
const targets = updateAst.targets ?? [targetSource];
|
|
115706
116766
|
const fromSources = updateAst.fromSources ?? [];
|
|
116767
|
+
if ((updateAst.set ?? []).length === 0) {
|
|
116768
|
+
throw new Error("update statements require at least one assignment");
|
|
116769
|
+
}
|
|
115707
116770
|
const assignments = updateAst.set.map((entry) => renderMutationAssignment(entry, state, dialect)).join(", ");
|
|
115708
116771
|
if (dialect.name === "mysql") {
|
|
115709
116772
|
const modifiers = renderMysqlMutationLock(updateAst.lock, "update");
|
|
115710
116773
|
const extraSources = renderFromSources(fromSources, state, dialect);
|
|
115711
|
-
const joinSources = updateAst.joins.map((join2) => join2.kind === "
|
|
116774
|
+
const joinSources = updateAst.joins.map((join2) => join2.kind === "full" ? (() => {
|
|
116775
|
+
throw new Error("Unsupported mysql full join");
|
|
116776
|
+
})() : join2.kind === "cross" ? `cross join ${renderSourceReference(join2.source, join2.tableName, join2.baseTableName, state, dialect)}` : `${join2.kind} join ${renderSourceReference(join2.source, join2.tableName, join2.baseTableName, state, dialect)} on ${renderExpression(join2.on, state, dialect)}`).join(" ");
|
|
115712
116777
|
const targetList = [
|
|
115713
116778
|
...targets.map((entry) => renderSourceReference(entry.source, entry.tableName, entry.baseTableName, state, dialect)),
|
|
115714
116779
|
...extraSources.length > 0 ? [extraSources] : []
|
|
@@ -115737,7 +116802,11 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115737
116802
|
if (dialect.name === "mysql" && updateAst.limit) {
|
|
115738
116803
|
sql += ` limit ${renderMysqlMutationLimit(updateAst.limit, state, dialect)}`;
|
|
115739
116804
|
}
|
|
115740
|
-
const
|
|
116805
|
+
const hasReturning = Object.keys(updateAst.select).length > 0;
|
|
116806
|
+
const returning2 = hasReturning ? renderSelectionList(updateAst.select, state, dialect, false) : { sql: "", projections: [] };
|
|
116807
|
+
if (dialect.name === "mysql" && returning2.sql.length > 0) {
|
|
116808
|
+
throw new Error("Unsupported mysql returning");
|
|
116809
|
+
}
|
|
115741
116810
|
projections = returning2.projections;
|
|
115742
116811
|
if (returning2.sql.length > 0) {
|
|
115743
116812
|
sql += ` returning ${returning2.sql}`;
|
|
@@ -115746,6 +116815,13 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115746
116815
|
}
|
|
115747
116816
|
case "delete": {
|
|
115748
116817
|
const deleteAst = ast;
|
|
116818
|
+
if (deleteAst.distinct) {
|
|
116819
|
+
throw new Error("distinct(...) is not supported for delete statements");
|
|
116820
|
+
}
|
|
116821
|
+
assertNoGroupedMutationClauses(deleteAst, "delete");
|
|
116822
|
+
if (deleteAst.offset) {
|
|
116823
|
+
throw new Error("offset(...) is not supported for delete statements");
|
|
116824
|
+
}
|
|
115749
116825
|
const targetSource = deleteAst.target;
|
|
115750
116826
|
const target = renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect);
|
|
115751
116827
|
const targets = deleteAst.targets ?? [targetSource];
|
|
@@ -115754,7 +116830,9 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115754
116830
|
const hasJoinedSources = deleteAst.joins.length > 0 || targets.length > 1;
|
|
115755
116831
|
const targetList = renderDeleteTargets(targets, dialect);
|
|
115756
116832
|
const fromSources = targets.map((entry) => renderSourceReference(entry.source, entry.tableName, entry.baseTableName, state, dialect)).join(", ");
|
|
115757
|
-
const joinSources = deleteAst.joins.map((join2) => join2.kind === "
|
|
116833
|
+
const joinSources = deleteAst.joins.map((join2) => join2.kind === "full" ? (() => {
|
|
116834
|
+
throw new Error("Unsupported mysql full join");
|
|
116835
|
+
})() : join2.kind === "cross" ? `cross join ${renderSourceReference(join2.source, join2.tableName, join2.baseTableName, state, dialect)}` : `${join2.kind} join ${renderSourceReference(join2.source, join2.tableName, join2.baseTableName, state, dialect)} on ${renderExpression(join2.on, state, dialect)}`).join(" ");
|
|
115758
116836
|
sql = hasJoinedSources ? `delete${modifiers} ${targetList} from ${fromSources}${joinSources.length > 0 ? ` ${joinSources}` : ""}` : `delete${modifiers} from ${fromSources}`;
|
|
115759
116837
|
} else {
|
|
115760
116838
|
sql = `delete from ${target}`;
|
|
@@ -115775,7 +116853,11 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115775
116853
|
if (dialect.name === "mysql" && deleteAst.limit) {
|
|
115776
116854
|
sql += ` limit ${renderMysqlMutationLimit(deleteAst.limit, state, dialect)}`;
|
|
115777
116855
|
}
|
|
115778
|
-
const
|
|
116856
|
+
const hasReturning = Object.keys(deleteAst.select).length > 0;
|
|
116857
|
+
const returning2 = hasReturning ? renderSelectionList(deleteAst.select, state, dialect, false) : { sql: "", projections: [] };
|
|
116858
|
+
if (dialect.name === "mysql" && returning2.sql.length > 0) {
|
|
116859
|
+
throw new Error("Unsupported mysql returning");
|
|
116860
|
+
}
|
|
115779
116861
|
projections = returning2.projections;
|
|
115780
116862
|
if (returning2.sql.length > 0) {
|
|
115781
116863
|
sql += ` returning ${returning2.sql}`;
|
|
@@ -115784,12 +116866,17 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115784
116866
|
}
|
|
115785
116867
|
case "truncate": {
|
|
115786
116868
|
const truncateAst = ast;
|
|
116869
|
+
assertNoStatementQueryClauses(truncateAst, "truncate");
|
|
116870
|
+
const truncate2 = expectTruncateClause(truncateAst.truncate);
|
|
115787
116871
|
const targetSource = truncateAst.target;
|
|
116872
|
+
if (dialect.name === "mysql" && (truncate2.restartIdentity || truncate2.cascade)) {
|
|
116873
|
+
throw new Error("Unsupported mysql truncate options");
|
|
116874
|
+
}
|
|
115788
116875
|
sql = `truncate table ${renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect)}`;
|
|
115789
|
-
if (
|
|
116876
|
+
if (truncate2.restartIdentity) {
|
|
115790
116877
|
sql += " restart identity";
|
|
115791
116878
|
}
|
|
115792
|
-
if (
|
|
116879
|
+
if (truncate2.cascade) {
|
|
115793
116880
|
sql += " cascade";
|
|
115794
116881
|
}
|
|
115795
116882
|
break;
|
|
@@ -115802,6 +116889,9 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115802
116889
|
const targetSource = mergeAst.target;
|
|
115803
116890
|
const usingSource = mergeAst.using;
|
|
115804
116891
|
const merge2 = mergeAst.merge;
|
|
116892
|
+
if (Object.keys(mergeAst.select).length > 0) {
|
|
116893
|
+
throw new Error("returning(...) is not supported for merge statements");
|
|
116894
|
+
}
|
|
115805
116895
|
sql = `merge into ${renderSourceReference(targetSource.source, targetSource.tableName, targetSource.baseTableName, state, dialect)} using ${renderSourceReference(usingSource.source, usingSource.tableName, usingSource.baseTableName, state, dialect)} on ${renderExpression(merge2.on, state, dialect)}`;
|
|
115806
116896
|
if (merge2.whenMatched) {
|
|
115807
116897
|
sql += " when matched";
|
|
@@ -115829,32 +116919,40 @@ var renderQueryAst = (ast, state, dialect) => {
|
|
|
115829
116919
|
case "savepoint":
|
|
115830
116920
|
case "rollbackTo":
|
|
115831
116921
|
case "releaseSavepoint": {
|
|
116922
|
+
assertNoStatementQueryClauses(ast, ast.kind);
|
|
115832
116923
|
sql = renderTransactionClause(ast.transaction, dialect);
|
|
115833
116924
|
break;
|
|
115834
116925
|
}
|
|
115835
116926
|
case "createTable": {
|
|
115836
116927
|
const createTableAst = ast;
|
|
115837
|
-
|
|
116928
|
+
assertNoStatementQueryClauses(createTableAst, "createTable");
|
|
116929
|
+
const ddl = expectDdlClauseKind(createTableAst.ddl, "createTable");
|
|
116930
|
+
sql = renderCreateTableSql(createTableAst.target, state, dialect, ddl.ifNotExists);
|
|
115838
116931
|
break;
|
|
115839
116932
|
}
|
|
115840
116933
|
case "dropTable": {
|
|
115841
116934
|
const dropTableAst = ast;
|
|
115842
|
-
|
|
115843
|
-
|
|
116935
|
+
assertNoStatementQueryClauses(dropTableAst, "dropTable");
|
|
116936
|
+
const ddl = expectDdlClauseKind(dropTableAst.ddl, "dropTable");
|
|
116937
|
+
sql = `drop table${ddl.ifExists ? " if exists" : ""} ${renderSourceReference(dropTableAst.target.source, dropTableAst.target.tableName, dropTableAst.target.baseTableName, state, dialect)}`;
|
|
115844
116938
|
break;
|
|
115845
116939
|
}
|
|
115846
116940
|
case "createIndex": {
|
|
115847
116941
|
const createIndexAst = ast;
|
|
115848
|
-
|
|
116942
|
+
assertNoStatementQueryClauses(createIndexAst, "createIndex");
|
|
116943
|
+
sql = renderCreateIndexSql(createIndexAst.target, expectDdlClauseKind(createIndexAst.ddl, "createIndex"), state, dialect);
|
|
115849
116944
|
break;
|
|
115850
116945
|
}
|
|
115851
116946
|
case "dropIndex": {
|
|
115852
116947
|
const dropIndexAst = ast;
|
|
115853
|
-
|
|
116948
|
+
assertNoStatementQueryClauses(dropIndexAst, "dropIndex");
|
|
116949
|
+
sql = renderDropIndexSql(dropIndexAst.target, expectDdlClauseKind(dropIndexAst.ddl, "dropIndex"), state, dialect);
|
|
115854
116950
|
break;
|
|
115855
116951
|
}
|
|
116952
|
+
default:
|
|
116953
|
+
throw new Error("Unsupported query statement kind");
|
|
115856
116954
|
}
|
|
115857
|
-
if (state.ctes.length === 0) {
|
|
116955
|
+
if (state.ctes.length === 0 || options2.emitCtes === false) {
|
|
115858
116956
|
return {
|
|
115859
116957
|
sql,
|
|
115860
116958
|
projections
|
|
@@ -115877,9 +116975,22 @@ var renderSourceReference = (source, tableName, baseTableName, state, dialect) =
|
|
|
115877
116975
|
};
|
|
115878
116976
|
if (typeof source === "object" && source !== null && "kind" in source && source.kind === "cte") {
|
|
115879
116977
|
const cte = source;
|
|
116978
|
+
const registeredCteSource = state.cteSources.get(cte.name);
|
|
116979
|
+
if (registeredCteSource !== undefined && registeredCteSource !== cte.plan) {
|
|
116980
|
+
throw new Error(`common table expression name is already registered with a different plan: ${cte.name}`);
|
|
116981
|
+
}
|
|
115880
116982
|
if (!state.cteNames.has(cte.name)) {
|
|
115881
116983
|
state.cteNames.add(cte.name);
|
|
115882
|
-
|
|
116984
|
+
state.cteSources.set(cte.name, cte.plan);
|
|
116985
|
+
const statement = getQueryState(cte.plan).statement;
|
|
116986
|
+
if (statement !== "select" && statement !== "set") {
|
|
116987
|
+
const cteAst = getAst(cte.plan);
|
|
116988
|
+
if (Object.keys(cteAst.select ?? {}).length > 0) {
|
|
116989
|
+
throw new Error("Unsupported mysql returning");
|
|
116990
|
+
}
|
|
116991
|
+
throw new Error("Unsupported mysql data-modifying cte");
|
|
116992
|
+
}
|
|
116993
|
+
const rendered = renderQueryAst(getAst(cte.plan), state, dialect, { emitCtes: false });
|
|
115883
116994
|
state.ctes.push({
|
|
115884
116995
|
name: cte.name,
|
|
115885
116996
|
sql: rendered.sql,
|
|
@@ -115891,11 +117002,11 @@ var renderSourceReference = (source, tableName, baseTableName, state, dialect) =
|
|
|
115891
117002
|
if (typeof source === "object" && source !== null && "kind" in source && source.kind === "derived") {
|
|
115892
117003
|
const derived = source;
|
|
115893
117004
|
if (!state.cteNames.has(derived.name)) {}
|
|
115894
|
-
return `(${renderQueryAst(getAst(derived.plan), state, dialect).sql}) as ${dialect.quoteIdentifier(derived.name)}`;
|
|
117005
|
+
return `(${renderQueryAst(getAst(derived.plan), nestedRenderState(state), dialect).sql}) as ${dialect.quoteIdentifier(derived.name)}`;
|
|
115895
117006
|
}
|
|
115896
117007
|
if (typeof source === "object" && source !== null && "kind" in source && source.kind === "lateral") {
|
|
115897
117008
|
const lateral2 = source;
|
|
115898
|
-
return `lateral (${renderQueryAst(getAst(lateral2.plan), state, dialect).sql}) as ${dialect.quoteIdentifier(lateral2.name)}`;
|
|
117009
|
+
return `lateral (${renderQueryAst(getAst(lateral2.plan), nestedRenderState(state), dialect).sql}) as ${dialect.quoteIdentifier(lateral2.name)}`;
|
|
115899
117010
|
}
|
|
115900
117011
|
if (typeof source === "object" && source !== null && source.kind === "values") {
|
|
115901
117012
|
const values2 = source;
|
|
@@ -115916,6 +117027,13 @@ var renderSourceReference = (source, tableName, baseTableName, state, dialect) =
|
|
|
115916
117027
|
const schemaName = typeof source === "object" && source !== null && TypeId4 in source ? source[TypeId4].schemaName : undefined;
|
|
115917
117028
|
return dialect.renderTableReference(tableName, baseTableName, schemaName);
|
|
115918
117029
|
};
|
|
117030
|
+
var renderSubqueryExpressionPlan = (plan, state, dialect) => {
|
|
117031
|
+
const statement = getQueryState(plan).statement;
|
|
117032
|
+
if (statement !== "select" && statement !== "set") {
|
|
117033
|
+
throw new Error("subquery expressions only accept select-like query plans");
|
|
117034
|
+
}
|
|
117035
|
+
return renderQueryAst(getAst(plan), state, dialect).sql;
|
|
117036
|
+
};
|
|
115919
117037
|
var renderExpression = (expression, state, dialect) => {
|
|
115920
117038
|
const rawAst = expression[TypeId2];
|
|
115921
117039
|
const jsonSql = renderJsonExpression(expression, rawAst, state, dialect);
|
|
@@ -115926,8 +117044,11 @@ var renderExpression = (expression, state, dialect) => {
|
|
|
115926
117044
|
const renderComparisonOperator = (operator) => operator === "eq" ? "=" : operator === "neq" ? "<>" : operator === "lt" ? "<" : operator === "lte" ? "<=" : operator === "gt" ? ">" : ">=";
|
|
115927
117045
|
switch (ast.kind) {
|
|
115928
117046
|
case "column":
|
|
115929
|
-
return ast.tableName.length === 0 ? dialect.quoteIdentifier(ast.columnName) : `${dialect.quoteIdentifier(ast.tableName)}.${dialect.quoteIdentifier(ast.columnName)}`;
|
|
117047
|
+
return state.rowLocalColumns || ast.tableName.length === 0 ? dialect.quoteIdentifier(ast.columnName) : `${dialect.quoteIdentifier(ast.tableName)}.${dialect.quoteIdentifier(ast.columnName)}`;
|
|
115930
117048
|
case "literal":
|
|
117049
|
+
if (typeof ast.value === "number" && !Number.isFinite(ast.value)) {
|
|
117050
|
+
throw new Error("Expected a finite numeric value");
|
|
117051
|
+
}
|
|
115931
117052
|
return dialect.renderLiteral(ast.value, state, expression[TypeId]);
|
|
115932
117053
|
case "excluded":
|
|
115933
117054
|
return dialect.name === "mysql" ? `values(${dialect.quoteIdentifier(ast.columnName)})` : `excluded.${dialect.quoteIdentifier(ast.columnName)}`;
|
|
@@ -115970,7 +117091,7 @@ var renderExpression = (expression, state, dialect) => {
|
|
|
115970
117091
|
return `(${left} @> ${right})`;
|
|
115971
117092
|
}
|
|
115972
117093
|
if (dialect.name === "mysql" && isJsonExpression(ast.left) && isJsonExpression(ast.right)) {
|
|
115973
|
-
return `json_contains(${
|
|
117094
|
+
return `json_contains(${renderJsonInputExpression(ast.left, state, dialect)}, ${renderJsonInputExpression(ast.right, state, dialect)})`;
|
|
115974
117095
|
}
|
|
115975
117096
|
throw new Error("Unsupported container operator for SQL rendering");
|
|
115976
117097
|
case "containedBy":
|
|
@@ -115980,7 +117101,7 @@ var renderExpression = (expression, state, dialect) => {
|
|
|
115980
117101
|
return `(${left} <@ ${right})`;
|
|
115981
117102
|
}
|
|
115982
117103
|
if (dialect.name === "mysql" && isJsonExpression(ast.left) && isJsonExpression(ast.right)) {
|
|
115983
|
-
return `json_contains(${
|
|
117104
|
+
return `json_contains(${renderJsonInputExpression(ast.right, state, dialect)}, ${renderJsonInputExpression(ast.left, state, dialect)})`;
|
|
115984
117105
|
}
|
|
115985
117106
|
throw new Error("Unsupported container operator for SQL rendering");
|
|
115986
117107
|
case "overlaps":
|
|
@@ -115990,7 +117111,7 @@ var renderExpression = (expression, state, dialect) => {
|
|
|
115990
117111
|
return `(${left} && ${right})`;
|
|
115991
117112
|
}
|
|
115992
117113
|
if (dialect.name === "mysql" && isJsonExpression(ast.left) && isJsonExpression(ast.right)) {
|
|
115993
|
-
return `json_overlaps(${
|
|
117114
|
+
return `json_overlaps(${renderJsonInputExpression(ast.left, state, dialect)}, ${renderJsonInputExpression(ast.right, state, dialect)})`;
|
|
115994
117115
|
}
|
|
115995
117116
|
throw new Error("Unsupported container operator for SQL rendering");
|
|
115996
117117
|
case "isNull":
|
|
@@ -116010,14 +117131,26 @@ var renderExpression = (expression, state, dialect) => {
|
|
|
116010
117131
|
case "min":
|
|
116011
117132
|
return `min(${renderExpression(ast.value, state, dialect)})`;
|
|
116012
117133
|
case "and":
|
|
117134
|
+
if (ast.values.length === 0) {
|
|
117135
|
+
throw new Error("and(...) requires at least one predicate");
|
|
117136
|
+
}
|
|
116013
117137
|
return `(${ast.values.map((value) => renderExpression(value, state, dialect)).join(" and ")})`;
|
|
116014
117138
|
case "or":
|
|
117139
|
+
if (ast.values.length === 0) {
|
|
117140
|
+
throw new Error("or(...) requires at least one predicate");
|
|
117141
|
+
}
|
|
116015
117142
|
return `(${ast.values.map((value) => renderExpression(value, state, dialect)).join(" or ")})`;
|
|
116016
117143
|
case "coalesce":
|
|
116017
117144
|
return `coalesce(${ast.values.map((value) => renderExpression(value, state, dialect)).join(", ")})`;
|
|
116018
117145
|
case "in":
|
|
117146
|
+
if (ast.values.length < 2) {
|
|
117147
|
+
throw new Error("in(...) requires at least one candidate value");
|
|
117148
|
+
}
|
|
116019
117149
|
return `(${renderExpression(ast.values[0], state, dialect)} in (${ast.values.slice(1).map((value) => renderExpression(value, state, dialect)).join(", ")}))`;
|
|
116020
117150
|
case "notIn":
|
|
117151
|
+
if (ast.values.length < 2) {
|
|
117152
|
+
throw new Error("notIn(...) requires at least one candidate value");
|
|
117153
|
+
}
|
|
116021
117154
|
return `(${renderExpression(ast.values[0], state, dialect)} not in (${ast.values.slice(1).map((value) => renderExpression(value, state, dialect)).join(", ")}))`;
|
|
116022
117155
|
case "between":
|
|
116023
117156
|
return `(${renderExpression(ast.values[0], state, dialect)} between ${renderExpression(ast.values[1], state, dialect)} and ${renderExpression(ast.values[2], state, dialect)})`;
|
|
@@ -116026,15 +117159,15 @@ var renderExpression = (expression, state, dialect) => {
|
|
|
116026
117159
|
case "case":
|
|
116027
117160
|
return `case ${ast.branches.map((branch) => `when ${renderExpression(branch.when, state, dialect)} then ${renderExpression(branch.then, state, dialect)}`).join(" ")} else ${renderExpression(ast.else, state, dialect)} end`;
|
|
116028
117161
|
case "exists":
|
|
116029
|
-
return `exists (${
|
|
117162
|
+
return `exists (${renderSubqueryExpressionPlan(ast.plan, state, dialect)})`;
|
|
116030
117163
|
case "scalarSubquery":
|
|
116031
|
-
return `(${
|
|
117164
|
+
return `(${renderSubqueryExpressionPlan(ast.plan, state, dialect)})`;
|
|
116032
117165
|
case "inSubquery":
|
|
116033
|
-
return `(${renderExpression(ast.left, state, dialect)} in (${
|
|
117166
|
+
return `(${renderExpression(ast.left, state, dialect)} in (${renderSubqueryExpressionPlan(ast.plan, state, dialect)}))`;
|
|
116034
117167
|
case "comparisonAny":
|
|
116035
|
-
return `(${renderExpression(ast.left, state, dialect)} ${renderComparisonOperator(ast.operator)} any (${
|
|
117168
|
+
return `(${renderExpression(ast.left, state, dialect)} ${renderComparisonOperator(ast.operator)} any (${renderSubqueryExpressionPlan(ast.plan, state, dialect)}))`;
|
|
116036
117169
|
case "comparisonAll":
|
|
116037
|
-
return `(${renderExpression(ast.left, state, dialect)} ${renderComparisonOperator(ast.operator)} all (${
|
|
117170
|
+
return `(${renderExpression(ast.left, state, dialect)} ${renderComparisonOperator(ast.operator)} all (${renderSubqueryExpressionPlan(ast.plan, state, dialect)}))`;
|
|
116038
117171
|
case "window": {
|
|
116039
117172
|
if (!Array.isArray(ast.partitionBy) || !Array.isArray(ast.orderBy) || typeof ast.function !== "string") {
|
|
116040
117173
|
break;
|
|
@@ -116069,7 +117202,8 @@ var renderMysqlPlan = (plan, options2 = {}) => {
|
|
|
116069
117202
|
params: [],
|
|
116070
117203
|
valueMappings: options2.valueMappings,
|
|
116071
117204
|
ctes: [],
|
|
116072
|
-
cteNames: new Set
|
|
117205
|
+
cteNames: new Set,
|
|
117206
|
+
cteSources: new Map
|
|
116073
117207
|
};
|
|
116074
117208
|
const rendered = renderQueryAst(getAst(plan), state, mysqlDialect);
|
|
116075
117209
|
return {
|
|
@@ -116103,8 +117237,8 @@ var fromDriver = (renderer, sqlDriver, driverMode = "raw", valueMappings) => ({
|
|
|
116103
117237
|
},
|
|
116104
117238
|
stream(plan) {
|
|
116105
117239
|
const rendered = renderer.render(plan);
|
|
116106
|
-
return Stream2.mapError(Stream2.
|
|
116107
|
-
try: () =>
|
|
117240
|
+
return Stream2.mapError(Stream2.mapArrayEffect(sqlDriver.stream(rendered), (rows) => Effect2.try({
|
|
117241
|
+
try: () => decodeRows(rendered, plan, rows, { driverMode, valueMappings }),
|
|
116108
117242
|
catch: (error) => error
|
|
116109
117243
|
})), (error) => {
|
|
116110
117244
|
if (typeof error === "object" && error !== null && "_tag" in error && error._tag === "RowDecodeError") {
|
|
@@ -116119,13 +117253,13 @@ var sqlClientDriver = () => driver2({
|
|
|
116119
117253
|
execute: (query) => Effect2.flatMap(SqlClient3.SqlClient, (sql) => sql.unsafe(query.sql, [...query.params])),
|
|
116120
117254
|
stream: (query) => streamFromSqlClient(query)
|
|
116121
117255
|
});
|
|
116122
|
-
function
|
|
117256
|
+
function make5(options2 = {}) {
|
|
116123
117257
|
if (options2.driver) {
|
|
116124
|
-
return fromDriver(options2.renderer ??
|
|
117258
|
+
return fromDriver(options2.renderer ?? make4("mysql", (plan) => renderMysqlPlan(plan, { valueMappings: options2.valueMappings })), options2.driver, options2.driverMode, options2.valueMappings);
|
|
116125
117259
|
}
|
|
116126
|
-
return fromDriver(options2.renderer ??
|
|
117260
|
+
return fromDriver(options2.renderer ?? make4("mysql", (plan) => renderMysqlPlan(plan, { valueMappings: options2.valueMappings })), sqlClientDriver(), options2.driverMode, options2.valueMappings);
|
|
116127
117261
|
}
|
|
116128
|
-
var custom3 = (execute) =>
|
|
117262
|
+
var custom3 = (execute) => make3("mysql", execute);
|
|
116129
117263
|
// src/mysql/query.ts
|
|
116130
117264
|
var exports_query2 = {};
|
|
116131
117265
|
__export(exports_query2, {
|
|
@@ -116221,14 +117355,17 @@ var from2 = exportedFrom;
|
|
|
116221
117355
|
// src/mysql/table.ts
|
|
116222
117356
|
var exports_table2 = {};
|
|
116223
117357
|
__export(exports_table2, {
|
|
117358
|
+
updateSchema: () => updateSchema3,
|
|
116224
117359
|
unique: () => unique4,
|
|
117360
|
+
selectSchema: () => selectSchema3,
|
|
116225
117361
|
schema: () => schema4,
|
|
116226
117362
|
primaryKey: () => primaryKey4,
|
|
116227
117363
|
options: () => options2,
|
|
116228
|
-
make: () =>
|
|
117364
|
+
make: () => make6,
|
|
117365
|
+
insertSchema: () => insertSchema3,
|
|
116229
117366
|
index: () => index4,
|
|
116230
117367
|
foreignKey: () => foreignKey3,
|
|
116231
|
-
check: () =>
|
|
117368
|
+
check: () => check3,
|
|
116232
117369
|
alias: () => alias2,
|
|
116233
117370
|
TypeId: () => TypeId10,
|
|
116234
117371
|
OptionsSymbol: () => OptionsSymbol2,
|
|
@@ -116237,11 +117374,14 @@ __export(exports_table2, {
|
|
|
116237
117374
|
var TypeId10 = TypeId4;
|
|
116238
117375
|
var OptionsSymbol2 = OptionsSymbol;
|
|
116239
117376
|
var options2 = options;
|
|
116240
|
-
var
|
|
116241
|
-
var schema4 = (schemaName) =>
|
|
116242
|
-
schemaName,
|
|
116243
|
-
|
|
116244
|
-
|
|
117377
|
+
var make6 = (name, fields2, schemaName = undefined) => make(name, fields2, schemaName);
|
|
117378
|
+
var schema4 = (schemaName) => {
|
|
117379
|
+
const table = (name, fields2, ...declaredOptions) => schema3(schemaName).table(name, fields2, ...declaredOptions);
|
|
117380
|
+
return {
|
|
117381
|
+
schemaName,
|
|
117382
|
+
table
|
|
117383
|
+
};
|
|
117384
|
+
};
|
|
116245
117385
|
var alias2 = (table, aliasName) => alias(table, aliasName);
|
|
116246
117386
|
var Class2 = (name, schemaName = undefined) => {
|
|
116247
117387
|
const base = Class(name, schemaName);
|
|
@@ -116251,16 +117391,19 @@ var primaryKey4 = primaryKey3;
|
|
|
116251
117391
|
var unique4 = unique3;
|
|
116252
117392
|
var index4 = index2;
|
|
116253
117393
|
var foreignKey3 = (columns, target, referencedColumns) => foreignKey2(columns, target, referencedColumns);
|
|
116254
|
-
var
|
|
117394
|
+
var check3 = check2;
|
|
117395
|
+
var selectSchema3 = selectSchema2;
|
|
117396
|
+
var insertSchema3 = insertSchema2;
|
|
117397
|
+
var updateSchema3 = updateSchema2;
|
|
116255
117398
|
// src/mysql/renderer.ts
|
|
116256
117399
|
var exports_renderer2 = {};
|
|
116257
117400
|
__export(exports_renderer2, {
|
|
116258
117401
|
mysql: () => mysql,
|
|
116259
|
-
make: () =>
|
|
117402
|
+
make: () => make7,
|
|
116260
117403
|
TypeId: () => TypeId8
|
|
116261
117404
|
});
|
|
116262
|
-
var
|
|
116263
|
-
var mysql =
|
|
117405
|
+
var make7 = (options3 = {}) => make4("mysql", (plan) => renderMysqlPlan(plan, options3));
|
|
117406
|
+
var mysql = make7();
|
|
116264
117407
|
export {
|
|
116265
117408
|
exports_table2 as Table,
|
|
116266
117409
|
exports_scalar as Scalar,
|