effect-qb 0.15.0 → 0.17.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 +1957 -595
- package/dist/postgres/metadata.js +2507 -182
- package/dist/postgres.js +9587 -8201
- package/dist/sqlite.js +8360 -0
- package/package.json +7 -2
- package/src/internal/column-state.ts +7 -0
- package/src/internal/column.ts +22 -0
- package/src/internal/derived-table.ts +29 -3
- package/src/internal/dialect.ts +14 -1
- package/src/internal/dsl-mutation-runtime.ts +173 -4
- package/src/internal/dsl-plan-runtime.ts +165 -20
- package/src/internal/dsl-query-runtime.ts +60 -6
- package/src/internal/dsl-transaction-ddl-runtime.ts +72 -2
- package/src/internal/executor.ts +62 -13
- package/src/internal/expression-ast.ts +3 -2
- package/src/internal/grouping-key.ts +141 -1
- package/src/internal/implication-runtime.ts +2 -1
- package/src/internal/json/types.ts +155 -40
- package/src/internal/predicate/analysis.ts +103 -1
- package/src/internal/predicate/atom.ts +7 -0
- package/src/internal/predicate/context.ts +170 -17
- package/src/internal/predicate/key.ts +64 -2
- package/src/internal/predicate/normalize.ts +115 -34
- package/src/internal/predicate/runtime.ts +144 -13
- package/src/internal/query.ts +563 -103
- package/src/internal/renderer.ts +39 -2
- package/src/internal/runtime/driver-value-mapping.ts +244 -0
- package/src/internal/runtime/normalize.ts +62 -38
- package/src/internal/runtime/schema.ts +5 -3
- package/src/internal/runtime/value.ts +153 -30
- package/src/internal/scalar.ts +11 -0
- package/src/internal/table-options.ts +108 -1
- package/src/internal/table.ts +87 -29
- package/src/mysql/column.ts +19 -2
- package/src/mysql/datatypes/index.ts +21 -0
- package/src/mysql/errors/catalog.ts +5 -5
- package/src/mysql/errors/normalize.ts +2 -2
- package/src/mysql/executor.ts +20 -5
- package/src/mysql/internal/dialect.ts +12 -6
- package/src/mysql/internal/dsl.ts +995 -263
- package/src/mysql/internal/renderer.ts +13 -3
- package/src/mysql/internal/sql-expression-renderer.ts +530 -128
- package/src/mysql/query.ts +9 -2
- package/src/mysql/renderer.ts +7 -2
- package/src/mysql/table.ts +38 -12
- package/src/postgres/cast.ts +22 -7
- package/src/postgres/column.ts +5 -2
- package/src/postgres/errors/normalize.ts +2 -2
- package/src/postgres/executor.ts +68 -10
- package/src/postgres/function/core.ts +19 -1
- package/src/postgres/internal/dialect.ts +12 -6
- package/src/postgres/internal/dsl.ts +958 -288
- package/src/postgres/internal/renderer.ts +13 -3
- package/src/postgres/internal/schema-ddl.ts +2 -1
- package/src/postgres/internal/schema-model.ts +6 -3
- package/src/postgres/internal/sql-expression-renderer.ts +477 -96
- package/src/postgres/json.ts +57 -17
- package/src/postgres/query.ts +9 -2
- package/src/postgres/renderer.ts +7 -2
- package/src/postgres/schema-management.ts +91 -4
- package/src/postgres/schema.ts +1 -1
- package/src/postgres/table.ts +189 -53
- package/src/postgres/type.ts +4 -0
- package/src/sqlite/column.ts +128 -0
- package/src/sqlite/datatypes/index.ts +79 -0
- package/src/sqlite/datatypes/spec.ts +98 -0
- package/src/sqlite/errors/catalog.ts +103 -0
- package/src/sqlite/errors/fields.ts +19 -0
- package/src/sqlite/errors/index.ts +19 -0
- package/src/sqlite/errors/normalize.ts +229 -0
- package/src/sqlite/errors/requirements.ts +71 -0
- package/src/sqlite/errors/types.ts +29 -0
- package/src/sqlite/executor.ts +227 -0
- package/src/sqlite/function/aggregate.ts +2 -0
- package/src/sqlite/function/core.ts +2 -0
- package/src/sqlite/function/index.ts +19 -0
- package/src/sqlite/function/string.ts +2 -0
- package/src/sqlite/function/temporal.ts +100 -0
- package/src/sqlite/function/window.ts +2 -0
- package/src/sqlite/internal/dialect.ts +37 -0
- package/src/sqlite/internal/dsl.ts +6926 -0
- package/src/sqlite/internal/renderer.ts +47 -0
- package/src/sqlite/internal/sql-expression-renderer.ts +1821 -0
- package/src/sqlite/json.ts +2 -0
- package/src/sqlite/query.ts +196 -0
- package/src/sqlite/renderer.ts +24 -0
- package/src/sqlite/table.ts +183 -0
- package/src/sqlite.ts +22 -0
package/src/mysql/query.ts
CHANGED
|
@@ -16,7 +16,6 @@ import {
|
|
|
16
16
|
type HavingPredicateInput,
|
|
17
17
|
type OrderDirection,
|
|
18
18
|
type OutputOfSelection,
|
|
19
|
-
type MutationInputOf,
|
|
20
19
|
type MutationTargetLike,
|
|
21
20
|
type NumericExpressionInput,
|
|
22
21
|
type PredicateInput,
|
|
@@ -136,8 +135,17 @@ export {
|
|
|
136
135
|
orderBy,
|
|
137
136
|
groupBy
|
|
138
137
|
} from "./internal/dsl.js"
|
|
138
|
+
import type * as Expression from "../internal/scalar.js"
|
|
139
139
|
export { mysqlType as type }
|
|
140
140
|
|
|
141
|
+
type MysqlMutationValueInput<Value> =
|
|
142
|
+
| Value
|
|
143
|
+
| Expression.Scalar<Value, Expression.DbType.Any, Expression.Nullability, "mysql", Expression.ScalarKind, Expression.BindingId>
|
|
144
|
+
|
|
145
|
+
export type MutationInputOf<Shape> = {
|
|
146
|
+
readonly [K in keyof Shape]: MysqlMutationValueInput<Shape[K]>
|
|
147
|
+
}
|
|
148
|
+
|
|
141
149
|
type StructuredSource = AnyValuesSource | AnyUnnestSource | AnyTableFunctionSource
|
|
142
150
|
|
|
143
151
|
type StructuredFromApi = <CurrentSource extends StructuredSource>(
|
|
@@ -163,7 +171,6 @@ export type {
|
|
|
163
171
|
HavingPredicateInput,
|
|
164
172
|
OrderDirection,
|
|
165
173
|
OutputOfSelection,
|
|
166
|
-
MutationInputOf,
|
|
167
174
|
MutationTargetLike,
|
|
168
175
|
NumericExpressionInput,
|
|
169
176
|
PredicateInput,
|
package/src/mysql/renderer.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as CoreRenderer from "../internal/renderer.js"
|
|
2
|
+
import type * as Expression from "../internal/scalar.js"
|
|
2
3
|
import { renderMysqlPlan } from "./internal/renderer.js"
|
|
3
4
|
|
|
4
5
|
/** MySQL-specialized rendered query shape. */
|
|
@@ -8,12 +9,16 @@ export type RowOf<Value extends RenderedQuery<any>> = CoreRenderer.RowOf<Value>
|
|
|
8
9
|
/** MySQL-specialized renderer contract. */
|
|
9
10
|
export type Renderer = CoreRenderer.Renderer<"mysql">
|
|
10
11
|
|
|
12
|
+
export interface MakeOptions {
|
|
13
|
+
readonly valueMappings?: Expression.DriverValueMappings
|
|
14
|
+
}
|
|
15
|
+
|
|
11
16
|
export { TypeId } from "../internal/renderer.js"
|
|
12
17
|
export type { Projection } from "../internal/renderer.js"
|
|
13
18
|
|
|
14
19
|
/** Creates the built-in MySQL renderer. */
|
|
15
|
-
export const make = (): Renderer =>
|
|
16
|
-
CoreRenderer.make("mysql", renderMysqlPlan)
|
|
20
|
+
export const make = (options: MakeOptions = {}): Renderer =>
|
|
21
|
+
CoreRenderer.make("mysql", (plan) => renderMysqlPlan(plan, options))
|
|
17
22
|
|
|
18
23
|
/** Shared built-in MySQL renderer instance. */
|
|
19
24
|
export const mysql = make()
|
package/src/mysql/table.ts
CHANGED
|
@@ -53,17 +53,31 @@ type SchemaNameOfTable<Table> = Table extends BaseTable.TableDefinition<any, any
|
|
|
53
53
|
? SchemaName
|
|
54
54
|
: never
|
|
55
55
|
|
|
56
|
+
type ApplySchemaTableOptions<
|
|
57
|
+
Name extends string,
|
|
58
|
+
Fields extends DialectFieldMap,
|
|
59
|
+
PrimaryKeyColumns extends keyof Fields & string,
|
|
60
|
+
SchemaName extends string,
|
|
61
|
+
Options extends BaseTable.DeclaredTableOptions
|
|
62
|
+
> = BaseTable.ApplyDeclaredOptions<
|
|
63
|
+
BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>,
|
|
64
|
+
Options
|
|
65
|
+
> extends BaseTable.TableDefinition<any, any, infer AppliedPrimaryKeyColumns extends keyof Fields & string, "schema", any>
|
|
66
|
+
? TableDefinition<Name, Fields, AppliedPrimaryKeyColumns, "schema", SchemaName>
|
|
67
|
+
: TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>
|
|
68
|
+
|
|
56
69
|
export type TableSchemaNamespace<SchemaName extends string> = {
|
|
57
70
|
readonly schemaName: SchemaName
|
|
58
71
|
readonly table: <
|
|
59
72
|
Name extends string,
|
|
60
73
|
Fields extends DialectFieldMap,
|
|
74
|
+
const Options extends BaseTable.DeclaredTableOptions,
|
|
61
75
|
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>
|
|
62
76
|
>(
|
|
63
77
|
name: Name,
|
|
64
78
|
fields: Fields,
|
|
65
|
-
...options: BaseTable.
|
|
66
|
-
) =>
|
|
79
|
+
...options: Options & BaseTable.ValidateDeclaredOptions<BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>, Options>
|
|
80
|
+
) => ApplySchemaTableOptions<Name, Fields, PrimaryKeyColumns, SchemaName, Options>
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
export type TableOption = BaseTable.TableOption
|
|
@@ -85,23 +99,31 @@ export const make = <
|
|
|
85
99
|
|
|
86
100
|
export const schema = <SchemaName extends string>(
|
|
87
101
|
schemaName: SchemaName
|
|
88
|
-
): TableSchemaNamespace<SchemaName> =>
|
|
89
|
-
|
|
90
|
-
table: <
|
|
102
|
+
): TableSchemaNamespace<SchemaName> => {
|
|
103
|
+
const table = <
|
|
91
104
|
Name extends string,
|
|
92
105
|
Fields extends DialectFieldMap,
|
|
106
|
+
const Options extends BaseTable.DeclaredTableOptions,
|
|
93
107
|
PrimaryKeyColumns extends keyof Fields & string = InlinePrimaryKeyKeys<Fields>
|
|
94
108
|
>(
|
|
95
109
|
name: Name,
|
|
96
110
|
fields: Fields,
|
|
97
|
-
...declaredOptions: BaseTable.
|
|
111
|
+
...declaredOptions: Options & BaseTable.ValidateDeclaredOptions<BaseTable.TableDefinition<Name, Fields, PrimaryKeyColumns, "schema", SchemaName>, Options>
|
|
98
112
|
) =>
|
|
99
|
-
BaseTable.schema(schemaName).table(
|
|
113
|
+
(BaseTable.schema(schemaName).table as (
|
|
114
|
+
name: Name,
|
|
115
|
+
fields: Fields,
|
|
116
|
+
...options: BaseTable.DeclaredTableOptions
|
|
117
|
+
) => BaseTable.TableDefinition<any, any, any, "schema", any>)(
|
|
100
118
|
name,
|
|
101
119
|
fields,
|
|
102
120
|
...declaredOptions
|
|
103
|
-
) as
|
|
104
|
-
|
|
121
|
+
) as ApplySchemaTableOptions<Name, Fields, PrimaryKeyColumns, SchemaName, Options>
|
|
122
|
+
return {
|
|
123
|
+
schemaName,
|
|
124
|
+
table
|
|
125
|
+
} as unknown as TableSchemaNamespace<SchemaName>
|
|
126
|
+
}
|
|
105
127
|
|
|
106
128
|
export const alias = <
|
|
107
129
|
Table extends AnyTable,
|
|
@@ -144,11 +166,15 @@ export const foreignKey = <
|
|
|
144
166
|
TargetTable extends AnyTable,
|
|
145
167
|
TargetColumns extends string | readonly string[]
|
|
146
168
|
>(
|
|
147
|
-
columns: LocalColumns
|
|
169
|
+
columns: LocalColumns & BaseTable.NonEmptyColumnInput<LocalColumns>,
|
|
148
170
|
target: () => TargetTable,
|
|
149
|
-
referencedColumns: TargetColumns
|
|
171
|
+
referencedColumns: TargetColumns & BaseTable.NonEmptyColumnInput<TargetColumns> & BaseTable.MatchingColumnArityInput<LocalColumns, TargetColumns>
|
|
150
172
|
): BaseTable.TableOption =>
|
|
151
|
-
BaseTable.foreignKey
|
|
173
|
+
BaseTable.foreignKey<LocalColumns, BaseTable.AnyTable, TargetColumns>(
|
|
174
|
+
columns as LocalColumns & BaseTable.NonEmptyColumnInput<LocalColumns>,
|
|
175
|
+
target as () => BaseTable.AnyTable,
|
|
176
|
+
referencedColumns as TargetColumns & BaseTable.NonEmptyColumnInput<TargetColumns> & BaseTable.MatchingColumnArityInput<LocalColumns, TargetColumns>
|
|
177
|
+
)
|
|
152
178
|
|
|
153
179
|
export const check = BaseTable.check
|
|
154
180
|
|
package/src/postgres/cast.ts
CHANGED
|
@@ -1,26 +1,41 @@
|
|
|
1
1
|
import type * as Expression from "../internal/scalar.js"
|
|
2
|
+
import type * as ExpressionAst from "../internal/expression-ast.js"
|
|
2
3
|
import type { ExpressionInput } from "../internal/query.js"
|
|
3
4
|
import { cast as postgresCast } from "./internal/dsl.js"
|
|
4
5
|
|
|
5
6
|
type CastInput = ExpressionInput
|
|
6
7
|
type CastTarget = Expression.DbType.Any
|
|
7
|
-
type
|
|
8
|
+
type CastNullability<Value extends CastInput> = Value extends Expression.Any
|
|
9
|
+
? Expression.NullabilityOf<Value>
|
|
10
|
+
: Value extends null ? "always" : "never"
|
|
11
|
+
type CastKind<Value extends CastInput> = Value extends Expression.Any
|
|
12
|
+
? Expression.KindOf<Value>
|
|
13
|
+
: "scalar"
|
|
14
|
+
type CastDependencies<Value extends CastInput> = Value extends Expression.Any
|
|
15
|
+
? Expression.DependenciesOf<Value>
|
|
16
|
+
: never
|
|
17
|
+
type CastExpression<
|
|
18
|
+
Value extends CastInput,
|
|
19
|
+
Target extends CastTarget
|
|
20
|
+
> = Expression.Scalar<
|
|
8
21
|
Expression.RuntimeOfDbType<Target>,
|
|
9
22
|
Target,
|
|
10
|
-
|
|
23
|
+
CastNullability<Value>,
|
|
11
24
|
Target["dialect"],
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
>
|
|
25
|
+
CastKind<Value>,
|
|
26
|
+
CastDependencies<Value>
|
|
27
|
+
> & {
|
|
28
|
+
readonly [ExpressionAst.TypeId]: ExpressionAst.CastNode<Value extends Expression.Any ? Value : Expression.Any, Target>
|
|
29
|
+
}
|
|
15
30
|
|
|
16
31
|
const to: {
|
|
17
32
|
<Value extends CastInput, Target extends CastTarget>(
|
|
18
33
|
value: Value,
|
|
19
34
|
target: Target
|
|
20
|
-
): CastExpression<Target>
|
|
35
|
+
): CastExpression<Value, Target>
|
|
21
36
|
<Target extends CastTarget>(
|
|
22
37
|
target: Target
|
|
23
|
-
): <Value extends CastInput>(value: Value) => CastExpression<Target>
|
|
38
|
+
): <Value extends CastInput>(value: Value) => CastExpression<Value, Target>
|
|
24
39
|
} = ((...args: [CastInput, CastTarget] | [CastTarget]) =>
|
|
25
40
|
args.length === 1
|
|
26
41
|
? ((value: CastInput) => postgresCast(value as never, args[0] as never))
|
package/src/postgres/column.ts
CHANGED
|
@@ -59,6 +59,8 @@ const boundedString = (length?: number): Schema.Schema<string> =>
|
|
|
59
59
|
? Schema.String
|
|
60
60
|
: Schema.String.pipe(Schema.maxLength(length))
|
|
61
61
|
|
|
62
|
+
const finiteNumber = Schema.Number.pipe(Schema.finite())
|
|
63
|
+
|
|
62
64
|
export const custom = <SchemaType extends Schema.Schema.Any, Db extends Expression.DbType.Any>(
|
|
63
65
|
schema: SchemaType,
|
|
64
66
|
dbType: Db
|
|
@@ -92,8 +94,8 @@ export const number = (options?: BaseColumn.NumericOptions) =>
|
|
|
92
94
|
ddlType: renderNumericDdlType("numeric", options),
|
|
93
95
|
identity: undefined
|
|
94
96
|
})
|
|
95
|
-
export const float4 = () => primitive(
|
|
96
|
-
export const float8 = () => primitive(
|
|
97
|
+
export const float4 = () => primitive(finiteNumber, postgresDatatypes.float4())
|
|
98
|
+
export const float8 = () => primitive(finiteNumber, postgresDatatypes.float8())
|
|
97
99
|
export const boolean = () => primitive(Schema.Boolean, postgresDatatypes.boolean())
|
|
98
100
|
export const date = () => primitive(LocalDateStringSchema, postgresDatatypes.date())
|
|
99
101
|
export const timestamp = () => primitive(LocalDateTimeStringSchema, postgresDatatypes.timestamp())
|
|
@@ -165,6 +167,7 @@ export const unique = BaseColumn.unique
|
|
|
165
167
|
const default_ = BaseColumn.default_
|
|
166
168
|
export const generated = BaseColumn.generated
|
|
167
169
|
export const ddlType = BaseColumn.ddlType
|
|
170
|
+
export const driverValueMapping = BaseColumn.driverValueMapping
|
|
168
171
|
export const array = BaseColumn.array
|
|
169
172
|
export const identityAlways = BaseColumn.identityAlways
|
|
170
173
|
export const identityByDefault = BaseColumn.identityByDefault
|
|
@@ -44,8 +44,8 @@ const asNumber = (value: unknown): number | undefined => {
|
|
|
44
44
|
if (typeof value === "number" && Number.isFinite(value)) {
|
|
45
45
|
return value
|
|
46
46
|
}
|
|
47
|
-
if (typeof value === "string" && value.trim()
|
|
48
|
-
const parsed = Number(value)
|
|
47
|
+
if (typeof value === "string" && /^[+-]?\d+$/.test(value.trim())) {
|
|
48
|
+
const parsed = Number(value.trim())
|
|
49
49
|
return Number.isFinite(parsed) ? parsed : undefined
|
|
50
50
|
}
|
|
51
51
|
return undefined
|
package/src/postgres/executor.ts
CHANGED
|
@@ -5,6 +5,7 @@ import * as Stream from "effect/Stream"
|
|
|
5
5
|
import * as CoreExecutor from "../internal/executor.js"
|
|
6
6
|
import * as CoreQuery from "../internal/query.js"
|
|
7
7
|
import * as CoreRenderer from "../internal/renderer.js"
|
|
8
|
+
import type * as Expression from "../internal/scalar.js"
|
|
8
9
|
import { renderPostgresPlan } from "./internal/renderer.js"
|
|
9
10
|
import {
|
|
10
11
|
narrowPostgresDriverErrorForReadQuery,
|
|
@@ -28,6 +29,7 @@ export interface MakeOptions<Error = never, Context = never> {
|
|
|
28
29
|
readonly renderer?: Renderer
|
|
29
30
|
readonly driver?: Driver<Error, Context>
|
|
30
31
|
readonly driverMode?: CoreExecutor.DriverMode
|
|
32
|
+
readonly valueMappings?: Expression.DriverValueMappings
|
|
31
33
|
}
|
|
32
34
|
/** Standard composed error shape for Postgres executors. */
|
|
33
35
|
export type PostgresExecutorError = PostgresDriverError | RowDecodeError
|
|
@@ -53,12 +55,55 @@ export interface QueryExecutor<Context = never> {
|
|
|
53
55
|
): Stream.Stream<CoreQuery.ResultRow<PlanValue>, PostgresQueryError<PlanValue>, Context>
|
|
54
56
|
}
|
|
55
57
|
|
|
58
|
+
type DriverExecute<Error, Context> = <Row>(
|
|
59
|
+
query: CoreRenderer.RenderedQuery<Row, "postgres">
|
|
60
|
+
) => Effect.Effect<ReadonlyArray<FlatRow>, Error, Context>
|
|
61
|
+
|
|
62
|
+
type DriverHandlers<Error, Context> = {
|
|
63
|
+
readonly execute: DriverExecute<Error, Context>
|
|
64
|
+
readonly stream: <Row>(
|
|
65
|
+
query: CoreRenderer.RenderedQuery<Row, "postgres">
|
|
66
|
+
) => Stream.Stream<FlatRow, Error, Context>
|
|
67
|
+
}
|
|
68
|
+
|
|
56
69
|
/** Constructs a Postgres-specialized SQL driver. */
|
|
57
|
-
export function driver
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
70
|
+
export function driver<
|
|
71
|
+
Error = never,
|
|
72
|
+
Context = never
|
|
73
|
+
>(
|
|
74
|
+
execute: DriverExecute<Error, Context>
|
|
75
|
+
): Driver<Error, Context>
|
|
76
|
+
export function driver<
|
|
77
|
+
Error = never,
|
|
78
|
+
Context = never
|
|
79
|
+
>(
|
|
80
|
+
handlers: DriverHandlers<Error, Context>
|
|
81
|
+
): Driver<Error, Context>
|
|
82
|
+
export function driver<
|
|
83
|
+
Error = never,
|
|
84
|
+
Context = never
|
|
85
|
+
>(
|
|
86
|
+
dialect: "postgres",
|
|
87
|
+
execute: DriverExecute<Error, Context>
|
|
88
|
+
): Driver<Error, Context>
|
|
89
|
+
export function driver<
|
|
90
|
+
Error = never,
|
|
91
|
+
Context = never
|
|
92
|
+
>(
|
|
93
|
+
dialect: "postgres",
|
|
94
|
+
handlers: DriverHandlers<Error, Context>
|
|
95
|
+
): Driver<Error, Context>
|
|
96
|
+
export function driver<
|
|
97
|
+
Error = never,
|
|
98
|
+
Context = never
|
|
99
|
+
>(
|
|
100
|
+
dialectOrExecute: "postgres" | DriverExecute<Error, Context> | DriverHandlers<Error, Context>,
|
|
101
|
+
maybeExecute?: DriverExecute<Error, Context> | DriverHandlers<Error, Context>
|
|
102
|
+
): Driver<Error, Context> {
|
|
103
|
+
const executeOrHandlers = typeof dialectOrExecute === "string" ? maybeExecute : dialectOrExecute
|
|
104
|
+
return typeof executeOrHandlers === "function"
|
|
105
|
+
? CoreExecutor.driver("postgres", executeOrHandlers)
|
|
106
|
+
: CoreExecutor.driver("postgres", executeOrHandlers as DriverHandlers<Error, Context>)
|
|
62
107
|
}
|
|
63
108
|
|
|
64
109
|
const fromDriver = <
|
|
@@ -67,7 +112,8 @@ const fromDriver = <
|
|
|
67
112
|
>(
|
|
68
113
|
renderer: Renderer,
|
|
69
114
|
sqlDriver: Driver<Error, Context>,
|
|
70
|
-
driverMode: CoreExecutor.DriverMode = "raw"
|
|
115
|
+
driverMode: CoreExecutor.DriverMode = "raw",
|
|
116
|
+
valueMappings?: Expression.DriverValueMappings
|
|
71
117
|
): QueryExecutor<Context> => ({
|
|
72
118
|
dialect: "postgres",
|
|
73
119
|
execute(plan) {
|
|
@@ -76,7 +122,7 @@ const fromDriver = <
|
|
|
76
122
|
Effect.flatMap(
|
|
77
123
|
sqlDriver.execute(rendered),
|
|
78
124
|
(rows) => Effect.try({
|
|
79
|
-
try: () => CoreExecutor.decodeRows(rendered, plan, rows, { driverMode }),
|
|
125
|
+
try: () => CoreExecutor.decodeRows(rendered, plan, rows, { driverMode, valueMappings }),
|
|
80
126
|
catch: (error) => error as RowDecodeError
|
|
81
127
|
})
|
|
82
128
|
),
|
|
@@ -97,7 +143,7 @@ const fromDriver = <
|
|
|
97
143
|
Stream.mapChunksEffect(
|
|
98
144
|
sqlDriver.stream(rendered),
|
|
99
145
|
(rows) => Effect.try({
|
|
100
|
-
try: () => CoreExecutor.decodeChunk(rendered, plan, rows, { driverMode }),
|
|
146
|
+
try: () => CoreExecutor.decodeChunk(rendered, plan, rows, { driverMode, valueMappings }),
|
|
101
147
|
catch: (error) => error as RowDecodeError
|
|
102
148
|
})
|
|
103
149
|
),
|
|
@@ -135,6 +181,7 @@ export function make(
|
|
|
135
181
|
options: {
|
|
136
182
|
readonly renderer?: Renderer
|
|
137
183
|
readonly driverMode?: CoreExecutor.DriverMode
|
|
184
|
+
readonly valueMappings?: Expression.DriverValueMappings
|
|
138
185
|
}
|
|
139
186
|
): QueryExecutor<SqlClient.SqlClient>
|
|
140
187
|
export function make<Error = never, Context = never>(
|
|
@@ -142,15 +189,26 @@ export function make<Error = never, Context = never>(
|
|
|
142
189
|
readonly renderer?: Renderer
|
|
143
190
|
readonly driver: Driver<Error, Context>
|
|
144
191
|
readonly driverMode?: CoreExecutor.DriverMode
|
|
192
|
+
readonly valueMappings?: Expression.DriverValueMappings
|
|
145
193
|
}
|
|
146
194
|
): QueryExecutor<Context>
|
|
147
195
|
export function make<Error = never, Context = never>(
|
|
148
196
|
options: MakeOptions<Error, Context> = {}
|
|
149
197
|
): QueryExecutor<any> {
|
|
150
198
|
if (options.driver) {
|
|
151
|
-
return fromDriver(
|
|
199
|
+
return fromDriver(
|
|
200
|
+
options.renderer ?? CoreRenderer.make("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options.valueMappings })),
|
|
201
|
+
options.driver,
|
|
202
|
+
options.driverMode,
|
|
203
|
+
options.valueMappings
|
|
204
|
+
)
|
|
152
205
|
}
|
|
153
|
-
return fromDriver(
|
|
206
|
+
return fromDriver(
|
|
207
|
+
options.renderer ?? CoreRenderer.make("postgres", (plan) => renderPostgresPlan(plan, { valueMappings: options.valueMappings })),
|
|
208
|
+
sqlClientDriver(),
|
|
209
|
+
options.driverMode,
|
|
210
|
+
options.valueMappings
|
|
211
|
+
)
|
|
154
212
|
}
|
|
155
213
|
|
|
156
214
|
/** Creates a Postgres-specialized executor from a typed implementation callback. */
|
|
@@ -12,11 +12,29 @@ import { isSequenceDefinition, type SequenceDefinition } from "../schema-managem
|
|
|
12
12
|
|
|
13
13
|
/** Postgres scalar core functions. */
|
|
14
14
|
export { coalesce, call, uuidGenerateV4 }
|
|
15
|
+
|
|
16
|
+
const safeUnquotedIdentifier = /^[a-z_][a-z0-9_$]*$/
|
|
17
|
+
|
|
18
|
+
const quoteIdentifier = (value: string): string =>
|
|
19
|
+
`"${value.replaceAll("\"", "\"\"")}"`
|
|
20
|
+
|
|
21
|
+
const renderIdentifier = (value: string): string =>
|
|
22
|
+
safeUnquotedIdentifier.test(value)
|
|
23
|
+
? value
|
|
24
|
+
: quoteIdentifier(value)
|
|
25
|
+
|
|
26
|
+
const renderQualifiedSequenceName = (
|
|
27
|
+
value: SequenceDefinition<string, string | undefined>
|
|
28
|
+
): string =>
|
|
29
|
+
value.schemaName === undefined || value.schemaName === "public"
|
|
30
|
+
? renderIdentifier(value.name)
|
|
31
|
+
: `${renderIdentifier(value.schemaName)}.${renderIdentifier(value.name)}`
|
|
32
|
+
|
|
15
33
|
export const nextVal = (
|
|
16
34
|
value: ExpressionInput | SequenceDefinition<string, string | undefined>
|
|
17
35
|
) =>
|
|
18
36
|
nextValInternal(
|
|
19
37
|
isSequenceDefinition(value)
|
|
20
|
-
? cast(literal(value
|
|
38
|
+
? cast(literal(renderQualifiedSequenceName(value)), postgresType.regclass())
|
|
21
39
|
: value
|
|
22
40
|
)
|
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import type { RenderState, SqlDialect } from "../../internal/dialect.js"
|
|
1
|
+
import type { RenderState, RenderValueContext, SqlDialect } from "../../internal/dialect.js"
|
|
2
|
+
import { toDriverValue } from "../../internal/runtime/driver-value-mapping.js"
|
|
2
3
|
|
|
3
4
|
const quoteIdentifier = (value: string): string => `"${value.replaceAll("\"", "\"\"")}"`
|
|
4
5
|
|
|
5
|
-
const renderLiteral = (value: unknown, state: RenderState): string => {
|
|
6
|
-
|
|
6
|
+
const renderLiteral = (value: unknown, state: RenderState, context: RenderValueContext = {}): string => {
|
|
7
|
+
const driverValue = toDriverValue(value, {
|
|
8
|
+
dialect: "postgres",
|
|
9
|
+
valueMappings: state.valueMappings,
|
|
10
|
+
...context
|
|
11
|
+
})
|
|
12
|
+
if (driverValue === null) {
|
|
7
13
|
return "null"
|
|
8
14
|
}
|
|
9
|
-
if (typeof
|
|
10
|
-
return
|
|
15
|
+
if (typeof driverValue === "boolean") {
|
|
16
|
+
return driverValue ? "true" : "false"
|
|
11
17
|
}
|
|
12
|
-
state.params.push(
|
|
18
|
+
state.params.push(driverValue)
|
|
13
19
|
return `$${state.params.length}`
|
|
14
20
|
}
|
|
15
21
|
|