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/query.ml | 1 + lib/query/query.mli | 5 +++++ lib/query/select.ml | 47 +++++++++++++++++++++++++++++++++-------------- lib/query/select.mli | 6 ++++++ 4 files changed, 45 insertions(+), 14 deletions(-) (limited to 'lib/query') 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. *) -- cgit v1.2.3