From e8aa1429fa45957a2174446df64b62d407544d8b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 9 Nov 2018 18:52:57 +0100 Subject: Add `get_database_connection()` Function to establish a database connection using a connection pool. Update `Purchaser::insert()` to take a `PooledConn` instead of a simple `Conn`. --- license-generator/src/database.rs | 13 +++++++++++++ license-generator/src/errors.rs | 2 ++ license-generator/src/lib.rs | 1 + license-generator/src/purchaser.rs | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 license-generator/src/database.rs (limited to 'license-generator/src') diff --git a/license-generator/src/database.rs b/license-generator/src/database.rs new file mode 100644 index 0000000..28673c4 --- /dev/null +++ b/license-generator/src/database.rs @@ -0,0 +1,13 @@ +use std::env; + +use mysql; + +use errors::*; + +fn get_database_connection() -> Result { + let connection_url = env::var("DATABASE_URL")?; + let pool = mysql::Pool::new_manual(10, 50, connection_url)?; + let cx = pool.get_conn()?; + + Ok(cx) +} diff --git a/license-generator/src/errors.rs b/license-generator/src/errors.rs index 7eb9eda..29f25ca 100644 --- a/license-generator/src/errors.rs +++ b/license-generator/src/errors.rs @@ -2,6 +2,8 @@ use mysql; error_chain! { foreign_links { + EnvVar(::std::env::VarError); + MySql(mysql::error::Error); } } diff --git a/license-generator/src/lib.rs b/license-generator/src/lib.rs index fd1bfa8..e129d17 100644 --- a/license-generator/src/lib.rs +++ b/license-generator/src/lib.rs @@ -5,5 +5,6 @@ extern crate rand; extern crate sha1; +mod database; mod errors; mod purchaser; diff --git a/license-generator/src/purchaser.rs b/license-generator/src/purchaser.rs index 2a7727c..3e9656e 100644 --- a/license-generator/src/purchaser.rs +++ b/license-generator/src/purchaser.rs @@ -29,7 +29,7 @@ impl<'a> Purchaser<'a> { self.secret = Some(digest); } - fn insert(&self, cx: &mut mysql::Conn) -> Result<()> { + fn insert(&self, cx: &mut mysql::PooledConn) -> Result<()> { let mut tx = cx.start_transaction( false, // consistent_snapshot None, // isolation_level -- cgit v1.2.3