From 6b377719c10d5ab3343fd5221f99a4a21008e25a Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 14 Mar 2024 08:26:58 +0100 Subject: Initial commit --- lib/sql/db.mli | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 lib/sql/db.mli (limited to 'lib/sql/db.mli') diff --git a/lib/sql/db.mli b/lib/sql/db.mli new file mode 100644 index 0000000..465b159 --- /dev/null +++ b/lib/sql/db.mli @@ -0,0 +1,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) *) -- cgit v1.2.3