summaryrefslogtreecommitdiff
path: root/test/insert.ml
blob: 9133832ede11bc0d008db79970a9249e6ae8e447 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let name = __FILE__

let description =
  "Insert two rows of the given content with a generated provider, and display \
   the inserted rows. The configuration is read on the standard input"

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))

(** Insert one row of [content], and return its json representation together
    with one line per value of each multivalued reference field *)
let insert_one :
    Caqti_lwt.connection ->
    Model.content ->
    Int64.t ->
    (string * Int64.t * string) list Lwt.t =
 fun db content row ->
  let ( let* ) = Lwt.bind in
  let* id = Query.insert db content (Common.provider_for content row) in
  let content_id = Model.Identifier.to_string (Model.Content.id content) in
  let module Db = (val db) in
  let* json_result = Db.find (Common.content_as_json content) id in
  let* json = Caqti_lwt.or_fail json_result in
  let* ext = Common.collect_references db content id in
  Lwt.return ((content_id, id, json) :: List.rev ext)

let exec : string list -> t = function
  | [ content_id ] ->
      Common.exec_with_conf (fun configuration db ->
          let content =
            Option.get
              (Configuration.Ast.get_content configuration
                 (Model.Identifier.of_string content_id))
          in
          let ( let* ) = Lwt.bind in
          let* lines1 = insert_one db content 1L in
          let* lines2 = insert_one db content 2L in
          Lwt.return (lines1 @ lines2))
  | _ -> failwith "Invalid arguments"