From cf7bf3e14ff4baddc264d99f890bf4cf529400b5 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 21 May 2020 09:36:10 +0200 Subject: [PATCH] Can get a database instance --- src/helpers/database.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 43124d0..d69fb92 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -1,8 +1,10 @@ use std::error::Error; -use std::sync::{Mutex, Arc}; -use crate::data::config::DatabaseConfig; +use std::sync::{Arc, Mutex}; + use mysql::Pool; +use crate::data::config::DatabaseConfig; + /// Database access helper /// /// @author Pierre Hubert @@ -25,4 +27,16 @@ pub fn connect(conf: &DatabaseConfig) -> Result<(), Box> { } Ok(()) +} + +/// Get a connection to the database +pub fn get_connection() -> Result> { + let pool: Pool; + + unsafe { + let guard = POOL.as_ref().unwrap().lock(); + pool = guard.unwrap().clone() + } + + Ok(pool.get_conn()?) } \ No newline at end of file