From 9f2a57603dbfce428af36e82fb1c7cdc6e1ad0da Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Sat, 21 Nov 2026 12:09:42 +0100 Subject: Load works for list and content values --- lib/model/reference.ml | 15 ++++++----- lib/model/reference.mli | 3 +++ lib/query/query.ml | 1 + lib/query/query.mli | 5 ++++ lib/query/select.ml | 47 +++++++++++++++++++++++---------- lib/query/select.mli | 6 +++++ lib/value/value.ml | 51 ++++++++++++++++++++--------------- lib/value/value.mli | 70 ++++++++++++++++++++++++++----------------------- 8 files changed, 124 insertions(+), 74 deletions(-) (limited to 'lib') diff --git a/lib/model/reference.ml b/lib/model/reference.ml index bdacf54..38d4346 100644 --- a/lib/model/reference.ml +++ b/lib/model/reference.ml @@ -6,12 +6,15 @@ type t = T : target Content.t Lazy.t -> t [@@unboxed] (** The target of a reference field, resolved: either a lazy pointer to a content, or a static list. The list case carries the list itself, whose own - fields are typed with this very [target] — references inside a list can - only target other lists (enforced by the configuration reader). *) -and target = - | Content of t - | List of target Data_list.t + fields are typed with this very [target] — references inside a list can only + target other lists (enforced by the configuration reader). *) +and target = Content of t | List of target Data_list.t let v : (unit -> target Content.t) -> t = fun f -> T (lazy (f ())) - let get : t -> target Content.t = fun (T target) -> Lazy.force target + +(** Reify an already resolved content as a {!target} — the lazy pointer is + created on the fly and only forced when needed (e.g. to read the row back) +*) +let of_content : target Content.t -> target = + fun content -> Content (v (fun () -> content)) diff --git a/lib/model/reference.mli b/lib/model/reference.mli index 2f9659b..09f70c5 100644 --- a/lib/model/reference.mli +++ b/lib/model/reference.mli @@ -8,3 +8,6 @@ and target = val v : (unit -> target Content.t) -> t val get : t -> target Content.t + +val of_content : target Content.t -> target +(** Reify an already resolved content as a {!target} *) diff --git a/lib/query/query.ml b/lib/query/query.ml index 8d8006c..df842bd 100644 --- a/lib/query/query.ml +++ b/lib/query/query.ml @@ -10,3 +10,4 @@ let insert_list = Insert.insert_list let create = Create.create let update = Update.q let select = Select.select +let select_list = Select.select_list diff --git a/lib/query/query.mli b/lib/query/query.mli index ae7245e..5e42f09 100644 --- a/lib/query/query.mli +++ b/lib/query/query.mli @@ -47,3 +47,8 @@ val select : 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] *) + +val select_list : + Caqti_lwt.connection -> 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] *) diff --git a/lib/query/select.ml b/lib/query/select.ml index 011544c..ed7fd0b 100644 --- a/lib/query/select.ml +++ b/lib/query/select.ml @@ -150,24 +150,29 @@ let select_multireference : Lwt.return_unit | _ -> raise Encoder.TypeMismatch -let select : - Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t = - fun db id content k -> +(** 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. *) +let select_row : + Caqti_lwt.connection -> + table:Model.Identifier.t -> + ?has_parent:bool -> + Model.field list -> + Int64.t -> + callback -> + unit Lwt.t = + fun db ~table ?(has_parent = false) fields id k -> let module Db = (val db) in let module Identifier = Model.Identifier in - let table = Model.Content.id content in - (* The [parent] column of a subcontent is exposed as a field, like on - insert *) let init_fields = - match Model.Content.parent content with - | None -> [] - | Some _ -> - [ - Model.Field.create (Model.Field.T Model.Typ.Integer) [] ~required:true - Model.Identifier.parent; - ] + if has_parent then + [ + Model.Field.create (Model.Field.T Model.Typ.Integer) [] ~required:true + Model.Identifier.parent; + ] + else [] in - let fields = init_fields @ List.of_seq (Model.Content.fields content) in + let fields = init_fields @ fields in let is_multireference field = match Model.Field.type_ field with | Model.Field.T (Model.Typ.MultiReference _) -> true @@ -204,3 +209,17 @@ let select : Lwt_list.iter_s (fun field -> select_multireference db table id field k) multi_fields + +let select : + Caqti_lwt.connection -> Int64.t -> Model.content -> callback -> unit Lwt.t = + fun db id content k -> + select_row db ~table:(Model.Content.id content) + ~has_parent: + (match Model.Content.parent content with None -> false | Some _ -> true) + (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 diff --git a/lib/query/select.mli b/lib/query/select.mli index 338cef8..f93455a 100644 --- a/lib/query/select.mli +++ b/lib/query/select.mli @@ -28,3 +28,9 @@ val select : 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. *) + +val select_list : + Caqti_lwt.connection -> 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. *) 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 diff --git a/lib/value/value.mli b/lib/value/value.mli index 5a8984d..9ebbca6 100644 --- a/lib/value/value.mli +++ b/lib/value/value.mli @@ -14,35 +14,33 @@ val single : ('db, 'ocaml, Model.target) Model.Typ.t * 'ocaml -> value ['ocaml] — a single value, or an [int64 Seq.t] for a multivalued reference — together with the type [typ] of the field *) -type 'a t -(** A value: a row of the table of the content or list ['a], identified by its - [id]. +type t +(** A value: a row of a content or list table in the database, identified by its + {!Model.target} and its [id]. - The 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]). + The type is abstract: a value is built with [v] (empty), [load] (built and + fully read from the database) or completed later with [load'], and inspected + with [target] and [id]. *) - The type is abstract: a value is built with [v] and inspected with [content] - and [id]. *) +val v : Model.target -> int64 -> t +(** Build a value from its target (a content or a static list) and its row id. + The fields dictionary is left empty — use {!load'} to read the fields from + the database. *) -val v : 'a -> int64 -> 'a t -(** Build a value from its content or list and its row id *) +val target : t -> Model.target +(** The content or static list the value belongs to *) -val content : 'a t -> 'a -(** The content or list the value belongs to *) - -val id : 'a t -> int64 -[@@deprecated - "Use [Value.get v Value.id_ref] to read the primary key as typed data"] -(** The primary key of the value in its table *) +val id : t -> int64 +(** The primary key of the value in its table — a direct field access, cheaper + 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.Typ.monoreference Model.Typ.named (** The pre-built {!Model.Typ.named} 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 : - 'a t -> ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> 'ocaml +val get : t -> ('db, 'ocaml, Model.target) Model.Typ.t Model.Typ.named -> '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]. @@ -50,24 +48,30 @@ val get : The match between the requested named type and the readable field is proved 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. *) + (populated by {!load} or {!load'}) 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. +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 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. *) + 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 pp : Format.formatter -> 'a t -> unit +val pp : Format.formatter -> 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. *) + The target of the value is not printed, and neither are the fields not + loaded yet: only what {!load} or {!load'} put in the dictionary is shown. *) -- cgit v1.2.3