import * as Result from "effect/Result";
import type * as PgProtocol from "./PgProtocol.ts";
import type { ValueSink } from "./PgProtocol.ts";
declare const CodecError_base: new = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
readonly _tag: "PgTypesCodecError";
} & Readonly;
/**
* Failure returned when a value cannot be encoded or decoded for its OID.
*
* @stability unstable
* @category errors
* @since 4.0.0
*/
export declare class CodecError extends CodecError_base<{
readonly message: string;
}> {
}
/**
* Type OIDs implemented by version 1 of this codec.
*
* @stability unstable
* @category constants
* @since 4.0.0
*/
export declare const OID: {
readonly bool: 16;
readonly bytea: 17;
readonly name: 19;
readonly int8: 20;
readonly int2: 21;
readonly int4: 23;
readonly text: 25;
readonly oid: 26;
readonly json: 114;
readonly cidr: 650;
readonly float4: 700;
readonly float8: 701;
readonly inet: 869;
readonly bpchar: 1042;
readonly varchar: 1043;
readonly date: 1082;
readonly time: 1083;
readonly timestamp: 1114;
readonly timestamptz: 1184;
readonly interval: 1186;
readonly timetz: 1266;
readonly numeric: 1700;
readonly regclass: 2205;
readonly uuid: 2950;
readonly jsonb: 3802;
readonly boolArray: 1000;
readonly byteaArray: 1001;
readonly nameArray: 1003;
readonly int8Array: 1016;
readonly int2Array: 1005;
readonly int4Array: 1007;
readonly textArray: 1009;
readonly oidArray: 1028;
readonly jsonArray: 199;
readonly cidrArray: 651;
readonly float4Array: 1021;
readonly float8Array: 1022;
readonly inetArray: 1041;
readonly bpcharArray: 1014;
readonly varcharArray: 1015;
readonly dateArray: 1182;
readonly timeArray: 1183;
readonly timestampArray: 1115;
readonly timestamptzArray: 1185;
readonly intervalArray: 1187;
readonly timetzArray: 1270;
readonly numericArray: 1231;
readonly regclassArray: 2210;
readonly uuidArray: 2951;
readonly jsonbArray: 3807;
};
/**
* Options for registering a codec.
*
* @stability unstable
* @category models
* @since 4.0.0
*/
export interface RegisterOptions {
/**
* Registers a one-dimensional array codec at this OID and enables array type
* inference for the scalar codec.
*/
readonly arrayOid?: number | undefined;
}
/**
* A client-specific set of PostgreSQL binary codecs. Each registry starts with
* the built-in codecs and does not affect the module-level registry.
*
* @stability unstable
* @category models
* @since 4.0.0
*/
export interface Registry {
readonly register: (oid: number, codec: Codec, options?: RegisterOptions) => void;
}
/**
* Returns the array OID whose elements have the given OID, or `undefined`
* when there is no array type registered for it.
*
* @stability unstable
* @category getters
* @since 4.0.0
*/
export declare const arrayOidFor: (elementOid: number, registry?: Registry) => number | undefined;
/**
* A binary codec for a single OID.
*
* @stability unstable
* @category models
* @since 4.0.0
*/
export interface Codec {
readonly encode: (value: A) => Result.Result;
readonly decode: (bytes: Uint8Array) => Result.Result;
/**
* Writes the value straight into a `Bind` frame, with no array of its own.
* Optional: a codec without one falls back to `encode` and a copy.
*/
readonly write?: (sink: ValueSink, value: A) => Result.Result;
/**
* Reads the value out of the `size` bytes at `offset`, with no view of its
* own. Optional: a codec without one is handed a view. Array elements are
* read through this, so it is what keeps decoding an array from allocating
* a view per element.
*/
readonly read?: (bytes: Uint8Array, offset: number, size: number) => Result.Result;
}
/**
* A PostgreSQL interval with independent signed calendar and time components.
* Months and days are int32 values; microseconds is an int64 value.
*
* @stability unstable
* @category models
* @since 4.0.0
*/
export interface Interval {
readonly months: number;
readonly days: number;
readonly microseconds: bigint;
}
/**
* Creates a client-specific registry containing the built-in codecs.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const makeRegistry: () => Registry;
/**
* Registers a binary codec for an OID the built-in catalogue does not cover,
* or overrides a built-in one. Registered codecs take precedence.
*
* **Details**
*
* Unregistered OIDs decode as UTF-8 text. Register binary user-defined types
* to avoid codec errors, which fail the query that read the row, or garbled
* output from a binary layout without NUL bytes. For arrays, use
* `makeRegistry().register` with `RegisterOptions.arrayOid` and pass the
* registry as the client's `types` option.
*
* @stability unstable
* @category registry
* @since 4.0.0
*/
export declare const register: (oid: number, codec: Codec) => void;
/**
* Removes a previously registered codec.
*
* @stability unstable
* @category registry
* @since 4.0.0
*/
export declare const unregister: (oid: number) => void;
/**
* A result column, as `RowDescription` describes one.
*
* @stability unstable
* @category models
* @since 4.0.0
*/
export interface Column {
readonly dataTypeOid: number;
readonly format: number;
/** Names the column in decoding errors. */
readonly name?: string | undefined;
/** The source table, or `0` when the column is not a table column. */
readonly tableOid?: number | undefined;
/** The column's attribute number within `tableOid`. */
readonly columnAttributeNumber?: number | undefined;
}
/**
* Creates a field reader for `PgProtocol.makeParser`.
*
* **Details**
*
* Codecs are resolved once per column. SQL `NULL` becomes `null`.
* Unregistered OIDs decode as UTF-8 text. Text-format columns fail with
* `CodecError`, as do unregistered values that are invalid UTF-8 or contain a
* NUL byte, which no PostgreSQL text value does. Those errors name the column
* and its type OID. A binary layout with no NUL bytes still decodes as
* garbled text, so register a codec for binary user-defined types.
*
* **Example** (Updating the reader after `RowDescription`)
*
* ```ts
* import { PgProtocol, PgTypes } from "@effect/sql-pg"
*
* const parser = PgProtocol.makeParser({ readField: Result.getOrThrow(PgTypes.makeFieldReader([])) })
* // on each RowDescription
* declare const description: PgProtocol.RowDescription
* parser.readField = Result.getOrThrow(PgTypes.makeFieldReader(description.fields))
* ```
*
* @stability unstable
* @category decoding
* @since 4.0.0
*/
export declare const makeFieldReader: (columns: ReadonlyArray, registry?: Registry) => Result.Result, CodecError>;
/**
* Encodes a JavaScript value as the binary representation of the given OID.
*
* **Details**
*
* Returns a `CodecError` failure when the value has the wrong JavaScript type,
* or when the OID is neither built in nor registered.
*
* @stability unstable
* @category encoding
* @since 4.0.0
*/
export declare const encode: (value: unknown, oid: number, registry?: Registry) => Result.Result;
/**
* Decodes the binary representation of the given OID.
*
* **Details**
*
* Only binary format (`1`) is supported. Unregistered OIDs decode as UTF-8
* text; a value that is invalid UTF-8 or contains a NUL byte fails with
* `CodecError`. See `register` for binary user-defined types, including
* arrays.
*
* @stability unstable
* @category decoding
* @since 4.0.0
*/
export declare const decode: (bytes: Uint8Array, oid: number, format: number, registry?: Registry) => Result.Result;
/**
* The runtime type identifier for PostgreSQL parameters.
*
* @stability unstable
* @category type IDs
* @since 4.0.0
*/
export declare const ParameterTypeId: ParameterTypeId;
/**
* The type-level identifier for PostgreSQL parameters.
*
* @stability unstable
* @category type IDs
* @since 4.0.0
*/
export type ParameterTypeId = "~@effect/sql-pg/PgTypes/Parameter";
/**
* A value paired with the OID it should be encoded as.
*
* @stability unstable
* @category models
* @since 4.0.0
*/
export interface Parameter {
readonly [ParameterTypeId]: ParameterTypeId;
readonly oid: number;
readonly value: unknown;
}
/**
* Returns whether a value is a parameter created by this module.
*
* @stability unstable
* @category guards
* @since 4.0.0
*/
export declare const isParameter: (value: unknown) => value is Parameter;
/**
* Encodes a parameter for a `Bind` message. SQL NULL stays `null`.
*
* @stability unstable
* @category encoding
* @since 4.0.0
*/
export declare const encodeParameter: (parameter: Parameter, registry?: Registry) => Result.Result;
/**
* Returns whether a parameter uses the text format in a `Bind` message.
* Untyped parameters (OID `0`) use text so PostgreSQL can infer their type;
* typed parameters use the binary format.
*
* @stability unstable
* @category encoding
* @since 4.0.0
*/
export declare const isTextFormat: (parameter: Parameter) => boolean;
/**
* Writes a parameter into a `Bind` frame.
*
* @stability unstable
* @category encoding
* @since 4.0.0
*/
export declare const writeParameter: (sink: ValueSink, parameter: Parameter, registry?: Registry) => Result.Result;
/**
* A `bool` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const bool: (value: boolean | null) => Parameter;
/**
* An `int2` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const int2: (value: number | null) => Parameter;
/**
* An `int4` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const int4: (value: number | null) => Parameter;
/**
* An `int8` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const int8: (value: bigint | null) => Parameter;
/**
* An `oid` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const oid: (value: number | null) => Parameter;
/**
* A `float4` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const float4: (value: number | null) => Parameter;
/**
* A `float8` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const float8: (value: number | null) => Parameter;
/**
* A `numeric` parameter, given as a decimal string or `"NaN"`.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const numeric: (value: string | null) => Parameter;
/**
* A `text` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const text: (value: string | null) => Parameter;
/**
* A `varchar` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const varchar: (value: string | null) => Parameter;
/**
* A `bpchar` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const bpchar: (value: string | null) => Parameter;
/**
* A `name` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const name: (value: string | null) => Parameter;
/**
* A `bytea` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const bytea: (value: Uint8Array | null) => Parameter;
/**
* A `json` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const json: (value: unknown) => Parameter;
/**
* A `jsonb` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const jsonb: (value: unknown) => Parameter;
/**
* A `uuid` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const uuid: (value: string | null) => Parameter;
/**
* An `inet` parameter, such as `"10.0.0.1"` or `"10.0.0.0/8"`.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const inet: (value: string | null) => Parameter;
/**
* A `cidr` parameter.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const cidr: (value: string | null) => Parameter;
/**
* A `date` parameter, given as `YYYY-MM-DD`, `"infinity"`, or `"-infinity"`.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const date: (value: string | null) => Parameter;
/**
* A `time` parameter, given as microseconds since midnight.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const time: (value: bigint | null) => Parameter;
/**
* A `timetz` parameter, such as `"12:34:56+02:00"`.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const timetz: (value: string | null) => Parameter;
/**
* A `timestamp` parameter from a `Date` or epoch milliseconds. UTC fields
* become the stored wall-clock fields, regardless of session `TimeZone`.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const timestamp: (value: Date | number | null) => Parameter;
/**
* A `timestamptz` parameter, given as a `Date` or Unix epoch milliseconds.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const timestamptz: (value: Date | number | null) => Parameter;
/**
* An `interval` parameter with independent months, days, and microseconds.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const interval: (value: Interval | null) => Parameter;
/**
* A one-dimensional array parameter whose elements have the given OID.
*
* @stability unstable
* @category constructors
* @since 4.0.0
*/
export declare const array: (values: ReadonlyArray | null, elementOid: number, registry?: Registry) => Result.Result;
export {};
//# sourceMappingURL=PgTypes.d.ts.map