Caqti_template.CreateSourceThis is a convenience API which collects everything needed to create request templates. A request template describes a database query and how to encode parameters and decode the result.
Consider the example:
let bounds_upto_req =
let open Caqti_template.Create in
static T.(t2 int32 float -->! option (t2 float float))
"SELECT min(y), max(y) FROM samples WHERE series_id = ? AND x < ?"First we opening the current module. We then pick the function static to create a template for prepared queries where the query template has static lifetime. The first argument describes the parameter type and the result row type combined with an arrow which describes the multiplicity of the result rows. The exclamation mark in the arrow indicates that precisely one result row is expected. The second argument is the query template, here in the form of a string.
In the query template, ? refer to parameters, but you can also use the PostgreSQL-style $1, $2, etc. if you prefer, as long as you stick to the same convention for a given query template. Caqti drivers translate parameter references to fit the database system, rearranging parameters if necessary.
Caqti provides a way to handle dialectical differences between database systems apart from the parameter syntax. The example above uses a shortcut, since it does not need this functionality. In the full form it looks like:
let bounds_upto_req =
let open Caqti_template.Create in
static_gen
T.(t2 int32 float -->! option (t2 float float)) @@ Fun.const @@
Q.parse
"SELECT min(y), max(y) FROM samples WHERE series_id = ? AND x < ?"The callback receives a Dialect.t and returns a Query.t. We can now see that the still same query string is explicitly parsed. Query and Query_fmt provides alternative ways of constructing query template which is more suitable for dynamically generated queries.
The following example makes use of the dialect argument to handle dialectical differences regarding string concatenation:
let concat_req =
let open Caqti_template.Create in
static_gen T.(t2 string string -->! string) @@ function
| D.Mysql _ -> Q.parse "SELECT concat(?, ?)"
| _ -> Q.parse "SELECT ? || ?"In summary
-->. for zero, -->! for one, -->? for zero or one, -->* for zero or more.If needed, you can supplement the current module with custom types:
module Ct : sig
open Caqti_template
include Caqti_template.CREATE
module T : sig
include module type of T
val password : string Row_type.t
val uri : Uri.t Row_type.t
end
end = struct
open Caqti_template
include Caqti_template.Create
module T = struct
include T
let password = redacted string (* a string redacted from logs *)
let uri =
let encode x = Ok (Uri.to_string x) in
let decode s = Ok (Uri.of_string s) in
Row_type.custom ~encode ~decode string
end
endinclude module type of Version.Infixinclude module type of Query.Infixpfx ^++ q is q prefixed with the literal fragment pfx, i.e. cat (lit pfx) q.
The following are shortcuts for Request.create and Query.parse In particular static, dynamic, and direct covers the most common case of sending a pre-composed query string to the database while the static_gen, dynamic_gen, and direct_gen are the correspending fully generic variants.
Where the query is supplied as function of a dialect, the function should be pure and may be recalled repeatedly, cf. Request.create.
val static :
('a Row_type.t * 'b Row_type.t * 'm Row_mult.t) ->
string ->
('a, 'b, 'm) Request.tCreates a template of static lifetime for prepared requests where the query template is provided as a string to be parsed by Query.parse.
val static_gen :
('a Row_type.t * 'b Row_type.t * 'm Row_mult.t) ->
(Dialect.t -> Query.t) ->
('a, 'b, 'm) Request.tCreates a template of static lifetime for prepared requests where the query template is dialect-dependent and explicitly constructed by the caller.
A variant of static_gen which executes multiple queries which must receive no parameters and return no result rows.
Depending on the chosen driver, statements may be prepared individually, with the limitations this implies, cf. Request.create_multi. Esp. direct_gen_multi is recommended for schema initialization and updates.
val dynamic :
('a Row_type.t * 'b Row_type.t * 'm Row_mult.t) ->
string ->
('a, 'b, 'm) Request.tCreates a template of static lifetime for prepared requests where the query template is provided as a string to be parsed by Query.parse.
val dynamic_gen :
('a Row_type.t * 'b Row_type.t * 'm Row_mult.t) ->
(Dialect.t -> Query.t) ->
('a, 'b, 'm) Request.tCreates a template of static lifetime for prepared requests where the query template is dialect-dependent and explicitly constructed by the caller.
A variant of dynamic_gen which executes multiple queries which must receive no parameters and return no result rows.
Depending on the chosen driver, statements may be prepared individually, with the limitations this implies, cf. Request.create_multi. Esp. direct_gen_multi is recommended for schema initialization and updates.
val direct :
('a Row_type.t * 'b Row_type.t * 'm Row_mult.t) ->
string ->
('a, 'b, 'm) Request.tCreates a template for non-prepared requests where the query template is provided as a string to be parsed by Query.parse. If non-prepared requests are not unsupported by the driver, a temporarily prepared request is used instead.
val direct_gen :
('a Row_type.t * 'b Row_type.t * 'm Row_mult.t) ->
(Dialect.t -> Query.t) ->
('a, 'b, 'm) Request.tCreates a template for non-prepared requests where the query template is dialect-dependent and explicitly constructed by the caller. If non-prepared requests are not unsupported by the driver, a temporarily prepared request is used instead.
A variant of direct_gen which executes multiple queries which must receive no parameters and return no result rows. To ensure that direct queries are used when available, the queries must have no quoted string or values, since these may produce implied parameters.