Source file TblStorage.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
(** {1 STORAGETYPE_storage} *)

open Sqlite3Ops

module Stmts = struct
  type t = { storagetype : StorageType.t }

  let create storagetype = { storagetype }

  let ddl_CREATE_TABLE { storagetype } =
    Printf.sprintf
      {|
CREATE TABLE IF NOT EXISTS %s_storage (
  id INTEGER PRIMARY KEY CHECK (id = 0), -- only one record allowed in table
  storagedir TEXT -- the absolute path to the cache directory
) STRICT
|}
      (StorageType.name storagetype)

  let dml_INSERT_INTO { storagetype } =
    Printf.sprintf
      {|
INSERT OR IGNORE INTO %s_storage (id, storagedir)
VALUES (0, :storagedir)
|}
      (StorageType.name storagetype)
end

(** Create the [STORAGETYPE_storage] table *)
let upgrade_table ~db ~schema_version:_ ~storagetype ~storagedir =
  let open Sqlite3 in
  let stmts = { Stmts.storagetype } in
  let name = StorageType.name storagetype in
  let ( let* ) = Result.bind in
  let* () =
    exec_ddl
      ~errbrief:(Printf.sprintf "Could not CREATE TABLE %s_storage." name)
      db
      (Stmts.ddl_CREATE_TABLE stmts)
  in
  (* Insert into the version record *)
  let* () =
    exec_dml
      ~errbrief:(Printf.sprintf "Could not INSERT INTO %s_storage." name)
      db
      (Stmts.dml_INSERT_INTO stmts)
      [
        (":storagedir", Data.TEXT (MlFront_Core.FilePath.to_string storagedir));
      ]
  in
  Ok ()