aboutsummaryrefslogtreecommitdiff
path: root/lib/sql/db.mli
blob: 465b159221c7ddc602cd4f56555c521e293be5c8 (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
module Syntax = ImportConf.Syntax

type 'a t
type 'a result = ('a, exn) Result.t

val with_db : string -> ('a t -> unit result) -> unit result

val check_table_schema : 'a t -> ImportAnalyser.Dependency.t -> bool result
(** Check if a table with the same structure already exists in the database. 

    This query allow to reuse the same data without reloading the file if
    nothing changed. *)

val create_table : 'a t -> ImportAnalyser.Dependency.t -> unit result
(** [create_table db name] will create a new table in the
    db with the given name, and the columns from the configuration (see
    [ImportAnalyser.Query.create_table])

    Any previous table with the same name will be deleted. *)

val prepare_insert : 'a t -> ImportAnalyser.Dependency.t -> Sqlite3.stmt result
(** Create a statement to use in an insert. 
    [prepare_insert db table] will prepare a statement for inserting
    the columns at the given index. *)

val finalize : Sqlite3.stmt -> unit result
(** Finalize the statement. The function shall be called once each insert are
    done, or after an error in the insert. *)

val reset : Sqlite3.stmt -> unit result

val eval_key :
  'a t ->
  Sqlite3.stmt option ->
  ImportAnalyser.Dependency.key list ->
  (int * ImportCSV.DataType.t) list ->
  (Sqlite3.stmt option * Sqlite3.Data.t list) result
(** Evaluate the keys in sqlite and get the results. 

    The function is intended to check if the values are null before inserting
    them in a batch *)

val insert :
  'a t ->
  Sqlite3.stmt ->
  id:int ->
  (int * ImportCSV.DataType.t) list ->
  unit result
(** Insert a new row in the database. 

 [insert db ~id statement values] will add a new row in the given table with
 [id]. The columns are identified with their index number (there is a
 difference by one with the column number)

 Thanks to SQLite Flexible Typing (https://www.sqlite.org/flextypegood.html)
 each column can contain values typed differently which is how the spreadsheet
 also works. 

 This function is expected to be run inside a transaction. *)

val begin_transaction : 'a t -> unit result
val commit : 'a t -> unit result
val rollback : 'a t -> unit result

val query :
  f:
    ((ImportDataTypes.Path.t ImportExpression.T.t * ImportCSV.DataType.t) array ->
    unit) ->
  'a t ->
  Syntax.t ->
  unit result
(** This one the most important function from the application. The function
    will transform the configuration into an sql query and will fetch the
    result from the sqlite engine. 

    The function [f] given in argument will be called for each line

    *)

val create_view : 'a t -> Syntax.t -> unit result
(** Create a view which represent the result *)

val check_foreign :
  f:((string * ImportCSV.DataType.t) array -> unit) ->
  'a t ->
  Syntax.t ->
  Syntax.extern ->
  unit result

val clear_duplicates :
  f:((string * ImportCSV.DataType.t) array -> unit) ->
  'a t ->
  ImportDataTypes.Table.t ->
  ImportAnalyser.Dependency.key list ->
  unit result
(** Remove all duplicated keys in the table by setting them to NULL. *)

val insert_header :
  'a t ->
  ImportDataTypes.Table.t ->
  (int * ImportCSV.DataType.t) array ->
  unit T.result

val query_headers :
  'a t -> ImportDataTypes.Table.t -> ImportCSV.DataType.t array T.result
(** Get all the headers from the database (used or not) *)