attemptconstconst attempt: (body: readonly Stmt[], ...handlers: readonly { readonly when: string; readonly then: readonly Stmt[]; }[]) => StmtRuns statements and, where one fails with a named condition (unique_violation), runs others instead.
Typed PL/pgSQL (commons data 0050-sql-plpgsql): the body of a routine written as variables and statements (assign, run, if, while, for each row, raise, return, return a query, try with handlers) and not as text. A variable has a type and a value of that type; a reference to a table, a column, a parameter or a variable is a value, written by the case style; and the block is checked before it is made: a variable that was not declared, a function that can come to its end without returning, a value returned from a procedure, a loop that names a table the schema lacks, are errors as values. The result is the body of a routine definition (language: "plpgsql"), so everything the routines extension checks is checked too.
attemptconstconst attempt: (body: readonly Stmt[], ...handlers: readonly { readonly when: string; readonly then: readonly Stmt[]; }[]) => StmtRuns statements and, where one fails with a named condition (unique_violation), runs others instead.
blockfunctionfunction block(spec: { readonly declare?: readonly (Var<unknown> & { readonly declared: Declared<LogicalType>; })[] | undefined; readonly body: readonly Stmt[]; readonly returns: Shape; }): Result<readonly Part[], readonly BlockError[]>A block as the body of a routine.
| Parameter | What it is |
|---|---|
spec | the variables it declares and its statements, and whether the routine returns a value or nothing |
Returns: the body (text and references), or what is wrong with the block
BlockErrortypeno test imports ittype BlockError =
| { readonly kind: "undeclared"; readonly variable: string }
| { readonly kind: "declared-twice"; readonly variable: string }
| { readonly kind: "no-end"; readonly why: string }
| { readonly kind: "return-value"; readonly why: string }
| { readonly kind: "state"; readonly code: string };What is wrong with a block.
doneconstconst done: () => StmtReturns from a procedure or a function that returns nothing.
Declaredinterfaceno test imports itinterface Declared<L extends LogicalType = LogicalType>A variable that a block declares.
| Member | Type | What it is |
|---|---|---|
name | string | |
type | L | |
initial? | Expr<Of<L>> |
exprfunctionfunction expr<T = unknown>(strings: TemplateStringsArray, ...holes: readonly Hole[]): Expr<T>An expression from text and values: a variable, a reference, another expression or a literal (written as SQL writes it).
expr`${column("invoice", "total")} > ${val(100)}`;Exprinterfaceinterface Expr<T = unknown>An expression of a type T: parts of a body. The type is the compiler's; it is not in the value.
| Member | Type | What it is |
|---|---|---|
parts | readonly Part[] | |
__t? | T |
forEachconstconst forEach: (record: string, query: Expr<unknown>, body: (row: (table: string, column: string) => Expr<unknown>) => readonly Stmt[]) => StmtRuns statements for each row of a query; the row's columns of a table are row(table, column).
foundconstconst found: Expr<boolean>Whether the last statement touched a row.
loopconstconst loop: (condition: Expr<boolean>, body: readonly Stmt[]) => StmtRepeats statements while a condition holds.
noticeconstconst notice: (message: string, ...args: readonly Expr<unknown>[]) => StmtSays something to whoever runs the routine and goes on.
Oftypeno test imports ittype Of<L extends LogicalType> = L extends { readonly kind: "text" | "date" | "instant" | "decimal" | "identifier" | "uuid" }
? string
: L extends { readonly kind: "integer" }
? number
: L extends { readonly kind: "boolean" }
? boolean
: unknown;The TypeScript type of the values of a logical type.
Parttypeno test imports ittype Part = string | Ref;A piece of a body: text, or a reference the case style writes.
raiseconstconst raise: (message: string, ...args: readonly Expr<unknown>[]) => StmtStops with an error; % in the message is replaced by the arguments, in order; code is the SQLSTATE (P0001 where none is given).
raiseCodeconstconst raiseCode: (code: string, message: string, ...args: readonly Expr<unknown>[]) => StmtStops with an error and a SQLSTATE a handler can name.
refconstno test imports itconst ref: <T = unknown>(r: Ref) => Expr<T>A reference as an expression.
retconstconst ret: (value: Expr<unknown>) => StmtReturns the value of the function.
retQueryconstconst retQuery: (query: Expr<unknown>) => StmtReturns the rows of a query as the rows of the function.
runconstconst run: (statement: Expr<unknown>) => StmtRuns a statement for its effect.
setconstconst set: <T>(target: Var<T>, value: Expr<NoInfer<T>>) => StmtAssigns a variable a value of its type.
Shapetypeno test imports ittype Shape = "value" | "none";What a block is checked against.
Stmttypeno test imports ittype Stmt =
| { readonly kind: "assign"; readonly target: string; readonly value: Expr }
| { readonly kind: "run"; r… (888 characters)type Stmt =
| { readonly kind: "assign"; readonly target: string; readonly value: Expr }
| { readonly kind: "run"; readonly statement: Expr }
| { readonly kind: "if"; readonly condition: Expr; readonly then: readonly Stmt[]; readonly otherwise: readonly Stmt[] }
| { readonly kind: "while"; readonly condition: Expr; readonly body: readonly Stmt[] }
| { readonly kind: "for"; readonly record: string; readonly query: Expr; readonly body: readonly Stmt[] }
| { readonly kind: "raise"; readonly level: "exception" | "notice"; readonly message: string; readonly args: readonly Expr[]; readonly code?: string }
| { readonly kind: "return"; readonly value?: Expr }
| { readonly kind: "return-query"; readonly query: Expr }
| { readonly kind: "attempt"; readonly body: readonly Stmt[]; readonly handlers: readonly { readonly when: string; readonly then: readonly Stmt[] }[] };A statement of a block.
valconstconst val: <T extends string | number | boolean | null>(v: T) => Expr<T>A literal of a type: a number, a boolean, text or null, written as SQL writes it.
variableconstconst variable: <L extends LogicalType>(name: string, type: L, initial?: Expr<Of<L>> | undefined) => Var<Of<L>> & { readonly declared: Declared<L>; }A variable, to be declared in a block and used in its statements.
Varinterfaceno test imports itinterface Var<T = unknown> extends Expr<T>A variable of a block.
| Member | Type | What it is |
|---|---|---|
variable | string | |
type | LogicalType | |
parts | readonly Part[] | |
__t? | T |
whenconstconst when: (condition: Expr<boolean>, then: readonly Stmt[], otherwise?: readonly Stmt[]) => StmtDoes one thing where a condition holds and, where given, another where it does not.
columnOfconstconst columnOf: <T = unknown>(table: string, name: string) => Expr<T>A column of a table, qualified.
paramOfconstconst paramOf: <T = unknown>(name: string) => Expr<T>A parameter of the routine.
tableOfconstconst tableOf: (name: string) => Expr<unknown>A table.