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/query/select.ml | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'lib/query/select.ml') 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 -- cgit v1.2.3