diff --git a/src/database/mod.rs b/src/database/mod.rs new file mode 100644 index 0000000..6aa782e --- /dev/null +++ b/src/database/mod.rs @@ -0,0 +1 @@ +pub mod postgresql; diff --git a/src/database/postgresql.rs b/src/database/postgresql.rs new file mode 100644 index 0000000..94367fb --- /dev/null +++ b/src/database/postgresql.rs @@ -0,0 +1,16 @@ +use std::error::Error; +use tokio_postgres::{NoTls, Client}; + +pub async fn connect_to_postgres(config: &std::string::String ) -> Result> { + // let config = "postgres://postgres:password@172.17.0.2:5432/postgres"; + let (client, _connection) = tokio_postgres::connect(&config, NoTls).await?; + tokio::spawn(async move { + if let Err(e) = _connection.await { + eprintln!("connection error: {}", e); + } + }); + + // Do any other necessary processing with the client or connection... + + Ok(client) +} diff --git a/src/main.rs b/src/main.rs index dca1436..c366d86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ mod configure; mod parser; mod serializer; +mod database; use std::error::Error; use futures::pin_mut; use tokio::task; use tokio_postgres::binary_copy::BinaryCopyInWriter; use tokio_postgres::types::{ToSql, Type}; -use tokio_postgres::{NoTls, Client}; +// use tokio_postgres::{NoTls, Client}; extern crate jemallocator; #[global_allocator] @@ -31,21 +32,6 @@ fn query_string(insert_max: &usize, table_name: &str) -> String { insert_template } - -async fn connect_to_postgres(config: &std::string::String ) -> Result> { - // let config = "postgres://postgres:password@172.17.0.2:5432/postgres"; - let (client, _connection) = tokio_postgres::connect(&config, NoTls).await?; - tokio::spawn(async move { - if let Err(e) = _connection.await { - eprintln!("connection error: {}", e); - } - }); - - // Do any other necessary processing with the client or connection... - - Ok(client) -} - // #[tokio::main(core_threads = 4)] // #[tokio::main(worker_threads = 4)] #[tokio::main] @@ -63,13 +49,13 @@ async fn main() -> Result<(), Box> { /* db connection */ - if let Err(err) = connect_to_postgres(&config.connection).await { + if let Err(err) = database::postgresql::connect_to_postgres(&config.connection).await { println!("Error chain: {:?}", err); println!("Error: {}", err); eprintln!("Error chain: {:?}", err); } - let client = connect_to_postgres(&config.connection).await.unwrap(); + let client = database::postgresql::connect_to_postgres(&config.connection).await.unwrap(); client .execute(&*format!("DROP TABLE IF EXISTS {}", &config.tablename), &[]) .await?;