From 22d7621eb0a6dec42e4fd5759469a6ba44265822 Mon Sep 17 00:00:00 2001 From: Chris Carlon Date: Sat, 12 Oct 2024 13:45:00 +0100 Subject: [PATCH] Allow users to define their own postgis uri --- src/duckdb_load/mod.rs | 36 ++++++++++++++++++++++++++---------- src/main.rs | 2 +- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/duckdb_load/mod.rs b/src/duckdb_load/mod.rs index f8f98a0..839210f 100644 --- a/src/duckdb_load/mod.rs +++ b/src/duckdb_load/mod.rs @@ -18,23 +18,30 @@ enum FileType { } // Struct representing core components -// This will include a UUID in the future that will be used for the PostGIS table name struct DuckDBFileProcessor { file_path: String, table_name: String, file_type: FileType, conn: Connection, + postgis_uri: String, } // Implementation for DuckDBFileProcessor impl DuckDBFileProcessor { - fn new_file(file_path: &str, table_name: &str) -> Result> { + fn new_file( + file_path: &str, + table_name: &str, + postgis_uri: &str, + ) -> Result> { // Determine FileType let file_type = Self::determine_file_type(file_path)?; // Create Connection Object let conn = Connection::open(":memory:")?; + // Define postgis_uri + let postgis_uri = postgis_uri; + // Install and load required extensions conn.execute("INSTALL spatial;", [])?; conn.execute("LOAD spatial;", [])?; @@ -46,6 +53,7 @@ impl DuckDBFileProcessor { table_name: table_name.to_string(), file_type, conn, + postgis_uri: postgis_uri.to_string(), }) } @@ -234,7 +242,10 @@ impl DuckDBFileProcessor { fn load_data_postgis(&self, geom_columns: &[String]) -> Result<(), Box> { // Attach Postgres DB instance self.conn.execute( - "ATTACH 'dbname=gridwalk user=admin password=password host=localhost port=5432' AS gridwalk_db (TYPE POSTGRES)", + &format!( + "ATTACH '{}' AS gridwalk_db (TYPE POSTGRES)", + self.postgis_uri + ), [], )?; @@ -280,14 +291,19 @@ impl DuckDBFileProcessor { } } -pub fn launch_process_file(file_path: &str, table_name: &str) -> Result<(), io::Error> { +pub fn launch_process_file( + file_path: &str, + table_name: &str, + postgis_uri: &str, +) -> Result<(), io::Error> { // Create new processor object - let processor = DuckDBFileProcessor::new_file(file_path, table_name).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Error creating FileProcessor for '{}': {}", file_path, e), - ) - })?; + let processor = + DuckDBFileProcessor::new_file(file_path, table_name, postgis_uri).map_err(|e| { + io::Error::new( + io::ErrorKind::Other, + format!("Error creating FileProcessor for '{}': {}", file_path, e), + ) + })?; println!( "Detected file type: {:?} for file: '{}'", diff --git a/src/main.rs b/src/main.rs index ca8e2c7..252ca71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,6 @@ mod duckdb_load; use duckdb_load::launch_process_file; fn main() -> Result<(), Box> { - launch_process_file("test_files/2011 Greenbelt/GreenBelt2011.shp", "my_table")?; + launch_process_file("FILE NAME HERE", "TABLE NAME HERE", "POSTGIS URI HERE")?; Ok(()) }