package sihl

  1. Overview
  2. Docs

Source file gen_repo.ml

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
let requests_postgresql =
  {|let clean_request =
  Caqti_request.exec Caqti_type.unit "TRUNCATE TABLE {{table_name}} CASCADE;"
;;

let insert_request =
  Caqti_request.exec
    {{caqti_type}}
    {sql|
INSERT INTO {{table_name}} (
  uuid,
  {{fields}},
  created_at,
  updated_at
) VALUES (
  ?::uuid,
  {{parameters}},
  ? AT TIME ZONE 'UTC',
  ? AT TIME ZONE 'UTC'
)
        |sql}
;;

let update_request =
  Caqti_request.exec
    {{caqti_type_update}}
    {sql|
UPDATE {{table_name}} SET
  {{update_fields}},
  updated_at = NOW() AT TIME ZONE 'UTC'
WHERE uuid = $1::uuid;
        |sql}
;;

let find_request =
  Caqti_request.find_opt
    Caqti_type.string
    {{caqti_type}}
    {sql|
SELECT
  uuid,
  {{fields}},
  created_at,
  updated_at
FROM {{table_name}}
WHERE uuid = ?::uuid
        |sql}
;;

let filter_fragment = {sql|
WHERE {{filter_fragment}}
|sql}

let search_query =
  {sql|
SELECT
  COUNT(*) OVER() as total,
  uuid,
  {{fields}},
  created_at,
  updated_at
FROM {{table_name}}
|sql}
;;

let search_request =
  Sihl.Database.prepare_search_request
    ~search_query
    ~filter_fragment
    ~sort_by_field:"id"
    {{caqti_type}}
;;

let delete_request =
  Caqti_request.exec
    Caqti_type.string
    {sql|
DELETE FROM {{table_name}}
WHERE uuid = ?::uuid
        |sql}
;;

|}
;;

let requests_mariadb =
  {|let clean_request =
  Caqti_request.exec Caqti_type.unit "TRUNCATE TABLE {{table_name}};"
;;

let insert_request =
  Caqti_request.exec
    {{caqti_type}}
    {sql|
INSERT INTO {{table_name}} (
  uuid,
  {{fields}},
  created_at,
  updated_at
) VALUES (
  UNHEX(REPLACE(?, '-', '')),
  {{parameters}},
  ?,
  ?
)
        |sql}
;;

let update_request =
  Caqti_request.exec
    {{caqti_type_update}}
    {sql|
UPDATE {{table_name}} SET
  {{update_fields}},
  updated_at = NOW()
WHERE uuid = UNHEX(REPLACE($1, '-', ''));
        |sql}
;;

let find_request =
  Caqti_request.find_opt
    Caqti_type.string
    {{caqti_type}}
    {sql|
SELECT
  LOWER(CONCAT(
    SUBSTR(HEX(uuid), 1, 8), '-',
    SUBSTR(HEX(uuid), 9, 4), '-',
    SUBSTR(HEX(uuid), 13, 4), '-',
    SUBSTR(HEX(uuid), 17, 4), '-',
    SUBSTR(HEX(uuid), 21)
  )),
  {{fields}},
  created_at,
  updated_at
FROM {{table_name}}
WHERE uuid = UNHEX(REPLACE(?, '-', ''))
        |sql}
;;

let filter_fragment = {sql|
WHERE {{filter_fragment}}
|sql}

let search_query =
  {sql|
SELECT
  COUNT(*) OVER() as total,
  LOWER(CONCAT(
    SUBSTR(HEX(uuid), 1, 8), '-',
    SUBSTR(HEX(uuid), 9, 4), '-',
    SUBSTR(HEX(uuid), 13, 4), '-',
    SUBSTR(HEX(uuid), 17, 4), '-',
    SUBSTR(HEX(uuid), 21)
  )),
  {{fields}},
  created_at,
  updated_at
FROM {{table_name}}
|sql}
;;

let search_request =
  Sihl.Database.prepare_search_request
    ~search_query
    ~filter_fragment
    ~sort_by_field:"id"
    {{caqti_type}}
;;

let delete_request =
  Caqti_request.exec
    Caqti_type.string
    {sql|
DELETE FROM {{table_name}}
WHERE uuid = UNHEX(REPLACE(?, '-', ''))
        |sql}
;;
|}
;;

let queries =
  {|

let clean () = Sihl.Database.exec clean_request ()
;;

let insert ({{name}} : Entity.t) =
  Sihl.Database.exec insert_request {{caqti_value}}
;;

let update ({{name}} : Entity.t) =
  Sihl.Database.exec update_request {{caqti_value_update}}
;;

let find (id : string) : Entity.t option Lwt.t =
  let open Lwt.Syntax in
  let* {{name}} = Sihl.Database.find_opt find_request id in
  Lwt.return
  @@ Option.map
       (fun {{destructured_fields}} ->
         Entity.{ id; {{created_value}} created_at; updated_at })
       {{name}}
;;

let search filter sort ~limit ~offset =
  let open Lwt.Syntax in
  let* result =
    Sihl.Database.run_search_request
      search_request
      sort
      filter
     ~limit
     ~offset
  in
  let {{name}}s =
    List.map
      ~f:(fun {{destructured_fields}} ->
        Entity.{ id; {{created_value}} created_at; updated_at })
      (fst result)
  in
  Lwt.return @@ ({{name}}s, snd result)
;;

let delete ({{name}} : Entity.t) : unit Lwt.t =
  Sihl.Database.exec delete_request {{name}}.Entity.id
;;
|}
;;

let caqti_type (schema : Gen_core.schema) =
  let rec loop = function
    | [ el1; el2 ] ->
      let el1 = Gen_core.caqti_type_of_gen_type el1 in
      let el2 = Gen_core.caqti_type_of_gen_type el2 in
      Format.sprintf "(tup2 %s %s)" el1 el2
    | el1 :: rest ->
      let el1 = Gen_core.caqti_type_of_gen_type el1 in
      Format.sprintf "(tup2 %s %s)" el1 (loop rest)
    | [] -> failwith "Empty schema provided"
  in
  let types =
    List.concat
      Gen_core.[ [ String ]; List.map snd schema; [ Datetime; Datetime ] ]
  in
  Format.sprintf "Caqti_type.%s" (loop types)
;;

let caqti_type_update (schema : Gen_core.schema) =
  let rec loop = function
    | [ el1; el2 ] ->
      let el1 = Gen_core.caqti_type_of_gen_type el1 in
      let el2 = Gen_core.caqti_type_of_gen_type el2 in
      Format.sprintf "(tup2 %s %s)" el1 el2
    | el1 :: rest ->
      let el1 = Gen_core.caqti_type_of_gen_type el1 in
      Format.sprintf "(tup2 %s %s)" el1 (loop rest)
    | [] -> failwith "Empty schema provided"
  in
  let types = List.cons Gen_core.String (List.map snd schema) in
  Format.sprintf "Caqti_type.%s" (loop types)
;;

let caqti_value name (schema : Gen_core.schema) =
  let rec loop = function
    | [ el1; el2 ] ->
      let el1 = Format.sprintf "%s.Entity.%s" name el1 in
      let el2 = Format.sprintf "%s.Entity.%s" name el2 in
      Format.sprintf "(%s, %s)" el1 el2
    | el1 :: rest ->
      let el1 = Format.sprintf "%s.Entity.%s" name el1 in
      Format.sprintf "(%s, %s)" el1 (loop rest)
    | [] -> failwith "Empty schema provided"
  in
  let names =
    List.concat
      [ [ "id" ]; List.map fst schema; [ "created_at"; "updated_at" ] ]
  in
  loop names
;;

let caqti_value_update name (schema : Gen_core.schema) =
  let rec loop = function
    | [ el1; el2 ] ->
      let el1 = Format.sprintf "%s.Entity.%s" name el1 in
      let el2 = Format.sprintf "%s.Entity.%s" name el2 in
      Format.sprintf "(%s, %s)" el1 el2
    | el1 :: rest ->
      let el1 = Format.sprintf "%s.Entity.%s" name el1 in
      Format.sprintf "(%s, %s)" el1 (loop rest)
    | [] -> failwith "Empty schema provided"
  in
  let names = List.cons "id" (List.map fst schema) in
  loop names
;;

let destructured_fields (schema : Gen_core.schema) =
  let rec loop = function
    | [ el1; el2 ] -> Format.sprintf "(%s, %s)" el1 el2
    | el1 :: rest -> Format.sprintf "(%s, %s)" el1 (loop rest)
    | [] -> failwith "Empty schema provided"
  in
  let names =
    List.concat
      [ [ "id" ]; List.map fst schema; [ "created_at"; "updated_at" ] ]
  in
  loop names
;;

let fields (schema : Gen_core.schema) =
  schema |> List.map fst |> String.concat ", \n  "
;;

let update_fields (schema : Gen_core.schema) =
  schema
  |> List.mapi (fun idx (name, _) ->
         (* We start with $2 because $1 is the id which is never updated. *)
         Format.sprintf "%s = $%d" name (idx + 2))
  |> String.concat ", \n  "
;;

let parameters (schema : Gen_core.schema) =
  schema |> List.map (fun _ -> "?") |> String.concat ",\n  "
;;

let filter_fragment (schema : Gen_core.schema) =
  let open Gen_core in
  schema
  |> List.filter (fun (_, type_) -> type_ == String)
  |> List.map fst
  |> List.map @@ Format.sprintf "%s LIKE $1"
  |> String.concat " OR "
;;

let file
    (database : Gen_core.database)
    (name : string)
    (schema : Gen_core.schema)
  =
  let open Gen_core in
  let params =
    [ "name", name
    ; "table_name", Format.sprintf "%ss" name
    ; "filter_fragment", filter_fragment schema
    ; "caqti_type", caqti_type schema
    ; "caqti_type_update", caqti_type_update schema
    ; "caqti_value", caqti_value name schema
    ; "caqti_value_update", caqti_value_update name schema
    ; "destructured_fields", destructured_fields schema
    ; "created_value", Gen_entity.created_value schema
    ; "fields", fields schema
    ; "update_fields", update_fields schema
    ; "parameters", parameters schema
    ]
  in
  let template =
    match database with
    | MariaDb -> requests_mariadb ^ queries
    | PostgreSql -> requests_postgresql ^ queries
  in
  Gen_core.{ name = "repo.ml"; template; params }
;;
OCaml

Innovation. Community. Security.