diff options
author | Akshay <[email protected]> | 2020-08-24 05:39:53 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-08-24 05:39:53 +0100 |
commit | 8ae488341a66d27c91230b1c76d415d59c0e418b (patch) | |
tree | 9c5be1b4b45f972813eec3e05d38b8263804c2dc /src/db.rs | |
parent | f9051a919fa9af44a5703f55afc11e91607d68c6 (diff) |
refactor db to init once, open connections otherwise
Diffstat (limited to 'src/db.rs')
-rw-r--r-- | src/db.rs | 20 |
1 files changed, 15 insertions, 5 deletions
@@ -1,16 +1,26 @@ | |||
1 | // extern | ||
1 | use anyhow::Result; | 2 | use anyhow::Result; |
2 | use log::{debug, info}; | 3 | use log::{debug, info}; |
3 | use rusqlite::{Connection, OpenFlags, NO_PARAMS}; | 4 | use rusqlite::{Connection, OpenFlags, NO_PARAMS}; |
4 | 5 | ||
6 | // std | ||
5 | use std::fmt; | 7 | use std::fmt; |
6 | use std::path::Path; | 8 | use std::path::Path; |
7 | 9 | ||
8 | pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> { | 10 | pub fn open_connection<P>(p: P) -> Result<Connection> |
9 | debug!("Looking for database at `{}`", p); | 11 | where |
10 | let conn = Connection::open_with_flags( | 12 | P: AsRef<Path> + fmt::Display, |
13 | { | ||
14 | info!("Opened connection to databse"); | ||
15 | Ok(Connection::open_with_flags( | ||
11 | &p, | 16 | &p, |
12 | OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, | 17 | OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE, |
13 | )?; | 18 | )?) |
19 | } | ||
20 | |||
21 | pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<()> { | ||
22 | debug!("Looking for database at `{}`", p); | ||
23 | let conn = open_connection(&p)?; | ||
14 | conn.execute( | 24 | conn.execute( |
15 | "CREATE TABLE IF NOT EXISTS urls ( | 25 | "CREATE TABLE IF NOT EXISTS urls ( |
16 | link TEXT PRIMARY KEY, | 26 | link TEXT PRIMARY KEY, |
@@ -19,5 +29,5 @@ pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> { | |||
19 | NO_PARAMS, | 29 | NO_PARAMS, |
20 | )?; | 30 | )?; |
21 | info!("SQLite3 database `{}` initialized", &p); | 31 | info!("SQLite3 database `{}` initialized", &p); |
22 | return Ok(conn); | 32 | Ok(()) |
23 | } | 33 | } |