summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-11-22 13:58:16 +0100
committerSébastien Dailly <sebastien@dailly.me>2026-11-22 13:58:16 +0100
commit056fcabaeec238a7aa927df39a90acea028b1aa0 (patch)
tree3246359561eb2375e85da4537e05a083a1348467 /lib
parentbc78bea63b7c75e8a9d222dc86fb75ed293a2242 (diff)
Added an option for loading a value content without the multivaluated fields (allow single query)main
Diffstat (limited to 'lib')
-rw-r--r--lib/model/field.ml36
-rw-r--r--lib/model/field.mli9
-rw-r--r--lib/model/types.ml50
-rw-r--r--lib/model/types.mli45
-rw-r--r--lib/query/encoder.ml2
-rw-r--r--lib/query/insert.ml2
-rw-r--r--lib/query/query.mli14
-rw-r--r--lib/query/select.ml55
-rw-r--r--lib/query/select.mli42
-rw-r--r--lib/query/update.ml10
-rw-r--r--lib/value/property.ml36
-rw-r--r--lib/value/property.mli33
-rw-r--r--lib/value/tests/load.t6
-rw-r--r--lib/value/value.ml96
-rw-r--r--lib/value/value.mli58
15 files changed, 268 insertions, 226 deletions
diff --git a/lib/model/field.ml b/lib/model/field.ml
index 241a9df..42a2d2a 100644
--- a/lib/model/field.ml
+++ b/lib/model/field.ml
@@ -6,8 +6,8 @@ type 't field_ex = T : ('db, 'ocaml, 't) Types.t -> 't field_ex
type 't t =
| F : {
- named : ('db, 'ocaml, 't) Types.t Types.named;
- (** Name and type of the field, tied together *)
+ name : Identifier.t;
+ t : ('db, 'ocaml, 't) Types.t;
label : textI18n;
placeholder : textI18n option;
icon : unit option;
@@ -17,15 +17,14 @@ type 't t =
(** Display the field when the criteria is valid *)
}
-> 't t
- (** The record is a GADT so that [named] keeps its existential data type
- ['a] — this is what lets {!named_of} re-stamp it with a known type.
- The type stays abstract in the interface. *)
+ (** The record is a GADT so that [t] keeps its type parameters ['db] and
+ ['ocaml] existential. The type stays abstract in the interface. *)
-let name : 'ref t -> Identifier.t = fun (F f) -> Types.name f.named
+let name : 'ref t -> Identifier.t = fun (F f) -> f.name
let label : 'ref t -> textI18n = fun (F f) -> f.label
let placeholder : 'ref t -> textI18n option = fun (F f) -> f.placeholder
let icon : 'ref t -> unit option = fun (F f) -> f.icon
-let type_ : 'ref t -> 'ref field_ex = fun (F f) -> T (Types.typ f.named)
+let type_ : 'ref t -> 'ref field_ex = fun (F f) -> T f.t
let required : 'ref t -> bool = fun (F f) -> f.required
let validation : 'ref t -> validation list = fun (F f) -> f.validation
let criteria : 'ref t -> string list Dsl.t option = fun (F f) -> f.criteria
@@ -42,25 +41,4 @@ let create :
't t =
fun (T typ) label ?placeholder ?icon ?(required = false) ?(validation = [])
?criteria name ->
- F
- {
- named = Types.named ~name ~typ;
- label;
- placeholder;
- icon;
- required;
- validation;
- criteria;
- }
-
-let named_of : type db ocaml ref.
- ref t ->
- (db, ocaml, ref) Types.t ->
- (db, ocaml, ref) Types.t Types.named option =
- fun (F f) typ ->
- match Types.eq typ f.named.typ with
- | None -> None
- | Some Type.Equal -> (
- match Types.eq_ocaml typ f.named.typ with
- | None -> None
- | Some Type.Equal -> Some f.named)
+ F { name; t = typ; label; placeholder; icon; required; validation; criteria }
diff --git a/lib/model/field.mli b/lib/model/field.mli
index 3a19ab8..2217f97 100644
--- a/lib/model/field.mli
+++ b/lib/model/field.mli
@@ -7,15 +7,6 @@ type 't field_ex = T : ('db, 'ocaml, 't) Types.t -> 't field_ex
type 'ref t
(** Description of a field *)
-val named_of :
- 'ref t ->
- ('db, 'ocaml, 'ref) Types.t ->
- ('db, 'ocaml, 'ref) Types.t Types.named option
-(** [named_of field typ] is [Some named] — the field's {!Types.named} type,
- stamped with ['db] and ['ocaml] — when [typ] matches the declared type of
- [field] (checked with {!Types.eq} and {!Types.eq_ocaml}); [None] on type
- mismatch *)
-
val create :
'ref field_ex ->
textI18n ->
diff --git a/lib/model/types.ml b/lib/model/types.ml
index ee244d3..7226d6f 100644
--- a/lib/model/types.ml
+++ b/lib/model/types.ml
@@ -49,8 +49,12 @@ let default_value : type a b c. (a, b, c) t -> a = function
| MonoReference _ -> None
| MultiReference _ -> raise Not_found
-(** Constructor-level equality on the ['db] representation *)
-let eq : type a b x y c. (a, x, c) t -> (b, y, c) t -> (a, b) Type.eq option =
+(** Constructor-level equality: [eq t1 t2] is [Some Equal] when [t1] and [t2]
+ have the same constructor. The pair ['a * 'x] is injective, so [Equal]
+ proves both ['a = 'b] (the database representations) and ['x = 'y] (the code
+ representations) at once *)
+let eq : type a b x y c.
+ (a, x, c) t -> (b, y, c) t -> (a * x, b * y) Type.eq option =
fun t1 t2 ->
match (t1, t2) with
| MultiReference _, MultiReference _ -> Some Type.Equal
@@ -58,45 +62,3 @@ let eq : type a b x y c. (a, x, c) t -> (b, y, c) t -> (a, b) Type.eq option =
| Integer, Integer -> Some Type.Equal
| Text, Text -> Some Type.Equal
| _ -> None
-
-(** Constructor-level equality on the ['ocaml] representation *)
-let eq_ocaml : type a b x y c.
- (a, x, c) t -> (b, y, c) t -> (x, y) Type.eq option =
- fun t1 t2 ->
- match (t1, t2) with
- | MultiReference _, MultiReference _ -> Some Type.Equal
- | MonoReference _, MonoReference _ -> Some Type.Equal
- | Integer, Integer -> Some Type.Equal
- | Text, Text -> Some Type.Equal
- | _ -> None
-
-type 'typ named = {
- name : Identifier.t; (** The internal name of the field *)
- typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
-}
-(** A named field type: a {!t} together with the field's name.
-
- Named types are built on the fly with {!named} (e.g. from a field, see
- {!Field.named_of}) and used locally as typed handles — e.g. to read the data
- of a field from a value. A pre-built instance exists for the reserved field
- [["id"]] (see [Value.id_ref]). *)
-
-(** Build a named type from a name and a type — no uniqueness involved, so
- values built on the fly denote the same field *)
-let named : name:Identifier.t -> typ:'typ -> 'typ named =
- fun ~name ~typ -> { name; typ }
-
-let name : 'typ named -> Identifier.t = fun n -> n.name
-let typ : 'typ named -> 'typ = fun n -> n.typ
-
-(** [provably_equal n1 n2] is [Some Equal] iff [n1] and [n2] denote the same
- field — same name — and their types match, which also proves that their
- ['ocaml] representations are equal *)
-let provably_equal (type a b x y c) (n1 : (a, x, c) t named)
- (n2 : (b, y, c) t named) : (x, y) Type.eq option =
- if Identifier.equal n1.name n2.name then eq_ocaml n1.typ n2.typ else None
-
-(** [same n1 n2] is [true] iff [n1] and [n2] denote the same field with matching
- types *)
-let same : ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> bool =
- fun n1 n2 -> provably_equal n1 n2 <> None
diff --git a/lib/model/types.mli b/lib/model/types.mli
index 81c9d5b..12f9c79 100644
--- a/lib/model/types.mli
+++ b/lib/model/types.mli
@@ -50,43 +50,10 @@ type 'target multireference = (int64, int64 Seq.t, 'target) t
val default_value : ('a, 'b, 'c) t -> 'a
(** The value to insert in the database when the field is empty *)
-val eq : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('a, 'b) Type.eq option
-(** Constructor-level equality on the ['db] representation: [eq t1 t2] is
- [Some Equal] when [t1] and [t2] have the same constructor, which also proves
- ['a = 'b] *)
+val eq : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('a * 'x, 'b * 'y) Type.eq option
+(** Constructor-level equality: [eq t1 t2] is [Some Equal] when [t1] and [t2]
+ have the same constructor.
-val eq_ocaml : ('a, 'x, 'c) t -> ('b, 'y, 'c) t -> ('x, 'y) Type.eq option
-(** Constructor-level equality on the ['ocaml] representation: [eq_ocaml t1 t2]
- is [Some Equal] when [t1] and [t2] have the same constructor, which also
- proves ['x = 'y] *)
-
-type 'typ named = {
- name : Identifier.t; (** The internal name of the field *)
- typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
-}
-(** A named field type: a {!t} together with the field's name.
-
- Named types are built on the fly with {!named} (e.g. from a field, see
- {!Field.named_of}) and used locally as typed handles — e.g. to read the data
- of a field from a value. A pre-built instance exists for the reserved field
- ["id"] (see [Value.id_ref]). *)
-
-val named : name:Identifier.t -> typ:'typ -> 'typ named
-(** Build a named type from a name and a type — no uniqueness involved, so
- values built on the fly denote the same field *)
-
-val name : 'typ named -> Identifier.t
-(** The name of the field *)
-
-val typ : 'typ named -> 'typ
-(** The type of the named field *)
-
-val provably_equal :
- ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> ('x, 'y) Type.eq option
-(** [provably_equal n1 n2] is [Some Equal] iff [n1] and [n2] denote the same
- field — same name — and their types match, which also proves that their
- ['ocaml] representations are equal *)
-
-val same : ('a, 'x, 'c) t named -> ('b, 'y, 'c) t named -> bool
-(** [same n1 n2] is [true] iff [n1] and [n2] denote the same field with matching
- types *)
+ Since the pair type is injective, [Equal] proves at once that ['a = 'b] (the
+ database representations match) and ['x = 'y] (the code representations
+ match). *)
diff --git a/lib/query/encoder.ml b/lib/query/encoder.ml
index 4c8d528..3c9484a 100644
--- a/lib/query/encoder.ml
+++ b/lib/query/encoder.ml
@@ -309,7 +309,7 @@ module DataToCaqtiValues = Map (struct
(* For the single-column fields handled here, the DB and code
representations coincide — match the field type to prove it *)
let check (type x) (ct : (x, x, Model.target) Model.Types.t) : x =
- match Model.Types.eq_ocaml ct field_type' with
+ match Model.Types.eq ct field_type' with
| Some Equal -> value
| None -> raise TypeMismatch
in
diff --git a/lib/query/insert.ml b/lib/query/insert.ml
index 98a5c88..c9a7eba 100644
--- a/lib/query/insert.ml
+++ b/lib/query/insert.ml
@@ -68,7 +68,7 @@ module MultiReferencesInsert = struct
match d with
| Model.Data.None -> acc
| Model.Data.V (field_type', values) -> (
- match Model.Types.eq_ocaml content_type field_type' with
+ match Model.Types.eq content_type field_type' with
| None -> raise Encoder.TypeMismatch
| Some Equal ->
let q =
diff --git a/lib/query/query.mli b/lib/query/query.mli
index 5e42f09..0265487 100644
--- a/lib/query/query.mli
+++ b/lib/query/query.mli
@@ -41,14 +41,22 @@ val update :
val select :
Caqti_lwt.connection ->
+ ?multivalued:bool ->
Int64.t ->
Model.content ->
Select.callback ->
unit Lwt.t
(** Read a row of a content table and call the callback once per field with the
- field's named type and its value. See [Query.Select.select] *)
+ field's name, type and value. With [~multivalued:false], the multivalued
+ reference fields are not read. See [Query.Select.select] *)
val select_list :
- Caqti_lwt.connection -> Int64.t -> Model.list -> Select.callback -> unit Lwt.t
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.list ->
+ Select.callback ->
+ unit Lwt.t
(** Read a row of a list table and call the callback once per field with the
- field's named type and its value. See [Query.Select.select_list] *)
+ field's name, type and value. With [~multivalued:false], the multivalued
+ reference fields are not read. See [Query.Select.select_list] *)
diff --git a/lib/query/select.ml b/lib/query/select.ml
index cdcaeed..8cb2b2b 100644
--- a/lib/query/select.ml
+++ b/lib/query/select.ml
@@ -6,7 +6,8 @@ let ( let* ) = Lwt.bind
type callback = {
f :
'db 'ocaml.
- ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named * 'ocaml -> unit;
+ (Model.Identifier.t * ('db, 'ocaml, Model.target) Model.Types.t) * 'ocaml ->
+ unit;
}
(** Call the callback for one single-column field. For those fields the database
@@ -15,15 +16,9 @@ type callback = {
let apply_field : type a b. callback -> (a, b) Intf.data -> a -> unit =
fun k Intf.{ name; content_type } value ->
match content_type with
- | Model.Types.Text ->
- let named = Model.Types.named ~name ~typ:content_type in
- k.f (named, value)
- | Model.Types.Integer ->
- let named = Model.Types.named ~name ~typ:content_type in
- k.f (named, value)
- | Model.Types.MonoReference _ ->
- let named = Model.Types.named ~name ~typ:content_type in
- k.f (named, value)
+ | Model.Types.Text -> k.f ((name, content_type), value)
+ | Model.Types.Integer -> k.f ((name, content_type), value)
+ | Model.Types.MonoReference _ -> k.f ((name, content_type), value)
| Model.Types.MultiReference _ -> raise Encoder.TypeMismatch
(** Call the callback for each field of the tuple, zipping the type descriptors
@@ -135,7 +130,6 @@ let select_multireference :
let (Model.Field.T content_type) = Model.Field.type_ field in
match content_type with
| Model.Types.MultiReference _ ->
- let named = Model.Types.named ~name ~typ:content_type in
let q =
Croquette_common.Format.format_string (fun format ->
Format.fprintf format "SELECT %s FROM %s WHERE %s = ?"
@@ -146,22 +140,25 @@ let select_multireference :
let request = Caqti.Templater.(direct T.(int64 -->* int64) q) in
let* res = Db.collect_list request id in
let* targets = Caqti_lwt.or_fail res in
- k.f (named, List.to_seq targets);
+ k.f ((name, content_type), List.to_seq targets);
Lwt.return_unit
| _ -> raise Encoder.TypeMismatch
(** Read the row [id] of [table], whose columns are described by [fields], and
call [callback] once per field. With [~has_parent], the reserved [parent]
- column is exposed as a field too, like on insert. *)
+ column is exposed as a field too, like on insert. With [~multivalued:false],
+ the multivalued reference fields are not read from their dedicated tables.
+*)
let select_row :
Caqti_lwt.connection ->
table:Model.Identifier.t ->
?has_parent:bool ->
+ ?multivalued:bool ->
Model.field list ->
Int64.t ->
callback ->
unit Lwt.t =
- fun db ~table ?(has_parent = false) fields id k ->
+ fun db ~table ?(has_parent = false) ?(multivalued = true) fields id k ->
let module Db = (val db) in
let module Identifier = Model.Identifier in
let init_fields =
@@ -206,20 +203,34 @@ let select_row :
let () = iter_fields k single row in
Lwt.return_unit
in
- Lwt_list.iter_s
- (fun field -> select_multireference db table id field k)
- multi_fields
+ if multivalued then
+ Lwt_list.iter_s
+ (fun field -> select_multireference db table id field k)
+ multi_fields
+ else Lwt.return_unit
let select :
- Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t =
- fun db id content k ->
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.content ->
+ callback ->
+ unit Lwt.t =
+ fun db ?(multivalued = true) id content k ->
select_row db ~table:(Model.Content.id content)
~has_parent:
(match Model.Content.parent content with None -> false | Some _ -> true)
+ ~multivalued
(List.of_seq (Model.Content.fields content))
id k
let select_list :
- Caqti_lwt.connection -> Int64.t -> Model.list -> callback -> unit Lwt.t =
- fun db id list k ->
- select_row db ~table:list.Model.DataList.id list.Model.DataList.fields id k
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.list ->
+ callback ->
+ unit Lwt.t =
+ fun db ?(multivalued = true) id list k ->
+ select_row db ~multivalued ~table:list.Model.DataList.id
+ list.Model.DataList.fields id k
diff --git a/lib/query/select.mli b/lib/query/select.mli
index 68e4d00..fb7787e 100644
--- a/lib/query/select.mli
+++ b/lib/query/select.mli
@@ -3,21 +3,27 @@
type callback = {
f :
'db 'ocaml.
- ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named * 'ocaml -> unit;
+ (Model.Identifier.t * ('db, 'ocaml, Model.target) Model.Types.t) * 'ocaml ->
+ unit;
}
-(** A callback called once per field of the selected row, with the field's named
- type and its data in the code representation ['ocaml] — a pair that can be
- stored in a value's dictionary and read back as typed data.
+(** A callback called once per field of the selected row, with the field's name
+ and type, and its data in the code representation ['ocaml] — a triple that
+ can be stored in a value's dictionary and read back as typed data.
The field is existentially typed: ['db] and ['ocaml] are universally
quantified in the record field, so a plain function works for a field of any
- type: [ { f = fun (named, data) -> ... } ]. *)
+ type: [ { f = fun ((name, typ), data) -> ... } ]. *)
val select :
- Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t
-(** [select db id content callback] reads the row [id] in the table of [content]
- and calls [callback.f] once per field of the content, with the field's named
- type and its value:
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.content ->
+ callback ->
+ unit Lwt.t
+(** [select ?multivalued db id content callback] reads the row [id] in the table
+ of [content] and calls [callback.f] once per field of the content, with the
+ field's name and type, and its value:
- the single-column fields ([Text], [Integer], [MonoReference], plus the
reserved [parent] column of a subcontent) are read from the content table
@@ -25,12 +31,22 @@ val select :
- each multivalued reference field is read from its dedicated table
([content.field]) and yields the sequence of its targets ([int64 Seq.t]).
+ [~multivalued] (default [true]) controls whether those multivalued reference
+ fields are also read: with [~multivalued:false], only the row of the content
+ table is read and the callback is not called for them.
+
The [id] of the row itself is not passed to the callback: it is the [id]
- argument. In the callback, each [(named, data)] pair can be stored in a
- value's dictionary to make the field readable as typed data. *)
+ argument. In the callback, each [((name, typ), data)] triple can be stored
+ in a value's dictionary to make the field readable as typed data. *)
val select_list :
- Caqti_lwt.connection -> Int64.t -> Model.list -> callback -> unit Lwt.t
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Int64.t ->
+ Model.list ->
+ callback ->
+ unit Lwt.t
(** Same as {!select}, for a row of the dedicated table of a static list. Lists
have no [parent] column; their multivalued reference fields are read from
- their dedicated tables ([list.field]), like for contents. *)
+ their dedicated tables ([list.field]), like for contents, unless
+ [~multivalued:false] is given. *)
diff --git a/lib/query/update.ml b/lib/query/update.ml
index 9bd1f2b..515bf2e 100644
--- a/lib/query/update.ml
+++ b/lib/query/update.ml
@@ -181,13 +181,9 @@ let q :
match d with
| Model.Data.None -> acc
| Model.Data.V (t, seq) -> (
- match
- ( Model.Types.eq content_type t,
- Model.Types.eq_ocaml content_type t )
- with
- | Some Type.Equal, Some Type.Equal ->
- (fields, (name, t, seq) :: references)
- | _ -> raise Encoder.TypeMismatch))
+ match Model.Types.eq content_type t with
+ | Some Type.Equal -> (fields, (name, t, seq) :: references)
+ | None -> raise Encoder.TypeMismatch))
| _ -> (
match d with
| Model.Data.None -> acc
diff --git a/lib/value/property.ml b/lib/value/property.ml
new file mode 100644
index 0000000..e89f05a
--- /dev/null
+++ b/lib/value/property.ml
@@ -0,0 +1,36 @@
+type 'typ t = {
+ name : Model.Identifier.t; (** The internal name of the field *)
+ typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
+}
+(** A property of a value: a field name tied to its type — a {!Model.Types.t}
+ together with the field's {!Model.Identifier.t}.
+
+ Properties are built on the fly with {!named} and used locally as typed
+ handles — e.g. to read the data of a field from a value. A pre-built
+ instance exists for the reserved field ["id"] (see {[id_ref]} in the main
+ module of this package). *)
+
+(** Build a property from a name and a type — no uniqueness involved, so values
+ built on the fly denote the same field *)
+let named : name:Model.Identifier.t -> typ:'typ -> 'typ t =
+ fun ~name ~typ -> { name; typ }
+
+let name : 'typ t -> Model.Identifier.t = fun n -> n.name
+let typ : 'typ t -> 'typ = fun n -> n.typ
+
+(** [provably_equal p1 p2] is [Some Equal] iff [p1] and [p2] denote the same
+ field — same name — and their types match, which also proves that their
+ ['ocaml] representations are equal *)
+let provably_equal (type a b x y c) (p1 : (a, x, c) Model.Types.t t)
+ (p2 : (b, y, c) Model.Types.t t) : (x, y) Type.eq option =
+ if Model.Identifier.equal p1.name p2.name then
+ match Model.Types.eq p1.typ p2.typ with
+ | Some Type.Equal -> Some Type.Equal
+ | None -> None
+ else None
+
+(** [same p1 p2] is [true] iff [p1] and [p2] denote the same field with matching
+ types *)
+let same : ('a, 'x, 'c) Model.Types.t t -> ('b, 'y, 'c) Model.Types.t t -> bool
+ =
+ fun p1 p2 -> provably_equal p1 p2 <> None
diff --git a/lib/value/property.mli b/lib/value/property.mli
new file mode 100644
index 0000000..af83dc0
--- /dev/null
+++ b/lib/value/property.mli
@@ -0,0 +1,33 @@
+(** A property of a value: a field name tied to its type.
+
+ A property is the typed handle used to read a field of a value with
+ {!Value.get}: the {!Model.Types.t} of the field together with its
+ {!Model.Identifier.t} name. *)
+
+type 'typ t = {
+ name : Model.Identifier.t; (** The internal name of the field *)
+ typ : 'typ; (** The type of the field, e.g. a ['target multireference] *)
+}
+(** A {!Model.Types.t} together with the field's name *)
+
+val named : name:Model.Identifier.t -> typ:'typ -> 'typ t
+(** Build a property from a name and a type — no uniqueness involved, so values
+ built on the fly denote the same field *)
+
+val name : 'typ t -> Model.Identifier.t
+(** The name of the field *)
+
+val typ : 'typ t -> 'typ
+(** The type of the field *)
+
+val provably_equal :
+ ('a, 'x, 'c) Model.Types.t t ->
+ ('b, 'y, 'c) Model.Types.t t ->
+ ('x, 'y) Type.eq option
+(** [provably_equal p1 p2] is [Some Equal] iff [p1] and [p2] denote the same
+ field — same name — and their types match, which also proves that their
+ ['ocaml] representations are equal *)
+
+val same : ('a, 'x, 'c) Model.Types.t t -> ('b, 'y, 'c) Model.Types.t t -> bool
+(** [same p1 p2] is [true] iff [p1] and [p2] denote the same field with matching
+ types *)
diff --git a/lib/value/tests/load.t b/lib/value/tests/load.t
index e20860a..dee6d0f 100644
--- a/lib/value/tests/load.t
+++ b/lib/value/tests/load.t
@@ -1,7 +1,9 @@
High level test of [Value.load]: two rows are inserted in the table of the
content given as argument (the configuration is read on the standard input),
loaded as values with [Value.load] and printed with [Value.pp] — the primary
-key and every field, as read back as typed data with [Value.get].
+key and every field, as read back as typed data with [Value.get]. The first row
+is then loaded again with [~multivalued:false]: only the row itself is read, so
+the multivalued field [aliases] is missing from the output.
$ tests --load "entity" <<EOF
> {
@@ -38,4 +40,6 @@ key and every field, as read back as typed data with [Value.get].
{id = 1; aliases = [1, 2]; country = 1; name = "name-1"; year = 1}
row 2:
{id = 2; aliases = [2]; country = 1; name = "name-2"; year = 2}
+ row 1 (without multivalued fields):
+ {id = 1; country = 1; name = "name-1"; year = 1}
diff --git a/lib/value/value.ml b/lib/value/value.ml
index 7cd0e52..1b1112d 100644
--- a/lib/value/value.ml
+++ b/lib/value/value.ml
@@ -7,24 +7,25 @@ let none : value = Model.Data.any Model.Data.none
let single : ('db, 'ocaml, Model.target) Model.Types.t * 'ocaml -> value =
fun data -> Model.Data.any (Model.Data.v data)
-(** The key of the fields dictionary: an existential pack over the named type of
- a field, hiding its ['db] and ['ocaml] parameters *)
-type key =
- | Named : ('db, 'ocaml, Model.target) Model.Types.t Model.Types.named -> key
+module Property = Property
+
+(** The key of the fields dictionary: an existential pack over the property of a
+ field, hiding its ['db] and ['ocaml] parameters *)
+type key = Named : ('db, 'ocaml, Model.target) Model.Types.t Property.t -> key
module Key = struct
type t = key
- let equal : t -> t -> bool = fun (Named a) (Named b) -> Model.Types.same a b
- let hash : t -> int = fun (Named n) -> Hashtbl.hash (Model.Types.name n)
+ let equal : t -> t -> bool = fun (Named a) (Named b) -> Property.same a b
+ let hash : t -> int = fun (Named n) -> Hashtbl.hash (Property.name n)
end
-module Fields = Hashtbl.Make (Key)
+module Properties = Hashtbl.Make (Key)
type t = {
target : Model.target; (** The content or list whose table holds the row *)
id : int64; (** The primary key of the row *)
- fields : value Fields.t;
+ fields : value Properties.t;
(** Cache of the other fields and their values, keyed by their named type
— empty by default, populating it requires reading the database *)
}
@@ -32,58 +33,83 @@ type t = {
its {!Model.target} and its [id] *)
let v : Model.target -> int64 -> t =
- fun target id -> { target; id; fields = Fields.create 0 }
+ fun target id -> { target; id; fields = Properties.create 0 }
let target : t -> Model.target = fun t -> t.target
let id : t -> int64 = fun t -> t.id
-let add_field : type ocaml.
- t ->
- ('db, ocaml, Model.target) Model.Types.t Model.Types.named ->
- ocaml ->
- unit =
+let add_property : type ocaml.
+ t -> ('db, ocaml, Model.target) Model.Types.t Property.t -> ocaml -> unit =
fun t ref data ->
- let typ = Model.Types.typ ref in
- Fields.replace t.fields (Named ref)
+ let typ = Property.typ ref in
+ Properties.replace t.fields (Named ref)
(Model.Data.any (Model.Data.v (typ, data)))
-let id_ref : Model.target Model.Types.monoreference Model.Types.named =
- Model.Types.named ~name:Model.Identifier.id ~typ:Model.Types.Integer
+let id_ref : Model.target Model.Types.monoreference Property.t =
+ Property.named ~name:Model.Identifier.id ~typ:Model.Types.Integer
+
+(** Cast a stored field data to the ['ocaml] representation of [ref_typ], when
+ the stored type matches the requested one. The existential parameters of the
+ stored data are packed behind the locally abstract types of this function,
+ so that the pair equality of {!Model.Types.eq} can be decomposed inside *)
+let cast : type db ocaml db' ocaml'.
+ (db, ocaml, Model.target) Model.Types.t ->
+ (db', ocaml', Model.target) Model.Types.t ->
+ ocaml' ->
+ ocaml option =
+ fun ref_typ typ payload ->
+ match Model.Types.eq ref_typ typ with
+ | Some Type.Equal -> Some payload
+ | None -> None
let get : type ocaml.
- t -> ('db, ocaml, Model.target) Model.Types.t Model.Types.named -> ocaml =
+ t -> ('db, ocaml, Model.target) Model.Types.t Property.t -> ocaml =
fun t ref ->
- match Model.Types.provably_equal id_ref ref with
+ match Property.provably_equal id_ref ref with
| Some Type.Equal -> Some t.id
| None -> (
- match Fields.find_opt t.fields (Named ref) with
+ match Properties.find_opt t.fields (Named ref) with
| Some (Model.Data.Any (Model.Data.V (typ, payload))) -> (
- match Model.Types.eq_ocaml (Model.Types.typ ref) typ with
- | Some Type.Equal -> payload
+ match cast (Property.typ ref) typ payload with
+ | Some data -> data
| None -> raise Not_found)
| _ -> raise Not_found)
(** Read the row of [t] from the database and store each field in [t]'s
- dictionary — the value is modified in place *)
-let load' : Caqti_lwt.connection -> t -> unit Lwt.t =
- fun db t ->
+ dictionary — the value is modified in place. With [~multivalued:false], the
+ multivalued reference fields are not read *)
+let load' : Caqti_lwt.connection -> ?multivalued:bool -> t -> unit Lwt.t =
+ fun db ?(multivalued = true) t ->
let ( let* ) = Lwt.bind in
let* () =
match t.target with
| Model.Reference.Content ref ->
- Query.select db (id t) (Model.Reference.get ref)
- { f = (fun (named, data) -> add_field t named data) }
+ Query.select db ~multivalued (id t) (Model.Reference.get ref)
+ {
+ f =
+ (fun ((name, typ), data) ->
+ add_property t (Property.named ~name ~typ) data);
+ }
| Model.Reference.List list ->
- Query.select_list db (id t) list
- { f = (fun (named, data) -> add_field t named data) }
+ Query.select_list db ~multivalued (id t) list
+ {
+ f =
+ (fun ((name, typ), data) ->
+ add_property t (Property.named ~name ~typ) data);
+ }
in
Lwt.return_unit
-let load : Caqti_lwt.connection -> Model.target -> Int64.t -> t Lwt.t =
- fun db target id ->
+let load :
+ Caqti_lwt.connection ->
+ ?multivalued:bool ->
+ Model.target ->
+ Int64.t ->
+ t Lwt.t =
+ fun db ?(multivalued = true) target id ->
let ( let* ) = Lwt.bind in
let v = v target id in
- let* () = load' db v in
+ let* () = load' db ~multivalued v in
Lwt.return v
(** Pretty-print the data of a field, whatever its type *)
@@ -113,8 +139,8 @@ let pp_data : type db ocaml.
let pp : Format.formatter -> t -> unit =
fun fmt t ->
let fields =
- Fields.fold
- (fun (Named named) data acc -> (Model.Types.name named, data) :: acc)
+ Properties.fold
+ (fun (Named named) data acc -> (Property.name named, data) :: acc)
t.fields []
|> List.sort (fun (n1, _) (n2, _) -> Model.Identifier.compare n1 n2)
in
diff --git a/lib/value/value.mli b/lib/value/value.mli
index eb03965..0d00bf7 100644
--- a/lib/value/value.mli
+++ b/lib/value/value.mli
@@ -14,6 +14,10 @@ val single : ('db, 'ocaml, Model.target) Model.Types.t * 'ocaml -> value
['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].
@@ -35,40 +39,50 @@ val id : t -> int64
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 Model.Types.named
-(** The pre-built {!Model.Types.named} handle of the reserved field ["id"]
+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 Model.Types.named -> 'ocaml
+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 named type and the readable field is proved
- by {!Model.Types.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 -> Model.target -> Int64.t -> t Lwt.t
-(** [load 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.
+ 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 -> t -> unit Lwt.t
-(** [load' db v] reads the row of [v] from the database (one query for the
- table, plus one per multivalued reference field) 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 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