summaryrefslogtreecommitdiff
path: root/test/insert.ml
diff options
context:
space:
mode:
Diffstat (limited to 'test/insert.ml')
-rw-r--r--test/insert.ml186
1 files changed, 186 insertions, 0 deletions
diff --git a/test/insert.ml b/test/insert.ml
new file mode 100644
index 0000000..eaa132f
--- /dev/null
+++ b/test/insert.ml
@@ -0,0 +1,186 @@
+open StdLabels
+
+let name = __FILE__
+let description = "Insert data and check the result"
+let speclist = []
+
+type t =
+ ((string * Int64.t * string) list, Configuration.Read_conf.error) result
+
+let pp : Format.formatter -> t -> unit =
+ Format.pp_print_result ~error:Configuration.Read_conf.pp_error
+ ~ok:
+ (Format.pp_print_list ~pp_sep:Format.pp_print_cut
+ (fun formatter (content_id, id, content) ->
+ Format.fprintf formatter "@[<v 2>%s:%Ld@;%s@]" content_id id content))
+
+(** Read the values to insert in the configuration *)
+module Make (S : Decoders.Decode.S) = struct
+ let ( let* ) = S.( let* )
+ let ( let+ ) = S.( let+ )
+
+ type value = Model.Reference.t Model.Typ.value
+
+ (** The values are encoded in a very simple way, depending of the nature of
+ the field in the json, we create a property in for text or numeric *)
+ let value : value S.decoder =
+ S.one_of
+ [
+ ( "numeric",
+ S.map (fun v -> Model.Typ.(V (Integer, Int64.of_int v))) S.int );
+ ("text", S.map (fun v -> Model.Typ.(V (Text, v))) S.string);
+ ("other", S.map (fun _ -> Model.Typ.None) S.value);
+ ]
+
+ type content = Model.Reference.t Model.Content.t
+ type properties = (string * value) list
+ type content_to_insert = content * properties list
+
+ let rec properties :
+ Configuration.Ast.setting ->
+ string ->
+ (content * (properties * content_to_insert list) list) S.decoder =
+ fun conf name ->
+ (* We are loading the same configuration template, the content is supposed
+ to exists by definition and will never be None *)
+ let content = Configuration.Ast.get_content conf name |> Option.get in
+ let* properties =
+ S.field_opt_or ~default:[] "values" (S.list (S.key_value_pairs value))
+ in
+ let* subcontents =
+ S.field_opt_or ~default:[] "values" (S.list (subcontents conf))
+ in
+ let to_insert = List.combine properties subcontents in
+ S.succeed (content, to_insert)
+
+ and subcontents :
+ Configuration.Ast.setting -> content_to_insert list S.decoder =
+ fun conf ->
+ let* properties =
+ S.key_value_pairs_seq (fun key ->
+ S.one_of
+ [
+ ( "subcontent",
+ fun v ->
+ match Configuration.Ast.get_content conf key with
+ | None -> Error (Decoders.Error.make "no content")
+ | Some subcontent ->
+ let res = S.list (S.key_value_pairs value) v in
+ let v = Result.map (fun v -> Some (subcontent, v)) res in
+ v );
+ ("other", fun _ -> Ok None);
+ ])
+ in
+ let properties = List.filter_map ~f:(fun v -> v) properties in
+ S.succeed properties
+
+ let content :
+ Configuration.Ast.setting ->
+ (content * (properties * content_to_insert list) list) S.decoder =
+ fun conf ->
+ let* name = S.field "id" S.string in
+ properties conf name
+
+ let configuration :
+ Configuration.Ast.setting ->
+ (content * (properties * content_to_insert list) list) list S.decoder =
+ fun conf -> S.field "content" (S.list (content conf))
+end
+
+module D = Make (Configuration.Json5.Decode)
+
+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
+
+let query table =
+ 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 insert_and_get_result ?parent db content entry =
+ let ( let* ) = Lwt.bind in
+ let* id =
+ Query.insert ?parent db content (fun v ->
+ match List.assoc_opt v entry with Some v -> v | None -> Model.Typ.None)
+ 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 = Caqti_lwt.or_fail json_result in
+ Lwt.return (content_id, id, json)
+
+let exec : string list -> t =
+ fun _ ->
+ let stdin = In_channel.input_all stdin in
+ let ( let* ) = Result.bind in
+ let* configuration = Configuration.Read_conf.read_string_conf stdin in
+ let* inserts =
+ Configuration.Json5.Decode.decode_string
+ (D.configuration configuration)
+ stdin
+ in
+ Lwt_main.run
+ (let ( let* ) = Lwt.bind in
+ let* db = Common.connect configuration in
+ let module Db = (val db) in
+ let* insertions =
+ Lwt_list.fold_left_s
+ (fun acc (content, entries) ->
+ Lwt_list.fold_left_s
+ (fun acc (entry, subcontents) ->
+ let* content_id, id, json =
+ insert_and_get_result db content entry
+ in
+
+ let acc = (content_id, id, json) :: acc in
+
+ (* Insert the subcontent associated *)
+ Lwt_list.fold_left_s
+ (fun acc (subcontent, entries) ->
+ let* acc =
+ Lwt_list.fold_left_s
+ (fun acc entry ->
+ let* subcontent_id, id, json =
+ insert_and_get_result ~parent:id db subcontent entry
+ in
+ Lwt.return ((subcontent_id, id, json) :: acc))
+ acc entries
+ in
+
+ Lwt.return acc)
+ acc subcontents)
+ acc entries)
+ [] inserts
+ in
+ Lwt.return_ok (List.rev insertions))