/** * @since 1.0.0 */ import type { Effect } from "effect/Effect" import type * as FiberRef from "effect/FiberRef" import type * as FiberRefs from "effect/FiberRefs" import type * as Layer from "effect/Layer" import type * as Option from "effect/Option" import type { Pipeable } from "effect/Pipeable" import type * as Stream from "effect/Stream" import type * as Tracer from "effect/Tracer" import * as internal from "./internal/statement.js" import type { Connection, Row } from "./SqlConnection.js" import type { SqlError } from "./SqlError.js" /** * @category type id * @since 1.0.0 */ export const FragmentId: unique symbol = internal.FragmentId /** * @category type id * @since 1.0.0 */ export type FragmentId = typeof FragmentId /** * @category model * @since 1.0.0 */ export interface Fragment { readonly [FragmentId]: (_: never) => FragmentId readonly segments: ReadonlyArray } /** * @category model * @since 1.0.0 */ export type Dialect = "sqlite" | "pg" | "mysql" | "mssql" | "clickhouse" /** * @category model * @since 1.0.0 */ export interface Statement extends Fragment, Effect, SqlError>, Pipeable { readonly raw: Effect readonly withoutTransform: Effect, SqlError> readonly stream: Stream.Stream readonly values: Effect>, SqlError> readonly unprepared: Effect, SqlError> readonly compile: (withoutTransform?: boolean | undefined) => readonly [ sql: string, params: ReadonlyArray ] } /** * @category model * @since 1.0.0 */ export declare namespace Statement { /** * @category model * @since 1.0.0 */ export type Transformer = ( self: Statement, sql: Constructor, context: FiberRefs.FiberRefs, span: Tracer.Span ) => Effect> } /** * @category transformer * @since 1.0.0 */ export const currentTransformer: FiberRef.FiberRef> = internal.currentTransformer /** * @category transformer * @since 1.0.0 */ export const withTransformer: { /** * @category transformer * @since 1.0.0 */ (f: Statement.Transformer): (effect: Effect) => Effect /** * @category transformer * @since 1.0.0 */ (effect: Effect, f: Statement.Transformer): Effect } = internal.withTransformer /** * @category transformer * @since 1.0.0 */ export const withTransformerDisabled: (effect: Effect) => Effect = internal.withTransformerDisabled /** * @category transformer * @since 1.0.0 */ export const setTransformer: (f: Statement.Transformer) => Layer.Layer = internal.setTransformer /** * @category guard * @since 1.0.0 */ export const isFragment: (u: unknown) => u is Fragment = internal.isFragment /** * @category guard * @since 1.0.0 */ export const isCustom: >( kind: A["kind"] ) => (u: unknown) => u is A = internal.isCustom /** * @category model * @since 1.0.0 */ export type Segment = | Literal | Identifier | Parameter | ArrayHelper | RecordInsertHelper | RecordUpdateHelper | RecordUpdateHelperSingle | Custom /** * @category model * @since 1.0.0 */ export interface Literal { readonly _tag: "Literal" readonly value: string readonly params?: ReadonlyArray | undefined } /** * @category model * @since 1.0.0 */ export interface Identifier { readonly _tag: "Identifier" readonly value: string } /** * @category model * @since 1.0.0 */ export interface Parameter { readonly _tag: "Parameter" readonly value: unknown } /** * @category model * @since 1.0.0 */ export interface ArrayHelper { readonly _tag: "ArrayHelper" readonly value: ReadonlyArray } /** * @category model * @since 1.0.0 */ export interface RecordInsertHelper { readonly _tag: "RecordInsertHelper" readonly value: ReadonlyArray> /** @internal */ readonly returningIdentifier: string | Fragment | undefined readonly returning: (sql: string | Identifier | Fragment) => RecordInsertHelper } /** * @category model * @since 1.0.0 */ export interface RecordUpdateHelper { readonly _tag: "RecordUpdateHelper" readonly value: ReadonlyArray> readonly alias: string /** @internal */ readonly returningIdentifier: string | Fragment | undefined readonly returning: (sql: string | Identifier | Fragment) => RecordUpdateHelper } /** * @category model * @since 1.0.0 */ export interface RecordUpdateHelperSingle { readonly _tag: "RecordUpdateHelperSingle" readonly value: Record readonly omit: ReadonlyArray /** @internal */ readonly returningIdentifier: string | Fragment | undefined readonly returning: (sql: string | Identifier | Fragment) => RecordUpdateHelperSingle } /** * @category model * @since 1.0.0 */ export interface Custom< T extends string = string, A = void, B = void, C = void > { readonly _tag: "Custom" readonly kind: T readonly i0: A readonly i1: B readonly i2: C } /** * @category constructor * @since 1.0.0 */ export const custom: >( kind: C["kind"] ) => (i0: C["i0"], i1: C["i1"], i2: C["i2"]) => Fragment = internal.custom /** * @category model * @since 1.0.0 */ export type PrimitiveKind = | "string" | "number" | "bigint" | "boolean" | "Date" | "null" | "Int8Array" | "Uint8Array" | "object" /** * @category model * @since 1.0.0 */ export type Helper = | ArrayHelper | RecordInsertHelper | RecordUpdateHelper | RecordUpdateHelperSingle | Identifier | Custom /** * @category model * @since 1.0.0 */ export interface Constructor { ( strings: TemplateStringsArray, ...args: Array ): Statement (value: string): Identifier /** * Create unsafe SQL query */ readonly unsafe: ( sql: string, params?: ReadonlyArray | undefined ) => Statement readonly literal: (sql: string) => Fragment readonly in: { (value: ReadonlyArray): ArrayHelper (column: string, value: ReadonlyArray): Fragment } readonly insert: { ( value: ReadonlyArray> ): RecordInsertHelper (value: Record): RecordInsertHelper } /** Update a single row */ readonly update: >( value: A, omit?: ReadonlyArray ) => RecordUpdateHelperSingle /** * Update multiple rows * * **Note:** Not supported in sqlite */ readonly updateValues: ( value: ReadonlyArray>, alias: string ) => RecordUpdateHelper /** * Create an `AND` chain for a where clause */ readonly and: (clauses: ReadonlyArray) => Fragment /** * Create an `OR` chain for a where clause */ readonly or: (clauses: ReadonlyArray) => Fragment /** * Create comma separated values, with an optional prefix * * Useful for `ORDER BY` and `GROUP BY` clauses */ readonly csv: { (values: ReadonlyArray): Fragment (prefix: string, values: ReadonlyArray): Fragment } readonly join: ( literal: string, addParens?: boolean, fallback?: string ) => (clauses: ReadonlyArray) => Fragment readonly onDialect: (options: { readonly sqlite: () => A readonly pg: () => B readonly mysql: () => C readonly mssql: () => D readonly clickhouse: () => E }) => A | B | C | D | E readonly onDialectOrElse: (options: { readonly orElse: () => A readonly sqlite?: () => B readonly pg?: () => C readonly mysql?: () => D readonly mssql?: () => E readonly clickhouse?: () => F }) => A | B | C | D | E | F } /** * @category constructor * @since 1.0.0 */ export const make: ( acquirer: Connection.Acquirer, compiler: Compiler, spanAttributes: ReadonlyArray, transformRows: ((row: ReadonlyArray) => ReadonlyArray) | undefined ) => Constructor = internal.make /** * @category constructor * @since 1.0.0 */ export const unsafeFragment: ( sql: string, params?: ReadonlyArray | undefined ) => Fragment = internal.unsafeFragment /** * @category constructor * @since 1.0.0 */ export const and: (clauses: ReadonlyArray) => Fragment = internal.and /** * @category constructor * @since 1.0.0 */ export const or: (clauses: ReadonlyArray) => Fragment = internal.or /** * @category constructor * @since 1.0.0 */ export const csv: { /** * @category constructor * @since 1.0.0 */ (values: ReadonlyArray): Fragment /** * @category constructor * @since 1.0.0 */ (prefix: string, values: ReadonlyArray): Fragment } = internal.csv /** * @category constructor * @since 1.0.0 */ export const join: ( literal: string, addParens?: boolean, fallback?: string ) => (clauses: ReadonlyArray) => Fragment = internal.join /** * @category compiler * @since 1.0.0 */ export interface Compiler { readonly dialect: Dialect readonly compile: ( statement: Fragment, withoutTransform: boolean ) => readonly [sql: string, params: ReadonlyArray] readonly withoutTransform: this } /** * @category compiler * @since 1.0.0 */ export const makeCompiler: = any>( options: { readonly dialect: Dialect readonly placeholder: (index: number, value: unknown) => string readonly onIdentifier: (value: string, withoutTransform: boolean) => string readonly onRecordUpdate: ( placeholders: string, alias: string, columns: string, values: ReadonlyArray>, returning: readonly [sql: string, params: ReadonlyArray] | undefined ) => readonly [sql: string, params: ReadonlyArray] readonly onCustom: ( type: C, placeholder: (u: unknown) => string, withoutTransform: boolean ) => readonly [sql: string, params: ReadonlyArray] readonly onInsert?: ( columns: ReadonlyArray, placeholders: string, values: ReadonlyArray>, returning: readonly [sql: string, params: ReadonlyArray] | undefined ) => readonly [sql: string, binds: ReadonlyArray] readonly onRecordUpdateSingle?: ( columns: ReadonlyArray, values: ReadonlyArray, returning: readonly [sql: string, params: ReadonlyArray] | undefined ) => readonly [sql: string, params: ReadonlyArray] } ) => Compiler = internal.makeCompiler /** * @category compiler * @since 1.0.0 */ export const makeCompilerSqlite: (transform?: ((_: string) => string) | undefined) => Compiler = internal.makeCompilerSqlite /** * @since 1.0.0 */ export const defaultEscape: (c: string) => (str: string) => string = internal.defaultEscape /** * @since 1.0.0 */ export const primitiveKind: (value: unknown) => PrimitiveKind = internal.primitiveKind /** * @since 1.0.0 */ export const defaultTransforms: ( transformer: (str: string) => string, nested?: boolean ) => { readonly value: (value: any) => any readonly object: (obj: Record) => any readonly array: (rows: ReadonlyArray) => ReadonlyArray } = internal.defaultTransforms