effect-qb 0.14.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mysql.js +61270 -4259
- package/dist/postgres/metadata.js +176 -82
- package/dist/postgres.js +5903 -3316
- package/package.json +14 -1
- package/src/internal/aggregation-validation.ts +3 -3
- package/src/internal/case-analysis.d.ts +18 -0
- package/src/internal/case-analysis.ts +4 -4
- package/src/internal/coercion/analysis.d.ts +7 -0
- package/src/internal/{coercion-analysis.ts → coercion/analysis.ts} +3 -3
- package/src/internal/coercion/errors.d.ts +17 -0
- package/src/internal/{coercion-errors.ts → coercion/errors.ts} +1 -1
- package/src/internal/coercion/kind.d.ts +4 -0
- package/src/internal/{coercion-kind.ts → coercion/kind.ts} +2 -2
- package/src/internal/{coercion-normalize.ts → coercion/normalize.ts} +1 -1
- package/src/internal/coercion/rules.d.ts +6 -0
- package/src/internal/{coercion-rules.ts → coercion/rules.ts} +2 -2
- package/src/internal/column-state.d.ts +190 -0
- package/src/internal/column-state.ts +36 -47
- package/src/internal/column.ts +21 -305
- package/src/internal/datatypes/define.d.ts +17 -0
- package/src/internal/datatypes/define.ts +18 -4
- package/src/internal/datatypes/lookup.d.ts +44 -0
- package/src/internal/datatypes/lookup.ts +61 -152
- package/src/internal/datatypes/shape.d.ts +16 -0
- package/src/internal/datatypes/shape.ts +1 -1
- package/src/internal/derived-table.d.ts +4 -0
- package/src/internal/derived-table.ts +21 -16
- package/src/internal/dsl-mutation-runtime.ts +378 -0
- package/src/internal/dsl-plan-runtime.ts +387 -0
- package/src/internal/dsl-query-runtime.ts +160 -0
- package/src/internal/dsl-transaction-ddl-runtime.ts +263 -0
- package/src/internal/executor.ts +132 -31
- package/src/internal/expression-ast.ts +15 -5
- package/src/internal/grouping-key.d.ts +3 -0
- package/src/internal/grouping-key.ts +1 -1
- package/src/internal/implication-runtime.d.ts +15 -0
- package/src/internal/implication-runtime.ts +4 -4
- package/src/internal/json/ast.d.ts +30 -0
- package/src/internal/json/ast.ts +1 -1
- package/src/internal/json/errors.d.ts +8 -0
- package/src/internal/json/path.d.ts +75 -0
- package/src/internal/json/path.ts +1 -1
- package/src/internal/json/types.d.ts +62 -0
- package/src/internal/predicate/analysis.d.ts +20 -0
- package/src/internal/{predicate-analysis.ts → predicate/analysis.ts} +3 -3
- package/src/internal/predicate/atom.d.ts +28 -0
- package/src/internal/{predicate-branches.ts → predicate/branches.ts} +2 -2
- package/src/internal/predicate/context.d.ts +67 -0
- package/src/internal/{predicate-context.ts → predicate/context.ts} +8 -5
- package/src/internal/predicate/formula.d.ts +35 -0
- package/src/internal/{predicate-formula.ts → predicate/formula.ts} +1 -1
- package/src/internal/predicate/key.d.ts +11 -0
- package/src/internal/{predicate-key.ts → predicate/key.ts} +2 -2
- package/src/internal/{predicate-nnf.ts → predicate/nnf.ts} +2 -2
- package/src/internal/predicate/normalize.d.ts +53 -0
- package/src/internal/{predicate-normalize.ts → predicate/normalize.ts} +19 -19
- package/src/internal/predicate/runtime.d.ts +31 -0
- package/src/internal/{predicate-runtime.ts → predicate/runtime.ts} +9 -6
- package/src/internal/projection-alias.d.ts +13 -0
- package/src/internal/projections.d.ts +31 -0
- package/src/internal/projections.ts +1 -1
- package/src/internal/query-ast.d.ts +217 -0
- package/src/internal/query-ast.ts +1 -1
- package/src/internal/query-requirements.d.ts +20 -0
- package/src/internal/query.d.ts +775 -0
- package/src/internal/query.ts +389 -313
- package/src/internal/renderer.ts +7 -21
- package/src/internal/row-set.d.ts +53 -0
- package/src/internal/{plan.ts → row-set.ts} +11 -9
- package/src/internal/{runtime-normalize.ts → runtime/normalize.ts} +9 -31
- package/src/internal/{runtime-schema.ts → runtime/schema.ts} +13 -38
- package/src/internal/runtime/value.d.ts +22 -0
- package/src/internal/{runtime-value.ts → runtime/value.ts} +2 -2
- package/src/internal/scalar.d.ts +107 -0
- package/src/internal/scalar.ts +191 -0
- package/src/internal/schema-derivation.d.ts +105 -0
- package/src/internal/schema-expression.d.ts +18 -0
- package/src/internal/schema-expression.ts +38 -7
- package/src/internal/table-options.d.ts +94 -0
- package/src/internal/table-options.ts +8 -2
- package/src/internal/table.d.ts +173 -0
- package/src/internal/table.ts +32 -14
- package/src/mysql/column.ts +94 -18
- package/src/mysql/datatypes/index.ts +47 -7
- package/src/mysql/errors/generated.ts +57336 -0
- package/src/mysql/errors/index.ts +1 -0
- package/src/mysql/errors/normalize.ts +55 -53
- package/src/mysql/errors/types.ts +74 -0
- package/src/mysql/executor.ts +69 -7
- package/src/mysql/function/aggregate.ts +1 -5
- package/src/mysql/function/core.ts +1 -4
- package/src/mysql/function/index.ts +0 -1
- package/src/mysql/function/string.ts +1 -5
- package/src/mysql/function/temporal.ts +12 -15
- package/src/mysql/function/window.ts +1 -6
- package/src/{internal/mysql-dialect.ts → mysql/internal/dialect.ts} +1 -1
- package/src/{internal/mysql-query.ts → mysql/internal/dsl.ts} +1024 -2082
- package/src/{internal/mysql-renderer.ts → mysql/internal/renderer.ts} +6 -6
- package/src/mysql/internal/sql-expression-renderer.ts +1455 -0
- package/src/mysql/json.ts +2 -0
- package/src/mysql/query.ts +111 -91
- package/src/mysql/renderer.ts +1 -1
- package/src/mysql/table.ts +1 -1
- package/src/mysql.ts +6 -4
- package/src/postgres/cast.ts +11 -12
- package/src/postgres/column.ts +178 -46
- package/src/postgres/datatypes/index.d.ts +515 -0
- package/src/postgres/datatypes/index.ts +22 -13
- package/src/postgres/datatypes/spec.d.ts +412 -0
- package/src/postgres/errors/generated.ts +2636 -0
- package/src/postgres/errors/index.ts +1 -0
- package/src/postgres/errors/normalize.ts +47 -62
- package/src/postgres/errors/types.ts +92 -34
- package/src/postgres/executor.ts +37 -5
- package/src/postgres/function/aggregate.ts +1 -5
- package/src/postgres/function/core.ts +12 -6
- package/src/postgres/function/index.ts +0 -1
- package/src/postgres/function/string.ts +1 -5
- package/src/postgres/function/temporal.ts +12 -15
- package/src/postgres/function/window.ts +1 -6
- package/src/{internal/postgres-dialect.ts → postgres/internal/dialect.ts} +1 -1
- package/src/{internal/postgres-query.ts → postgres/internal/dsl.ts} +1085 -2089
- package/src/{internal/postgres-renderer.ts → postgres/internal/renderer.ts} +6 -6
- package/src/postgres/internal/schema-ddl.ts +108 -0
- package/src/{internal/postgres-schema-model.ts → postgres/internal/schema-model.ts} +12 -6
- package/src/{internal → postgres/internal}/sql-expression-renderer.ts +19 -17
- package/src/postgres/{function/json.ts → json.ts} +77 -85
- package/src/postgres/metadata.ts +2 -2
- package/src/postgres/query.ts +113 -89
- package/src/postgres/renderer.ts +3 -13
- package/src/postgres/schema-expression.ts +2 -1
- package/src/postgres/schema-management.ts +1 -1
- package/src/postgres/table.ts +12 -4
- package/src/postgres/type.ts +29 -2
- package/src/postgres.ts +6 -4
- package/src/internal/expression.ts +0 -327
- package/src/internal/schema-ddl.ts +0 -55
- package/src/mysql/function/json.ts +0 -4
- package/src/mysql/private/query.ts +0 -1
- package/src/postgres/private/query.ts +0 -1
- /package/src/internal/{predicate-atom.ts → predicate/atom.ts} +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as Query from "
|
|
2
|
-
import { type RenderState } from "
|
|
3
|
-
import { postgresDialect } from "./
|
|
4
|
-
import { type Projection } from "
|
|
1
|
+
import * as Query from "../../internal/query.js"
|
|
2
|
+
import { type RenderState } from "../../internal/dialect.js"
|
|
3
|
+
import { postgresDialect } from "./dialect.js"
|
|
4
|
+
import { type Projection } from "../../internal/projections.js"
|
|
5
5
|
import { renderQueryAst } from "./sql-expression-renderer.js"
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -19,7 +19,7 @@ export interface PostgresRenderResult {
|
|
|
19
19
|
/**
|
|
20
20
|
* Renders the current query AST into Postgres SQL plus bind parameters.
|
|
21
21
|
*/
|
|
22
|
-
export const renderPostgresPlan = <PlanValue extends Query.
|
|
22
|
+
export const renderPostgresPlan = <PlanValue extends Query.Plan.Any>(
|
|
23
23
|
plan: Query.DialectCompatiblePlan<PlanValue, "postgres">
|
|
24
24
|
): PostgresRenderResult => {
|
|
25
25
|
const state: RenderState = {
|
|
@@ -28,7 +28,7 @@ export const renderPostgresPlan = <PlanValue extends Query.QueryPlan<any, any, a
|
|
|
28
28
|
cteNames: new Set<string>()
|
|
29
29
|
}
|
|
30
30
|
const rendered = renderQueryAst(
|
|
31
|
-
Query.getAst(plan as Query.
|
|
31
|
+
Query.getAst(plan as Query.Plan.Any) as any,
|
|
32
32
|
state,
|
|
33
33
|
postgresDialect
|
|
34
34
|
)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type * as Expression from "../../internal/scalar.js"
|
|
2
|
+
import type { RenderState, SqlDialect } from "../../internal/dialect.js"
|
|
3
|
+
import * as SchemaExpression from "../../internal/schema-expression.js"
|
|
4
|
+
import { renderExpression } from "./sql-expression-renderer.js"
|
|
5
|
+
import type { DdlExpressionLike } from "../../internal/table-options.js"
|
|
6
|
+
import { parse, toSql } from "pgsql-ast-parser"
|
|
7
|
+
import { postgresDialect } from "./dialect.js"
|
|
8
|
+
|
|
9
|
+
export const renderDdlExpression = (
|
|
10
|
+
expression: DdlExpressionLike,
|
|
11
|
+
state: RenderState,
|
|
12
|
+
dialect: SqlDialect
|
|
13
|
+
): string =>
|
|
14
|
+
SchemaExpression.isSchemaExpression(expression)
|
|
15
|
+
? SchemaExpression.render(expression)
|
|
16
|
+
: renderExpression(expression as Expression.Any, state, dialect)
|
|
17
|
+
|
|
18
|
+
const escapeString = (value: string): string => `'${value.replaceAll("'", "''")}'`
|
|
19
|
+
|
|
20
|
+
const inlineLiteralDialect: SqlDialect<"postgres"> = {
|
|
21
|
+
...postgresDialect,
|
|
22
|
+
renderLiteral(value) {
|
|
23
|
+
if (value === null) {
|
|
24
|
+
return "null"
|
|
25
|
+
}
|
|
26
|
+
if (typeof value === "boolean") {
|
|
27
|
+
return value ? "true" : "false"
|
|
28
|
+
}
|
|
29
|
+
if (typeof value === "number" || typeof value === "bigint") {
|
|
30
|
+
return String(value)
|
|
31
|
+
}
|
|
32
|
+
if (value instanceof Date) {
|
|
33
|
+
return escapeString(value.toISOString())
|
|
34
|
+
}
|
|
35
|
+
return escapeString(String(value))
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const renderDdlExpressionSql = (expression: DdlExpressionLike): string =>
|
|
40
|
+
SchemaExpression.isSchemaExpression(expression)
|
|
41
|
+
? SchemaExpression.render(expression)
|
|
42
|
+
: renderExpression(expression as Expression.Any, {
|
|
43
|
+
params: [],
|
|
44
|
+
ctes: [],
|
|
45
|
+
cteNames: new Set()
|
|
46
|
+
}, inlineLiteralDialect)
|
|
47
|
+
|
|
48
|
+
const stripRedundantOuterParens = (value: string): string => {
|
|
49
|
+
let current = value.trim()
|
|
50
|
+
while (current.startsWith("(") && current.endsWith(")")) {
|
|
51
|
+
let depth = 0
|
|
52
|
+
let wrapsWholeExpression = true
|
|
53
|
+
let inSingleQuote = false
|
|
54
|
+
let inDoubleQuote = false
|
|
55
|
+
for (let index = 0; index < current.length; index++) {
|
|
56
|
+
const char = current[index]!
|
|
57
|
+
const previous = index > 0 ? current[index - 1] : undefined
|
|
58
|
+
if (char === "'" && !inDoubleQuote && previous !== "\\") {
|
|
59
|
+
inSingleQuote = !inSingleQuote
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
if (char === "\"" && !inSingleQuote && previous !== "\\") {
|
|
63
|
+
inDoubleQuote = !inDoubleQuote
|
|
64
|
+
continue
|
|
65
|
+
}
|
|
66
|
+
if (inSingleQuote || inDoubleQuote) {
|
|
67
|
+
continue
|
|
68
|
+
}
|
|
69
|
+
if (char === "(") {
|
|
70
|
+
depth += 1
|
|
71
|
+
} else if (char === ")") {
|
|
72
|
+
depth -= 1
|
|
73
|
+
if (depth === 0 && index < current.length - 1) {
|
|
74
|
+
wrapsWholeExpression = false
|
|
75
|
+
break
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (!wrapsWholeExpression) {
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
current = current.slice(1, -1).trim()
|
|
83
|
+
}
|
|
84
|
+
return current
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const canonicalizeDdlExpressionSql = (value: string): string =>
|
|
88
|
+
stripRedundantOuterParens(
|
|
89
|
+
value
|
|
90
|
+
.trim()
|
|
91
|
+
.replace(/\s+/g, " ")
|
|
92
|
+
.replace(/"[^"]+"\./g, "")
|
|
93
|
+
.replace(/"([A-Za-z_][A-Za-z0-9_]*)"/g, "$1")
|
|
94
|
+
.replace(/\bCOLLATE\b/g, "collate")
|
|
95
|
+
.replace(
|
|
96
|
+
/cast\(((?:'(?:[^']|'')*'|"[^"]+"|[a-zA-Z_][a-zA-Z0-9_]*|\([^()]+\))) as ([^)]+)\)/gi,
|
|
97
|
+
(_, expression: string, target: string) => `${expression}::${target.trim()}`
|
|
98
|
+
)
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
export const normalizeDdlExpressionSql = (expression: DdlExpressionLike): string => {
|
|
102
|
+
const rendered = renderDdlExpressionSql(expression)
|
|
103
|
+
try {
|
|
104
|
+
return canonicalizeDdlExpressionSql(toSql.expr(parse(rendered, "expr")))
|
|
105
|
+
} catch {
|
|
106
|
+
return canonicalizeDdlExpressionSql(rendered)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import * as Table from "
|
|
2
|
-
import type { AnyColumnDefinition } from "
|
|
1
|
+
import * as Table from "../../internal/table.js"
|
|
2
|
+
import type { AnyColumnDefinition } from "../../internal/column-state.js"
|
|
3
3
|
import { normalizeDdlExpressionSql } from "./schema-ddl.js"
|
|
4
|
-
import type { TableOptionSpec } from "
|
|
5
|
-
import type { EnumDefinition } from "../
|
|
6
|
-
import { EnumTypeId } from "../
|
|
4
|
+
import type { TableOptionSpec } from "../../internal/table-options.js"
|
|
5
|
+
import type { EnumDefinition } from "../schema-management.js"
|
|
6
|
+
import { EnumTypeId } from "../schema-management.js"
|
|
7
7
|
|
|
8
8
|
export interface EnumModel {
|
|
9
9
|
readonly kind: "enum"
|
|
@@ -88,7 +88,13 @@ export const toTableModel = (table: Table.AnyTable): TableModel => {
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
export const toEnumModel =
|
|
91
|
+
export const toEnumModel = <
|
|
92
|
+
Name extends string,
|
|
93
|
+
Values extends readonly [string, ...string[]],
|
|
94
|
+
SchemaName extends string | undefined
|
|
95
|
+
>(
|
|
96
|
+
definition: EnumDefinition<Name, Values, SchemaName>
|
|
97
|
+
): EnumModel => ({
|
|
92
98
|
kind: "enum",
|
|
93
99
|
schemaName: definition.schemaName,
|
|
94
100
|
name: definition.name,
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import * as Query from "
|
|
2
|
-
import * as Expression from "
|
|
3
|
-
import * as Table from "
|
|
4
|
-
import * as QueryAst from "
|
|
5
|
-
import type { RenderState, SqlDialect } from "
|
|
6
|
-
import * as ExpressionAst from "
|
|
7
|
-
import * as JsonPath from "
|
|
8
|
-
import { flattenSelection, type Projection } from "
|
|
9
|
-
import { type SelectionValue, validateAggregationSelection } from "
|
|
10
|
-
import * as SchemaExpression from "
|
|
11
|
-
import type { DdlExpressionLike } from "
|
|
1
|
+
import * as Query from "../../internal/query.js"
|
|
2
|
+
import * as Expression from "../../internal/scalar.js"
|
|
3
|
+
import * as Table from "../../internal/table.js"
|
|
4
|
+
import * as QueryAst from "../../internal/query-ast.js"
|
|
5
|
+
import type { RenderState, SqlDialect } from "../../internal/dialect.js"
|
|
6
|
+
import * as ExpressionAst from "../../internal/expression-ast.js"
|
|
7
|
+
import * as JsonPath from "../../internal/json/path.js"
|
|
8
|
+
import { flattenSelection, type Projection } from "../../internal/projections.js"
|
|
9
|
+
import { type SelectionValue, validateAggregationSelection } from "../../internal/aggregation-validation.js"
|
|
10
|
+
import * as SchemaExpression from "../../internal/schema-expression.js"
|
|
11
|
+
import type { DdlExpressionLike } from "../../internal/table-options.js"
|
|
12
12
|
|
|
13
13
|
const renderDbType = (
|
|
14
14
|
dialect: SqlDialect,
|
|
@@ -818,7 +818,7 @@ export const renderQueryAst = (
|
|
|
818
818
|
case "set": {
|
|
819
819
|
const setAst = ast as QueryAst.Ast<Record<string, unknown>, any, "set">
|
|
820
820
|
const base = renderQueryAst(
|
|
821
|
-
Query.getAst(setAst.setBase as Query.
|
|
821
|
+
Query.getAst(setAst.setBase as Query.Plan.Any) as QueryAst.Ast<
|
|
822
822
|
Record<string, unknown>,
|
|
823
823
|
any,
|
|
824
824
|
QueryAst.QueryStatement
|
|
@@ -831,7 +831,7 @@ export const renderQueryAst = (
|
|
|
831
831
|
`(${base.sql})`,
|
|
832
832
|
...(setAst.setOperations ?? []).map((entry) => {
|
|
833
833
|
const rendered = renderQueryAst(
|
|
834
|
-
Query.getAst(entry.query as Query.
|
|
834
|
+
Query.getAst(entry.query as Query.Plan.Any) as QueryAst.Ast<
|
|
835
835
|
Record<string, unknown>,
|
|
836
836
|
any,
|
|
837
837
|
QueryAst.QueryStatement
|
|
@@ -858,7 +858,7 @@ export const renderQueryAst = (
|
|
|
858
858
|
} else if (insertAst.insertSource?.kind === "query") {
|
|
859
859
|
const columns = insertAst.insertSource.columns.map((column) => dialect.quoteIdentifier(column)).join(", ")
|
|
860
860
|
const renderedQuery = renderQueryAst(
|
|
861
|
-
Query.getAst(insertAst.insertSource.query as Query.
|
|
861
|
+
Query.getAst(insertAst.insertSource.query as Query.Plan.Any) as QueryAst.Ast<
|
|
862
862
|
Record<string, unknown>,
|
|
863
863
|
any,
|
|
864
864
|
QueryAst.QueryStatement
|
|
@@ -1151,7 +1151,7 @@ const renderSourceReference = (
|
|
|
1151
1151
|
if (typeof source === "object" && source !== null && "kind" in source && (source as { readonly kind?: string }).kind === "cte") {
|
|
1152
1152
|
const cte = source as unknown as {
|
|
1153
1153
|
readonly name: string
|
|
1154
|
-
readonly plan: Query.
|
|
1154
|
+
readonly plan: Query.Plan.Any
|
|
1155
1155
|
readonly recursive?: boolean
|
|
1156
1156
|
}
|
|
1157
1157
|
if (!state.cteNames.has(cte.name)) {
|
|
@@ -1168,7 +1168,7 @@ const renderSourceReference = (
|
|
|
1168
1168
|
if (typeof source === "object" && source !== null && "kind" in source && (source as { readonly kind?: string }).kind === "derived") {
|
|
1169
1169
|
const derived = source as unknown as {
|
|
1170
1170
|
readonly name: string
|
|
1171
|
-
readonly plan: Query.
|
|
1171
|
+
readonly plan: Query.Plan.Any
|
|
1172
1172
|
}
|
|
1173
1173
|
if (!state.cteNames.has(derived.name)) {
|
|
1174
1174
|
// derived tables are inlined, so no CTE registration is needed
|
|
@@ -1178,7 +1178,7 @@ const renderSourceReference = (
|
|
|
1178
1178
|
if (typeof source === "object" && source !== null && "kind" in source && (source as { readonly kind?: string }).kind === "lateral") {
|
|
1179
1179
|
const lateral = source as unknown as {
|
|
1180
1180
|
readonly name: string
|
|
1181
|
-
readonly plan: Query.
|
|
1181
|
+
readonly plan: Query.Plan.Any
|
|
1182
1182
|
}
|
|
1183
1183
|
return `lateral (${renderQueryAst(Query.getAst(lateral.plan) as QueryAst.Ast<Record<string, unknown>, any, QueryAst.QueryStatement>, state, dialect).sql}) as ${dialect.quoteIdentifier(lateral.name)}`
|
|
1184
1184
|
}
|
|
@@ -1260,6 +1260,8 @@ export const renderExpression = (
|
|
|
1260
1260
|
: `excluded.${dialect.quoteIdentifier(ast.columnName)}`
|
|
1261
1261
|
case "cast":
|
|
1262
1262
|
return `cast(${renderExpression(ast.value, state, dialect)} as ${renderCastType(dialect, ast.target)})`
|
|
1263
|
+
case "collate":
|
|
1264
|
+
return `(${renderExpression(ast.value, state, dialect)} collate ${ast.collation.map((segment) => dialect.quoteIdentifier(segment)).join(".")})`
|
|
1263
1265
|
case "function":
|
|
1264
1266
|
return renderFunctionCall(ast.name, Array.isArray(ast.args) ? ast.args : [], state, dialect)
|
|
1265
1267
|
case "eq":
|