Caqti_template.Row_type
SourceDatabase row types, also used for parameters.
Type descriptor for row types.
Type descriptor used for building cartesian products of row types.
If t1
and t2
are the same row type representations, then unify t1 t2
is the witness of the unification of their static type parameters, otherwise it is None
.
equal_value t
is the equality predicate for values of row type t
.
pp ppf t
prints a human presentation of t
on ppf
.
pp_any ppf t
prints a human presentation of t
on ppf
.
pp_value ppf (t, v)
prints a human representation of v
given the type descriptor t
. This function is meant for debugging; the output is neither guaranteed to be consistent across releases nor to contain a complete record of the data.
field ft
is a row of a single field of type ft
. This function can be used when adding new field types; use the below functions otherwise.
Implementers of product
types may raise this exception to signal that a conversion cannot be carried out.
Given a set of projection functions p1 : t -> t1
, ..., pN : t -> tN
and a function intro : t1 -> ... -> tN -> t
to reconstruct values of t
from the projections,
product intro
@@ proj t1 p1
@@ ...
@@ proj tN pN
@@ proj_end
defines a Caqti type for t
, which on the database side will be represented by a consecutive list of fields corresponding to the types t1
, ..., tN
, each of which may be represented by multiple fields. That is, intro [project1 x] ... [projectN x]
is equivalent to x
according to an enforced or effective abstraction of t
deemed adequate for the application logic.
intro
may raise Reject
to indicate that a value cannot be constructed from the given arguments. Projection operators may also raise this exception to indicate that an object cannot be represented in the database, e.g. due to an overflow.
The above only states that intro
is a left (pseudo-)inverse of the projections, which is what matters for a faithful representation of OCaml values. The opposite (projection functions being the left inverse of intro
) may be relevant if the application needs preserve the database representation when updating objects.
enum ~encode ~decode name
creates an enum type which on the SQL side is named name
, with cases which are converted with encode
and decode
functions. This is implemented in terms of the Field_type.t.Enum
field type.
val custom :
encode:('a -> ('b, string) result) ->
decode:('b -> ('a, string) result) ->
'b t ->
'a t
custom ~encode ~decode rep
creates a custom type represented by rep
, where encode
is used to encode parameters into rep
and decode
is used to decode result rows from rep
.
include STD
The following types correspond to what usually fits in a single field of a result row or input parameter set.
A bool
mapped to boolean
on the SQL side if supported, otherwise mapped to an integer.
A float
mapped to double precision
or (best alternative) on the SQL side. Serialization may be lossy (e.g. base 10 may be used), so even if both sides support IEEE 754 double precision numbers, there may be discrepancies in the last digits of the binary representaton.
An UTF-8 string. The database should accept UTF-8 if non-ASCII characters are present.
A string
mapped to whichever type is used to represent binary data on the SQL side.
An absolute time with driver-dependent precision. This corresponds to an SQL timestamp with time zone
or a suitable alternative where not available:
datetime
which is similar to the SQL timestamp
and timestamp
which is similar to the SQL timestamp with time zone
, but the driver does not make the distinction. Caqti sets the session time zone to UTC to avoid misinterpretation, since time values are passed in both directions without time zones. Values have microsecond precision, but you will need to specify the desired precision in the database schema to avoid truncation.timestamp with time zone
is stored as UTC without time zone, taking up no more space then timestamp
. The PostgreSQL timestamp
type is problematic since how conversions work and the manual indicate that it is meant to be a local time, and since database columns of this type stores the value without conversion to UTC, it becomes prone to time zone changes. To mitigate the issue, Caqti sets the time zone of sessions to UTC.CURRENT_TIMESTAMP
return, except for an additional three decimals to achive millisecond precision.It might seem better to use standard RFC3339 format, since it is accepted by the SQLite functions, but that would misorder some time values if mixed with the results of these functions, even just the "Z" suffix would misorder values with different precision.
Date and time values which comes from the database without time zone are interpreted as UTC. This is not necessarily correct, and it is highly recommended to use SQL types which are transmitted with time zone information, even if this is UTC.
A period of time. If the database lacks a dedicated representation, the integer number of seconds is used.
option t
turns a set of fields encoded as t
into a correspending set of nullable fields. The encoder will encode None
as into a tuple of NULL
values and the decoder will return None
if all fields are NULL
.
If the type t
itself is option t'
for some t'
, or contains nested tuples and options such that all field types are nested under an option type, then it would have been possible to decode an all-NULL
segment of a row as Some x
where x
is a corresponding tuple-option-tree terminating in None
values. The above paragraph resolves this ambiguity since it implies that the outermost option possible will be decoded as None
.
As a common case of composite types, constructors for tuples up to 12 components are predefined here. Higher tuples can be created with Row_type.product
.
A type holding no fields. This is used to pass no parameters and as the result for queries which does not return any rows. It can also be nested in tuples, in which case it will not contribute to the total number of fields.
Creates a 5-tuple type.
val t6 :
'a1 t ->
'a2 t ->
'a3 t ->
'a4 t ->
'a5 t ->
'a6 t ->
('a1 * 'a2 * 'a3 * 'a4 * 'a5 * 'a6) t
Creates a 6-tuple type.
val t7 :
'a1 t ->
'a2 t ->
'a3 t ->
'a4 t ->
'a5 t ->
'a6 t ->
'a7 t ->
('a1 * 'a2 * 'a3 * 'a4 * 'a5 * 'a6 * 'a7) t
Creates a 7-tuple type.
val t8 :
'a1 t ->
'a2 t ->
'a3 t ->
'a4 t ->
'a5 t ->
'a6 t ->
'a7 t ->
'a8 t ->
('a1 * 'a2 * 'a3 * 'a4 * 'a5 * 'a6 * 'a7 * 'a8) t
Creates a 8-tuple type.
val t9 :
'a1 t ->
'a2 t ->
'a3 t ->
'a4 t ->
'a5 t ->
'a6 t ->
'a7 t ->
'a8 t ->
'a9 t ->
('a1 * 'a2 * 'a3 * 'a4 * 'a5 * 'a6 * 'a7 * 'a8 * 'a9) t
Creates a 9-tuple type.
val t10 :
'a1 t ->
'a2 t ->
'a3 t ->
'a4 t ->
'a5 t ->
'a6 t ->
'a7 t ->
'a8 t ->
'a9 t ->
'a10 t ->
('a1 * 'a2 * 'a3 * 'a4 * 'a5 * 'a6 * 'a7 * 'a8 * 'a9 * 'a10) t
Creates a 10-tuple type.