Skip to content

Commit

Permalink
binary copy
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcfrancisco committed Jul 7, 2024
1 parent e8865b1 commit 9605531
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
42 changes: 36 additions & 6 deletions src/pg/binary_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
}
2 changes: 1 addition & 1 deletion src/pg/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Row {

#[derive(Debug)]
pub struct Rows {
pub rows: Vec<Row>,
pub row: Vec<Row>,
}

impl Row {
Expand All @@ -36,18 +36,18 @@ 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);
}
}

// Enum to hold accepted data types
#[derive(Debug)]
pub enum AcceptedTypes {
Int(i64),
// Float(f64),
Float(f64),
Double(f64),
Text(String),
Bool(bool),
Expand Down
4 changes: 2 additions & 2 deletions src/utils/shp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn determine_data_types(file_path: &str) -> Result<Vec<NewTableTypes>> {
FieldValue::Numeric(_) => {
table_config.push(NewTableTypes {
column_name,
data_type: Type::INT8,
data_type: Type::FLOAT8,
});
}
FieldValue::Float(_) => {
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn read_shapefile(file_path: &str) -> Result<Rows> {
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) => {
Expand Down

0 comments on commit 9605531

Please sign in to comment.