From c284321b1073e06481c63e2c061a1600fa68254d Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Thu, 10 Apr 2025 20:27:59 +0200 Subject: Added filters expressions in the externals --- lib/analysers/query.ml | 83 +++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 48 deletions(-) (limited to 'lib/analysers/query.ml') diff --git a/lib/analysers/query.ml b/lib/analysers/query.ml index f89f5f0..dac4d89 100644 --- a/lib/analysers/query.ml +++ b/lib/analysers/query.ml @@ -135,41 +135,24 @@ let select : ImporterSyntax.t -> query * Path.t ImportExpression.T.t array = let () = Chunk.create_from_statement_of_chunck conf request_header in Chunk.append ~head:request_header ~tail:filters; - let formatter = Format.formatter_of_buffer b in (match conf.ImporterSyntax.uniq with | [] -> () | uniq -> - Format.fprintf formatter "\nGROUP BY %a" - (Format.pp_print_list - ~pp_sep:(fun f () -> Format.fprintf f ", ") - (fun formatter column -> - let seq = - Q.query_of_expression Q.BindParam formatter (Printers.path ~conf) - column - in - Queue.transfer seq parameters)) + Chunk.add_string request_header "\nGROUP BY "; + Chunk.add_expressions ~repr:(Printers.path ~conf) ~sep:", " request_header uniq); (match conf.ImporterSyntax.sort with | [] -> () | sort -> - Format.fprintf formatter "\nORDER BY %a" - (Format.pp_print_list - ~pp_sep:(fun f () -> Format.fprintf f ", ") - (fun formatter column -> - let seq = - Q.query_of_expression Q.BindParam formatter (Printers.path ~conf) - column - in - Queue.transfer seq parameters)) + Chunk.add_string request_header "\nORDER BY "; + Chunk.add_expressions ~repr:(Printers.path ~conf) ~sep:", " request_header sort); - Format.pp_print_flush formatter (); - ({ q = Buffer.contents b; parameters = Queue.to_seq parameters }, headers) let check_external : ImporterSyntax.t -> ImporterSyntax.Extern.t -> query = fun conf external_ -> let internal_chunk = Chunk.create () in - Chunk.add_expression ~conf internal_chunk + Chunk.add_expression ~repr:(Printers.path ~conf) internal_chunk external_.ImporterSyntax.Extern.intern_key; let external_key_buffer = Buffer.create 16 in @@ -180,6 +163,7 @@ let check_external : ImporterSyntax.t -> ImporterSyntax.Extern.t -> query = let pointed_tables = pointed_tables conf external_.intern_key in let parameters = Queue.create () in + (* We do a copy before the transfert because the Queue is reused later in the query *) Queue.transfer (Queue.copy internal_chunk.parameters) parameters; @@ -195,44 +179,47 @@ let check_external : ImporterSyntax.t -> ImporterSyntax.Extern.t -> query = ImporterSyntax.Extern.t list -> ImporterSyntax.Extern.t list = fun table init -> - let res = - (* Do not add the same external if the value is already present *) - let init = - match List.find_opt init ~f:(fun ext -> table == ext) with - | None -> table :: init - | Some _ -> init - in - - Expression.T.fold_values ~init table.ImporterSyntax.Extern.intern_key - ~f:(fun acc expr -> - match expr.Path.alias with - | None -> acc - | Some _ as path -> ( - let table = ImporterSyntax.get_table_for_name conf path in - (* Look for this table in the externals *) - let external_opt = - List.find_opt conf.ImporterSyntax.externals ~f:(fun t -> - t.ImporterSyntax.Extern.target == table) - in - match external_opt with - | None -> acc - | Some ext -> collect_links ext acc)) + (* Do not add the same external if the value is already present *) + let init = + match + List.find_opt init ~f:(fun ext -> ImporterSyntax.Extern.equal table ext) + with + | None -> table :: init + | Some _ -> init in - res + + Expression.T.fold_values ~init table.ImporterSyntax.Extern.intern_key + ~f:(fun acc expr -> + match expr.Path.alias with + | None -> acc + | Some _ as path -> ( + let table = ImporterSyntax.get_table_for_name conf path in + (* Look for this table in the externals *) + let external_opt = + List.find_opt conf.ImporterSyntax.externals ~f:(fun t -> + t.ImporterSyntax.Extern.target == table) + in + match external_opt with + | None -> acc + | Some ext -> collect_links ext acc)) in let dependencies = collect_links external_ [] in let join_content = Buffer.contents external_key_buffer in let request = Chunk.create () in + Chunk.add_string request "SELECT "; + + (* Check if we can identify the line number in the file. It’s only possible + if we have a single source used as a key *) let () = match pointed_tables with - | [] -> - (* Otherwise, just return -1 *) - Chunk.add_string request "-1" | (table, _name) :: _ -> (* If we have a single source, extract the row number. *) Chunk.add_string request (Table.print_column table "id") + | [] -> + (* Otherwise, just return -1 *) + Chunk.add_string request "-1" in Chunk.add_string request ", "; Chunk.append ~head:request ~tail:(Chunk.copy internal_chunk); -- cgit v1.2.3