diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-21 11:23:41 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-21 11:23:41 +0100 |
| commit | ce13c93cf14cb71894481b0303a7058a72a6f4ce (patch) | |
| tree | bd93186a65f6645cb4401f188d266d743cc2fde7 /lib/value/value.ml | |
| parent | 172a02b80d8dbdcfe09129d7fa76015bb5ec4d79 (diff) | |
Load values from the database
Diffstat (limited to 'lib/value/value.ml')
| -rw-r--r-- | lib/value/value.ml | 104 |
1 files changed, 95 insertions, 9 deletions
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 |
