aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2024-12-11 22:04:40 +0100
committerSébastien Dailly <sebastien@dailly.me>2024-12-12 14:19:48 +0100
commit39f39919fb4749787393e95503f9814912265a73 (patch)
tree57745ab257e50f9ef4d6924c3c77d51fd8fb4d4d /bin
parent5a558038874765f20b9dc1bcb1890600e2a2065d (diff)
Review the consistency request
Diffstat (limited to 'bin')
-rw-r--r--bin/importer.ml103
1 files changed, 53 insertions, 50 deletions
diff --git a/bin/importer.ml b/bin/importer.ml
index 0d9b751..4ca04fd 100644
--- a/bin/importer.ml
+++ b/bin/importer.ml
@@ -101,12 +101,11 @@ module Args = struct
}
end
-(** Print the result from the query.
+(** Print the result from the query.
- Each value is given with the associated expression in the configuration,
- the function is expected to convert the result into string in order to
- include the content in the output CSV.
- *)
+ Each value is given with the associated expression in the configuration, the
+ function is expected to convert the result into string in order to include
+ the content in the output CSV. *)
let printer : Path.t ImportExpression.T.t * ImportCSV.DataType.t -> string =
fun (column, value) ->
ignore column;
@@ -169,50 +168,52 @@ let process_table :
let text_headers = Array.map v ~f:ImportCSV.DataType.to_string in
Headers.SheeetMap.add source text_headers map
in
+ headers
- (* For each external check if the values are loaded *)
- let dependancies =
- ImportConf.get_dependancies_for_table conf.configuration source
- in
- List.iter dependancies ~f:(fun ext ->
- match ext.ImportConf.Syntax.allow_missing with
- | true -> ()
- | false -> (
- Printf.printf "Checking dependancies for %s %!"
- ext.ImportConf.Syntax.target.ImportDataTypes.Table.name;
- try
- ignore
- @@ Db.check_foreign db conf.configuration ext ~f:(fun values ->
- Helpers.Console.update_cursor ();
-
- let row =
- match snd (Array.get values 0) with
- | ImportCSV.DataType.Integer i -> i
- | _ -> -1
- and value = snd (Array.get values 1) in
- let error =
- ImportErrors.
- {
- source;
- sheet = source.Table.tab;
- row;
- value;
- target = Some ext.ImportConf.Syntax.target;
- exn =
- Failure
- (Printf.sprintf "Key '%s' not found"
- (CSV.DataType.to_string value));
- }
- in
-
- ImportErrors.output_error log_error error);
- Helpers.Console.close_cursor ()
- with
- | Sqlite3.Error _ ->
- (* We can have errors here if we have cycles in the
+let check_deps :
+ 'a Db.t -> Csv.out_channel Lazy.t -> ImportConf.Syntax.t -> Table.t -> unit
+ =
+ fun db log_error conf source ->
+ (* For each external check if the values are loaded *)
+ let dependancies = ImportConf.get_dependancies_for_table conf source in
+ List.iter dependancies ~f:(fun ext ->
+ match ext.ImportConf.Syntax.Extern.allow_missing with
+ | true -> ()
+ | false -> (
+ Printf.printf "Checking dependancies for %s\n%!"
+ ext.ImportConf.Syntax.Extern.target.ImportDataTypes.Table.name;
+ try
+ ignore
+ @@ Db.check_foreign db conf ext ~f:(fun values ->
+ Helpers.Console.update_cursor ();
+
+ let row =
+ match snd (Array.get values 0) with
+ | ImportCSV.DataType.Integer i -> i
+ | _ -> -1
+ and value = snd (Array.get values 1) in
+ let error =
+ ImportErrors.
+ {
+ source;
+ sheet = source.Table.tab;
+ row;
+ value;
+ target = Some ext.ImportConf.Syntax.Extern.target;
+ exn =
+ Failure
+ (Printf.sprintf "Key '%s' not found"
+ (CSV.DataType.to_string value));
+ }
+ in
+
+ ImportErrors.output_error log_error error);
+ Helpers.Console.close_cursor ()
+ with
+ | Sqlite3.Error _ ->
+ (* We can have errors here if we have cycles in the
dependencies, but it’s OK at this step.*)
- ()));
- headers
+ ()))
let () =
let conf = Args.load () in
@@ -283,6 +284,10 @@ let () =
List.fold_left process_order ~init:Headers.SheeetMap.empty
~f:(process_table db dirname log_error conf)
in
+ let () =
+ check_deps db log_error conf.configuration
+ conf.configuration.source
+ in
let first_line = Headers.columns conf.configuration headers in
Csv.output_record out_csv first_line;
@@ -291,13 +296,11 @@ let () =
ignore @@ Db.create_view db conf.configuration;
Printf.printf "Extracting results %!";
match
- Db.query
- ~f:(fun v ->
+ Db.query db conf.configuration ~f:(fun v ->
let arr = Array.to_seq v |> Seq.map printer |> List.of_seq in
Helpers.Console.update_cursor ();
Csv.output_record out_csv arr)
- db conf.configuration
with
| Ok () ->
Helpers.Console.close_cursor ();