@minnowdb/core 0.6.8 → 0.7.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.
@@ -72,5 +72,11 @@ export declare function externalSqlDomainValue(value: unknown): unknown;
72
72
  * those shapes keeps `||` from inventing a text concatenation PostgreSQL does not have —
73
73
  * and from ever concatenating internal domain encodings.
74
74
  */
75
- export declare function concatenatedSqlValue(left: string, right: string): string;
75
+ export declare function concatenatedSqlValue(leftValue: unknown, rightValue: unknown): string;
76
+ /**
77
+ * Orders two INTERVAL values the way PostgreSQL's interval comparison does: a month counts as
78
+ * 30 days and a day as 24 hours, so INTERVAL '3 months' equals INTERVAL '90 days' and
79
+ * INTERVAL '2 days' sorts below INTERVAL '10 days'. Undefined unless both are intervals.
80
+ */
81
+ export declare function intervalDomainCompare(left: unknown, right: unknown): number | undefined;
76
82
  export declare function isSqlDomainValue(value: unknown): value is string;
@@ -765,7 +765,15 @@ function concatenationDomainKind(value) {
765
765
  * those shapes keeps `||` from inventing a text concatenation PostgreSQL does not have —
766
766
  * and from ever concatenating internal domain encodings.
767
767
  */
768
- export function concatenatedSqlValue(left, right) {
768
+ export function concatenatedSqlValue(leftValue, rightValue) {
769
+ // PostgreSQL has text || anynonarray and anynonarray || text: one side is text and the other
770
+ // renders as text. Two non-text operands (1 || 2, TRUE || FALSE) have no || operator.
771
+ const leftText = typeof leftValue === "string";
772
+ const rightText = typeof rightValue === "string";
773
+ if (!leftText && !rightText)
774
+ throw new TypeError("|| requires a string operand");
775
+ const left = leftText ? leftValue : concatenationOperandText(leftValue);
776
+ const right = rightText ? rightValue : concatenationOperandText(rightValue);
769
777
  const leftKind = concatenationDomainKind(left);
770
778
  const rightKind = concatenationDomainKind(right);
771
779
  for (const kind of [leftKind, rightKind]) {
@@ -781,6 +789,35 @@ export function concatenatedSqlValue(left, right) {
781
789
  }
782
790
  return protectedSqlTextValue(String(externalSqlDomainValue(left)) + String(externalSqlDomainValue(right)));
783
791
  }
792
+ /** The text a non-string || operand contributes: PostgreSQL's output rendering of the value. */
793
+ function concatenationOperandText(value) {
794
+ if (typeof value === "number")
795
+ return String(value);
796
+ if (typeof value === "boolean")
797
+ return value ? "true" : "false";
798
+ if (value instanceof Date)
799
+ return dateIsoString(value);
800
+ throw new TypeError("|| requires a string operand");
801
+ }
802
+ /**
803
+ * Orders two INTERVAL values the way PostgreSQL's interval comparison does: a month counts as
804
+ * 30 days and a day as 24 hours, so INTERVAL '3 months' equals INTERVAL '90 days' and
805
+ * INTERVAL '2 days' sorts below INTERVAL '10 days'. Undefined unless both are intervals.
806
+ */
807
+ export function intervalDomainCompare(left, right) {
808
+ if (typeof left !== "string" ||
809
+ typeof right !== "string" ||
810
+ !left.startsWith(INTERVAL_VALUE) ||
811
+ !right.startsWith(INTERVAL_VALUE)) {
812
+ return undefined;
813
+ }
814
+ const total = (value) => {
815
+ const [months, days, microseconds] = JSON.parse(value.slice(INTERVAL_VALUE.length));
816
+ return (months * 30 + days) * 86_400_000_000 + microseconds;
817
+ };
818
+ const difference = total(left) - total(right);
819
+ return difference === 0 ? 0 : difference < 0 ? -1 : 1;
820
+ }
784
821
  export function isSqlDomainValue(value) {
785
822
  return typeof value === "string" && value.startsWith(PREFIX);
786
823
  }
@@ -0,0 +1,11 @@
1
+ export type SimpleScalarResult = "string" | "number" | "boolean" | "datetime" | "date" | "interval" | "argument";
2
+ export interface SimpleScalarFunction {
3
+ readonly minArgs: number;
4
+ readonly maxArgs: number;
5
+ /** The output type; "argument" carries the first argument's type through. */
6
+ readonly returns: SimpleScalarResult;
7
+ /** False for functions that read NULL arguments as data (CONCAT) instead of returning NULL. */
8
+ readonly nullOnNull?: false;
9
+ readonly evaluate: (values: readonly unknown[]) => unknown;
10
+ }
11
+ export declare const simpleScalarFunctions: ReadonlyMap<string, SimpleScalarFunction>;