-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
631a73a
commit e8865b1
Showing
7 changed files
with
166 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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 postgres::binary_copy::BinaryCopyInWriter; | ||
use postgres::CopyInWriter; | ||
|
||
use crate::utils::{NewTableTypes, Rows}; | ||
use crate::pg::crud::create_connection; | ||
|
||
#[derive(Debug)] | ||
pub struct Wkb { | ||
pub geometry: Vec<u8>, | ||
} | ||
|
||
impl ToSql for Wkb { | ||
fn to_sql( | ||
&self, | ||
_: &Type, | ||
out: &mut BytesMut, | ||
) -> std::result::Result<IsNull, Box<dyn Error + Send + Sync>> { | ||
out.extend_from_slice(&self.geometry); | ||
Ok(IsNull::No) | ||
} | ||
|
||
fn accepts(ty: &Type) -> bool { | ||
ty.name() == "geometry" | ||
} | ||
|
||
to_sql_checked!(); | ||
} | ||
|
||
pub fn infer_geom_type(stmt: Statement) -> Result<Type> { | ||
let column = stmt.columns().get(0).expect("Failed to get columns"); | ||
Ok(column.type_().clone()) | ||
} | ||
|
||
pub fn insert_rows<'a>( | ||
rows: &Rows, | ||
config: &Vec<NewTableTypes>, | ||
geom_type: Type, | ||
uri: &str, | ||
schema: &Option<String>, | ||
table: &str, | ||
) -> Result<()> { | ||
// Create connection | ||
let mut client = create_connection(uri)?; | ||
|
||
// Prepare types for binary copy | ||
let mut types: Vec<Type> = Vec::new(); | ||
for column in config.iter() { | ||
types.push(column.data_type.clone()); | ||
} | ||
types.push(geom_type); | ||
|
||
// Binary copy in writer | ||
let mut query = String::from("COPY "); | ||
if let Some(schema) = schema { | ||
query.push_str(&format!("{}.{}", schema, table)); | ||
} else { | ||
query.push_str(&table); | ||
} | ||
query.push_str(" ("); | ||
for column in config.iter() { | ||
query.push_str(&format!("{},", column.column_name)); | ||
} | ||
query.push_str("geom) FROM stdin BINARY"); | ||
let writer: CopyInWriter = client.copy_in(&query)?; | ||
|
||
let mut writer = BinaryCopyInWriter::new(writer, &types); | ||
|
||
println!("{:?}", types); | ||
|
||
for row in rows.rows.iter() { | ||
// Transform row into vector of ToSql | ||
|
||
// writer | ||
// .write(&[&row]) | ||
// .expect("Failed to insert row into database"); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::Result; | ||
use postgres::types::Type; | ||
use postgres::Statement; | ||
|
||
use postgres::{Client, NoTls}; | ||
|
||
use crate::utils::NewTableTypes; | ||
|
||
pub fn create_connection(uri: &str) -> Result<Client> { | ||
let client = Client::connect(uri, NoTls)?; | ||
Ok(client) | ||
} | ||
|
||
pub fn create_table( | ||
table_name: &str, | ||
schema_name: &Option<String>, | ||
config: &Vec<NewTableTypes>, | ||
uri: &str, | ||
) -> Result<Statement> { | ||
let mut query = String::from("CREATE TABLE IF NOT EXISTS "); | ||
if let Some(schema) = schema_name { | ||
query.push_str(&format!("{}.{} ", schema, table_name)); | ||
} else { | ||
query.push_str(&table_name); | ||
} | ||
query.push_str("("); | ||
for column in config.iter() { | ||
match column.data_type { | ||
Type::INT8 => { | ||
query.push_str(&format!("{} INT,", column.column_name)); | ||
} | ||
Type::FLOAT8 => { | ||
query.push_str(&format!("{} DOUBLE,", column.column_name)); | ||
} | ||
Type::TEXT => { | ||
query.push_str(&format!("{} TEXT,", column.column_name)); | ||
} | ||
Type::BOOL => { | ||
query.push_str(&format!("{} BOOL,", column.column_name)); | ||
} | ||
_ => println!("Type currently not supported"), | ||
} | ||
} | ||
query.push_str("geom Geometry(Geometry, 4326)"); | ||
query.push_str(");"); | ||
println!("{}", query); | ||
|
||
let mut client = create_connection(uri)?; | ||
client.execute(&query, &[])?; | ||
|
||
let stmt = if let Some(schema) = schema_name { | ||
client.prepare(&format!("SELECT geom FROM {}.{}", schema, table_name))? | ||
} else { | ||
client.prepare(&format!("SELECT geom FROM {}", table_name))? | ||
}; | ||
|
||
Ok(stmt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,2 @@ | ||
use crate::Result; | ||
use postgres::Statement; | ||
use postgres::types::Type; | ||
|
||
use postgres::{Client, NoTls, CopyInWriter}; | ||
|
||
use crate::utils::NewTableTypes; | ||
|
||
pub fn create_connection(uri: &str) -> Result<Client> { | ||
let client = Client::connect(uri, NoTls)?; | ||
Ok(client) | ||
} | ||
|
||
// pub fn create_table(client: &mut Client) -> Result<Statement> { | ||
// client.execute("CREATE TABLE IF NOT EXISTS pio ( | ||
// id INT, | ||
// properties JSONB, | ||
// geometry geometry);", &[])?; | ||
// | ||
// let stmt = client.prepare("SELECT geometry FROM pio")?; | ||
// Ok(stmt) | ||
// } | ||
|
||
pub fn infer_geom_type(stmt: Statement) -> Result<Type> { | ||
let column = stmt.columns().get(0).expect("Failed to get columns"); | ||
Ok(column.type_().clone()) | ||
} | ||
|
||
pub fn create_binary_writer<'a>(client: &'a mut Client) -> Result<CopyInWriter<'a>> { | ||
let writer:CopyInWriter = client.copy_in("COPY pio (id, properties, geometry) FROM stdin BINARY")?; | ||
Ok(writer) | ||
} | ||
|
||
pub fn create_table(table_name: &str, schema_name: Option<String>, config: &Vec<NewTableTypes>, client: &mut Client) -> Result<()> { | ||
let mut query = String::from("CREATE TABLE IF NOT EXISTS "); | ||
if let Some(schema) = schema_name { | ||
query.push_str(&format!("{}.{} ", schema, table_name)); | ||
} else { | ||
query.push_str(&table_name); | ||
} | ||
query.push_str("("); | ||
for column in config.iter() { | ||
match column.data_type { | ||
Type::INT8 => { | ||
query.push_str(&format!(" {} INT,", column.column_name)); | ||
}, | ||
Type::FLOAT8 => { | ||
query.push_str(&format!(" {} DOUBLE,", column.column_name)); | ||
}, | ||
Type::TEXT => { | ||
query.push_str(&format!(" {} TEXT,", column.column_name)); | ||
}, | ||
Type::BOOL => { | ||
query.push_str(&format!(" {} BOOL,", column.column_name)); | ||
}, | ||
_ => println!("Type currently not supported"), | ||
} | ||
} | ||
query.push_str("geom GEOMETRY"); | ||
query.push_str(");"); | ||
println!("{}", query); | ||
client.execute(&query, &[])?; | ||
Ok(()) | ||
} | ||
pub mod binary_copy; | ||
pub mod crud; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters