1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
(** Operations on the values stored in the database (fetching, updating, …).
A value is one row of a content or list table in the database. *)
type value = Model.target Model.Data.any
(** The data of a field of a content or a list, whatever the type of the data
held: an existential pack over {!Model.Data.data} *)
val none : value
(** [none]: the field is empty (or left as is, on update) *)
val single : ('db, 'ocaml, Model.target) Model.Types.t * 'ocaml -> value
(** [single (typ, data)]: the value for the field, in its code representation
['ocaml] — a single value, or an [int64 Seq.t] for a multivalued reference —
together with the type [typ] of the field *)
module Property = Property
(** A property of a value: a field name tied to its {!Model.Types.t} — the typed
handle used to read a field of a value with {!get}. *)
type t
(** A value: a row of a content or list table in the database, identified by its
{!Model.target} and its [id].
The type is abstract: a value is built with [v] (empty), [load] (built and
fully read from the database) or completed later with [load'], and inspected
with [target] and [id]. *)
val v : Model.target -> int64 -> t
(** Build a value from its target (a content or a static list) and its row id.
The fields dictionary is left empty — use {!load'} to read the fields from
the database. *)
val target : t -> Model.target
(** The content or static list the value belongs to *)
val id : t -> int64
(** The primary key of the value in its table — a direct field access, cheaper
than [get v id_ref] when the raw [int64] is what is needed (e.g. to run a
query on the row) *)
val id_ref : Model.target Model.Types.monoreference Property.t
(** The pre-built {!Property.t} handle of the reserved field ["id"]
({!Model.Identifier.id}) — an integer field present in every row, content or
list. This is the key to read the primary key of any value. *)
val get : t -> ('db, 'ocaml, Model.target) Model.Types.t Property.t -> 'ocaml
(** Read a field of the value and return its data directly in its code
representation ['ocaml] — e.g. [get v id_ref : int64 option] is the primary
key of [v], and a multivalued reference field would yield an [int64 Seq.t].
The match between the requested property and the readable field is proved by
{!Property.provably_equal} on name and type. The reserved field [id_ref] is
always readable; the other fields are read from the value's dictionary
(populated by {!load} or {!load'}) and raise [Not_found] when absent. *)
val load :
Caqti_lwt.connection ->
?multivalued:bool ->
Model.target ->
Int64.t ->
t Lwt.t
(** [load ?multivalued db target id] builds the value for the row [id] of
[target] — a content (wrap it with {!Model.Reference.of_content}) or a
static list — and reads the row from the database (one query for the table,
plus one per multivalued reference field), storing each field in the value's
dictionary. The returned value is complete: {!get} reads every field as
typed data.
[~multivalued] (default [true]) tells whether the multivalued reference
fields are also loaded: with [~multivalued:false], only the row of the table
is read, and the multivalued fields are left out of the dictionary — {!get}
raises [Not_found] on them.
The primary key needs no loading — {!get} with {!id_ref} reads it directly
from the value. Use {!v} instead when the fields are already known and no
database read is wanted. *)
val load' : Caqti_lwt.connection -> ?multivalued:bool -> t -> unit Lwt.t
(** [load' ?multivalued db v] reads the row of [v] from the database (one query
for the table, plus one per multivalued reference field, unless
[~multivalued:false]) and stores each field in [v]'s dictionary. The value
is modified in place — the dictionary is part of its state — so the function
returns [unit]. Use it to populate a value built with {!v}; {!load} is the
shortcut that builds and loads in one call. *)
val pp : Format.formatter -> t -> unit
(** Pretty-print a value as [{ id = …; field = …; … }]: the primary key and the
fields currently held in the value's dictionary, sorted by field name (the
dictionary is a hashtable, sorting keeps the output stable).
The target of the value is not printed, and neither are the fields not
loaded yet: only what {!load} or {!load'} put in the dictionary is shown. *)
|