created database module

This commit is contained in:
gurkenhabicht 2023-08-06 19:38:12 +02:00
parent d12bc54837
commit 1722a9a1fa
3 changed files with 21 additions and 18 deletions

1
src/database/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod postgresql;

View File

@ -0,0 +1,16 @@
use std::error::Error;
use tokio_postgres::{NoTls, Client};
pub async fn connect_to_postgres(config: &std::string::String ) -> Result<Client, Box<dyn Error>> {
// 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)
}

View File

@ -1,12 +1,13 @@
mod configure; mod configure;
mod parser; mod parser;
mod serializer; mod serializer;
mod database;
use std::error::Error; use std::error::Error;
use futures::pin_mut; use futures::pin_mut;
use tokio::task; use tokio::task;
use tokio_postgres::binary_copy::BinaryCopyInWriter; use tokio_postgres::binary_copy::BinaryCopyInWriter;
use tokio_postgres::types::{ToSql, Type}; use tokio_postgres::types::{ToSql, Type};
use tokio_postgres::{NoTls, Client}; // use tokio_postgres::{NoTls, Client};
extern crate jemallocator; extern crate jemallocator;
#[global_allocator] #[global_allocator]
@ -31,21 +32,6 @@ fn query_string(insert_max: &usize, table_name: &str) -> String {
insert_template insert_template
} }
async fn connect_to_postgres(config: &std::string::String ) -> Result<Client, Box<dyn Error>> {
// 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(core_threads = 4)]
// #[tokio::main(worker_threads = 4)] // #[tokio::main(worker_threads = 4)]
#[tokio::main] #[tokio::main]
@ -63,13 +49,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
/* db connection */ /* 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 chain: {:?}", err);
println!("Error: {}", err); println!("Error: {}", err);
eprintln!("Error chain: {:?}", 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 client
.execute(&*format!("DROP TABLE IF EXISTS {}", &config.tablename), &[]) .execute(&*format!("DROP TABLE IF EXISTS {}", &config.tablename), &[])
.await?; .await?;