summaryrefslogtreecommitdiff
path: root/test/insert.ml
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2026-04-30 09:16:03 +0200
committerSébastien Dailly <sebastien@dailly.me>2026-05-06 16:45:25 +0200
commit960d5b8f2728f71dd9ce038d527844d82ca2f0f2 (patch)
tree9a15dc43f91a0b58a3aaa10223bba35abf7ef5f2 /test/insert.ml
parenta5efd7d15b4d049a4ed539ed5d9c2f1037d00973 (diff)
Allow updates for existing contents
Diffstat (limited to 'test/insert.ml')
-rw-r--r--test/insert.ml93
1 files changed, 6 insertions, 87 deletions
diff --git a/test/insert.ml b/test/insert.ml
index 70b98d6..bc65422 100644
--- a/test/insert.ml
+++ b/test/insert.ml
@@ -2,7 +2,7 @@ open StdLabels
let name = __FILE__
let description = "Insert data and check the result"
-let speclist = []
+let speclist () = []
type t =
((string * Int64.t * string) list, Configuration.Read_conf.error) result
@@ -14,53 +14,7 @@ let pp : Format.formatter -> t -> unit =
(fun formatter (content_id, id, content) ->
Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
-let query_columns : 'a Model.Content.t -> string =
- 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
-
- Buffer.contents buff
-
type content = Model.Reference.t Model.Content.t
-
-let query (table : content) =
- Caqti_request.Infix.(Caqti_type.(int64) ->! Caqti_type.string)
- ~oneshot:true
- ("SELECT json_object(" ^ query_columns table ^ ") from "
- ^ Model.Content.id table ^ " where id=?")
-
-let query_values (table : content) (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=?")
-
type value = Model.Reference.t Model.Typ.value
type properties = (string * value) list
@@ -83,7 +37,7 @@ let insert_and_get_result :
in
let content_id = Model.Content.id content in
let module Db = (val db) in
- let* json_result = Db.find (query content) id in
+ let* json_result = Db.find (Common.content_as_json content) id in
let* json = Caqti_lwt.or_fail json_result in
Lwt.return (content_id, id, json)
@@ -258,43 +212,6 @@ end
module D = Make (Configuration.Json5.Decode)
-(** 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 -> content -> int64 -> 'a 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 =
- List.fold_left ~init:acc json ~f:(fun acc json ->
- (content_id, id, json) :: acc)
- in
- Lwt.return line)
- [] fields
- in
-
- results
-
let exec : string list -> t =
fun _ ->
let stdin = In_channel.input_all stdin in
@@ -324,7 +241,7 @@ let exec : string list -> t =
let acc = (content_id, id, json) :: acc in
(* Also query the multivaluated references *)
- let* ext = collect_references db content id in
+ let* ext = Common.collect_references db content id in
let acc = List.append ext acc in
(* Insert the subcontent associated *)
Lwt_list.fold_left_s
@@ -339,7 +256,9 @@ let exec : string list -> t =
let acc = (subcontent_id, id, json) :: acc in
(* Also query the multivaluated references *)
- let* ext = collect_references db subcontent id in
+ let* ext =
+ Common.collect_references db subcontent id
+ in
let acc = List.append ext acc in
Lwt.return acc)
acc entries