blob: 6223846972f8977a143f7c37208dc979be62da90 (
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
(** Common fuctions shared between the tests *)
let ( let* ) = Lwt.bind
(** Connect to the database in memory and create a schema matching the given
configuration *)
let connect : Configuration.Ast.setting -> Caqti_lwt.connection Lwt.t =
fun conf ->
(* Pin the driver tweaks below 1.8 so that the sqlite3 driver does not turn
on PRAGMA foreign_keys: the tests insert references to rows that are not
present in the referenced table. *)
let config = Caqti.Connect.Config.(set tweaks_version (1, 7) default) in
let* conn = Caqti_lwt_unix.connect ~config (Uri.of_string "sqlite3::memory:") in
let* db = Caqti_lwt.or_fail conn in
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 :
Model.content -> (int64, string, [ `One ]) Caqti.Template.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 ->
let name = Model.Field.name field in
Buffer.add_string buff
( Croquette_common.Sql.quote_literal (Model.Identifier.to_string name)
^ "," ^ Model.Identifier.quote name ^ "," ))
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
( Croquette_common.Sql.quote_literal "parent" ^ ","
^ Model.Identifier.quote (Model.Identifier.parent) )
in
let q = Buffer.contents buff in
let open Caqti.Templater in
direct T.(int64 -->! string)
("SELECT json_object(" ^ q ^ ") from "
^ Model.Identifier.quote (Model.Content.id content)
^ " where " ^ Model.Identifier.quote (Model.Identifier.id) ^ "=?")
let query_values (table : Model.content)
(feature : Model.Identifier.t) =
let open Caqti.Templater in
direct T.(int64 -->* string)
("SELECT json_object("
^ Croquette_common.Sql.quote_literal (Model.Identifier.to_string feature)
^ ", " ^ Model.Identifier.quote (Model.Identifier.target) ^ ") from "
^ Model.Identifier.quote (Model.Identifier.dot (Model.Content.id table) feature)
^ " where " ^ Model.Identifier.quote (Model.Identifier.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.content ->
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.Identifier.to_string (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
|