summaryrefslogtreecommitdiff
path: root/lib/value
diff options
context:
space:
mode:
Diffstat (limited to 'lib/value')
-rw-r--r--lib/value/data.ml24
-rw-r--r--lib/value/data.mli45
-rw-r--r--lib/value/dune3
-rw-r--r--lib/value/tests/dune11
-rw-r--r--lib/value/tests/load.t41
-rw-r--r--lib/value/value.ml104
-rw-r--r--lib/value/value.mli36
7 files changed, 173 insertions, 91 deletions
diff --git a/lib/value/data.ml b/lib/value/data.ml
deleted file mode 100644
index f0e321e..0000000
--- a/lib/value/data.ml
+++ /dev/null
@@ -1,24 +0,0 @@
-(** The data carried by a field of a value.
-
- [None] tells the field is empty (or left as is, on update), [V] carries a
- single value together with the type of its field, and [Seq] carries a
- sequence of references for a multivalued field.
-
- The type is parameterized over the type of the data held (['a]: the DB
- representation of the field, e.g. [string option] or [int64 option]) and
- over ['t], the target of the field for references — a reference is held as
- an [int64] (the id of the target row), while ['t] records what it points to.
- The field type {!Model.Typ.t} stays in the model package; only the data
- itself lives here. *)
-type ('ocaml, 't) data =
- | None
- | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data
-
-let none : ('ocaml, 't) data = None
-
-let v : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data =
- fun (typ, value) -> V (typ, value)
-
-type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed]
-
-let any : ('ocaml, 't) data -> 't any = fun data -> Any data
diff --git a/lib/value/data.mli b/lib/value/data.mli
deleted file mode 100644
index 7b7a5cc..0000000
--- a/lib/value/data.mli
+++ /dev/null
@@ -1,45 +0,0 @@
-(** The data carried by a field of a value.
-
- [none] tells the field is empty (or left as is, on update), and [v] carries
- the value of the field together with its type — a single value, or a
- sequence of references for a multivalued field.
-
- The type is parameterized over ['ocaml], the in-code representation of the
- data held (e.g. [string option], [int64 option], or [int64 Seq.t] for a
- multivalued reference), and over ['t], the target of the field for
- references. The DB representation ['db] appears only inside the field type
- carried by the constructors. The field type {!Model.Typ.t} stays in the
- model package; only the data itself lives here. *)
-
-type ('ocaml, 't) data = private
- | None
- | V : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data
-
-val none : ('ocaml, 't) data
-(** [none] tells the field is empty in the database (or left as is, on update)
-*)
-
-val v : ('db, 'ocaml, 't) Model.Typ.t * 'ocaml -> ('ocaml, 't) data
-(** [v (typ, value)] is the value for the field, in its code representation
- ['ocaml] — e.g. a single [string option], or an [int64 Seq.t] for a
- multivalued reference. The field type [typ] is also reported: it ties the
- payload to the field and determines the result type. Whether the value is a
- sequence is carried by the type, not by a separate constructor. *)
-
-(** Existential pack over the held type: the data of a field whose type is not
- known statically (e.g. the data of any field of a content, as returned by a
- provider function). Matchable from the outside, but built with [any]. *)
-type 't any = Any : ('ocaml, 't) data -> 't any [@@unboxed]
-
-val any : ('ocaml, 't) data -> 't any
-(** Pack a typed [data] into an [any], hiding the type of the data held *)
-
-(** Example for inserting data — build the data of each field, then pack it:
- [
- (* Function providing the data of each field to insert in the database *)
-
- let f : string -> Model.target any = function
- | "siret" -> any (v (Model.Typ.Text, Some "siret"))
- | "name" -> any (v (Model.Typ.Text, Some "name"))
- | _ -> any none
- ] *)
diff --git a/lib/value/dune b/lib/value/dune
index bcfb9a8..761cdf7 100644
--- a/lib/value/dune
+++ b/lib/value/dune
@@ -1,4 +1,3 @@
(library
(name value)
- (libraries
- model))
+ (libraries model caqti-lwt lwt query))
diff --git a/lib/value/tests/dune b/lib/value/tests/dune
new file mode 100644
index 0000000..c5913da
--- /dev/null
+++ b/lib/value/tests/dune
@@ -0,0 +1,11 @@
+; Add the test library in the path
+
+(env
+ (_
+ (binaries %{project_root}/test/tests.exe)))
+
+; And depend of it. The binary will be available in the
+; cram tests under the name "tests"
+
+(cram
+ (deps %{bin:tests}))
diff --git a/lib/value/tests/load.t b/lib/value/tests/load.t
new file mode 100644
index 0000000..e20860a
--- /dev/null
+++ b/lib/value/tests/load.t
@@ -0,0 +1,41 @@
+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].
+
+ $ tests --load "entity" <<EOF
+ > {
+ > "list": [
+ > {
+ > "id": "country_list",
+ > "field": [
+ > { "name": "code", "label": {}, "type": "text" }
+ > ]
+ > }
+ > ],
+ > "content": [
+ > {
+ > "id": "entity",
+ > "section": [
+ > {
+ > "field": [
+ > { "name": "name", "label": {}, "type": "text" },
+ > { "name": "year", "label": {}, "type": "integer" },
+ > { "name": "country", "label": {}, "type": { "content": "country_list" } },
+ > {
+ > "name": "aliases",
+ > "label": {},
+ > "type": { "content": "country_list", "multivaluated": true }
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > ]
+ > }
+ > EOF
+ row 1:
+ {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}
+
diff --git a/lib/value/value.ml b/lib/value/value.ml
index 84bed4c..ab5b5a7 100644
--- a/lib/value/value.ml
+++ b/lib/value/value.ml
@@ -1,19 +1,32 @@
-module Data = Data
-(** The data carried by a field: {!Data.data}, parameterized over the type of
- the data held, built with the smart constructors [Data.none], [Data.v] *)
-
-type value = Model.target Data.any
+type value = Model.target Model.Data.any
(** A resolved field data: the data of a field of a content or a list, whatever
the type of the data held *)
-let none : value = Data.any Data.none
+let none : value = Model.Data.any Model.Data.none
let single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value =
- fun data -> Data.any (Data.v data)
+ 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.Typ.t Model.Typ.named -> key
+
+module Key = struct
+ type t = key
+
+ let equal : t -> t -> bool = fun (Named a) (Named b) -> Model.Typ.same a b
+ let hash : t -> int = fun (Named n) -> Hashtbl.hash (Model.Typ.name n)
+end
+
+module Fields = Hashtbl.Make (Key)
type 'a t = {
content : 'a; (** The content or list whose table holds the row *)
id : int64; (** The primary key of the row *)
+ fields : value Fields.t;
+ (** Cache of the other fields and their values, keyed by their named type
+ — empty by default, populating it requires reading the database *)
}
(** A value: one row of a content or list table in the database.
@@ -21,10 +34,22 @@ type 'a t = {
the row: [Model.content] for contents ([Model.content Value.t]) and
[Model.list] for lists ([Model.list Value.t]). *)
-let v : 'a -> int64 -> 'a t = fun content id -> { content; id }
+let v : 'a -> int64 -> 'a t =
+ fun content id -> { content; id; fields = Fields.create 0 }
+
let content : 'a t -> 'a = fun t -> t.content
let id : 'a t -> int64 = fun t -> t.id
+let add_field : type ocaml.
+ 'a t ->
+ ('db, ocaml, Model.target) Model.Typ.t Model.Typ.named ->
+ ocaml ->
+ unit =
+ fun t ref data ->
+ let typ = Model.Typ.typ ref in
+ Fields.replace t.fields (Named ref)
+ (Model.Data.any (Model.Data.v (typ, data)))
+
let id_ref : Model.target Model.Typ.monoreference Model.Typ.named =
Model.Typ.named ~name:Model.Identifier.id ~typ:Model.Typ.Integer
@@ -33,4 +58,65 @@ let get : type ocaml.
fun t ref ->
match Model.Typ.provably_equal id_ref ref with
| Some Type.Equal -> Some t.id
- | None -> raise Not_found
+ | None -> (
+ match Fields.find_opt t.fields (Named ref) with
+ | Some (Model.Data.Any (Model.Data.V (typ, payload))) -> (
+ match Model.Typ.eq_ocaml (Model.Typ.typ ref) typ with
+ | Some Type.Equal -> payload
+ | None -> raise Not_found)
+ | _ -> raise Not_found)
+
+let load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t =
+ fun db v ->
+ let ( let* ) = Lwt.bind in
+ let* () =
+ Query.select db v.id v.content
+ { f = (fun (named, data) -> add_field v named data) }
+ in
+ Lwt.return_unit
+
+(** Pretty-print the data of a field, whatever its type *)
+let pp_data : type db ocaml.
+ Format.formatter -> (db, ocaml, Model.target) Model.Typ.t -> ocaml -> unit =
+ fun fmt typ data ->
+ let pp_option pp fmt data =
+ match data with
+ | Some v -> pp fmt v
+ | None -> Format.pp_print_string fmt "NULL"
+ in
+ match typ with
+ | Model.Typ.Text ->
+ pp_option (fun fmt s -> Format.fprintf fmt "%S" s) fmt data
+ | Model.Typ.Integer ->
+ pp_option (fun fmt i -> Format.fprintf fmt "%Ld" i) fmt data
+ | Model.Typ.MonoReference _ ->
+ pp_option (fun fmt i -> Format.fprintf fmt "%Ld" i) fmt data
+ | Model.Typ.MultiReference _ ->
+ Format.fprintf fmt "[%a]"
+ (Format.pp_print_list
+ ~pp_sep:(fun fmt () -> Format.pp_print_string fmt ", ")
+ (fun fmt i -> Format.fprintf fmt "%Ld" i))
+ (List.of_seq data)
+
+let pp : Format.formatter -> 'a t -> unit =
+ fun fmt t ->
+ let fields =
+ Fields.fold
+ (fun (Named named) data acc -> (Model.Typ.name named, data) :: acc)
+ t.fields []
+ |> List.sort (fun (n1, _) (n2, _) -> Model.Identifier.compare n1 n2)
+ in
+ let pp_field fmt (name, data) =
+ let (Model.Data.Any d) = data in
+ match d with
+ | Model.Data.None ->
+ Format.fprintf fmt "%s = NULL" (Model.Identifier.to_string name)
+ | Model.Data.V (typ, payload) ->
+ Format.fprintf fmt "%s = " (Model.Identifier.to_string name);
+ pp_data fmt typ payload
+ in
+ Format.fprintf fmt "{@[id = %Ld;@ %a@]}" t.id
+ (Format.pp_print_list
+ ~pp_sep:(fun fmt () -> Format.pp_print_string fmt "; ")
+ pp_field)
+ fields
diff --git a/lib/value/value.mli b/lib/value/value.mli
index 5c493aa..5a8984d 100644
--- a/lib/value/value.mli
+++ b/lib/value/value.mli
@@ -2,15 +2,9 @@
A value is one row of a content or list table in the database. *)
-module Data = Data
-(** The data carried by a field: {!Data.data}, parameterized over the type of
- the data held, built with the smart constructors ([Data.none], [Data.v]);
- and {!Data.any}, the existential pack used when the held type is not known
- statically *)
-
-type value = Model.target Data.any
+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 {!Data.data} *)
+ held: an existential pack over {!Model.Data.data} *)
val none : value
(** [none]: the field is empty (or left as is, on update) *)
@@ -54,6 +48,26 @@ val get :
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.Typ.provably_equal} on name and type. Only the reserved field
- [id_ref] is readable for now; the other fields are read from the database
- (not implemented yet) and raise [Not_found]. *)
+ by {!Model.Typ.provably_equal} on name and type. The reserved field [id_ref]
+ is always readable; the other fields are read from the value's dictionary
+ (see {!add_field}) and raise [Not_found] when absent. *)
+
+val load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t
+(** [load db v] reads the row of [v] in the database (one query for the content
+ table, plus one per multivalued reference field) and stores each field in
+ [v]'s dictionary with {!add_field}, so that {!get} can then read every field
+ as typed data.
+
+ The value is modified in place — the dictionary is part of its state — so
+ the function returns [unit]. The dictionary is empty when the value is built
+ with {!v}: [load] is what populates it. The primary key needs no loading —
+ {!get} with {!id_ref} reads it directly from the value. *)
+
+val pp : Format.formatter -> 'a 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 content or list the value belongs to is not printed — its type ['a] is
+ abstract here — and neither are the fields not loaded yet: only what {!load}
+ (or {!add_field}) put in the dictionary is shown. *)