diff --git a/.gitignore b/.gitignore index 056656e..397e14d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target /local/postgresql/data +.DS_Store diff --git a/src/duckdb_load/mod.rs b/src/duckdb_load/mod.rs index 2efe632..91891ba 100644 --- a/src/duckdb_load/mod.rs +++ b/src/duckdb_load/mod.rs @@ -118,61 +118,6 @@ fn load_data_postgis(conn: &Connection, table_name: &str) -> Result<(), Box Result<()> { - let conn = Connection::open_in_memory()?; - conn.execute("INSTALL spatial;", [])?; - conn.execute("LOAD spatial;", [])?; - conn.execute("INSTALL postgres;", [])?; - conn.execute("LOAD postgres;", [])?; - - let create_table_query = match file_type { - FileType::Geopackage | FileType::Shapefile | FileType::Geojson => { - format!( - "CREATE TABLE data AS - SELECT * - FROM ST_Read('{}');", - file_path - ) - } - FileType::Excel => { - format!( - "CREATE TABLE data AS SELECT * FROM st_read('{}');", - file_path - ) - } - FileType::Csv => { - format!( - "CREATE TABLE data AS SELECT * FROM read_csv('{}');", - file_path - ) - } - FileType::Parquet => { - format!( - "CREATE TABLE data AS SELECT * FROM parquet_scan('{}');", - file_path - ) - } - }; - - // Create the table in DuckDB - conn.execute(&create_table_query, [])?; - - // Call to query and print data schema - query_and_print_schema(&conn)?; - - // Transform - duckdb_transform(&conn, file_path)?; - - // Call to load data into postgres and handle the result - match load_data_postgis(&conn, "lllllll") { - Ok(_) => println!("Data successfully loaded into PostgreSQL"), - Err(e) => eprintln!("Error loading data into PostgreSQL: {}", e), - } - - Ok(()) -} - fn get_crs_number(conn: &Connection, file_path: &str) -> Result { let query = format!( "SELECT layers[1].geometry_fields[1].crs.auth_code AS crs_number FROM st_read_meta('{}');", @@ -242,6 +187,61 @@ fn duckdb_transform(conn: &Connection, file_path: &str) -> Result Result<()> { + let conn = Connection::open_in_memory()?; + conn.execute("INSTALL spatial;", [])?; + conn.execute("LOAD spatial;", [])?; + conn.execute("INSTALL postgres;", [])?; + conn.execute("LOAD postgres;", [])?; + + let create_table_query = match file_type { + FileType::Geopackage | FileType::Shapefile | FileType::Geojson => { + format!( + "CREATE TABLE data AS + SELECT * + FROM ST_Read('{}');", + file_path + ) + } + FileType::Excel => { + format!( + "CREATE TABLE data AS SELECT * FROM st_read('{}');", + file_path + ) + } + FileType::Csv => { + format!( + "CREATE TABLE data AS SELECT * FROM read_csv('{}');", + file_path + ) + } + FileType::Parquet => { + format!( + "CREATE TABLE data AS SELECT * FROM parquet_scan('{}');", + file_path + ) + } + }; + + // Create the table in DuckDB + conn.execute(&create_table_query, [])?; + + // Call to query and print data schema + query_and_print_schema(&conn)?; + + // Transform + duckdb_transform(&conn, file_path)?; + + // Call to load data into postgres and handle the result + match load_data_postgis(&conn, "lllllll") { + Ok(_) => println!("Data successfully loaded into PostgreSQL"), + Err(e) => eprintln!("Error loading data into PostgreSQL: {}", e), + } + + Ok(()) +} + // Process file pub fn launch_process_file(file_path: &str) -> io::Result<()> { let mut file = File::open(file_path)?; diff --git a/src/main.rs b/src/main.rs index f02ff04..a1cc128 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,11 @@ mod duckdb_load; fn main() -> Result<(), Box> { - let file_path = "test_files/GLA_High_Street_boundaries.gpkg"; + let file_path = "test_files/hotosm_twn_populated_places_points_geojson.geojson"; println!("Processing file: {}", file_path); match duckdb_load::launch_process_file(file_path) { - Ok(_) => { - println!("File processed successfully."); - Ok(()) - } + Ok(_) => Ok(()), Err(e) => { eprintln!("Error processing file: {}", e); Err(Box::new(e))