diff options
Diffstat (limited to 'lib/value/value.ml')
| -rw-r--r-- | lib/value/value.ml | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/lib/value/value.ml b/lib/value/value.ml index ab5b5a7..7a3a4c2 100644 --- a/lib/value/value.ml +++ b/lib/value/value.ml @@ -21,30 +21,25 @@ end module Fields = Hashtbl.Make (Key) -type 'a t = { - content : 'a; (** The content or list whose table holds the row *) +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; (** 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. +(** A value: one row of a content or list table in the database, identified by + its {!Model.target} and its [id] *) - The type parameter ['a] is the content or the static list whose table holds - the row: [Model.content] for contents ([Model.content Value.t]) and - [Model.list] for lists ([Model.list Value.t]). *) +let v : Model.target -> int64 -> t = + fun target id -> { target; id; fields = Fields.create 0 } -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 target : t -> Model.target = fun t -> t.target +let id : 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 = + 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) @@ -54,7 +49,7 @@ let id_ref : Model.target Model.Typ.monoreference Model.Typ.named = Model.Typ.named ~name:Model.Identifier.id ~typ:Model.Typ.Integer let get : type ocaml. - 'a t -> ('db, ocaml, Model.target) Model.Typ.t Model.Typ.named -> ocaml = + t -> ('db, ocaml, Model.target) Model.Typ.t Model.Typ.named -> ocaml = fun t ref -> match Model.Typ.provably_equal id_ref ref with | Some Type.Equal -> Some t.id @@ -66,15 +61,29 @@ let get : type ocaml. | None -> raise Not_found) | _ -> raise Not_found) -let load : Caqti_lwt.connection -> Model.content t -> unit Lwt.t = - fun db v -> +(** 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 -> let ( let* ) = Lwt.bind in let* () = - Query.select db v.id v.content - { f = (fun (named, data) -> add_field v named data) } + 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) } + | Model.Reference.List list -> + Query.select_list db (id t) list + { f = (fun (named, data) -> add_field t named data) } in Lwt.return_unit +let load : Caqti_lwt.connection -> Model.target -> Int64.t -> t Lwt.t = + fun db target id -> + let ( let* ) = Lwt.bind in + let v = v target id in + let* () = load' db v in + Lwt.return v + (** 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 = @@ -98,7 +107,7 @@ let pp_data : type db ocaml. (fun fmt i -> Format.fprintf fmt "%Ld" i)) (List.of_seq data) -let pp : Format.formatter -> 'a t -> unit = +let pp : Format.formatter -> t -> unit = fun fmt t -> let fields = Fields.fold |
