summaryrefslogtreecommitdiff
path: root/test/load.ml
blob: e992e32033010200d9e8ea58aae3826d1129b2de (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
49
50
let name = __FILE__

let description =
  "Insert two rows of the given content, load them as values with [Value.load] \
   and print them with [Value.pp]; then load the first row again with \
   [~multivalued:false], which reads only the row itself. The configuration is \
   read on the standard input"

let speclist () = []

type 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 Format.pp_print_string)

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* id1 = Query.insert db content (Common.provider_for content 1L) in
          let* id2 = Query.insert db content (Common.provider_for content 2L) in
          let load_one id =
            let* v = Value.load db (Model.Reference.of_content content) id in
            Lwt.return
              [ Format.sprintf "row %Ld:" id; Format.asprintf "%a" Value.pp v ]
          in
          let load_one_partial id =
            let* v =
              Value.load db ~multivalued:false
                (Model.Reference.of_content content)
                id
            in
            Lwt.return
              [
                Format.sprintf "row %Ld (without multivalued fields):" id;
                Format.asprintf "%a" Value.pp v;
              ]
          in
          let* lines1 = load_one id1 in
          let* lines2 = load_one id2 in
          let* lines3 = load_one_partial id1 in
          Lwt.return (lines1 @ lines2 @ lines3))
  | _ -> failwith "Invalid arguments"