@eidos.space/eidos-file 2.2.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/FIELD-CONVERSION.md +40 -19
  2. package/dist/adapter-contract-SrPzXysz.d.mts +281 -0
  3. package/dist/adapter-contract-SrPzXysz.d.mts.map +1 -0
  4. package/dist/better-sqlite3.d.mts +2 -2
  5. package/dist/better-sqlite3.mjs +1 -1
  6. package/dist/browser.d.mts +4 -4
  7. package/dist/browser.mjs +2 -2
  8. package/dist/{csv-BVaUkU1o.mjs → csv-NK8Rti5O.mjs} +38 -7
  9. package/dist/csv-NK8Rti5O.mjs.map +1 -0
  10. package/dist/csv.d.mts +1 -1
  11. package/dist/csv.d.mts.map +1 -1
  12. package/dist/csv.mjs +1 -1
  13. package/dist/index.d.mts +57 -9
  14. package/dist/index.d.mts.map +1 -1
  15. package/dist/index.mjs +66 -499
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/node-sqlite.d.mts +3 -3
  18. package/dist/node-sqlite.d.mts.map +1 -1
  19. package/dist/node-sqlite.mjs +10 -4
  20. package/dist/node-sqlite.mjs.map +1 -1
  21. package/dist/runtime-CUCgAnbo.d.mts +1682 -0
  22. package/dist/runtime-CUCgAnbo.d.mts.map +1 -0
  23. package/dist/{runtime-D9Vp-lxn.mjs → runtime-CrsC-uwG.mjs} +1390 -293
  24. package/dist/runtime-CrsC-uwG.mjs.map +1 -0
  25. package/dist/{sqlite-wasm-DzuW0TZZ.mjs → sqlite-wasm-BQqcna0d.mjs} +9 -2
  26. package/dist/sqlite-wasm-BQqcna0d.mjs.map +1 -0
  27. package/dist/{system-metadata-merge-DeHwokcY.d.mts → system-metadata-merge-BY7ebqnp.d.mts} +2 -2
  28. package/dist/{system-metadata-merge-DeHwokcY.d.mts.map → system-metadata-merge-BY7ebqnp.d.mts.map} +1 -1
  29. package/dist/{system-metadata-merge-DJ9yCprr.mjs → system-metadata-merge-CVa87L1C.mjs} +2 -2
  30. package/dist/{system-metadata-merge-DJ9yCprr.mjs.map → system-metadata-merge-CVa87L1C.mjs.map} +1 -1
  31. package/dist/{temporal-CnuhXeP8.d.mts → temporal-BMFnxGcx.d.mts} +6 -3
  32. package/dist/temporal-BMFnxGcx.d.mts.map +1 -0
  33. package/package.json +3 -3
  34. package/dist/adapter-contract-CNigdeQ7.d.mts +0 -1137
  35. package/dist/adapter-contract-CNigdeQ7.d.mts.map +0 -1
  36. package/dist/csv-BVaUkU1o.mjs.map +0 -1
  37. package/dist/runtime-BSYpcOBe.d.mts +0 -801
  38. package/dist/runtime-BSYpcOBe.d.mts.map +0 -1
  39. package/dist/runtime-D9Vp-lxn.mjs.map +0 -1
  40. package/dist/sqlite-wasm-DzuW0TZZ.mjs.map +0 -1
  41. package/dist/temporal-CnuhXeP8.d.mts.map +0 -1
@@ -1,7 +1,7 @@
1
1
  import { A as isEidosFileUuid, C as assertEidosFileDisplayName, E as assertEidosFileUuid, F as EidosFileError, M as canonicalizeEidosFileJson, N as isCanonicalEidosFileJson, O as createEidosFileUuid, P as parseEidosFileJson, T as assertEidosFileTableName, c as eidosFileInstantSqlCheck, d as normalizeEidosFileDate, f as normalizeEidosFileInstant, h as decodeEidosFileValues, i as parseEidosFileSelectDefaultOption, j as quoteIdentifier, k as isEidosFileReservedTableName, l as isCanonicalEidosFileDate, n as assertEidosFileSelectDefaultOption, o as currentEidosFileInstant, p as assertEidosFileValues, r as assertEidosFileSelectOptions, s as eidosFileDateSqlCheck, t as assertEidosFileMultiSelectHasNoDefaultOption, u as isCanonicalEidosFileInstant } from "./select-options-DSzDn3qm.mjs";
2
2
  //#region src/connection-port.ts
3
- const INT64_MIN$2 = -9223372036854775808n;
4
- const INT64_MAX$2 = 9223372036854775807n;
3
+ const INT64_MIN$3 = -9223372036854775808n;
4
+ const INT64_MAX$3 = 9223372036854775807n;
5
5
  /** Validates the exact `?1` through `?N` EA-Connection binding grammar. */
6
6
  function exactConnectionBindingCount(sql) {
7
7
  const occurrences = /* @__PURE__ */ new Map();
@@ -119,7 +119,7 @@ var EidosAdapterError = class extends Error {
119
119
  function assertInt64Decimal(value) {
120
120
  if (!/^(?:0|-[1-9][0-9]*|[1-9][0-9]*)$/.test(value) || value === "-0") throw new EidosAdapterError("invalid-sql-value", "INTEGER must use canonical signed int64 decimal text");
121
121
  const parsed = BigInt(value);
122
- if (parsed < INT64_MIN$2 || parsed > INT64_MAX$2) throw new EidosAdapterError("invalid-sql-value", "INTEGER is outside signed int64 range");
122
+ if (parsed < INT64_MIN$3 || parsed > INT64_MAX$3) throw new EidosAdapterError("invalid-sql-value", "INTEGER is outside signed int64 range");
123
123
  return parsed;
124
124
  }
125
125
  function assertUnicodeString(value, label = "TEXT") {
@@ -140,7 +140,7 @@ function sqlValueToNative(value) {
140
140
  function nativeToSqlValue(value) {
141
141
  if (value === null) return { tag: "null" };
142
142
  if (typeof value === "bigint") {
143
- if (value < INT64_MIN$2 || value > INT64_MAX$2) throw new EidosAdapterError("invalid-sql-value", "SQLite returned an INTEGER outside signed int64 range", false, true);
143
+ if (value < INT64_MIN$3 || value > INT64_MAX$3) throw new EidosAdapterError("invalid-sql-value", "SQLite returned an INTEGER outside signed int64 range", false, true);
144
144
  return {
145
145
  tag: "integer",
146
146
  value: String(value)
@@ -222,7 +222,7 @@ var ConnectionPortEidosFileConnection = class {
222
222
  arity,
223
223
  deterministic: true,
224
224
  directOnly: true
225
- }, (...values) => nativeParameterToSqlValue(operation(...values.map(sqlValueToLegacy))));
225
+ }, (...values) => nativeToSqlValue(operation(...values.map(sqlValueToLegacy))));
226
226
  }
227
227
  transaction(operation) {
228
228
  return this.port.transaction("write", operation);
@@ -251,7 +251,7 @@ function nativeParameterToSqlValue(value) {
251
251
  };
252
252
  if (typeof value === "number") {
253
253
  if (!Number.isFinite(value)) throw new EidosAdapterError("invalid-sql-value", "SQL number must be finite");
254
- return Number.isInteger(value) ? {
254
+ return Number.isSafeInteger(value) ? {
255
255
  tag: "integer",
256
256
  value: String(value)
257
257
  } : {
@@ -281,6 +281,438 @@ function sqlValueToLegacy(value) {
281
281
  }
282
282
  }
283
283
  //#endregion
284
+ //#region src/canonical-conversion.ts
285
+ /**
286
+ * Product defaults for conversions requested by the Eidos File editor.
287
+ *
288
+ * These policies are intentionally explicit: the Runtime still reports an
289
+ * `explicit-lossy` preflight and requires confirmation whenever the values in
290
+ * the current Field would actually lose information.
291
+ */
292
+ function recommendedEidosFileConversionPolicies(from, to) {
293
+ const policies = [];
294
+ if (from === "integer" && to === "number") policies.push("round-binary64");
295
+ if (from === "number" && to === "integer") policies.push("round-ties-even");
296
+ if ((from === "integer" || from === "number") && to === "checkbox") policies.push("zero-false-nonzero-true");
297
+ if (from === "datetime" && to === "date") policies.push("utc-date");
298
+ if (from === "multi-select" && to === "select") policies.push("first");
299
+ if (!LIST_TYPES.has(from) && LIST_TYPES.has(to)) policies.push("null-to-empty-list");
300
+ return policies;
301
+ }
302
+ /** Returns the nullable shape used by editor-initiated conversions. */
303
+ function eidosFileConversionTargetNullable(from, to, sourceNullable) {
304
+ if (LIST_TYPES.has(to)) return false;
305
+ if (LIST_TYPES.has(from)) return true;
306
+ return sourceNullable;
307
+ }
308
+ /**
309
+ * Splits a Text value into Multi-select option names using the Runtime rule.
310
+ *
311
+ * A JSON array of strings is read as its ordered choice names; any other text
312
+ * (including malformed JSON, non-string arrays, or commas) is one choice. The
313
+ * Editor reuses this so inferred Field options match converted cell values.
314
+ */
315
+ function eidosFileTextMultiSelectChoices(text) {
316
+ try {
317
+ const parsed = JSON.parse(text);
318
+ if (Array.isArray(parsed) && parsed.every((item) => typeof item === "string")) return parsed;
319
+ } catch {}
320
+ return [text];
321
+ }
322
+ const INT64_MIN$2 = -9223372036854775808n;
323
+ const INT64_MAX$2 = 9223372036854775807n;
324
+ const INT64 = /^(?:0|-[1-9][0-9]*|[1-9][0-9]*)$/u;
325
+ const LIST_TYPES = new Set([
326
+ "multi-select",
327
+ "file",
328
+ "relation"
329
+ ]);
330
+ const TEXT_TYPES = new Set([
331
+ "text",
332
+ "date",
333
+ "datetime",
334
+ "url",
335
+ "select"
336
+ ]);
337
+ const SHARED_TEXT_COLUMN_TYPES = new Set([
338
+ "text",
339
+ "select",
340
+ "url"
341
+ ]);
342
+ /** True when a conversion can retain the exact nullable SQLite column DDL. */
343
+ function eidosFileConversionCanReusePhysicalColumn(from, to, sourceNullable, targetNullable) {
344
+ return sourceNullable === targetNullable && SHARED_TEXT_COLUMN_TYPES.has(from) && SHARED_TEXT_COLUMN_TYPES.has(to);
345
+ }
346
+ function planCanonicalFieldConversion(input) {
347
+ const policies = new Set(input.policies ?? []);
348
+ const sourceHasEmptyList = input.rows.some((row) => {
349
+ if (row.value === null || typeof row.value !== "string") return false;
350
+ return row.value === "[]";
351
+ });
352
+ const changes = /* @__PURE__ */ new Map();
353
+ const rows = [];
354
+ let severity = 0;
355
+ let error;
356
+ for (const row of input.rows) try {
357
+ let converted;
358
+ if (row.value === null) converted = convertNull(input, policies, sourceHasEmptyList);
359
+ else converted = convertNonNull(input, row.value, policies);
360
+ if (converted.value === null && !input.toNullable && !LIST_TYPES.has(input.to)) throw new Error("Destination non-nullability rejects SQL NULL");
361
+ validateDestination(input, converted.value);
362
+ const changed = storageClass(input.from) !== storageClass(input.to) || !sameStorageValue(row.value, converted.value);
363
+ rows.push({
364
+ id: row.id,
365
+ value: converted.value,
366
+ changed
367
+ });
368
+ if (changed) {
369
+ severity = Math.max(severity, converted.class);
370
+ changes.set("value-reencoded", (changes.get("value-reencoded") ?? 0) + 1);
371
+ if (converted.code) changes.set(converted.code, (changes.get(converted.code) ?? 0) + 1);
372
+ }
373
+ } catch (cause) {
374
+ severity = 3;
375
+ error = cause instanceof Error ? cause.message : "Conversion is forbidden";
376
+ break;
377
+ }
378
+ if (severity === 3) return {
379
+ classification: "forbidden",
380
+ rows: [],
381
+ affectedRows: "0",
382
+ valueChanges: [],
383
+ error
384
+ };
385
+ const affectedRows = rows.filter((row) => row.changed).length;
386
+ return {
387
+ classification: severity === 2 ? "explicit-lossy" : severity === 1 ? "lossless-rewrite" : "metadata-only",
388
+ rows,
389
+ affectedRows: String(affectedRows),
390
+ valueChanges: Array.from(changes, ([code, count]) => ({
391
+ code,
392
+ rows: String(count)
393
+ }))
394
+ };
395
+ }
396
+ function convertNull(input, policies, sourceHasEmptyList) {
397
+ if (LIST_TYPES.has(input.to)) {
398
+ if (!policies.has("null-to-empty-list")) throw new Error("SQL NULL requires null-to-empty-list for a list destination");
399
+ return {
400
+ value: "[]",
401
+ class: sourceHasEmptyList ? 2 : 1,
402
+ code: "null-to-empty-list"
403
+ };
404
+ }
405
+ if (!input.toNullable) throw new Error("Destination non-nullability rejects SQL NULL");
406
+ return {
407
+ value: null,
408
+ class: 0
409
+ };
410
+ }
411
+ function convertNonNull(input, value, policies) {
412
+ const { from, to } = input;
413
+ if (from === to) return {
414
+ value,
415
+ class: 0
416
+ };
417
+ if (TEXT_TYPES.has(from) && TEXT_TYPES.has(to)) {
418
+ if (to === "datetime" && from === "date") return {
419
+ value: `${asText(value)}T00:00:00.000Z`,
420
+ class: 1
421
+ };
422
+ if (to === "date" && from === "datetime") {
423
+ const text = asText(value);
424
+ if (text.endsWith("T00:00:00.000Z")) return {
425
+ value: text.slice(0, 10),
426
+ class: 1
427
+ };
428
+ if (!policies.has("utc-date")) throw new Error("Datetime with a time component requires utc-date");
429
+ return {
430
+ value: text.slice(0, 10),
431
+ class: 2,
432
+ code: "datetime-to-date"
433
+ };
434
+ }
435
+ return {
436
+ value: asText(value),
437
+ class: 0
438
+ };
439
+ }
440
+ if (to === "text") {
441
+ if (from === "number") return {
442
+ value: canonicalNumber(asNumber$1(value)),
443
+ class: 1
444
+ };
445
+ if (from === "integer") return {
446
+ value: String(asInteger$1(value)),
447
+ class: 1
448
+ };
449
+ if (from === "checkbox") return {
450
+ value: asCheckbox(value) ? "true" : "false",
451
+ class: 1
452
+ };
453
+ if (LIST_TYPES.has(from)) return {
454
+ value: asText(value),
455
+ class: 0
456
+ };
457
+ }
458
+ if (to === "select") {
459
+ if (from === "number") return {
460
+ value: canonicalNumber(asNumber$1(value)),
461
+ class: 1
462
+ };
463
+ if (from === "integer") return {
464
+ value: String(asInteger$1(value)),
465
+ class: 1
466
+ };
467
+ if (from === "checkbox") return {
468
+ value: asCheckbox(value) ? "true" : "false",
469
+ class: 1
470
+ };
471
+ if (from === "multi-select") {
472
+ const list = parseStringList(value);
473
+ if (list.length === 1) return {
474
+ value: list[0],
475
+ class: 1
476
+ };
477
+ if (!policies.has("first")) throw new Error("Multi-select to Select requires a singleton or first");
478
+ if (list.length === 0) {
479
+ if (!input.toNullable) throw new Error("Empty list requires nullable Select");
480
+ return {
481
+ value: null,
482
+ class: 1,
483
+ code: "list-empty-to-null"
484
+ };
485
+ }
486
+ return {
487
+ value: list[0],
488
+ class: 2,
489
+ code: "list-tail-dropped"
490
+ };
491
+ }
492
+ }
493
+ if (to === "number") {
494
+ if (from === "integer") {
495
+ const integer = asInteger$1(value);
496
+ const number = Number(integer);
497
+ if (Number.isInteger(number) && BigInt(number) === integer) return {
498
+ value: number,
499
+ class: 1
500
+ };
501
+ if (!policies.has("round-binary64") || !Number.isFinite(number)) throw new Error("Integer is not exactly representable as binary64");
502
+ return {
503
+ value: number,
504
+ class: 2,
505
+ code: "binary64-rounded"
506
+ };
507
+ }
508
+ if (from === "checkbox") return {
509
+ value: asCheckbox(value) ? 1 : 0,
510
+ class: 1
511
+ };
512
+ if (from === "text" || from === "select") {
513
+ const text = asText(value);
514
+ const number = Number(text);
515
+ if (!Number.isFinite(number) || canonicalNumber(number) !== text) throw new Error("Text is not the exact inverse binary64 spelling");
516
+ return {
517
+ value: number,
518
+ class: 1
519
+ };
520
+ }
521
+ }
522
+ if (to === "integer") {
523
+ if (from === "checkbox") return {
524
+ value: asCheckbox(value) ? 1n : 0n,
525
+ class: 0
526
+ };
527
+ if (from === "text" || from === "select") return {
528
+ value: parseInt64(asText(value)),
529
+ class: 1
530
+ };
531
+ if (from === "number") return numberToInteger(asNumber$1(value), policies);
532
+ }
533
+ if (to === "checkbox") {
534
+ if (from === "text" || from === "select") {
535
+ const text = asText(value);
536
+ if (text === "true") return {
537
+ value: 1n,
538
+ class: 1
539
+ };
540
+ if (text === "false") return {
541
+ value: 0n,
542
+ class: 1
543
+ };
544
+ throw new Error("Text is not lowercase true or false");
545
+ }
546
+ if (from === "integer") {
547
+ const integer = asInteger$1(value);
548
+ if (integer === 0n || integer === 1n) return {
549
+ value: integer,
550
+ class: 0
551
+ };
552
+ if (!policies.has("zero-false-nonzero-true")) throw new Error("Integer Checkbox conversion requires exact 0/1");
553
+ return {
554
+ value: integer === 0n ? 0n : 1n,
555
+ class: 2,
556
+ code: "numeric-to-checkbox"
557
+ };
558
+ }
559
+ if (from === "number") {
560
+ const number = asNumber$1(value);
561
+ if (number === 0 || number === 1) return {
562
+ value: BigInt(number),
563
+ class: 1
564
+ };
565
+ if (!policies.has("zero-false-nonzero-true")) throw new Error("Number Checkbox conversion requires exact 0.0/1.0");
566
+ return {
567
+ value: number === 0 ? 0n : 1n,
568
+ class: 2,
569
+ code: "numeric-to-checkbox"
570
+ };
571
+ }
572
+ }
573
+ if (LIST_TYPES.has(to)) {
574
+ if (from === "select" && to === "multi-select") return {
575
+ value: canonicalizeEidosFileJson([asText(value)]),
576
+ class: 1
577
+ };
578
+ if (from === "text" && to === "multi-select") {
579
+ const text = asText(value);
580
+ const encoded = canonicalizeEidosFileJson(eidosFileTextMultiSelectChoices(text));
581
+ return {
582
+ value: encoded,
583
+ class: encoded === text ? 0 : 1
584
+ };
585
+ }
586
+ if (from === "multi-select" && to === "relation" || from === "relation" && to === "multi-select" || from === to) return {
587
+ value: asText(value),
588
+ class: 0
589
+ };
590
+ }
591
+ throw new Error(`Conversion from ${from} to ${to} is forbidden`);
592
+ }
593
+ function numberToInteger(number, policies) {
594
+ let result = number;
595
+ let code;
596
+ let classification = 1;
597
+ if (!Number.isInteger(result)) {
598
+ if (policies.has("truncate-toward-zero")) {
599
+ result = Math.trunc(result);
600
+ code = "fraction-truncated";
601
+ } else if (policies.has("round-ties-even")) {
602
+ result = roundTiesEven(result);
603
+ code = "integer-rounded";
604
+ } else throw new Error("Fractional Number requires an Integer rounding policy");
605
+ classification = 2;
606
+ }
607
+ if (!Number.isInteger(result)) throw new Error("Integer result is not integral");
608
+ const integer = BigInt(result);
609
+ if (integer < INT64_MIN$2 || integer > INT64_MAX$2) throw new Error("Integer result is outside signed int64");
610
+ return {
611
+ value: integer,
612
+ class: classification,
613
+ ...code ? { code } : {}
614
+ };
615
+ }
616
+ function validateDestination(input, value) {
617
+ if (value === null) return;
618
+ switch (input.to) {
619
+ case "number":
620
+ asNumber$1(value);
621
+ return;
622
+ case "integer":
623
+ asInteger$1(value);
624
+ return;
625
+ case "checkbox":
626
+ asCheckbox(value);
627
+ return;
628
+ case "date":
629
+ if (!isCanonicalEidosFileDate(value)) throw new Error("Invalid Date value");
630
+ return;
631
+ case "datetime":
632
+ if (!isCanonicalEidosFileInstant(value)) throw new Error("Invalid Datetime value");
633
+ return;
634
+ case "url":
635
+ if (!isEidosFileUriReference(asText(value))) throw new Error("Invalid URI-reference");
636
+ return;
637
+ case "multi-select":
638
+ parseStringList(value);
639
+ return;
640
+ case "file":
641
+ decodeEidosFileValues(asText(value));
642
+ return;
643
+ case "relation": {
644
+ const ids = parseStringList(value);
645
+ if (!ids.every(isEidosFileUuid)) throw new Error("Invalid Relation Row ID");
646
+ if (input.relationCardinality === "one" && ids.length > 1) throw new Error("One-cardinality Relation contains more than one Row ID");
647
+ if (input.relationIdValid && !ids.every(input.relationIdValid)) throw new Error("Relation Row ID does not resolve in the target Table");
648
+ return;
649
+ }
650
+ case "text":
651
+ case "select":
652
+ asText(value);
653
+ return;
654
+ }
655
+ }
656
+ function parseStringList(value) {
657
+ const text = asText(value);
658
+ if (!isCanonicalEidosFileJson(text)) throw new Error("List is not canonical JSON");
659
+ const parsed = parseEidosFileJson(text);
660
+ if (!Array.isArray(parsed) || !parsed.every((entry) => typeof entry === "string")) throw new Error("List must contain only strings");
661
+ if (new Set(parsed).size !== parsed.length) throw new Error("List values must be unique");
662
+ return parsed;
663
+ }
664
+ function asText(value) {
665
+ if (typeof value !== "string") throw new Error("Expected TEXT storage");
666
+ return value;
667
+ }
668
+ function asNumber$1(value) {
669
+ if (typeof value !== "number" || !Number.isFinite(value)) throw new Error("Expected finite REAL storage");
670
+ return Object.is(value, -0) ? 0 : value;
671
+ }
672
+ function asInteger$1(value) {
673
+ if (typeof value === "bigint") {
674
+ if (value < INT64_MIN$2 || value > INT64_MAX$2) throw new Error("INTEGER outside int64");
675
+ return value;
676
+ }
677
+ if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new Error("Expected exact INTEGER storage");
678
+ return BigInt(value);
679
+ }
680
+ function asCheckbox(value) {
681
+ const integer = asInteger$1(value);
682
+ if (integer !== 0n && integer !== 1n) throw new Error("Checkbox must be 0 or 1");
683
+ return integer === 1n;
684
+ }
685
+ function parseInt64(value) {
686
+ if (!INT64.test(value) || value === "-0") throw new Error("Invalid int64 spelling");
687
+ const integer = BigInt(value);
688
+ if (integer < INT64_MIN$2 || integer > INT64_MAX$2) throw new Error("INTEGER outside int64");
689
+ return integer;
690
+ }
691
+ function canonicalNumber(value) {
692
+ return canonicalizeEidosFileJson(value);
693
+ }
694
+ function roundTiesEven(value) {
695
+ const floor = Math.floor(value);
696
+ const fraction = value - floor;
697
+ if (fraction < .5) return floor;
698
+ if (fraction > .5) return floor + 1;
699
+ return floor % 2 === 0 ? floor : floor + 1;
700
+ }
701
+ /** RFC 3986 URI-reference lexical validation shared by writes and conversions. */
702
+ function isEidosFileUriReference(value) {
703
+ return typeof value === "string" && /^(?:[A-Za-z0-9\-._~!$&'()*+,;=:@/?#\[\]]|%[0-9A-Fa-f]{2})*$/u.test(value);
704
+ }
705
+ function sameStorageValue(left, right) {
706
+ if (left instanceof Uint8Array || right instanceof Uint8Array) return left instanceof Uint8Array && right instanceof Uint8Array && left.byteLength === right.byteLength && left.every((byte, index) => byte === right[index]);
707
+ if ((typeof left === "bigint" || typeof left === "number" && Number.isSafeInteger(left)) && (typeof right === "bigint" || typeof right === "number" && Number.isSafeInteger(right))) return BigInt(left) === BigInt(right);
708
+ return typeof left === typeof right && Object.is(left, right);
709
+ }
710
+ function storageClass(type) {
711
+ if (type === "integer" || type === "checkbox") return "integer";
712
+ if (type === "number") return "real";
713
+ return "text";
714
+ }
715
+ //#endregion
284
716
  //#region src/column-stats.ts
285
717
  const COMMON_STATS = [
286
718
  "count-all",
@@ -333,7 +765,7 @@ function displayType(field) {
333
765
  }
334
766
  function eidosFileColumnStatTypesForField(field) {
335
767
  const type = displayType(field);
336
- if (type === "number" || type === "rating") return NUMERIC_STATS;
768
+ if (type === "number" || type === "integer" || type === "rating") return NUMERIC_STATS;
337
769
  if (type === "date" || type === "datetime" || type === "created-time" || type === "last-edited-time") return DATE_STATS;
338
770
  if (type === "checkbox") return CHECKBOX_STATS;
339
771
  if (type === "relation") return RELATION_STATS;
@@ -399,40 +831,261 @@ const EIDOS_FILE_SYSTEM_COLUMNS = [
399
831
  "_updated_at"
400
832
  ];
401
833
  //#endregion
834
+ //#region src/field-query-capabilities.ts
835
+ const EMPTY_OPERATORS = ["is-empty", "is-not-empty"];
836
+ const EQUALITY_OPERATORS = ["equals", "not-equals"];
837
+ const STRING_OPERATORS = [
838
+ ...EQUALITY_OPERATORS,
839
+ "contains",
840
+ "not-contains",
841
+ "starts-with",
842
+ "ends-with",
843
+ ...EMPTY_OPERATORS
844
+ ];
845
+ const ORDERED_OPERATORS = [
846
+ ...EQUALITY_OPERATORS,
847
+ "greater-than",
848
+ "greater-than-or-equal",
849
+ "less-than",
850
+ "less-than-or-equal",
851
+ ...EMPTY_OPERATORS
852
+ ];
853
+ const TEMPORAL_OPERATORS = [
854
+ ...ORDERED_OPERATORS,
855
+ "is-between",
856
+ "is-relative-to-today"
857
+ ];
858
+ const LIST_OPERATORS = [
859
+ "contains",
860
+ "not-contains",
861
+ "is-any-of",
862
+ "is-all-of",
863
+ "is-none-of",
864
+ ...EMPTY_OPERATORS
865
+ ];
866
+ const CHECKBOX_OPERATORS = [...EQUALITY_OPERATORS, ...EMPTY_OPERATORS];
867
+ const FILE_OPERATORS = [...EMPTY_OPERATORS];
868
+ const TYPE_REF_ATOMS = new Set([
869
+ "text",
870
+ "number",
871
+ "integer",
872
+ "checkbox",
873
+ "date",
874
+ "datetime",
875
+ "url",
876
+ "select",
877
+ "multi-select",
878
+ "file",
879
+ "relation",
880
+ "row-id",
881
+ "file-entry"
882
+ ]);
883
+ const LIST_ATOMS = new Set([
884
+ "text",
885
+ "number",
886
+ "integer",
887
+ "checkbox",
888
+ "date",
889
+ "datetime",
890
+ "url",
891
+ "select",
892
+ "row-id",
893
+ "file-entry"
894
+ ]);
895
+ function configuredTypeRef(value) {
896
+ if (typeof value === "string" && TYPE_REF_ATOMS.has(value)) return value;
897
+ if (value !== null && typeof value === "object" && !Array.isArray(value) && value.kind === "list" && typeof value.element === "string" && LIST_ATOMS.has(value.element)) return {
898
+ kind: "list",
899
+ element: value.element
900
+ };
901
+ return null;
902
+ }
903
+ /** Resolve the public logical TypeRef represented by an editor/runtime Field. */
904
+ function eidosFileFieldValueType(field) {
905
+ if (field.systemRole === "row-id" || field.type === "row-id") return "row-id";
906
+ if (field.systemRole === "created-time" || field.systemRole === "updated-time" || field.type === "created-time" || field.type === "last-edited-time") return "datetime";
907
+ if (field.type === "rating") return "integer";
908
+ if (field.type === "formula" || field.type === "lookup") {
909
+ const generated = configuredTypeRef(field.property?.valueType);
910
+ if (generated) return generated;
911
+ const legacyFileEntry = field.type === "lookup" && field.property?.displayType === "json";
912
+ const display = configuredTypeRef(field.property?.displayType) ?? "text";
913
+ if (field.type === "lookup" && field.property?.aggregate === "values") return {
914
+ kind: "list",
915
+ element: display === "file" || legacyFileEntry ? "file-entry" : display === "relation" ? "row-id" : display === "multi-select" ? "select" : typeof display === "object" ? display.element : display
916
+ };
917
+ if (legacyFileEntry) return "file-entry";
918
+ return display;
919
+ }
920
+ if (field.type === "relation") return "relation";
921
+ if (field.type === "multi-select") return "multi-select";
922
+ if (field.type === "file") return "file";
923
+ return configuredTypeRef(field.type) ?? "text";
924
+ }
925
+ /**
926
+ * Canonical query capability policy for Runtime validation and UI affordances.
927
+ * It intentionally describes implemented behavior, not SQLite storage classes.
928
+ */
929
+ function eidosFileTypeRefQueryCapabilities(type, context = {}) {
930
+ const listElement = typeof type === "object" ? type.element : type === "multi-select" ? "select" : type === "relation" ? "row-id" : type === "file" ? "file-entry" : null;
931
+ const list = listElement !== null;
932
+ const fileValue = type === "file-entry" || listElement === "file-entry";
933
+ const ordered = typeof type === "string" && [
934
+ "text",
935
+ "number",
936
+ "integer",
937
+ "checkbox",
938
+ "date",
939
+ "datetime",
940
+ "url",
941
+ "select",
942
+ "row-id"
943
+ ].includes(type);
944
+ const stringMatch = typeof type === "string" && [
945
+ "text",
946
+ "url",
947
+ "select",
948
+ "row-id"
949
+ ].includes(type);
950
+ const searchableList = listElement !== null && [
951
+ "text",
952
+ "url",
953
+ "select",
954
+ "file-entry"
955
+ ].includes(listElement);
956
+ const recordLabelSearchable = context.isRecordLabel === true && typeof type === "string" && [
957
+ "text",
958
+ "url",
959
+ "select",
960
+ "number",
961
+ "integer",
962
+ "checkbox",
963
+ "date",
964
+ "datetime",
965
+ "row-id"
966
+ ].includes(type);
967
+ const contextualRowId = context.contextualRowId === true && typeof type === "object" && type.element === "row-id";
968
+ const searchable = stringMatch || searchableList || type === "file-entry" || type === "relation" || recordLabelSearchable || contextualRowId;
969
+ const sortPosition = !ordered ? null : type === "row-id" ? "last" : "any";
970
+ let filterOperators;
971
+ if (fileValue) filterOperators = FILE_OPERATORS;
972
+ else if (list) filterOperators = LIST_OPERATORS;
973
+ else if (type === "checkbox") filterOperators = CHECKBOX_OPERATORS;
974
+ else if (type === "date" || type === "datetime") filterOperators = TEMPORAL_OPERATORS;
975
+ else if (type === "number" || type === "integer") filterOperators = ORDERED_OPERATORS;
976
+ else if (stringMatch) filterOperators = STRING_OPERATORS;
977
+ else filterOperators = EMPTY_OPERATORS;
978
+ return {
979
+ filterOperators,
980
+ ordered,
981
+ membership: list,
982
+ stringMatch,
983
+ searchable,
984
+ sortable: sortPosition !== null,
985
+ sortPosition,
986
+ groupable: ordered
987
+ };
988
+ }
989
+ function eidosFileFieldQueryCapabilities(field) {
990
+ const type = eidosFileFieldValueType(field);
991
+ const contextualRowIdLookup = field.type === "lookup" && typeof type === "object" && type.element === "row-id" && typeof field.property?.relationField === "string";
992
+ return eidosFileTypeRefQueryCapabilities(type, {
993
+ isRecordLabel: field.isRecordLabel,
994
+ contextualRowId: contextualRowIdLookup
995
+ });
996
+ }
997
+ //#endregion
402
998
  //#region src/formula.ts
403
999
  /** Eidos File 1.0 has no prop()/props() reference syntax. */
404
1000
  const EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES = [];
1001
+ const EIDOS_FILE_FORMULA_PROFILE = "sqlite-3.45";
405
1002
  const EIDOS_FILE_FORMULA_FUNCTION_NAMES = [
406
1003
  "abs",
407
- "coalesce",
408
- "if",
409
- "is_null",
410
- "floor",
411
1004
  "ceil",
1005
+ "ceiling",
1006
+ "char",
1007
+ "coalesce",
412
1008
  "concat",
1009
+ "concat_ws",
1010
+ "date",
1011
+ "datetime",
1012
+ "floor",
1013
+ "format",
1014
+ "glob",
1015
+ "hex",
1016
+ "ifnull",
1017
+ "iif",
1018
+ "instr",
1019
+ "julianday",
413
1020
  "length",
1021
+ "like",
1022
+ "lower",
1023
+ "ltrim",
414
1024
  "max",
415
1025
  "min",
1026
+ "nullif",
1027
+ "octet_length",
1028
+ "printf",
1029
+ "quote",
1030
+ "replace",
1031
+ "round",
1032
+ "rtrim",
1033
+ "sign",
1034
+ "strftime",
416
1035
  "substr",
417
- "lower_ascii",
418
- "upper_ascii",
419
- "date_add_days",
420
- "date_diff_days",
421
- "datetime_add_milliseconds",
422
- "datetime_diff_milliseconds"
1036
+ "substring",
1037
+ "time",
1038
+ "timediff",
1039
+ "trim",
1040
+ "typeof",
1041
+ "unicode",
1042
+ "unixepoch",
1043
+ "upper"
423
1044
  ];
424
1045
  const ALLOWED_FUNCTIONS = new Set(EIDOS_FILE_FORMULA_FUNCTION_NAMES);
425
1046
  const ALLOWED_KEYWORDS = new Set([
426
1047
  "and",
1048
+ "as",
1049
+ "case",
1050
+ "cast",
1051
+ "else",
1052
+ "end",
427
1053
  "false",
1054
+ "integer",
1055
+ "is",
428
1056
  "not",
429
1057
  "null",
430
1058
  "or",
431
- "true"
1059
+ "real",
1060
+ "text",
1061
+ "then",
1062
+ "true",
1063
+ "when"
432
1064
  ]);
433
1065
  const MAX_FORMULA_BYTES = 4096;
434
1066
  const MAX_FORMULA_NODES = 1e4;
435
1067
  const MAX_FORMULA_DEPTH = 256;
1068
+ /** Fails fast when a Host SQLite build cannot execute the fixed 1.0 profile. */
1069
+ function assertEidosFileFormulaProfile(connection) {
1070
+ try {
1071
+ connection.get(`SELECT
1072
+ abs(-1), ceil(1.5), ceiling(1.5), char(65), coalesce(NULL, 1),
1073
+ concat(1, NULL, 'x'), concat_ws('-', 1, NULL, 2),
1074
+ date('2026-01-01'), datetime('2026-01-01'), floor(1.5),
1075
+ format('%d', 1), glob('a*', 'abc'), hex('A'), ifnull(NULL, 1),
1076
+ iif(1, 1, 0), instr('abc', 'b'), julianday('2026-01-01'),
1077
+ length('abc'), like('a%', 'abc'), lower('A'), ltrim(' x'),
1078
+ max(1, 2), min(1, 2), nullif(1, 2), octet_length('😀'),
1079
+ printf('%d', 1), quote('x'), replace('abc', 'b', 'x'), round(1.5),
1080
+ rtrim('x '), sign(-1), strftime('%Y', '2026-01-01'),
1081
+ substr('abc', 1, 2), substring('abc', 1, 2),
1082
+ time('2026-01-01T12:00:00Z'),
1083
+ timediff('2026-01-02', '2026-01-01'), trim(' x '), typeof(1),
1084
+ unicode('A'), unixepoch('2026-01-01'), upper('a')`);
1085
+ } catch (error) {
1086
+ throw new EidosFileError("unsupported-feature", `SQLite build does not satisfy the Eidos Formula 1.0 function profile: ${error instanceof Error ? error.message : String(error)}`);
1087
+ }
1088
+ }
436
1089
  function nextNonWhitespace(source, index) {
437
1090
  while (index < source.length && /\s/u.test(source[index])) index += 1;
438
1091
  return source[index];
@@ -504,7 +1157,7 @@ function eidosFileFormulaReferences(source) {
504
1157
  index += 1;
505
1158
  while (index < source.length && /[A-Za-z0-9_]/.test(source[index])) index += 1;
506
1159
  const token = source.slice(start, index).toLowerCase();
507
- if (nextNonWhitespace(source, index) === "(") {
1160
+ if (nextNonWhitespace(source, index) === "(" && token !== "cast") {
508
1161
  if (!ALLOWED_FUNCTIONS.has(token)) throw new EidosFileError("invalid-schema", `Unsupported Eidos File formula function: ${token}`);
509
1162
  } else if (!ALLOWED_KEYWORDS.has(token)) throw new EidosFileError("invalid-schema", `Field references must be double-quoted: ${source.slice(start, index)}`);
510
1163
  continue;
@@ -518,7 +1171,7 @@ function eidosFileFormulaReferences(source) {
518
1171
  }
519
1172
  continue;
520
1173
  }
521
- if ("()+-*/%<>=!&,".includes(character)) {
1174
+ if ("()+-*/%<>=!|,".includes(character)) {
522
1175
  index += 1;
523
1176
  continue;
524
1177
  }
@@ -644,7 +1297,10 @@ function parseExactFormula(source) {
644
1297
  if ([
645
1298
  "!=",
646
1299
  "<=",
647
- ">="
1300
+ ">=",
1301
+ "==",
1302
+ "<>",
1303
+ "||"
648
1304
  ].includes(pair)) {
649
1305
  tokens.push({
650
1306
  kind: "operator",
@@ -653,7 +1309,7 @@ function parseExactFormula(source) {
653
1309
  index += 2;
654
1310
  continue;
655
1311
  }
656
- if ("+-*/%<>=&".includes(character)) {
1312
+ if ("+-*/%<>=|".includes(character)) {
657
1313
  tokens.push({
658
1314
  kind: "operator",
659
1315
  text: character
@@ -704,6 +1360,49 @@ function parseExactFormula(source) {
704
1360
  type: "boolean",
705
1361
  value: canonical === "TRUE"
706
1362
  };
1363
+ if (canonical === "CAST") {
1364
+ if (take().kind !== "left") throw new EidosFileError("invalid-formula", "CAST requires an opening parenthesis");
1365
+ const operand = expression();
1366
+ if (!word("AS")) throw new EidosFileError("invalid-formula", "CAST requires AS");
1367
+ take();
1368
+ const target = take();
1369
+ const targetName = target.text.toUpperCase();
1370
+ if (target.kind !== "word" || ![
1371
+ "TEXT",
1372
+ "INTEGER",
1373
+ "REAL"
1374
+ ].includes(targetName)) throw new EidosFileError("invalid-formula", "CAST target must be TEXT, INTEGER, or REAL");
1375
+ if (take().kind !== "right") throw new EidosFileError("invalid-formula", "CAST is missing “)”");
1376
+ return {
1377
+ type: "cast",
1378
+ name: targetName.toLowerCase(),
1379
+ operand
1380
+ };
1381
+ }
1382
+ if (canonical === "CASE") {
1383
+ const args = [];
1384
+ while (word("WHEN")) {
1385
+ take();
1386
+ args.push(expression());
1387
+ if (!word("THEN")) throw new EidosFileError("invalid-formula", "CASE WHEN requires THEN");
1388
+ take();
1389
+ args.push(expression());
1390
+ }
1391
+ if (args.length === 0) throw new EidosFileError("invalid-formula", "CASE requires at least one WHEN");
1392
+ let hasElse = false;
1393
+ if (word("ELSE")) {
1394
+ take();
1395
+ args.push(expression());
1396
+ hasElse = true;
1397
+ }
1398
+ if (!word("END")) throw new EidosFileError("invalid-formula", "CASE requires END");
1399
+ take();
1400
+ return {
1401
+ type: "case",
1402
+ value: hasElse,
1403
+ args
1404
+ };
1405
+ }
707
1406
  if (peek().kind !== "left") throw new EidosFileError("invalid-formula", `Field references must be double-quoted: ${token.text}`);
708
1407
  if (!ALLOWED_FUNCTIONS.has(token.text.toLowerCase())) throw new EidosFileError("invalid-formula", `Unsupported Formula function: ${token.text}`);
709
1408
  take();
@@ -743,10 +1442,22 @@ function parseExactFormula(source) {
743
1442
  "%"
744
1443
  ]);
745
1444
  const additive = () => binary(multiplicative, ["+", "-"]);
746
- const concatenation = () => binary(additive, ["&"]);
1445
+ const concatenation = () => binary(additive, ["||"]);
747
1446
  const comparison = () => {
748
1447
  const left = concatenation();
749
- if (!operator("=", "!=", "<", "<=", ">", ">=")) return left;
1448
+ if (word("IS")) {
1449
+ take();
1450
+ const negate = word("NOT");
1451
+ if (negate) take();
1452
+ if (!word("NULL")) throw new EidosFileError("invalid-formula", "Formula IS only supports IS NULL and IS NOT NULL");
1453
+ take();
1454
+ return {
1455
+ type: "is-null",
1456
+ op: negate ? "IS NOT NULL" : "IS NULL",
1457
+ operand: left
1458
+ };
1459
+ }
1460
+ if (!operator("=", "==", "!=", "<>", "<", "<=", ">", ">=")) return left;
750
1461
  return {
751
1462
  type: "binary",
752
1463
  op: take().text,
@@ -803,8 +1514,7 @@ function formulaOperandType(field) {
803
1514
  "checkbox",
804
1515
  "date",
805
1516
  "datetime",
806
- "url",
807
- "json"
1517
+ "url"
808
1518
  ].includes(String(type))) throw new EidosFileError("invalid-schema", `Field “${field.name}” is not a Formula scalar operand`);
809
1519
  return type;
810
1520
  }
@@ -825,6 +1535,35 @@ function exactFormulaType(node, fields, numbers, expected) {
825
1535
  node.literal = literal;
826
1536
  return setFormulaType(node, literal.type);
827
1537
  }
1538
+ if (node.type === "cast") {
1539
+ if (!node.operand) throw new EidosFileError("invalid-schema", "CAST requires an operand");
1540
+ infer(node.operand, "any");
1541
+ const result = {
1542
+ text: "text",
1543
+ integer: "integer",
1544
+ real: "number"
1545
+ }[String(node.name)];
1546
+ if (!result) throw new EidosFileError("invalid-schema", "Invalid CAST target");
1547
+ return setFormulaType(node, result);
1548
+ }
1549
+ if (node.type === "is-null") {
1550
+ if (!node.operand) throw new EidosFileError("invalid-schema", "IS NULL requires an operand");
1551
+ infer(node.operand, "any");
1552
+ return setFormulaType(node, "checkbox");
1553
+ }
1554
+ if (node.type === "case") {
1555
+ const args = node.args ?? [];
1556
+ const hasElse = node.value === true;
1557
+ const branchNodes = [];
1558
+ const pairLength = hasElse ? args.length - 1 : args.length;
1559
+ for (let index = 0; index < pairLength; index += 2) {
1560
+ requireFormulaType(infer(args[index], "checkbox"), "checkbox", "CASE");
1561
+ branchNodes.push(args[index + 1]);
1562
+ }
1563
+ if (hasElse) branchNodes.push(args[args.length - 1]);
1564
+ else branchNodes.push({ type: "null" });
1565
+ return setFormulaType(node, inferFormulaSequence(branchNodes, fields, numbers, expected));
1566
+ }
828
1567
  if (node.type === "unary") {
829
1568
  if (!node.operand || ![
830
1569
  "+",
@@ -855,9 +1594,9 @@ function exactFormulaType(node, fields, numbers, expected) {
855
1594
  requireFormulaType(infer(node.right, "checkbox"), "checkbox", op);
856
1595
  return setFormulaType(node, "checkbox");
857
1596
  }
858
- if (op === "&") {
859
- requireFormulaType(infer(node.left, "text"), "text", "&");
860
- requireFormulaType(infer(node.right, "text"), "text", "&");
1597
+ if (op === "||") {
1598
+ infer(node.left, "any");
1599
+ infer(node.right, "any");
861
1600
  return setFormulaType(node, "text");
862
1601
  }
863
1602
  if ([
@@ -875,14 +1614,16 @@ function exactFormulaType(node, fields, numbers, expected) {
875
1614
  }
876
1615
  if ([
877
1616
  "=",
1617
+ "==",
878
1618
  "!=",
1619
+ "<>",
879
1620
  "<",
880
1621
  "<=",
881
1622
  ">",
882
1623
  ">="
883
1624
  ].includes(op)) {
884
1625
  const pair = inferFormulaPair(node.left, node.right, fields, numbers);
885
- if (!pair.left || !pair.right || !formulaTypesComparable(pair.left, pair.right, op)) throw new EidosFileError("invalid-schema", `${op} operands have incompatible types`);
1626
+ if (!pair.left || !pair.right || !formulaTypesComparable(pair.left, pair.right)) throw new EidosFileError("invalid-schema", `${op} operands have incompatible types`);
886
1627
  return setFormulaType(node, "checkbox");
887
1628
  }
888
1629
  throw new EidosFileError("invalid-schema", `Unsupported Formula operator: ${op}`);
@@ -893,95 +1634,206 @@ function exactFormulaType(node, fields, numbers, expected) {
893
1634
  const arity = (minimum, maximum = minimum) => {
894
1635
  if (args.length < minimum || args.length > maximum) throw new EidosFileError("invalid-schema", `${name} expects ${minimum === maximum ? minimum : `${minimum}–${maximum}`} arguments`);
895
1636
  };
896
- if (name === "if") {
1637
+ if (name === "iif") {
897
1638
  arity(3);
898
- requireFormulaType(infer(args[0], "checkbox"), "checkbox", "IF");
1639
+ requireFormulaType(infer(args[0], "checkbox"), "checkbox", "IIF");
899
1640
  const pair = inferFormulaPair(args[1], args[2], fields, numbers, expected);
900
- if (!pair.left || !pair.right || pair.left !== pair.right) throw new EidosFileError("invalid-schema", "IF branches require one exact type");
1641
+ if (!pair.left || !pair.right || pair.left !== pair.right) throw new EidosFileError("invalid-schema", "IIF branches require one exact type");
901
1642
  return setFormulaType(node, pair.left);
902
1643
  }
903
- if (name === "coalesce") {
904
- arity(2, 16);
1644
+ if (name === "coalesce" || name === "ifnull") {
1645
+ if (name === "ifnull") arity(2);
1646
+ else arity(2, 16);
905
1647
  return setFormulaType(node, inferFormulaSequence(args, fields, numbers, expected));
906
1648
  }
907
- if (name === "is_null") {
908
- arity(1);
909
- infer(args[0], "any");
910
- return setFormulaType(node, "checkbox");
1649
+ if (name === "nullif") {
1650
+ arity(2);
1651
+ const pair = inferFormulaPair(args[0], args[1], fields, numbers, expected);
1652
+ if (!pair.left || !pair.right || !formulaTypesComparable(pair.left, pair.right)) throw new EidosFileError("invalid-schema", "NULLIF arguments have incompatible types");
1653
+ return setFormulaType(node, pair.left);
911
1654
  }
912
- if (name === "abs") {
1655
+ if ([
1656
+ "abs",
1657
+ "ceil",
1658
+ "ceiling",
1659
+ "floor"
1660
+ ].includes(name)) {
913
1661
  arity(1);
914
1662
  const type = infer(args[0], expected);
915
- if (!isNumericFormulaType(type)) throw new EidosFileError("invalid-schema", "ABS requires numeric input");
1663
+ if (!isNumericFormulaType(type)) throw new EidosFileError("invalid-schema", `${name.toUpperCase()} requires numeric input`);
916
1664
  return setFormulaType(node, type);
917
1665
  }
1666
+ if (name === "round") {
1667
+ arity(1, 2);
1668
+ if (!isNumericFormulaType(infer(args[0], "number"))) throw new EidosFileError("invalid-schema", "ROUND requires numeric input");
1669
+ if (args[1]) requireFormulaType(infer(args[1], "integer"), "integer", "ROUND");
1670
+ return setFormulaType(node, "number");
1671
+ }
1672
+ if (name === "sign") {
1673
+ arity(1);
1674
+ if (!isNumericFormulaType(infer(args[0], "integer"))) throw new EidosFileError("invalid-schema", "SIGN requires numeric input");
1675
+ return setFormulaType(node, "integer");
1676
+ }
918
1677
  if (name === "min" || name === "max") {
919
1678
  arity(2, 16);
920
- const type = inferFormulaSequence(args, fields, numbers, expected, true);
921
- if (type === "json") throw new EidosFileError("invalid-formula", `${name} requires sortable arguments`);
922
- return setFormulaType(node, type);
1679
+ return setFormulaType(node, inferFormulaSequence(args, fields, numbers, expected));
923
1680
  }
924
- const signatures = {
925
- floor: {
926
- args: ["number"],
927
- result: "integer"
1681
+ if (name === "concat") {
1682
+ arity(1, 16);
1683
+ for (const argument of args) infer(argument, "any");
1684
+ return setFormulaType(node, "text");
1685
+ }
1686
+ if (name === "concat_ws") {
1687
+ arity(2, 16);
1688
+ requireFormulaType(infer(args[0], "text"), "text", "CONCAT_WS");
1689
+ for (const argument of args.slice(1)) infer(argument, "any");
1690
+ return setFormulaType(node, "text");
1691
+ }
1692
+ if (name === "format" || name === "printf") {
1693
+ arity(1, 16);
1694
+ requireFormulaType(infer(args[0], "text"), "text", name);
1695
+ for (const argument of args.slice(1)) infer(argument, "any");
1696
+ return setFormulaType(node, "text");
1697
+ }
1698
+ if (name === "char") {
1699
+ arity(1, 16);
1700
+ for (const argument of args) requireFormulaType(infer(argument, "integer"), "integer", "CHAR");
1701
+ return setFormulaType(node, "text");
1702
+ }
1703
+ const fixedSignature = {
1704
+ glob: {
1705
+ args: ["text", "text"],
1706
+ result: "checkbox"
928
1707
  },
929
- ceil: {
930
- args: ["number"],
1708
+ instr: {
1709
+ args: ["text", "text"],
931
1710
  result: "integer"
932
1711
  },
933
1712
  length: {
934
1713
  args: ["text"],
935
1714
  result: "integer"
936
1715
  },
937
- substr: {
1716
+ like: {
938
1717
  args: [
939
1718
  "text",
940
- "integer",
941
- "integer"
1719
+ "text",
1720
+ "text"
942
1721
  ],
943
- result: "text",
1722
+ result: "checkbox",
944
1723
  optionalLast: true
945
1724
  },
946
- lower_ascii: {
1725
+ lower: {
947
1726
  args: ["text"],
948
1727
  result: "text"
949
1728
  },
950
- upper_ascii: {
1729
+ ltrim: {
1730
+ args: ["text", "text"],
1731
+ result: "text",
1732
+ optionalLast: true
1733
+ },
1734
+ octet_length: {
951
1735
  args: ["text"],
1736
+ result: "integer"
1737
+ },
1738
+ replace: {
1739
+ args: [
1740
+ "text",
1741
+ "text",
1742
+ "text"
1743
+ ],
952
1744
  result: "text"
953
1745
  },
954
- date_add_days: {
955
- args: ["date", "integer"],
956
- result: "date"
1746
+ rtrim: {
1747
+ args: ["text", "text"],
1748
+ result: "text",
1749
+ optionalLast: true
957
1750
  },
958
- date_diff_days: {
959
- args: ["date", "date"],
960
- result: "integer"
1751
+ substr: {
1752
+ args: [
1753
+ "text",
1754
+ "integer",
1755
+ "integer"
1756
+ ],
1757
+ result: "text",
1758
+ optionalLast: true
961
1759
  },
962
- datetime_add_milliseconds: {
963
- args: ["datetime", "integer"],
964
- result: "datetime"
1760
+ substring: {
1761
+ args: [
1762
+ "text",
1763
+ "integer",
1764
+ "integer"
1765
+ ],
1766
+ result: "text",
1767
+ optionalLast: true
1768
+ },
1769
+ trim: {
1770
+ args: ["text", "text"],
1771
+ result: "text",
1772
+ optionalLast: true
965
1773
  },
966
- datetime_diff_milliseconds: {
967
- args: ["datetime", "datetime"],
1774
+ unicode: {
1775
+ args: ["text"],
968
1776
  result: "integer"
1777
+ },
1778
+ upper: {
1779
+ args: ["text"],
1780
+ result: "text"
969
1781
  }
970
- };
971
- if (name === "concat") {
972
- arity(2, 16);
973
- for (const argument of args) requireFormulaType(infer(argument, "text"), "text", "CONCAT");
1782
+ }[name];
1783
+ if (fixedSignature) {
1784
+ arity(fixedSignature.optionalLast ? fixedSignature.args.length - 1 : fixedSignature.args.length, fixedSignature.args.length);
1785
+ args.forEach((argument, index) => requireFormulaType(infer(argument, fixedSignature.args[index]), fixedSignature.args[index], name));
1786
+ return setFormulaType(node, fixedSignature.result);
1787
+ }
1788
+ if ([
1789
+ "hex",
1790
+ "quote",
1791
+ "typeof"
1792
+ ].includes(name)) {
1793
+ arity(1);
1794
+ infer(args[0], "any");
974
1795
  return setFormulaType(node, "text");
975
1796
  }
976
- const signature = signatures[name];
977
- if (!signature) throw new EidosFileError("invalid-schema", `Unsupported Formula function: ${name}`);
978
- arity(signature.optionalLast ? signature.args.length - 1 : signature.args.length, signature.args.length);
979
- if (name === "substr" && args.length === 3 && args[2]?.type === "unary" && args[2].op === "-" && args[2].operand?.literal) {
980
- const literal = args[2].operand.literal;
981
- if (literal.type === "integer" && BigInt(literal.raw) > 0n) throw new EidosFileError("invalid-formula", "SUBSTR literal length cannot be negative");
1797
+ if ([
1798
+ "date",
1799
+ "datetime",
1800
+ "julianday",
1801
+ "strftime",
1802
+ "time",
1803
+ "timediff",
1804
+ "unixepoch"
1805
+ ].includes(name)) {
1806
+ const timeValue = (argument) => {
1807
+ if (argument.type === "string") {
1808
+ if (String(argument.value).toLowerCase() === "now") throw new EidosFileError("invalid-formula", `${name.toUpperCase()} does not allow the current-time literal`);
1809
+ return;
1810
+ }
1811
+ const type = infer(argument, "date");
1812
+ if (type !== "date" && type !== "datetime") throw new EidosFileError("invalid-schema", `${name.toUpperCase()} requires a date or datetime time value`);
1813
+ };
1814
+ const modifier = (argument) => {
1815
+ if (argument.type !== "string") throw new EidosFileError("invalid-formula", `${name.toUpperCase()} modifiers must be string literals`);
1816
+ const value = String(argument.value).toLowerCase();
1817
+ if ([
1818
+ "localtime",
1819
+ "utc",
1820
+ "auto"
1821
+ ].includes(value) || name === "unixepoch" && ["subsec", "subsecond"].includes(value)) throw new EidosFileError("invalid-formula", `${name.toUpperCase()} modifier is not deterministic`);
1822
+ };
1823
+ if (name === "timediff") {
1824
+ arity(2);
1825
+ timeValue(args[0]);
1826
+ timeValue(args[1]);
1827
+ return setFormulaType(node, "text");
1828
+ }
1829
+ const firstTimeValue = name === "strftime" ? 1 : 0;
1830
+ arity(name === "strftime" ? 2 : 1, 9);
1831
+ if (name === "strftime" && args[0]?.type !== "string") throw new EidosFileError("invalid-formula", "STRFTIME format must be a string literal");
1832
+ timeValue(args[firstTimeValue]);
1833
+ for (const argument of args.slice(firstTimeValue + 1)) modifier(argument);
1834
+ return setFormulaType(node, name === "date" ? "date" : name === "datetime" ? "datetime" : name === "julianday" ? "number" : name === "unixepoch" ? "integer" : "text");
982
1835
  }
983
- args.forEach((argument, index) => requireFormulaType(infer(argument, signature.args[index]), signature.args[index], name));
984
- return setFormulaType(node, signature.result);
1836
+ throw new EidosFileError("invalid-schema", `Unsupported Formula function: ${name}`);
985
1837
  }
986
1838
  throw new EidosFileError("invalid-schema", `Unsupported Formula AST node: ${node.type}`);
987
1839
  }
@@ -1023,10 +1875,10 @@ function requireFormulaType(actual, expected, operation) {
1023
1875
  function isNumericFormulaType(type) {
1024
1876
  return type === "integer" || type === "number";
1025
1877
  }
1026
- function formulaTypesComparable(left, right, operator) {
1878
+ function formulaTypesComparable(left, right) {
1027
1879
  if (isNumericFormulaType(left) && isNumericFormulaType(right)) return true;
1028
1880
  if (left !== right) return false;
1029
- return operator === "=" || operator === "!=" || left !== "json";
1881
+ return true;
1030
1882
  }
1031
1883
  function compileExactFormulaNode(node, fields, resolve) {
1032
1884
  const compile = (child) => compileExactFormulaNode(child, fields, resolve);
@@ -1042,6 +1894,17 @@ function compileExactFormulaNode(node, fields, resolve) {
1042
1894
  if (!field) throw new EidosFileError("field-not-found", `Formula Field not found: ${node.name}`);
1043
1895
  return `(${resolve(field)})`;
1044
1896
  }
1897
+ if (node.type === "cast") return `CAST((${compile(node.operand)}) AS ${String(node.name).toUpperCase()})`;
1898
+ if (node.type === "is-null") return `((${compile(node.operand)}) ${String(node.op).toUpperCase()})`;
1899
+ if (node.type === "case") {
1900
+ const args = node.args ?? [];
1901
+ const hasElse = node.value === true;
1902
+ const pairLength = hasElse ? args.length - 1 : args.length;
1903
+ const clauses = [];
1904
+ for (let index = 0; index < pairLength; index += 2) clauses.push(`WHEN (${compile(args[index])}) THEN (${compile(args[index + 1])})`);
1905
+ if (hasElse) clauses.push(`ELSE (${compile(args[args.length - 1])})`);
1906
+ return `(CASE ${clauses.join(" ")} END)`;
1907
+ }
1045
1908
  if (node.type === "unary") {
1046
1909
  const operand = compile(node.operand);
1047
1910
  if (node.op === "NOT") return `(NOT (${operand}))`;
@@ -1053,7 +1916,7 @@ function compileExactFormulaNode(node, fields, resolve) {
1053
1916
  const right = compile(node.right);
1054
1917
  const op = String(node.op).toUpperCase();
1055
1918
  if (op === "AND" || op === "OR") return `((${left}) ${op} (${right}))`;
1056
- if (op === "&") return `((${left}) || (${right}))`;
1919
+ if (op === "||") return `((${left}) || (${right}))`;
1057
1920
  if ([
1058
1921
  "+",
1059
1922
  "-",
@@ -1064,6 +1927,7 @@ function compileExactFormulaNode(node, fields, resolve) {
1064
1927
  }
1065
1928
  if (op === "/") return `eidos_formula_num_div(${left}, ${right})`;
1066
1929
  if (op === "%") return `eidos_formula_int_mod(${left}, ${right})`;
1930
+ const normalizedOp = op === "==" ? "=" : op === "<>" ? "!=" : op;
1067
1931
  const leftType = node.left.inferred;
1068
1932
  const rightType = node.right.inferred;
1069
1933
  if (isNumericFormulaType(leftType) && isNumericFormulaType(rightType) && (leftType !== rightType || leftType === "number")) return `eidos_formula_numeric_${{
@@ -1073,25 +1937,10 @@ function compileExactFormulaNode(node, fields, resolve) {
1073
1937
  "<=": "lte",
1074
1938
  ">": "gt",
1075
1939
  ">=": "gte"
1076
- }[op]}(${left}, ${right})`;
1077
- return `((${left}) COLLATE BINARY ${op} (${right}) COLLATE BINARY)`;
1078
- }
1079
- if (node.type === "call") {
1080
- const name = String(node.function?.name).toLowerCase();
1081
- const args = (node.args ?? []).map(compile);
1082
- if (name === "if") return `(CASE WHEN (${args[0]}) THEN (${args[1]}) ELSE (${args[2]}) END)`;
1083
- if (name === "coalesce") return `coalesce(${args.join(", ")})`;
1084
- if (name === "is_null") return `((${args[0]}) IS NULL)`;
1085
- if (name === "abs") return `eidos_formula_${node.inferred === "integer" ? "int" : "num"}_abs(${args[0]})`;
1086
- if (name === "floor" || name === "ceil") return `eidos_formula_${name}(${args[0]})`;
1087
- if (name === "min" || name === "max") return (node.args ?? []).some((arg) => arg.inferred === "number") && (node.args ?? []).some((arg) => arg.inferred === "integer") ? `eidos_formula_numeric_${name}(${args.join(", ")})` : `${name}(${args.join(", ")})`;
1088
- if (name === "concat") return `(${args.map((arg) => `(${arg})`).join(" || ")})`;
1089
- if (name === "length") return `eidos_formula_length(${args[0]})`;
1090
- if (name === "substr") return `eidos_formula_substr${args.length}(${args.join(", ")})`;
1091
- if (name === "lower_ascii") return `eidos_formula_lower_ascii(${args[0]})`;
1092
- if (name === "upper_ascii") return `eidos_formula_upper_ascii(${args[0]})`;
1093
- return `eidos_formula_${name}(${args.join(", ")})`;
1940
+ }[normalizedOp]}(${left}, ${right})`;
1941
+ return `((${left}) COLLATE BINARY ${normalizedOp} (${right}) COLLATE BINARY)`;
1094
1942
  }
1943
+ if (node.type === "call") return `${String(node.function?.name).toLowerCase()}(${(node.args ?? []).map(compile).join(", ")})`;
1095
1944
  throw new EidosFileError("invalid-schema", `Unsupported Formula AST node: ${node.type}`);
1096
1945
  }
1097
1946
  function exactFormulaLiteral(node, numbers) {
@@ -1106,16 +1955,19 @@ function exactFormulaPrecedence(node) {
1106
1955
  if (op === "AND") return 2;
1107
1956
  if ([
1108
1957
  "=",
1958
+ "==",
1109
1959
  "!=",
1960
+ "<>",
1110
1961
  "<",
1111
1962
  "<=",
1112
1963
  ">",
1113
1964
  ">="
1114
1965
  ].includes(op)) return 4;
1115
- if (op === "&") return 5;
1966
+ if (op === "||") return 5;
1116
1967
  if (op === "+" || op === "-") return 6;
1117
1968
  return 7;
1118
1969
  }
1970
+ if (node.type === "is-null") return 4;
1119
1971
  if (node.type === "unary") return node.op === "NOT" ? 3 : 8;
1120
1972
  return 9;
1121
1973
  }
@@ -1126,6 +1978,22 @@ function serializeExactFormulaNode(node, numbers) {
1126
1978
  if (node.type === "boolean") return node.value === true ? "TRUE" : "FALSE";
1127
1979
  if (node.type === "string") return `'${String(node.value).replace(/'/g, "''")}'`;
1128
1980
  if (node.type === "ref") return quoteIdentifier(String(node.name));
1981
+ if (node.type === "cast") return `CAST(${serializeExactFormulaNode(node.operand, numbers)} AS ${String(node.name).toUpperCase()})`;
1982
+ if (node.type === "is-null") {
1983
+ const operand = node.operand;
1984
+ let serialized = serializeExactFormulaNode(operand, numbers);
1985
+ if (exactFormulaPrecedence(operand) < 5) serialized = `(${serialized})`;
1986
+ return `${serialized} ${String(node.op).toUpperCase()}`;
1987
+ }
1988
+ if (node.type === "case") {
1989
+ const args = node.args ?? [];
1990
+ const hasElse = node.value === true;
1991
+ const pairLength = hasElse ? args.length - 1 : args.length;
1992
+ const clauses = [];
1993
+ for (let index = 0; index < pairLength; index += 2) clauses.push(`WHEN ${serializeExactFormulaNode(args[index], numbers)} THEN ${serializeExactFormulaNode(args[index + 1], numbers)}`);
1994
+ if (hasElse) clauses.push(`ELSE ${serializeExactFormulaNode(args[args.length - 1], numbers)}`);
1995
+ return `CASE ${clauses.join(" ")} END`;
1996
+ }
1129
1997
  if (node.type === "unary") {
1130
1998
  const operand = node.operand;
1131
1999
  if (node.op === "-" && exactFormulaLiteral(operand, numbers)?.raw === "9223372036854775808") return "-9223372036854775808";
@@ -1135,10 +2003,13 @@ function serializeExactFormulaNode(node, numbers) {
1135
2003
  }
1136
2004
  if (node.type === "binary") {
1137
2005
  const op = String(node.op).toUpperCase();
2006
+ const serializedOp = op === "==" ? "=" : op === "<>" ? "!=" : op;
1138
2007
  const precedence = exactFormulaPrecedence(node);
1139
2008
  const comparison = [
1140
2009
  "=",
2010
+ "==",
1141
2011
  "!=",
2012
+ "<>",
1142
2013
  "<",
1143
2014
  "<=",
1144
2015
  ">",
@@ -1150,7 +2021,7 @@ function serializeExactFormulaNode(node, numbers) {
1150
2021
  if (childPrecedence < precedence || right && childPrecedence === precedence || comparison && value.type === "binary" && childPrecedence === 4) serialized = `(${serialized})`;
1151
2022
  return serialized;
1152
2023
  };
1153
- return `${child(node.left, false)} ${op} ${child(node.right, true)}`;
2024
+ return `${child(node.left, false)} ${serializedOp} ${child(node.right, true)}`;
1154
2025
  }
1155
2026
  if (node.type === "call") return `${String(node.function?.name).toUpperCase()}(${(node.args ?? []).map((argument) => serializeExactFormulaNode(argument, numbers)).join(", ")})`;
1156
2027
  throw new EidosFileError("invalid-formula", `Unsupported Formula AST node: ${node.type}`);
@@ -1259,7 +2130,11 @@ function eidosFileLookupTargetValueType(target) {
1259
2130
  const type = typeof target === "string" ? target : target.type;
1260
2131
  const generated = typeof target === "string" ? void 0 : target.property?.valueType;
1261
2132
  const generatedObject = generated && typeof generated === "object" ? generated : void 0;
1262
- if (typeof generated === "string" || generatedObject?.kind === "list" && typeof generatedObject.element === "string") return generated;
2133
+ if (typeof generated === "string") {
2134
+ if (generated === "json") return type === "lookup" ? "file-entry" : "text";
2135
+ return generated;
2136
+ }
2137
+ if (generatedObject?.kind === "list" && typeof generatedObject.element === "string") return generated;
1263
2138
  if (type === "row-id") return "row-id";
1264
2139
  if (type === "created-time" || type === "last-edited-time") return "datetime";
1265
2140
  if (type === "multi-select") return "multi-select";
@@ -1267,7 +2142,10 @@ function eidosFileLookupTargetValueType(target) {
1267
2142
  if (type === "relation") return "relation";
1268
2143
  if (type === "select") return "select";
1269
2144
  if (type === "rating") return "number";
1270
- if (type === "formula" || type === "lookup") return eidosFileLookupTargetDisplayType(target);
2145
+ if (type === "formula" || type === "lookup") {
2146
+ if (type === "lookup" && typeof target !== "string" && target.property?.displayType === "json") return "file-entry";
2147
+ return eidosFileLookupTargetDisplayType(target);
2148
+ }
1271
2149
  return type;
1272
2150
  }
1273
2151
  function eidosFileLookupElementType(target) {
@@ -1292,21 +2170,21 @@ function eidosFileLookupValueType(aggregate, target) {
1292
2170
  function eidosFileLookupTargetDisplayType(target) {
1293
2171
  const type = typeof target === "string" ? target : target.type;
1294
2172
  const configuredDisplayType = typeof target === "string" ? void 0 : target.property?.displayType;
1295
- if ((type === "formula" || type === "lookup") && (configuredDisplayType === "text" || configuredDisplayType === "number" || configuredDisplayType === "integer" || configuredDisplayType === "checkbox" || configuredDisplayType === "date" || configuredDisplayType === "datetime" || configuredDisplayType === "url" || configuredDisplayType === "json")) return configuredDisplayType;
2173
+ if ((type === "formula" || type === "lookup") && (configuredDisplayType === "text" || configuredDisplayType === "number" || configuredDisplayType === "integer" || configuredDisplayType === "checkbox" || configuredDisplayType === "date" || configuredDisplayType === "datetime" || configuredDisplayType === "url")) return configuredDisplayType;
1296
2174
  if (type === "number" || type === "rating") return "number";
1297
2175
  if (type === "integer") return "integer";
1298
2176
  if (type === "checkbox") return "checkbox";
1299
2177
  if (type === "date") return "date";
1300
2178
  if (type === "datetime") return "datetime";
1301
2179
  if (type === "url") return "url";
1302
- if (type === "file") return "json";
2180
+ if (type === "file") return "text";
1303
2181
  return "text";
1304
2182
  }
1305
2183
  function eidosFileLookupDisplayType(aggregate, target) {
1306
2184
  const valueType = eidosFileLookupValueType(aggregate, target);
1307
2185
  const atom = typeof valueType === "object" ? valueType.element : valueType;
1308
2186
  if (atom === "row-id" || atom === "select") return "text";
1309
- if (atom === "file-entry") return "json";
2187
+ if (atom === "file-entry") return "text";
1310
2188
  return atom;
1311
2189
  }
1312
2190
  function eidosFileFieldStoresJsonArray(field) {
@@ -1473,9 +2351,60 @@ function caseInsensitiveLike(expression, pattern, searchText, params) {
1473
2351
  LIKE eidos_casefold(?) ESCAPE '\\'` : `COALESCE(CAST(${expression} AS TEXT), '')
1474
2352
  LIKE ? COLLATE NOCASE ESCAPE '\\'`;
1475
2353
  }
2354
+ function searchExpression(field, pattern, searchText, params) {
2355
+ const column = quoteIdentifier(field.tableColumnName);
2356
+ const type = eidosFileFieldValueType(field);
2357
+ if (field.isRecordLabel && typeof type === "string") return caseInsensitiveLike(type === "checkbox" ? `CASE ${column} WHEN 1 THEN 'true' WHEN 0 THEN 'false' ELSE NULL END` : type === "number" ? `eidos_canonical_number(${column})` : column, pattern, searchText, params);
2358
+ const contextualRowIdLookup = field.type === "lookup" && typeof type === "object" && type.element === "row-id" && typeof field.property?.relationField === "string";
2359
+ if (type === "relation" || contextualRowIdLookup) {
2360
+ const fragments = quoteIdentifier(`${field.tableColumnName}__search`);
2361
+ return `EXISTS (
2362
+ SELECT 1
2363
+ FROM json_each(
2364
+ CASE
2365
+ WHEN json_valid(${fragments}) AND json_type(${fragments}) = 'array'
2366
+ THEN ${fragments}
2367
+ ELSE '[]'
2368
+ END
2369
+ ) item
2370
+ WHERE ${caseInsensitiveLike("item.value", pattern, searchText, params)}
2371
+ )`;
2372
+ }
2373
+ const listElement = typeof type === "object" ? type.element : type === "multi-select" ? "select" : type === "file" ? "file-entry" : null;
2374
+ const fileEntryPredicate = (entry) => {
2375
+ const name = `json_extract(${entry}, '$.name')`;
2376
+ const mediaType = `json_extract(${entry}, '$.mediaType')`;
2377
+ const uri = `json_extract(${entry}, '$.uri')`;
2378
+ return `(
2379
+ ${caseInsensitiveLike(name, pattern, searchText, params)}
2380
+ OR ${caseInsensitiveLike(mediaType, pattern, searchText, params)}
2381
+ OR (
2382
+ lower(COALESCE(CAST(${uri} AS TEXT), '')) NOT LIKE 'data:%'
2383
+ AND ${caseInsensitiveLike(uri, pattern, searchText, params)}
2384
+ )
2385
+ )`;
2386
+ };
2387
+ if (type === "file-entry") return fileEntryPredicate(`CASE WHEN json_valid(${column}) THEN ${column} ELSE '{}' END`);
2388
+ if (listElement === null) return caseInsensitiveLike(column, pattern, searchText, params);
2389
+ return `EXISTS (
2390
+ SELECT 1
2391
+ FROM json_each(
2392
+ CASE
2393
+ WHEN json_valid(${column}) AND json_type(${column}) = 'array'
2394
+ THEN ${column}
2395
+ ELSE '[]'
2396
+ END
2397
+ ) item
2398
+ WHERE ${listElement === "file-entry" ? fileEntryPredicate("CASE WHEN item.type = 'object' THEN item.value ELSE '{}' END") : caseInsensitiveLike("item.value", pattern, searchText, params)}
2399
+ )`;
2400
+ }
1476
2401
  function searchableFields(fields, selected) {
1477
2402
  const selection = selected ? new Set(selected) : null;
1478
- return fields.filter((field) => (!selection || !!field.id && selection.has(field.id)) && !field.isHidden && (field.isRecordLabel || field.valueKind === "source" || field.valueKind === "materialized" || field.valueKind === "derived"));
2403
+ return fields.filter((field) => (!selection || !!field.id && selection.has(field.id)) && (!field.isHidden || selection !== null) && eidosFileFieldQueryCapabilities(field).searchable && (field.isRecordLabel || field.valueKind === "source" || field.valueKind === "materialized" || field.valueKind === "derived" || field.valueKind === "relation" || selection !== null && field.valueKind === "system"));
2404
+ }
2405
+ function eidosFileRowQuerySearchFields(fields, query) {
2406
+ const normalized = normalizeEidosFileRowQuery(query);
2407
+ return normalized.search === void 0 || normalized.search === "" ? [] : searchableFields(fields, normalized.searchFields);
1479
2408
  }
1480
2409
  function collectFilterFields(filter, target) {
1481
2410
  if (!filter) return;
@@ -1715,17 +2644,21 @@ function compileGroup(group, fields, params, referenceInstant) {
1715
2644
  return group.negated ? `NOT (${expression})` : expression;
1716
2645
  }
1717
2646
  function eidosFileSortExpression(field) {
1718
- const column = quoteIdentifier(field.tableColumnName);
1719
- return field.storageCodec === "json_array" || field.storageCodec === "relation" ? `(SELECT item.value FROM json_each(${column}) item
1720
- WHERE item.value IS NOT NULL ORDER BY CAST(item.key AS INTEGER) LIMIT 1)` : column;
2647
+ if (!eidosFileFieldQueryCapabilities(field).sortable) throw new EidosFileError("invalid-query", `${field.name} does not support sorting`);
2648
+ return quoteIdentifier(field.tableColumnName);
1721
2649
  }
1722
2650
  function compileSorts(sorts, fields) {
1723
2651
  const seen = /* @__PURE__ */ new Set();
1724
2652
  const clauses = [];
1725
- for (const sort of sorts ?? []) {
1726
- if (seen.has(sort.field)) continue;
1727
- const field = requireField(fields, sort.field);
2653
+ const uniqueSorts = (sorts ?? []).filter((sort) => {
2654
+ if (seen.has(sort.field)) return false;
1728
2655
  seen.add(sort.field);
2656
+ return true;
2657
+ });
2658
+ for (const [index, sort] of uniqueSorts.entries()) {
2659
+ const field = requireField(fields, sort.field);
2660
+ const capabilities = eidosFileFieldQueryCapabilities(field);
2661
+ if (!capabilities.sortable || capabilities.sortPosition === "last" && index < uniqueSorts.length - 1) throw new EidosFileError("invalid-query", `${field.name} does not support this sort position`);
1729
2662
  const expression = eidosFileSortExpression(field);
1730
2663
  clauses.push(`${expression} ${sort.direction === "desc" ? "DESC" : "ASC"} NULLS ${sort.nulls === "first" ? "FIRST" : "LAST"}`);
1731
2664
  }
@@ -1740,7 +2673,7 @@ function compileEidosFileRowQuery(fields, query = {}, context = {}) {
1740
2673
  const search = query.search;
1741
2674
  if (search !== void 0 && search !== "") {
1742
2675
  const pattern = `%${escapeLike(search)}%`;
1743
- const searchClauses = searchableFields(fields, query.searchFields).map((field) => caseInsensitiveLike(quoteIdentifier(field.tableColumnName), pattern, search, params));
2676
+ const searchClauses = eidosFileRowQuerySearchFields(fields, query).map((field) => searchExpression(field, pattern, search, params));
1744
2677
  if (searchClauses.length > 0) where.push(`(${searchClauses.join(" OR ")})`);
1745
2678
  }
1746
2679
  if (query.filter) where.push(compileGroup(query.filter, byColumn, params, context.referenceInstant ?? currentEidosFileInstant()));
@@ -1850,6 +2783,17 @@ const INT64_MIN$1 = -9223372036854775808n;
1850
2783
  const INT64_MAX$1 = 9223372036854775807n;
1851
2784
  function registerEidosFormulaFunctions(connection) {
1852
2785
  const fixed = (name, arity, operation) => connection.registerFunction(name, operation, arity);
2786
+ const fixedWhenMissing = (name, arity, operation) => {
2787
+ try {
2788
+ connection.get(`SELECT ${name}(1.5) AS value`);
2789
+ } catch {
2790
+ fixed(name, arity, operation);
2791
+ }
2792
+ };
2793
+ fixedWhenMissing("floor", 1, (value) => sqliteIntegralBoundary(value, Math.floor));
2794
+ fixedWhenMissing("ceil", 1, (value) => sqliteIntegralBoundary(value, Math.ceil));
2795
+ fixedWhenMissing("ceiling", 1, (value) => sqliteIntegralBoundary(value, Math.ceil));
2796
+ fixedWhenMissing("sign", 1, sqliteSign);
1853
2797
  fixed("eidos_formula_int_add", 2, (a, b) => intBinary(a, b, (x, y) => x + y));
1854
2798
  fixed("eidos_formula_int_sub", 2, (a, b) => intBinary(a, b, (x, y) => x - y));
1855
2799
  fixed("eidos_formula_int_mul", 2, (a, b) => intBinary(a, b, (x, y) => x * y));
@@ -1864,11 +2808,6 @@ function registerEidosFormulaFunctions(connection) {
1864
2808
  const integer = asInteger(value);
1865
2809
  return integer === INT64_MIN$1 ? null : -integer;
1866
2810
  });
1867
- fixed("eidos_formula_int_abs", 1, (value) => {
1868
- if (value === null) return null;
1869
- const integer = asInteger(value);
1870
- return integer === INT64_MIN$1 ? null : integer < 0n ? -integer : integer;
1871
- });
1872
2811
  fixed("eidos_formula_num_add", 2, (a, b) => numberBinary(a, b, (x, y) => x + y));
1873
2812
  fixed("eidos_formula_num_sub", 2, (a, b) => numberBinary(a, b, (x, y) => x - y));
1874
2813
  fixed("eidos_formula_num_mul", 2, (a, b) => numberBinary(a, b, (x, y) => x * y));
@@ -1877,12 +2816,6 @@ function registerEidosFormulaFunctions(connection) {
1877
2816
  if (value === null) return null;
1878
2817
  return finiteNumber$1(-asNumber(value));
1879
2818
  });
1880
- fixed("eidos_formula_num_abs", 1, (value) => {
1881
- if (value === null) return null;
1882
- return finiteNumber$1(Math.abs(asNumber(value)));
1883
- });
1884
- fixed("eidos_formula_floor", 1, (value) => roundedInteger(value, Math.floor));
1885
- fixed("eidos_formula_ceil", 1, (value) => roundedInteger(value, Math.ceil));
1886
2819
  for (const [suffix, predicate] of [
1887
2820
  ["eq", (order) => order === 0],
1888
2821
  ["ne", (order) => order !== 0],
@@ -1894,28 +2827,18 @@ function registerEidosFormulaFunctions(connection) {
1894
2827
  if (a === null || b === null) return null;
1895
2828
  return predicate(compareNumeric(a, b)) ? 1n : 0n;
1896
2829
  });
1897
- for (let arity = 2; arity <= 16; arity += 1) {
1898
- fixed("eidos_formula_numeric_min", arity, (...values) => numericExtreme(values, "min"));
1899
- fixed("eidos_formula_numeric_max", arity, (...values) => numericExtreme(values, "max"));
1900
- }
1901
- fixed("eidos_formula_substr2", 2, (text, start) => scalarSubstring(text, start));
1902
- fixed("eidos_formula_substr3", 3, (text, start, length) => scalarSubstring(text, start, length));
1903
- fixed("eidos_formula_length", 1, (value) => {
1904
- if (value === null) return null;
1905
- if (typeof value !== "string") throw new TypeError("LENGTH expected text");
1906
- return BigInt(Array.from(value).length);
1907
- });
1908
- fixed("eidos_formula_lower_ascii", 1, (value) => asciiCase(value, "lower"));
1909
- fixed("eidos_formula_upper_ascii", 1, (value) => asciiCase(value, "upper"));
1910
- fixed("eidos_formula_date_add_days", 2, dateAddDays);
1911
- fixed("eidos_formula_date_diff_days", 2, dateDiffDays);
1912
- fixed("eidos_formula_datetime_add_milliseconds", 2, datetimeAddMilliseconds);
1913
- fixed("eidos_formula_datetime_diff_milliseconds", 2, datetimeDiffMilliseconds);
1914
- }
1915
- function asciiCase(value, kind) {
2830
+ }
2831
+ function sqliteIntegralBoundary(value, operation) {
2832
+ if (value === null) return null;
2833
+ if (typeof value === "bigint") return value;
2834
+ if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError("SQLite math function expected numeric input");
2835
+ return operation(value);
2836
+ }
2837
+ function sqliteSign(value) {
1916
2838
  if (value === null) return null;
1917
- if (typeof value !== "string") throw new TypeError("ASCII case expected text");
1918
- return value.replace(kind === "lower" ? /[A-Z]/g : /[a-z]/g, (character) => String.fromCharCode(character.charCodeAt(0) + (kind === "lower" ? 32 : -32)));
2839
+ if (typeof value === "bigint") return value < 0n ? -1n : value > 0n ? 1n : 0n;
2840
+ if (typeof value !== "number" || !Number.isFinite(value)) throw new TypeError("SQLite SIGN expected numeric input");
2841
+ return value < 0 ? -1n : value > 0 ? 1n : 0n;
1919
2842
  }
1920
2843
  function asInteger(value) {
1921
2844
  if (typeof value === "bigint") return value;
@@ -1942,21 +2865,6 @@ function numberBinary(left, right, operation) {
1942
2865
  if (left === null || right === null) return null;
1943
2866
  return finiteNumber$1(operation(asNumber(left), asNumber(right)));
1944
2867
  }
1945
- function roundedInteger(value, operation) {
1946
- if (value === null) return null;
1947
- const rounded = operation(asNumber(value));
1948
- if (!Number.isFinite(rounded) || !Number.isInteger(rounded)) return null;
1949
- return boundedInteger$1(BigInt(rounded));
1950
- }
1951
- function numericExtreme(values, kind) {
1952
- if (values.some((value) => value === null)) return null;
1953
- let selected = values[0];
1954
- for (const value of values.slice(1)) {
1955
- const order = compareNumeric(value, selected);
1956
- if (kind === "min" && order < 0 || kind === "max" && order > 0) selected = value;
1957
- }
1958
- return values.some((value) => typeof value === "number") ? finiteNumber$1(asNumber(selected)) : selected;
1959
- }
1960
2868
  function compareNumeric(left, right) {
1961
2869
  if (typeof left === "bigint" && typeof right === "bigint") return left < right ? -1 : left > right ? 1 : 0;
1962
2870
  if (typeof left === "number" && typeof right === "number") return left < right ? -1 : left > right ? 1 : 0;
@@ -1992,112 +2900,6 @@ function binary64Rational(value) {
1992
2900
  denominator: 1n << BigInt(-exponent)
1993
2901
  };
1994
2902
  }
1995
- function scalarSubstring(text, start, length) {
1996
- if (text === null || start === null || length === null) return null;
1997
- if (typeof text !== "string") throw new TypeError("SUBSTR expected text");
1998
- const scalars = Array.from(text);
1999
- const rawStart = asInteger(start);
2000
- const clampedStart = rawStart < 0n ? BigInt(scalars.length) + rawStart < 0n ? 0n : BigInt(scalars.length) + rawStart : rawStart;
2001
- if (clampedStart > BigInt(Number.MAX_SAFE_INTEGER)) return "";
2002
- if (length === void 0) return scalars.slice(Number(clampedStart)).join("");
2003
- const count = asInteger(length);
2004
- if (count < 0n) return null;
2005
- if (count > BigInt(Number.MAX_SAFE_INTEGER)) return scalars.slice(Number(clampedStart)).join("");
2006
- return scalars.slice(Number(clampedStart), Number(clampedStart + count)).join("");
2007
- }
2008
- function dateAddDays(date, amount) {
2009
- if (date === null || amount === null) return null;
2010
- const parts = parseDate(date);
2011
- const result = civilFromDays(daysFromCivil(parts.year, parts.month, parts.day) + asInteger(amount));
2012
- return result.year < 1n || result.year > 9999n ? null : formatDate(result);
2013
- }
2014
- function dateDiffDays(left, right) {
2015
- if (left === null || right === null) return null;
2016
- const a = parseDate(left);
2017
- const b = parseDate(right);
2018
- return boundedInteger$1(daysFromCivil(a.year, a.month, a.day) - daysFromCivil(b.year, b.month, b.day));
2019
- }
2020
- function datetimeAddMilliseconds(datetime, amount) {
2021
- if (datetime === null || amount === null) return null;
2022
- return formatInstant(parseInstant(datetime) + asInteger(amount));
2023
- }
2024
- function datetimeDiffMilliseconds(left, right) {
2025
- if (left === null || right === null) return null;
2026
- return boundedInteger$1(parseInstant(left) - parseInstant(right));
2027
- }
2028
- function parseDate(value) {
2029
- if (typeof value !== "string") throw new TypeError("Formula expected a date");
2030
- const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
2031
- if (!match) throw new TypeError("Formula date is not canonical");
2032
- return {
2033
- year: BigInt(match[1]),
2034
- month: BigInt(match[2]),
2035
- day: BigInt(match[3])
2036
- };
2037
- }
2038
- function parseInstant(value) {
2039
- if (typeof value !== "string") throw new TypeError("Formula expected a datetime");
2040
- const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\.(\d{3})Z$/.exec(value);
2041
- if (!match) throw new TypeError("Formula datetime is not canonical");
2042
- const date = {
2043
- year: BigInt(match[1]),
2044
- month: BigInt(match[2]),
2045
- day: BigInt(match[3])
2046
- };
2047
- return daysFromCivil(date.year, date.month, date.day) * 86400000n + BigInt(match[4]) * 3600000n + BigInt(match[5]) * 60000n + BigInt(match[6]) * 1000n + BigInt(match[7]);
2048
- }
2049
- function formatInstant(milliseconds) {
2050
- let days = milliseconds / 86400000n;
2051
- let remainder = milliseconds % 86400000n;
2052
- if (remainder < 0n) {
2053
- days -= 1n;
2054
- remainder += 86400000n;
2055
- }
2056
- const date = civilFromDays(days);
2057
- if (date.year < 1n || date.year > 9999n) return null;
2058
- const hour = remainder / 3600000n;
2059
- remainder %= 3600000n;
2060
- const minute = remainder / 60000n;
2061
- remainder %= 60000n;
2062
- const second = remainder / 1000n;
2063
- const millisecond = remainder % 1000n;
2064
- return `${formatDate(date)}T${pad(hour, 2)}:${pad(minute, 2)}:${pad(second, 2)}.${pad(millisecond, 3)}Z`;
2065
- }
2066
- function daysFromCivil(year, month, day) {
2067
- const adjustedYear = year - (month <= 2n ? 1n : 0n);
2068
- const era = floorDiv(adjustedYear, 400n);
2069
- const yearOfEra = adjustedYear - era * 400n;
2070
- const dayOfYear = (153n * (month + (month > 2n ? -3n : 9n)) + 2n) / 5n + day - 1n;
2071
- const dayOfEra = yearOfEra * 365n + yearOfEra / 4n - yearOfEra / 100n + dayOfYear;
2072
- return era * 146097n + dayOfEra - 719468n;
2073
- }
2074
- function civilFromDays(days) {
2075
- const shifted = days + 719468n;
2076
- const era = floorDiv(shifted, 146097n);
2077
- const dayOfEra = shifted - era * 146097n;
2078
- const yearOfEra = (dayOfEra - dayOfEra / 1460n + dayOfEra / 36524n - dayOfEra / 146096n) / 365n;
2079
- let year = yearOfEra + era * 400n;
2080
- const dayOfYear = dayOfEra - (365n * yearOfEra + yearOfEra / 4n - yearOfEra / 100n);
2081
- const monthPrime = (5n * dayOfYear + 2n) / 153n;
2082
- const day = dayOfYear - (153n * monthPrime + 2n) / 5n + 1n;
2083
- const month = monthPrime + (monthPrime < 10n ? 3n : -9n);
2084
- year += month <= 2n ? 1n : 0n;
2085
- return {
2086
- year,
2087
- month,
2088
- day
2089
- };
2090
- }
2091
- function floorDiv(value, divisor) {
2092
- const quotient = value / divisor;
2093
- return value < 0n && value % divisor !== 0n ? quotient - 1n : quotient;
2094
- }
2095
- function formatDate(value) {
2096
- return `${pad(value.year, 4)}-${pad(value.month, 2)}-${pad(value.day, 2)}`;
2097
- }
2098
- function pad(value, width) {
2099
- return value.toString().padStart(width, "0");
2100
- }
2101
2903
  //#endregion
2102
2904
  //#region src/lookup-functions.ts
2103
2905
  const INT64_MIN = -9223372036854775808n;
@@ -2312,7 +3114,7 @@ CREATE TABLE eidos__fields(
2312
3114
  AND instr(physical_name,char(0))=0
2313
3115
  AND physical_name COLLATE BINARY = name COLLATE BINARY)),
2314
3116
  type TEXT NOT NULL CHECK(type IN (
2315
- 'text','number','integer','checkbox','date','datetime','url','json',
3117
+ 'text','number','integer','checkbox','date','datetime','url',
2316
3118
  'select','multi-select','file','relation','formula','lookup'
2317
3119
  )),
2318
3120
  system_role TEXT CHECK(system_role IN ('row-id','created-time','updated-time')),
@@ -2422,7 +3224,7 @@ CREATE TABLE eidos__formula_fields(
2422
3224
  source_text TEXT NOT NULL
2423
3225
  CHECK(length(CAST(source_text AS BLOB)) BETWEEN 1 AND 4096),
2424
3226
  result_type TEXT NOT NULL
2425
- CHECK(result_type IN ('text','number','integer','checkbox','date','datetime','url','json'))
3227
+ CHECK(result_type IN ('text','number','integer','checkbox','date','datetime','url'))
2426
3228
  ) STRICT, WITHOUT ROWID;
2427
3229
 
2428
3230
  CREATE TABLE eidos__lookup_fields(
@@ -2683,6 +3485,7 @@ const ID_COLUMNS = {
2683
3485
  const REQUIRED_INDEXES = new Set(["eidos__fields_one_system_role", "eidos__relation_one_inverse"]);
2684
3486
  const REQUIRED_TRIGGERS = new Set(["eidos__meta_no_delete", "eidos__meta_no_key_update"]);
2685
3487
  const DYNAMIC_TRIGGER_NAME = /^eidos__(?:row_id_immutable|relation_(?:validate_(?:insert|update)|restrict|detach))__[0-9a-f]{32}$/;
3488
+ const DYNAMIC_FIELD_INDEX_NAME = /^eidos__index__([0-9a-f]{32})$/;
2686
3489
  const FIELD_TYPES = new Set([
2687
3490
  "text",
2688
3491
  "number",
@@ -2718,6 +3521,16 @@ const FORMULA_RESULT_TYPES = new Set([
2718
3521
  "url"
2719
3522
  ]);
2720
3523
  const NUMERIC_LOOKUP_TYPES = new Set(["number", "integer"]);
3524
+ const INDEXABLE_FIELD_TYPES = new Set([
3525
+ "text",
3526
+ "number",
3527
+ "integer",
3528
+ "checkbox",
3529
+ "date",
3530
+ "datetime",
3531
+ "url",
3532
+ "select"
3533
+ ]);
2721
3534
  const FILTER_OPERATORS = new Set([
2722
3535
  "equals",
2723
3536
  "not-equals",
@@ -2812,6 +3625,144 @@ function declaresBinaryText(sql, column) {
2812
3625
  const identifier = `(?:"${escaped}"|\\[${escaped}\\]|\`${escaped}\`|${escaped})`;
2813
3626
  return new RegExp(`(?:\\(|,)\\s*${identifier}\\s+TEXT\\b[^,)]*\\bCOLLATE\\s+BINARY\\b`, "i").test(sql);
2814
3627
  }
3628
+ function readSqliteIdentifier(source, offset = 0) {
3629
+ let start = offset;
3630
+ while (/\s/.test(source[start] ?? "")) start += 1;
3631
+ const opener = source[start];
3632
+ const closer = opener === "[" ? "]" : opener;
3633
+ if (opener === "\"" || opener === "`" || opener === "[") {
3634
+ let value = "";
3635
+ for (let index = start + 1; index < source.length; index += 1) {
3636
+ const character = source[index];
3637
+ if (character !== closer) {
3638
+ value += character;
3639
+ continue;
3640
+ }
3641
+ if (closer !== "]" && source[index + 1] === closer) {
3642
+ value += closer;
3643
+ index += 1;
3644
+ continue;
3645
+ }
3646
+ return {
3647
+ value,
3648
+ end: index + 1
3649
+ };
3650
+ }
3651
+ return null;
3652
+ }
3653
+ let end = start;
3654
+ while (end < source.length && !/[\s(),]/.test(source[end])) end += 1;
3655
+ return end === start ? null : {
3656
+ value: source.slice(start, end),
3657
+ end
3658
+ };
3659
+ }
3660
+ function sqliteTableDefinitions(sql) {
3661
+ let quote = null;
3662
+ let open = -1;
3663
+ for (let index = 0; index < sql.length; index += 1) {
3664
+ const character = sql[index];
3665
+ if (quote) {
3666
+ if (character !== quote) continue;
3667
+ if (quote !== "]" && sql[index + 1] === quote) index += 1;
3668
+ else quote = null;
3669
+ continue;
3670
+ }
3671
+ if (character === "\"" || character === "'" || character === "`") {
3672
+ quote = character;
3673
+ continue;
3674
+ }
3675
+ if (character === "[") {
3676
+ quote = "]";
3677
+ continue;
3678
+ }
3679
+ if (character === "(") {
3680
+ open = index;
3681
+ break;
3682
+ }
3683
+ }
3684
+ if (open === -1) return [];
3685
+ const definitions = [];
3686
+ let depth = 1;
3687
+ let start = open + 1;
3688
+ quote = null;
3689
+ for (let index = start; index < sql.length; index += 1) {
3690
+ const character = sql[index];
3691
+ if (quote) {
3692
+ if (character !== quote) continue;
3693
+ if (quote !== "]" && sql[index + 1] === quote) index += 1;
3694
+ else quote = null;
3695
+ continue;
3696
+ }
3697
+ if (character === "\"" || character === "'" || character === "`") {
3698
+ quote = character;
3699
+ continue;
3700
+ }
3701
+ if (character === "[") {
3702
+ quote = "]";
3703
+ continue;
3704
+ }
3705
+ if (character === "(") {
3706
+ depth += 1;
3707
+ continue;
3708
+ }
3709
+ if (character === ")") {
3710
+ depth -= 1;
3711
+ if (depth === 0) {
3712
+ definitions.push(sql.slice(start, index).trim());
3713
+ break;
3714
+ }
3715
+ continue;
3716
+ }
3717
+ if (character === "," && depth === 1) {
3718
+ definitions.push(sql.slice(start, index).trim());
3719
+ start = index + 1;
3720
+ }
3721
+ }
3722
+ return definitions;
3723
+ }
3724
+ function declaredColumnCollation(tableSql, column) {
3725
+ if (!tableSql) return null;
3726
+ const definition = sqliteTableDefinitions(tableSql).find((candidate) => readSqliteIdentifier(candidate)?.value === column);
3727
+ if (!definition) return null;
3728
+ const leading = readSqliteIdentifier(definition);
3729
+ let depth = 0;
3730
+ let quote = null;
3731
+ for (let index = leading.end; index < definition.length; index += 1) {
3732
+ const character = definition[index];
3733
+ if (quote) {
3734
+ if (character !== quote) continue;
3735
+ if (quote !== "]" && definition[index + 1] === quote) index += 1;
3736
+ else quote = null;
3737
+ continue;
3738
+ }
3739
+ if (character === "\"" || character === "'" || character === "`") {
3740
+ quote = character;
3741
+ continue;
3742
+ }
3743
+ if (character === "[") {
3744
+ quote = "]";
3745
+ continue;
3746
+ }
3747
+ if (character === "(") {
3748
+ depth += 1;
3749
+ continue;
3750
+ }
3751
+ if (character === ")") {
3752
+ depth -= 1;
3753
+ continue;
3754
+ }
3755
+ if (depth !== 0 || !/[A-Za-z_]/.test(character)) continue;
3756
+ let end = index + 1;
3757
+ while (/[A-Za-z0-9_$]/.test(definition[end] ?? "")) end += 1;
3758
+ if (definition.slice(index, end).toLowerCase() !== "collate") {
3759
+ index = end - 1;
3760
+ continue;
3761
+ }
3762
+ return readSqliteIdentifier(definition, end)?.value ?? null;
3763
+ }
3764
+ return "BINARY";
3765
+ }
2815
3766
  function validateEidosFile(connection, options = {}) {
2816
3767
  const level = options.level ?? "full";
2817
3768
  const errors = [];
@@ -2835,7 +3786,7 @@ function validateEidosFile(connection, options = {}) {
2835
3786
  ...REQUIRED_INDEXES,
2836
3787
  ...REQUIRED_TRIGGERS
2837
3788
  ]);
2838
- for (const object of sqliteObjects) if (object.name.replace(/[A-Z]/g, (character) => character.toLowerCase()).startsWith("eidos__") && !allowedStaticObjects.has(object.name) && !DYNAMIC_TRIGGER_NAME.test(object.name)) add(errors, "invalid-schema", `Undeclared reserved SQLite object: ${object.type} ${object.name}`, object.name);
3789
+ for (const object of sqliteObjects) if (object.name.replace(/[A-Z]/g, (character) => character.toLowerCase()).startsWith("eidos__") && !allowedStaticObjects.has(object.name) && !DYNAMIC_TRIGGER_NAME.test(object.name) && !(object.type === "index" && DYNAMIC_FIELD_INDEX_NAME.test(object.name))) add(errors, "invalid-schema", `Undeclared reserved SQLite object: ${object.type} ${object.name}`, object.name);
2839
3790
  const indexes = new Set(sqliteObjects.filter((object) => object.type === "index").map((object) => object.name));
2840
3791
  for (const indexName of REQUIRED_INDEXES) if (!indexes.has(indexName)) add(errors, "invalid-schema", `Missing required metadata index: ${indexName}`);
2841
3792
  const triggers = new Set(sqliteObjects.filter((object) => object.type === "trigger").map((object) => object.name));
@@ -2911,12 +3862,15 @@ function validateEidosFile(connection, options = {}) {
2911
3862
  const tableRows = sqliteTables.has("eidos__tables") ? connection.query(`SELECT * FROM ${EIDOS_FILE_TABLES_TABLE} ORDER BY position, id`) : [];
2912
3863
  const tables = [];
2913
3864
  const tableRowsById = /* @__PURE__ */ new Map();
3865
+ const tableSettingsById = /* @__PURE__ */ new Map();
2914
3866
  const tableNames = /* @__PURE__ */ new Set();
2915
3867
  for (const row of tableRows) try {
2916
3868
  const id = uuid$1(row.id, "Table ID");
2917
3869
  tableRowsById.set(id, row);
2918
3870
  const tableSettings = isCanonicalEidosFileJson(row.settings_json) ? parseEidosFileJson(row.settings_json) : null;
2919
3871
  const settings = tableSettings && !Array.isArray(tableSettings) && typeof tableSettings === "object" ? tableSettings : {};
3872
+ tableSettingsById.set(id, settings);
3873
+ if (settings.contentFieldId !== void 0 && typeof settings.contentFieldId !== "string") add(errors, "invalid-schema", `Table ${row.name} contentFieldId must be a Field ID string`, row.physical_name);
2920
3874
  tables.push({
2921
3875
  id,
2922
3876
  name: row.name,
@@ -2925,6 +3879,7 @@ function validateEidosFile(connection, options = {}) {
2925
3879
  position: row.position,
2926
3880
  icon: typeof settings.icon === "string" ? settings.icon : null,
2927
3881
  description: typeof settings.description === "string" ? settings.description : null,
3882
+ contentFieldId: typeof settings.contentFieldId === "string" ? settings.contentFieldId : null,
2928
3883
  createdAt: row.created_at,
2929
3884
  updatedAt: row.updated_at
2930
3885
  });
@@ -2949,12 +3904,14 @@ function validateEidosFile(connection, options = {}) {
2949
3904
  const lookups = subtypeMap(lookupRows);
2950
3905
  const fieldRows = sqliteTables.has("eidos__fields") ? connection.query(`SELECT * FROM ${EIDOS_FILE_FIELDS_TABLE} ORDER BY table_id, position, id`) : [];
2951
3906
  const fieldRowsById = /* @__PURE__ */ new Map();
3907
+ const fieldRowsByHexId = /* @__PURE__ */ new Map();
2952
3908
  const fieldsByTable = /* @__PURE__ */ new Map();
2953
3909
  for (const row of fieldRows) try {
2954
3910
  const id = uuid$1(row.id, "Field ID");
2955
3911
  const tableId = uuid$1(row.table_id, "Field Table ID");
2956
3912
  const table = tableRowsById.get(tableId);
2957
3913
  fieldRowsById.set(id, row);
3914
+ fieldRowsByHexId.set(id.replace(/-/g, ""), row);
2958
3915
  if (!table) {
2959
3916
  add(errors, "invalid-schema", `Field ${id} references an unknown Table`);
2960
3917
  continue;
@@ -2994,7 +3951,7 @@ function validateEidosFile(connection, options = {}) {
2994
3951
  relationField: uuid$1(lookup.relation_field_id, "Lookup Relation Field ID"),
2995
3952
  targetField: uuid$1(lookup.target_field_id, "Lookup Target Field ID"),
2996
3953
  aggregate: lookup.aggregate,
2997
- displayType: lookup.aggregate === "values" ? "json" : "text"
3954
+ displayType: "text"
2998
3955
  } : relation ? {
2999
3956
  targetTableId: uuid$1(relation.target_table_id, "Relation Target Table ID"),
3000
3957
  direction: relation.direction,
@@ -3029,6 +3986,28 @@ function validateEidosFile(connection, options = {}) {
3029
3986
  } catch (error) {
3030
3987
  add(errors, "invalid-schema", error instanceof Error ? error.message : "Invalid Field metadata");
3031
3988
  }
3989
+ for (const indexObject of sqliteObjects.filter((object) => object.type === "index" && DYNAMIC_FIELD_INDEX_NAME.test(object.name))) {
3990
+ const fieldHex = DYNAMIC_FIELD_INDEX_NAME.exec(indexObject.name)[1];
3991
+ const field = fieldRowsByHexId.get(fieldHex);
3992
+ if (!field) {
3993
+ add(errors, "invalid-schema", `Reserved Field index ${indexObject.name} references an unknown Field`, indexObject.name);
3994
+ continue;
3995
+ }
3996
+ if (!field.physical_name || !INDEXABLE_FIELD_TYPES.has(field.type)) {
3997
+ add(errors, "invalid-schema", `Reserved Field index ${indexObject.name} must reference a stored scalar Field`, indexObject.name);
3998
+ continue;
3999
+ }
4000
+ const table = tableRowsById.get(field.table_id);
4001
+ if (!table || indexObject.tbl_name !== table.physical_name) {
4002
+ add(errors, "invalid-schema", `Reserved Field index ${indexObject.name} is attached to the wrong Table`, indexObject.name);
4003
+ continue;
4004
+ }
4005
+ const listedIndex = connection.query(`PRAGMA index_list(${quoteIdentifier(table.physical_name)})`).find((index) => index.name === indexObject.name);
4006
+ const keyColumns = connection.query(`PRAGMA index_xinfo(${quoteIdentifier(indexObject.name)})`).filter((column) => column.key === 1);
4007
+ const keyColumn = keyColumns[0];
4008
+ const declaredCollation = declaredColumnCollation(sqliteTables.get(table.physical_name)?.sql ?? null, field.physical_name);
4009
+ if (!listedIndex || listedIndex.unique !== 0 || listedIndex.partial !== 0 || listedIndex.origin !== "c" || indexObject.sql === null || keyColumns.length !== 1 || !keyColumn || keyColumn.cid < 0 || keyColumn.name !== field.physical_name || !declaredCollation || keyColumn.coll?.toLowerCase() !== declaredCollation.toLowerCase()) add(errors, "invalid-schema", `Reserved Field index ${indexObject.name} has an invalid definition`, indexObject.name);
4010
+ }
3032
4011
  for (const [tableId, fields] of fieldsByTable) {
3033
4012
  const table = tableRowsById.get(tableId);
3034
4013
  if (!table) continue;
@@ -3164,10 +4143,15 @@ function validateEidosFile(connection, options = {}) {
3164
4143
  element: valueType.slice(5)
3165
4144
  } : valueType;
3166
4145
  const atom = valueType.startsWith("list:") ? valueType.slice(5) : valueType;
3167
- field.property.displayType = atom === "row-id" || atom === "select" ? "text" : atom === "file-entry" ? "json" : atom;
4146
+ field.property.displayType = atom === "row-id" || atom === "select" ? "text" : atom === "file-entry" ? "text" : atom;
3168
4147
  }
3169
4148
  for (const [tableId, fields] of fieldsByTable) {
3170
4149
  const table = tableRowsById.get(tableId);
4150
+ const contentFieldId = tableSettingsById.get(tableId)?.contentFieldId;
4151
+ if (typeof contentFieldId === "string") {
4152
+ const contentField = fields.find((field) => field.id === contentFieldId);
4153
+ if (!contentField || contentField.type !== "text" || contentField.valueKind !== "source" || contentField.systemRole != null || !contentField.physicalName) add(errors, "invalid-schema", `Table ${table.name} Content Field must reference an ordinary Text Field in the same Table`);
4154
+ }
3171
4155
  const labels = fields.filter((field) => field.isRecordLabel);
3172
4156
  if (labels.length !== 1) add(errors, "invalid-schema", `Table ${table.name} must have exactly one Record Label Field`);
3173
4157
  for (const label of labels) {
@@ -3490,6 +4474,7 @@ function validateEidosFile(connection, options = {}) {
3490
4474
  if (field.type === "number" && (typeof value !== "number" || !Number.isFinite(value))) add(errors, "invalid-value", `${table.name}.${field.name} must be finite REAL`);
3491
4475
  if (field.type === "checkbox" && value !== 0 && value !== 1) add(errors, "invalid-value", `${table.name}.${field.name} must be 0, 1, or NULL`);
3492
4476
  if (field.type === "date" && !isCanonicalEidosFileDate(value)) add(errors, "invalid-value", `${table.name}.${field.name} must be canonical YYYY-MM-DD text`);
4477
+ if (field.type === "url" && !isEidosFileUriReference(value)) add(errors, "invalid-value", `${table.name}.${field.name} must be a valid URI-reference`);
3493
4478
  if ([
3494
4479
  "datetime",
3495
4480
  "created-time",
@@ -4250,7 +5235,13 @@ var EidosFileRuntime = class {
4250
5235
  this.allocateId = environment.allocateId ?? (() => createEidosFileUuid());
4251
5236
  connection.exec("PRAGMA foreign_keys = ON; PRAGMA trusted_schema = OFF;");
4252
5237
  connection.registerFunction("eidos_casefold", (value) => value === null ? null : String(value).toUpperCase().toLowerCase());
5238
+ connection.registerFunction("eidos_canonical_number", (value) => {
5239
+ if (value === null) return null;
5240
+ const number = Number(value);
5241
+ return Number.isFinite(number) ? canonicalizeEidosFileJson(Object.is(number, -0) ? 0 : number) : null;
5242
+ }, 1);
4253
5243
  registerEidosFormulaFunctions(connection);
5244
+ assertEidosFileFormulaProfile(connection);
4254
5245
  registerEidosLookupFunctions(connection);
4255
5246
  }
4256
5247
  close() {
@@ -4402,7 +5393,8 @@ var EidosFileRuntime = class {
4402
5393
  const settings = jsonObject(row.settings_json);
4403
5394
  return {
4404
5395
  icon: typeof settings.icon === "string" ? settings.icon : null,
4405
- description: typeof settings.description === "string" ? settings.description : null
5396
+ description: typeof settings.description === "string" ? settings.description : null,
5397
+ contentFieldId: typeof settings.contentFieldId === "string" ? settings.contentFieldId : null
4406
5398
  };
4407
5399
  })(),
4408
5400
  id: uuid(row.id),
@@ -4425,6 +5417,7 @@ var EidosFileRuntime = class {
4425
5417
  position: row.position,
4426
5418
  icon: typeof settings.icon === "string" ? settings.icon : null,
4427
5419
  description: typeof settings.description === "string" ? settings.description : null,
5420
+ contentFieldId: typeof settings.contentFieldId === "string" ? settings.contentFieldId : null,
4428
5421
  createdAt: row.created_at,
4429
5422
  updatedAt: row.updated_at
4430
5423
  };
@@ -4616,6 +5609,7 @@ var EidosFileRuntime = class {
4616
5609
  position,
4617
5610
  icon: input.icon ?? null,
4618
5611
  description: input.description ?? null,
5612
+ contentFieldId: null,
4619
5613
  createdAt: now,
4620
5614
  updatedAt: now
4621
5615
  };
@@ -4764,6 +5758,7 @@ var EidosFileRuntime = class {
4764
5758
  }
4765
5759
  updateTable(tableId, changes) {
4766
5760
  const table = this.getTable(tableId);
5761
+ const persistedSettings = jsonObject(this.tableRow(tableId).settings_json);
4767
5762
  return this.mutateSchemaState(() => {
4768
5763
  let name = table.name;
4769
5764
  let physicalName = table.physicalName ?? table.rawTableName;
@@ -4788,15 +5783,30 @@ var EidosFileRuntime = class {
4788
5783
  }
4789
5784
  }
4790
5785
  const now = this.operationInstant();
4791
- const settings = canonicalizeEidosFileJson({
4792
- ...changes.icon === void 0 ? table.icon === null ? {} : { icon: table.icon } : changes.icon === null ? {} : { icon: changes.icon },
4793
- ...changes.description === void 0 ? table.description === null ? {} : { description: table.description } : changes.description === null ? {} : { description: changes.description }
4794
- });
5786
+ const nextSettings = { ...persistedSettings };
5787
+ if (changes.icon !== void 0) if (changes.icon === null) delete nextSettings.icon;
5788
+ else nextSettings.icon = changes.icon;
5789
+ if (changes.description !== void 0) if (changes.description === null) delete nextSettings.description;
5790
+ else nextSettings.description = changes.description;
5791
+ if (changes.contentFieldId !== void 0) if (changes.contentFieldId === null) delete nextSettings.contentFieldId;
5792
+ else {
5793
+ const contentField = this.fieldByKey(tableId, changes.contentFieldId);
5794
+ if (contentField.type !== "text" || contentField.valueKind !== "source" || contentField.systemRole != null || !contentField.physicalName) throw new EidosFileError("invalid-schema", "The Content Field must be an ordinary Text Field in the same Table");
5795
+ nextSettings.contentFieldId = contentField.id;
5796
+ }
5797
+ let recordLabelFieldId = this.tableRow(tableId).label_field_id;
5798
+ if (changes.recordLabelFieldId !== void 0) {
5799
+ const labelField = this.fieldByKey(tableId, changes.recordLabelFieldId);
5800
+ if (labelField.type === "lookup" || !LABEL_TYPES.has(labelField.type)) throw new EidosFileError("invalid-schema", "The Record Label Field must be a scalar Field in the same Table");
5801
+ recordLabelFieldId = labelField.id;
5802
+ }
5803
+ const settings = canonicalizeEidosFileJson(nextSettings);
4795
5804
  this.connection.run(`UPDATE ${EIDOS_FILE_TABLES_TABLE}
4796
- SET name = ?, physical_name = ?, settings_json = ?, updated_at = ?
5805
+ SET name = ?, physical_name = ?, label_field_id = ?, settings_json = ?, updated_at = ?
4797
5806
  WHERE id = ?`, [
4798
5807
  name,
4799
5808
  physicalName,
5809
+ recordLabelFieldId,
4800
5810
  settings,
4801
5811
  now,
4802
5812
  tableId
@@ -5241,6 +6251,16 @@ var EidosFileRuntime = class {
5241
6251
  const replacement = replacementRecordLabelFieldId ? this.fieldByKey(tableId, replacementRecordLabelFieldId) : void 0;
5242
6252
  if (replacement?.id === field.id) throw new EidosFileError("constraint-conflict", "The replacement Record Label Field must be a different Field");
5243
6253
  return this.mutateSchemaState(() => {
6254
+ if (table.contentFieldId === field.id) {
6255
+ const settings = jsonObject(this.tableRow(tableId).settings_json);
6256
+ delete settings.contentFieldId;
6257
+ this.connection.run(`UPDATE ${EIDOS_FILE_TABLES_TABLE}
6258
+ SET settings_json = ?, updated_at = ? WHERE id = ?`, [
6259
+ canonicalizeEidosFileJson(settings),
6260
+ this.operationInstant(),
6261
+ tableId
6262
+ ]);
6263
+ }
5244
6264
  if (field.isRecordLabel && replacement?.id) this.connection.run(`UPDATE ${EIDOS_FILE_TABLES_TABLE}
5245
6265
  SET label_field_id = ?, updated_at = ? WHERE id = ?`, [
5246
6266
  replacement.id,
@@ -5544,6 +6564,52 @@ var EidosFileRuntime = class {
5544
6564
  )
5545
6565
  ), '[]')`;
5546
6566
  }
6567
+ rowIdSearchTargetTableId(field, schema, visiting = /* @__PURE__ */ new Set()) {
6568
+ if (!field.id || visiting.has(field.id)) return null;
6569
+ const nextVisiting = new Set(visiting);
6570
+ nextVisiting.add(field.id);
6571
+ if (field.type === "row-id") return field.tableId ?? null;
6572
+ const relation = schema.relations.get(field.id);
6573
+ if (relation) return uuid(relation.target_table_id);
6574
+ const lookup = schema.lookups.get(field.id);
6575
+ if (!lookup) return null;
6576
+ const target = schema.fields.get(uuid(lookup.target_field_id));
6577
+ return target ? this.rowIdSearchTargetTableId(target, schema, nextVisiting) : null;
6578
+ }
6579
+ recordLabelSearchExpression(field, rowAlias, schema) {
6580
+ const expression = this.fieldExpression(field, rowAlias, schema);
6581
+ const type = eidosFileFieldValueType(field);
6582
+ if (typeof type !== "string") return "NULL";
6583
+ if (type === "checkbox") return `CASE ${expression} WHEN 1 THEN 'true' WHEN 0 THEN 'false' ELSE NULL END`;
6584
+ if (type === "number") return `eidos_canonical_number(${expression})`;
6585
+ return `CAST(${expression} AS TEXT)`;
6586
+ }
6587
+ rowIdSearchFragmentsExpression(field, rowAlias, schema) {
6588
+ const targetTableId = this.rowIdSearchTargetTableId(field, schema);
6589
+ if (!targetTableId) return null;
6590
+ const targetTable = schema.tables.get(targetTableId);
6591
+ const label = (schema.fieldsByTable.get(targetTableId) ?? []).find((candidate) => candidate.isRecordLabel);
6592
+ if (!targetTable || !label) return null;
6593
+ const ids = this.fieldExpression(field, rowAlias, schema);
6594
+ return `coalesce((
6595
+ SELECT json_group_array(fragment) FROM (
6596
+ SELECT CASE
6597
+ WHEN search_target."_id" IS NULL THEN CAST(item.value AS TEXT)
6598
+ ELSE ${this.recordLabelSearchExpression(label, "search_target", schema)}
6599
+ END AS fragment
6600
+ FROM json_each(${ids}) item
6601
+ LEFT JOIN ${quoteIdentifier(targetTable.physical_name)} search_target
6602
+ ON item.value = search_target."_id"
6603
+ ORDER BY CAST(item.key AS INTEGER)
6604
+ )
6605
+ ), '[]')`;
6606
+ }
6607
+ querySearchFragmentFieldIds(fields, query) {
6608
+ return new Set(eidosFileRowQuerySearchFields(fields, query).flatMap((field) => {
6609
+ const type = eidosFileFieldValueType(field);
6610
+ return field.id && (type === "relation" || field.type === "lookup" && typeof type === "object" && type.element === "row-id") ? [field.id] : [];
6611
+ }));
6612
+ }
5547
6613
  requiredQueryFieldKeys(fields, query) {
5548
6614
  const required = /* @__PURE__ */ new Set();
5549
6615
  const collectFilter = (group) => {
@@ -5553,12 +6619,10 @@ var EidosFileRuntime = class {
5553
6619
  };
5554
6620
  collectFilter(query.filter);
5555
6621
  for (const sort of query.sorts ?? []) required.add(sort.field);
5556
- if (query.search?.trim()) {
5557
- for (const field of fields) if (!field.isHidden && (field.isRecordLabel || field.valueKind === "source" || field.valueKind === "relation" || field.valueKind === "derived")) required.add(field.id);
5558
- }
6622
+ for (const field of eidosFileRowQuerySearchFields(fields, query)) if (field.id) required.add(field.id);
5559
6623
  return required;
5560
6624
  }
5561
- logicalSource(tableId, requestedKeys, includeRecordLabel = true, includeRelationDisplays = true) {
6625
+ logicalSource(tableId, requestedKeys, includeRecordLabel = true, includeRelationDisplays = true, searchFragmentFieldIds = /* @__PURE__ */ new Set()) {
5562
6626
  const schema = this.allSchema();
5563
6627
  const table = schema.tables.get(tableId);
5564
6628
  if (!table) throw new EidosFileError("table-not-found", `Table not found: ${tableId}`);
@@ -5581,6 +6645,10 @@ var EidosFileRuntime = class {
5581
6645
  sql: `SELECT base."_id" AS "__base_rowid", ${fields.filter((field) => projected.has(field.id)).flatMap((field) => {
5582
6646
  const result = [`${this.fieldExpression(field, "base", schema)} AS ${quoteIdentifier(field.tableColumnName)}`];
5583
6647
  if (includeRelationDisplays && field.type === "relation") result.push(`${this.relationDisplayExpression(field, "base", schema)} AS ${quoteIdentifier(`${field.tableColumnName}__display`)}`);
6648
+ if (searchFragmentFieldIds.has(field.id)) {
6649
+ const fragments = this.rowIdSearchFragmentsExpression(field, "base", schema);
6650
+ if (fragments) result.push(`${fragments} AS ${quoteIdentifier(`${field.tableColumnName}__search`)}`);
6651
+ }
5584
6652
  return result;
5585
6653
  }).join(", ")}
5586
6654
  FROM ${quoteIdentifier(table.physical_name)} base`,
@@ -5645,7 +6713,7 @@ var EidosFileRuntime = class {
5645
6713
  ...projection.preservedColumns ?? [],
5646
6714
  ...this.requiredQueryFieldKeys(fields, compatibleQuery)
5647
6715
  ]) : void 0;
5648
- const source = this.logicalSource(tableId, requested, projection?.includeRecordLabel !== false, projection?.includeRelationDisplays !== false);
6716
+ const source = this.logicalSource(tableId, requested, projection?.includeRecordLabel !== false, projection?.includeRelationDisplays !== false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
5649
6717
  const querySignature = canonicalizeEidosFileJson(compatibleQuery);
5650
6718
  const sorts = uniqueSortFields(source.fields, compatibleQuery.sorts);
5651
6719
  const revision = String(this.info().revision ?? 0);
@@ -5723,7 +6791,7 @@ var EidosFileRuntime = class {
5723
6791
  const id = assertEidosFileUuid(rowId, "Row ID");
5724
6792
  const compatibleQuery = this.compatibilityQuery(tableId, query);
5725
6793
  const fields = this.listFields(tableId);
5726
- const source = this.logicalSource(tableId, this.requiredQueryFieldKeys(fields, compatibleQuery), false, false);
6794
+ const source = this.logicalSource(tableId, this.requiredQueryFieldKeys(fields, compatibleQuery), false, false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
5727
6795
  const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery, { referenceInstant: this.nowInstant() });
5728
6796
  const sorts = uniqueSortFields(source.fields, compatibleQuery.sorts);
5729
6797
  const targetWhere = compiled.whereSql ? `${compiled.whereSql} AND "__base_rowid" = ?` : "WHERE \"__base_rowid\" = ?";
@@ -5736,6 +6804,29 @@ var EidosFileRuntime = class {
5736
6804
  SELECT COUNT(*) AS count FROM logical ${beforeWhere}`, [...compiled.params, ...before.params]);
5737
6805
  return Number(result?.count ?? 0);
5738
6806
  }
6807
+ getRecordNeighbors(tableId, rowId, query = {}) {
6808
+ const id = assertEidosFileUuid(rowId, "Row ID");
6809
+ const compatibleQuery = this.compatibilityQuery(tableId, query);
6810
+ const fields = this.listFields(tableId);
6811
+ const source = this.logicalSource(tableId, this.requiredQueryFieldKeys(fields, compatibleQuery), false, false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
6812
+ const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery, { referenceInstant: this.nowInstant() });
6813
+ const targetWhere = compiled.whereSql ? `${compiled.whereSql} AND "__base_rowid" = ?` : "WHERE \"__base_rowid\" = ?";
6814
+ const target = this.connection.get(`WITH logical AS (${source.sql}) SELECT * FROM logical ${targetWhere} LIMIT 1`, [...compiled.params, id]);
6815
+ if (!target) return { found: false };
6816
+ const sorts = uniqueSortFields(source.fields, compatibleQuery.sorts);
6817
+ const values = sorts.map(({ field }) => cursorSortValue(target, field));
6818
+ const reverseOrder = [...sorts.map(({ field, sort }) => `${eidosFileSortExpression(field)} ${sort.direction === "desc" ? "ASC" : "DESC"} NULLS ${sort.nulls === "first" ? "LAST" : "FIRST"}`), "\"__base_rowid\" DESC"].join(", ");
6819
+ const neighbor = (previous) => {
6820
+ const boundary = (previous ? compileKeysetBefore : compileKeysetAfter)(sorts, values, id);
6821
+ const where = compiled.whereSql ? `${compiled.whereSql} AND ${boundary.sql}` : `WHERE ${boundary.sql}`;
6822
+ return this.connection.get(`WITH logical AS (${source.sql}) SELECT "__base_rowid" AS id FROM logical ${where} ${previous ? `ORDER BY ${reverseOrder}` : compiled.orderSql} LIMIT 1`, [...compiled.params, ...boundary.params])?.id ?? null;
6823
+ };
6824
+ return {
6825
+ found: true,
6826
+ previousId: neighbor(true),
6827
+ nextId: neighbor(false)
6828
+ };
6829
+ }
5739
6830
  getRow(tableId, rowId) {
5740
6831
  const source = this.logicalSource(tableId);
5741
6832
  const row = this.connection.get(`WITH logical AS (${source.sql}) SELECT * FROM logical WHERE "__base_rowid" = ?`, [assertEidosFileUuid(rowId, "Row ID")]);
@@ -5817,7 +6908,8 @@ var EidosFileRuntime = class {
5817
6908
  */
5818
6909
  runtimeScanLogicalRows(tableId, fieldIds, query) {
5819
6910
  const compatibleQuery = this.compatibilityQuery(tableId, query);
5820
- const source = this.logicalSource(tableId, fieldIds, false, false);
6911
+ const fields = this.listFields(tableId);
6912
+ const source = this.logicalSource(tableId, [...fieldIds, ...this.requiredQueryFieldKeys(fields, compatibleQuery)], false, false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
5821
6913
  const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery, { referenceInstant: this.nowInstant() });
5822
6914
  const selectedFields = fieldIds.map((fieldId) => this.fieldByKey(tableId, fieldId));
5823
6915
  const encodedValues = selectedFields.map((field) => {
@@ -5864,7 +6956,7 @@ var EidosFileRuntime = class {
5864
6956
  if (storage === "real") return [field.id, Number(value)];
5865
6957
  if (storage === "text") {
5866
6958
  const logicalType = fieldValueTypeForScan(field);
5867
- if (logicalType === "json" || logicalType === "file" || logicalType === "multi-select" || logicalType === "relation" || logicalType === "file-entry" || typeof logicalType === "object") return [field.id, parseEidosFileJson(String(value))];
6959
+ if (logicalType === "file" || logicalType === "multi-select" || logicalType === "relation" || logicalType === "file-entry" || typeof logicalType === "object") return [field.id, parseEidosFileJson(String(value))];
5868
6960
  return [field.id, String(value)];
5869
6961
  }
5870
6962
  throw new EidosFileError("invalid-value", "Logical aggregate scan returned an unsupported storage class");
@@ -5895,7 +6987,7 @@ var EidosFileRuntime = class {
5895
6987
  countRows(tableId, query = {}, referenceInstant = this.nowInstant()) {
5896
6988
  const compatibleQuery = this.compatibilityQuery(tableId, query);
5897
6989
  const fields = this.listFields(tableId);
5898
- const source = this.logicalSource(tableId, this.requiredQueryFieldKeys(fields, compatibleQuery), false, false);
6990
+ const source = this.logicalSource(tableId, this.requiredQueryFieldKeys(fields, compatibleQuery), false, false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
5899
6991
  const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery, { referenceInstant });
5900
6992
  return this.connection.get(`WITH logical AS (${source.sql}) SELECT count(*) AS count FROM logical ${compiled.whereSql}`, compiled.params)?.count ?? 0;
5901
6993
  }
@@ -5903,7 +6995,7 @@ var EidosFileRuntime = class {
5903
6995
  const field = this.fieldByKey(tableId, fieldKey);
5904
6996
  const compatibleQuery = this.compatibilityQuery(tableId, query);
5905
6997
  const fields = this.listFields(tableId);
5906
- const source = this.logicalSource(tableId, [field.id, ...this.requiredQueryFieldKeys(fields, compatibleQuery)], false, false);
6998
+ const source = this.logicalSource(tableId, [field.id, ...this.requiredQueryFieldKeys(fields, compatibleQuery)], false, false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
5907
6999
  const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery, { referenceInstant: this.nowInstant() });
5908
7000
  const column = quoteIdentifier(field.tableColumnName);
5909
7001
  if (field.storageCodec === "json_array" || field.storageCodec === "relation") return this.connection.query(`WITH logical AS (${source.sql}), filtered AS (
@@ -5920,7 +7012,7 @@ var EidosFileRuntime = class {
5920
7012
  const compatibleQuery = this.compatibilityQuery(tableId, query);
5921
7013
  const fields = this.listFields(tableId);
5922
7014
  configs = normalizeEidosFileColumnStatConfigs(configs, fields);
5923
- const source = this.logicalSource(tableId, [...configs.map((config) => config.fieldId), ...this.requiredQueryFieldKeys(fields, compatibleQuery)], false, false);
7015
+ const source = this.logicalSource(tableId, [...configs.map((config) => config.fieldId), ...this.requiredQueryFieldKeys(fields, compatibleQuery)], false, false, this.querySearchFragmentFieldIds(fields, compatibleQuery));
5924
7016
  const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery, { referenceInstant: this.nowInstant() });
5925
7017
  return configs.map((config) => {
5926
7018
  const field = this.fieldByKey(tableId, config.fieldId);
@@ -5946,9 +7038,11 @@ var EidosFileRuntime = class {
5946
7038
  "relation-distinct-target-count": `(SELECT count(DISTINCT value) FROM ${flattened} WHERE value_type = 'text')`
5947
7039
  }[config.type];
5948
7040
  if (!expression) throw new EidosFileError("invalid-query", `Unsupported statistic: ${config.type}`);
5949
- const value = this.connection.get(`WITH logical AS (${source.sql}), filtered AS (
7041
+ const rawValue = this.connection.get(`WITH logical AS (${source.sql}), filtered AS (
5950
7042
  SELECT * FROM logical ${compiled.whereSql}
5951
7043
  ) SELECT ${expression} AS value FROM filtered`, compiled.params)?.value ?? null;
7044
+ const value = typeof rawValue === "bigint" ? String(rawValue) : rawValue;
7045
+ if (value instanceof Uint8Array) throw new EidosFileError("invalid-query", `Statistic ${config.type} returned an invalid binary value`);
5952
7046
  return {
5953
7047
  ...config,
5954
7048
  value
@@ -5991,6 +7085,7 @@ var EidosFileRuntime = class {
5991
7085
  }
5992
7086
  if (field.type === "select" || field.type === "text" || field.type === "url") {
5993
7087
  if (typeof value !== "string") throw new EidosFileError("invalid-value", `${field.name} must be text or NULL`);
7088
+ if (field.type === "url" && !isEidosFileUriReference(value)) throw new EidosFileError("invalid-value", `${field.name} contains an invalid URI-reference`);
5994
7089
  return value;
5995
7090
  }
5996
7091
  if (field.type === "multi-select") {
@@ -6645,6 +7740,8 @@ var EidosFileRuntime = class {
6645
7740
  function fieldValueTypeForScan(field) {
6646
7741
  if (field.systemRole === "row-id") return "row-id";
6647
7742
  if (field.systemRole === "created-time" || field.systemRole === "updated-time") return "datetime";
7743
+ const configured = configuredTypeRef(field.property?.valueType);
7744
+ if (configured) return configured;
6648
7745
  if (field.type === "lookup" && field.property?.aggregate === "values") {
6649
7746
  const display = String(field.property?.displayType ?? "text");
6650
7747
  return {
@@ -6659,6 +7756,6 @@ function fieldValueTypeForScan(field) {
6659
7756
  return field.type;
6660
7757
  }
6661
7758
  //#endregion
6662
- export { EIDOS_FILE_REQUIRED_TABLES as $, eidosFileLookupElementType as A, eidosFileFormulaReferences as B, normalizeEidosFileFilter as C, eidosFileFieldStoresJsonArray as D, removeEidosFileFilterField as E, EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES as F, EIDOS_FILE_FIELDS_TABLE as G, EIDOS_FILE_APPLICATION_ID as H, EIDOS_FILE_FORMULA_FUNCTION_NAMES as I, EIDOS_FILE_FORMULA_FIELDS_TABLE as J, EIDOS_FILE_FORMAT as K, compileEidosFileFormula as L, eidosFileLookupTargetDisplayType as M, eidosFileLookupTargetValueType as N, eidosFileLookupAggregateSupportsTarget as O, eidosFileLookupValueType as P, EIDOS_FILE_RELATION_FIELDS_TABLE as Q, compileEidosFileFormulaFields as R, isEidosFileFilterOperator as S, normalizeEidosFileSorts as T, EIDOS_FILE_EXTENSION as U, rewriteEidosFileFormulaFieldReferences as V, EIDOS_FILE_FEATURES_TABLE as W, EIDOS_FILE_META_TABLE as X, EIDOS_FILE_LOOKUP_FIELDS_TABLE as Y, EIDOS_FILE_MIME_TYPE as Z, assertEidosFileRowQuery as _, EIDOS_FILE_FORMAT_VERSION_TEXT as a, hasEidosFileSqliteHeader as at, eidosFileRowQueryPredicateColumns as b, incrementEidosFileRevision as c, ConnectionPortEidosFileConnection as ct, decodeEidosFileRelationDisplay as d, assertInt64Decimal as dt, EIDOS_FILE_SCHEMA_VERSION as et, decodeEidosFileRelationIds as f, assertUnicodeString as ft, parseNonNegativeInt64 as g, cancellationPortFromSignal as h, sqlValueToNative as ht, scanStableRowPages as i, SQLITE_HEADER as it, eidosFileLookupStorageCodec as j, eidosFileLookupDisplayType as k, initializeEidosFileSchema as l, EidosAdapterError as lt, MemoryByteSource as m, nativeToSqlValue as mt, validateEidosFile as n, EIDOS_FILE_TABLES_TABLE as nt, EIDOS_FILE_SCHEMA_SQL as o, eidosFileColumnStatLabel as ot, encodeEidosFileRelationIds as p, exactConnectionBindingCount as pt, EIDOS_FILE_FORMAT_VERSION as q, smallestDependencyCycle as r, EIDOS_FILE_VIEWS_TABLE as rt, configureEidosFileConnection as s, eidosFileColumnStatTypesForField as st, EidosFileRuntime as t, EIDOS_FILE_SYSTEM_COLUMNS as tt, setEidosFileMetadata as u, assertExactConnectionBindings as ut, compileEidosFileRowQuery as v, normalizeEidosFileRowQuery as w, eidosFileSortExpression as x, eidosFileRowQueryAffectedByFieldChanges as y, compileEidosFileFormulaSource as z };
7759
+ export { EIDOS_FILE_FORMAT as $, eidosFileLookupDisplayType as A, compileEidosFileFormula as B, isEidosFileFilterOperator as C, assertExactConnectionBindings as Ct, removeEidosFileFilterField as D, nativeToSqlValue as Dt, normalizeEidosFileSorts as E, exactConnectionBindingCount as Et, eidosFileLookupValueType as F, configuredTypeRef as G, compileEidosFileFormulaSource as H, EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES as I, eidosFileTypeRefQueryCapabilities as J, eidosFileFieldQueryCapabilities as K, EIDOS_FILE_FORMULA_FUNCTION_NAMES as L, eidosFileLookupStorageCodec as M, eidosFileLookupTargetDisplayType as N, eidosFileFieldStoresJsonArray as O, sqlValueToNative as Ot, eidosFileLookupTargetValueType as P, EIDOS_FILE_FIELDS_TABLE as Q, EIDOS_FILE_FORMULA_PROFILE as R, eidosFileSortExpression as S, EidosAdapterError as St, normalizeEidosFileRowQuery as T, assertUnicodeString as Tt, eidosFileFormulaReferences as U, compileEidosFileFormulaFields as V, rewriteEidosFileFormulaFieldReferences as W, EIDOS_FILE_EXTENSION as X, EIDOS_FILE_APPLICATION_ID as Y, EIDOS_FILE_FEATURES_TABLE as Z, assertEidosFileRowQuery as _, eidosFileTextMultiSelectChoices as _t, EIDOS_FILE_FORMAT_VERSION_TEXT as a, EIDOS_FILE_RELATION_FIELDS_TABLE as at, eidosFileRowQueryPredicateColumns as b, recommendedEidosFileConversionPolicies as bt, incrementEidosFileRevision as c, EIDOS_FILE_SYSTEM_COLUMNS as ct, decodeEidosFileRelationDisplay as d, SQLITE_HEADER as dt, EIDOS_FILE_FORMAT_VERSION as et, decodeEidosFileRelationIds as f, hasEidosFileSqliteHeader as ft, parseNonNegativeInt64 as g, eidosFileConversionTargetNullable as gt, cancellationPortFromSignal as h, eidosFileConversionCanReusePhysicalColumn as ht, scanStableRowPages as i, EIDOS_FILE_MIME_TYPE as it, eidosFileLookupElementType as j, eidosFileLookupAggregateSupportsTarget as k, initializeEidosFileSchema as l, EIDOS_FILE_TABLES_TABLE as lt, MemoryByteSource as m, eidosFileColumnStatTypesForField as mt, validateEidosFile as n, EIDOS_FILE_LOOKUP_FIELDS_TABLE as nt, EIDOS_FILE_SCHEMA_SQL as o, EIDOS_FILE_REQUIRED_TABLES as ot, encodeEidosFileRelationIds as p, eidosFileColumnStatLabel as pt, eidosFileFieldValueType as q, smallestDependencyCycle as r, EIDOS_FILE_META_TABLE as rt, configureEidosFileConnection as s, EIDOS_FILE_SCHEMA_VERSION as st, EidosFileRuntime as t, EIDOS_FILE_FORMULA_FIELDS_TABLE as tt, setEidosFileMetadata as u, EIDOS_FILE_VIEWS_TABLE as ut, compileEidosFileRowQuery as v, isEidosFileUriReference as vt, normalizeEidosFileFilter as w, assertInt64Decimal as wt, eidosFileRowQuerySearchFields as x, ConnectionPortEidosFileConnection as xt, eidosFileRowQueryAffectedByFieldChanges as y, planCanonicalFieldConversion as yt, assertEidosFileFormulaProfile as z };
6663
7760
 
6664
- //# sourceMappingURL=runtime-D9Vp-lxn.mjs.map
7761
+ //# sourceMappingURL=runtime-CrsC-uwG.mjs.map