fn source_db_setup() -> r2d2::Pool<r2d2_odbc::ODBCConnectionManager> {
let manager = ODBCConnectionManager::new("DSN=SAMPLE");
let db_pool = match r2d2::Pool::builder().max_size(10).build(manager) {
Ok(t) => t,
Err(e) => {
error!(LOG, "Db Pool Creation Error: {:?}" ,e; "From" => "Main Thread");
panic!("DB Pool Creation Error")
}
};
db_pool
}
I want DSN to be passed based on user input. However in Lazy Static it is not possible. I need db pool to be lazy static so that I dont have to pass the pool to multiple functions. Could you please let me know how to solve this issue.
use odbc::*;
use r2d2_odbc::ODBCConnectionManager;
use std::thread;
lazy_static! {
pub static ref SOURCE_DB_POOL: Option<r2d2::Pool<r2d2_odbc::ODBCConnectionManager>> = None;
}
fn source_db_setup(connection_string : String) -> r2d2::Pool<r2d2_odbc::ODBCConnectionManager> {
let manager = ODBCConnectionManager::new(connection_string);
let db_pool = match r2d2::Pool::builder().max_size(10).build(manager) {
Ok(t) => t,
Err(e) => {
panic!("DB Pool Creation Error")
}
};
db_pool
}
fn main() {
SOURCE_DB_POOL = source_db_setup("DSN=SAMPLE".to_string());
let pool_conn = SOURCE_DB_POOL
.get()
.unwrap();
let conn = pool_conn.raw();
let stmt = Statement::with_parent(&conn).unwrap();
let epoch_sql = format!("select * from dual");
let mut sql_output : String = Default::default();;
if let Data(mut stmt) = stmt
.exec_direct(&epoch_sql)
.unwrap()
{
while let Some(mut cursor) = stmt
.fetch()
.unwrap()
{
if let Some(val) = cursor.get_data::<&str>(1).unwrap() {
sql_output = val.to_owned();
}
else {
println!("No cursor data");
}
}
}
println!("The value of sql_output is {}",sql_output);