Module Sql.Stmt

Typed SQL statements.

This module provides a low-level mechanism to type SQL statements arguments (parameters) and bind their arguments to value via an OCaml binding function.

See the SQL statement typing howto for a short introduction.


type arg =
| Arg : 'a Type.t * 'a -> arg

The type for SQL statement arguments (parameters).

val pp_arg : Stdlib.Format.formatter -> arg -> unit

pp_arg formats an argument with Type.value_pp.


type 'r t

The type for a closed (all arguments are bound) SQL statements returning rows of type 'r.

val v : string -> rev_args:arg list -> result:'r Row.t -> 'r t

v sql rev_args result is a closed statement with srouce sql, revered list of arguments (parameters) rev_args and yielding rows of type result.

val src : 'r t -> string

src st is the source SQL statement of st.

val result : 'r t -> 'r Row.t

result s is the result of s.

val rev_args : 'r t -> arg list

rev_args st is the reversed list of arguments of the statement.

val pp_src : Stdlib.Format.formatter -> 'r t -> unit

pp_src formats the statement's source.

val pp : Stdlib.Format.formatter -> 'r t -> unit

pp ppf st formats the statement's source and its arguments.

Binding functions

type 'a func

The type for open SQL statements with argument binding function of type 'a.

val func : string -> 'a func -> 'a

func sql f is the binding function of f used on the source SQL statement sql.

val ret : 'r Row.t -> 'r t func

ret st row is an open SQL statement st returning values of type row.

val arg : 'a Type.t -> 'b func -> ( 'a -> 'b ) func

arg t f binds a new variable of type t to f.

val (@->) : 'a Type.t -> 'b func -> ( 'a -> 'b ) func

t @-> f is arg t f.

val unit : unit t func

unit is ret Row.empty.

The following constants get redefined here to allow consise specification with the Sql.Stmt.() notation.

val bool : bool Type.t

bool is Type.Bool.

val int : int Type.t

int is Type.Int.

val int64 : int64 Type.t

int64 is Type.Int64.

val float : float Type.t

float is Type.Float.

val text : string Type.t

text is Type.Text.

val blob : string Type.t

blob is Type.Blob.

val option : 'a Type.t -> 'a option Type.t

option t is !Type.option t.

Binding projections

See the this section of the SQL statement typing howto.

val nop : 'b func -> ( 'a -> 'b ) func

nop f adds an unused argument to f.

val proj : ( 'r -> 'a ) -> 'a Type.t -> ( 'r -> 'b ) func -> ( 'r -> 'b ) func

proj p t binds the projection p of a value of type t.

val col : ( 'r, 'a ) Col.t -> ( 'r -> 'b ) func -> ( 'r -> 'b ) func

col c f binds the projection on column c of a row of type 'r