From 960d5b8f2728f71dd9ce038d527844d82ca2f0f2 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 30 Apr 2026 09:16:03 +0200 Subject: Allow updates for existing contents --- test/common.ml | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'test/common.ml') diff --git a/test/common.ml b/test/common.ml index 807289a..f06d431 100644 --- a/test/common.ml +++ b/test/common.ml @@ -11,3 +11,89 @@ let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t = let module Db = (val db) in let* () = Query.create db conf in Lwt.return db + +(** Load the given content and return the values as a json *) +let content_as_json : + 'a Model.Content.t -> (int64, string, [ `One ]) Caqti_request.t = + fun content -> + let fields = + Model.Content.fields content + (* Remove the multivaluated fields from the query *) + |> Seq.filter (fun t -> + let (Model.Field.T ty) = Model.Field.type_ t in + match ty with Model.Typ.MultiReference _ -> false | _ -> true) + in + let buff = Buffer.create 64 in + let () = + Seq.iter + (fun field -> + Buffer.add_char buff '\''; + Buffer.add_string buff (Model.Field.name field); + + Buffer.add_string buff "',"; + Buffer.add_string buff (Model.Field.name field); + Buffer.add_string buff ",") + fields + in + + let () = + match Model.Content.parent content with + | None -> + let length = Buffer.length buff in + if length > 1 then Buffer.truncate buff (length - 1); + () + | Some _ -> Buffer.add_string buff "'parent',parent" + in + + let q = Buffer.contents buff in + Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string) + ~oneshot:true + ("SELECT json_object(" ^ q ^ ") from " ^ Model.Content.id content + ^ " where id=?") + +let query_values (table : Model.Reference.t Model.Content.t) (feature : string) + = + Caqti_request.Infix.(Caqti_type.(int64) ->* Caqti_type.string) + ~oneshot:true + ("SELECT json_object('" ^ feature ^ "', target) from \"" + ^ Model.Content.id table ^ "." ^ feature ^ "\" where parent=?") + +(** Query all the multireferences tables for the given content and the given id. + Collect all the values and return them in the same json format as the same + content. *) +let collect_references : + Caqti_lwt.connection -> + Model.Reference.t Model.Content.t -> + int64 -> + (string * int64 * string) list Lwt.t = + fun db content id -> + let module Db = (val db) in + let ( let* ) = Lwt.bind in + let content_id = Model.Content.id content in + + (* Only keep the multivaluated fields *) + let fields = + Seq.filter + (fun field -> + let (T ty) = Model.Field.type_ field in + match ty with Model.Typ.MultiReference _ -> true | _ -> false) + (Model.Content.fields content) + |> List.of_seq + in + let results = + Lwt_list.fold_left_s + (fun acc field -> + let field_name = Model.Field.name field in + let* json_result = + Db.collect_list (query_values content field_name) id + in + let* json = Caqti_lwt.or_fail json_result in + let line = + ListLabels.fold_left ~init:acc json ~f:(fun acc json -> + (content_id, id, json) :: acc) + in + Lwt.return line) + [] fields + in + + results -- cgit v1.2.3