summaryrefslogtreecommitdiff
path: root/test/schema.ml
blob: 4323368a793cb4b1258b569de062e4054d9a5c29 (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
let name = __FILE__
let description = "Print the schema for the given tables"
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_newline
         Format.pp_print_string)

let ( let* ) = Lwt.bind
let ( let+ ) opt cont = Result.map cont opt

let schema_query =
  Caqti.Templater.static
    Caqti.Templater.T.(string -->! string)
    "SELECT sql from sqlite_schema where name=?"

let get_schema : Caqti_lwt.connection -> string -> string Lwt.t =
 fun db table_name ->
  let module Db = (val db) in
  let* result = Db.find schema_query table_name in
  Caqti_lwt.or_fail result

let exec : string list -> t =
 fun tables ->
  let stdin = Common.read_stdin () in
  let+ configuration = Configuration.Read_conf.read_string_conf stdin in
  Common.with_db configuration (fun db ->
      Lwt_list.map_s (get_schema db) (List.rev tables))