diff --git a/src/pg/binary_copy.rs b/src/pg/binary_copy.rs index af2a42f..02412a9 100644 --- a/src/pg/binary_copy.rs +++ b/src/pg/binary_copy.rs @@ -2,14 +2,14 @@ use crate::Result; use bytes::BytesMut; use postgres::types::to_sql_checked; use postgres::types::{IsNull, ToSql, Type}; -use std::error::Error; use postgres::Statement; +use std::error::Error; use postgres::binary_copy::BinaryCopyInWriter; use postgres::CopyInWriter; -use crate::utils::{NewTableTypes, Rows}; use crate::pg::crud::create_connection; +use crate::utils::{AcceptedTypes, NewTableTypes, Rows}; #[derive(Debug)] pub struct Wkb { @@ -74,13 +74,43 @@ pub fn insert_rows<'a>( println!("{:?}", types); - for row in rows.rows.iter() { + for row in rows.row.iter() { // Transform row into vector of ToSql + let mut vec: Vec<&(dyn ToSql + Sync)> = Vec::new(); + for column in row.columns.iter() { + match column { + AcceptedTypes::Int(value) => { + vec.push(value); + } + AcceptedTypes::Float(value) => { + vec.push(value); + } + AcceptedTypes::Double(value) => { + vec.push(value); + } + AcceptedTypes::Text(value) => { + vec.push(value); + } + AcceptedTypes::Bool(value) => { + vec.push(value); + } + AcceptedTypes::Geometry(value) => { + vec.push(value); + } + } + } - // writer - // .write(&[&row]) - // .expect("Failed to insert row into database"); + // Convert the vector to a slice of references + let vec_slice: &[&(dyn ToSql + Sync)] = &vec; + + // Write row to database + writer + .write(vec_slice) + .expect("Failed to insert row into database"); } + // Finish writing + writer.finish()?; + Ok(()) } diff --git a/src/pg/crud.rs b/src/pg/crud.rs index e7fe457..054144f 100644 --- a/src/pg/crud.rs +++ b/src/pg/crud.rs @@ -30,7 +30,7 @@ pub fn create_table( query.push_str(&format!("{} INT,", column.column_name)); } Type::FLOAT8 => { - query.push_str(&format!("{} DOUBLE,", column.column_name)); + query.push_str(&format!("{} DOUBLE PRECISION,", column.column_name)); } Type::TEXT => { query.push_str(&format!("{} TEXT,", column.column_name)); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index caf9b8f..0f89e4c 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -22,7 +22,7 @@ pub struct Row { #[derive(Debug)] pub struct Rows { - pub rows: Vec, + pub row: Vec, } impl Row { @@ -36,10 +36,10 @@ impl Row { impl Rows { pub fn new() -> Self { - Rows { rows: Vec::new() } + Rows { row: Vec::new() } } pub fn add(&mut self, row: Row) { - self.rows.push(row); + self.row.push(row); } } @@ -47,7 +47,7 @@ impl Rows { #[derive(Debug)] pub enum AcceptedTypes { Int(i64), - // Float(f64), + Float(f64), Double(f64), Text(String), Bool(bool), diff --git a/src/utils/shp.rs b/src/utils/shp.rs index d9ddfd4..3db03a7 100644 --- a/src/utils/shp.rs +++ b/src/utils/shp.rs @@ -17,7 +17,7 @@ pub fn determine_data_types(file_path: &str) -> Result> { FieldValue::Numeric(_) => { table_config.push(NewTableTypes { column_name, - data_type: Type::INT8, + data_type: Type::FLOAT8, }); } FieldValue::Float(_) => { @@ -68,7 +68,7 @@ pub fn read_shapefile(file_path: &str) -> Result { match data_type { FieldValue::Numeric(value) => { if let Some(value) = value { - row.add(AcceptedTypes::Int(value as i64)); + row.add(AcceptedTypes::Float(value)); } } FieldValue::Float(value) => {