From 8ae488341a66d27c91230b1c76d415d59c0e418b Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 24 Aug 2020 10:09:53 +0530 Subject: refactor db to init once, open connections otherwise --- src/db.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 4025e12..299e07c 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,16 +1,26 @@ +// extern use anyhow::Result; use log::{debug, info}; use rusqlite::{Connection, OpenFlags, NO_PARAMS}; +// std use std::fmt; use std::path::Path; -pub fn init_db + fmt::Display>(p: P) -> Result { - debug!("Looking for database at `{}`", p); - let conn = Connection::open_with_flags( +pub fn open_connection

(p: P) -> Result +where + P: AsRef + fmt::Display, +{ + info!("Opened connection to databse"); + Ok(Connection::open_with_flags( &p, OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, - )?; + )?) +} + +pub fn init_db + fmt::Display>(p: P) -> Result<()> { + debug!("Looking for database at `{}`", p); + let conn = open_connection(&p)?; conn.execute( "CREATE TABLE IF NOT EXISTS urls ( link TEXT PRIMARY KEY, @@ -19,5 +29,5 @@ pub fn init_db + fmt::Display>(p: P) -> Result { NO_PARAMS, )?; info!("SQLite3 database `{}` initialized", &p); - return Ok(conn); + Ok(()) } -- cgit v1.2.3