diff options
| author | Sébastien Dailly <sebastien@dailly.me> | 2026-11-22 13:58:16 +0100 |
|---|---|---|
| committer | Sébastien Dailly <sebastien@dailly.me> | 2026-11-22 13:58:16 +0100 |
| commit | 056fcabaeec238a7aa927df39a90acea028b1aa0 (patch) | |
| tree | 3246359561eb2375e85da4537e05a083a1348467 /lib/value/value.ml | |
| parent | bc78bea63b7c75e8a9d222dc86fb75ed293a2242 (diff) | |
Added an option for loading a value content without the multivaluated fields (allow single query)main
Diffstat (limited to 'lib/value/value.ml')
| -rw-r--r-- | lib/value/value.ml | 96 |
1 files changed, 61 insertions, 35 deletions
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 |
