aboutsummaryrefslogtreecommitdiff
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs20
1 files changed, 15 insertions, 5 deletions
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 @@
1// extern
1use anyhow::Result; 2use anyhow::Result;
2use log::{debug, info}; 3use log::{debug, info};
3use rusqlite::{Connection, OpenFlags, NO_PARAMS}; 4use rusqlite::{Connection, OpenFlags, NO_PARAMS};
4 5
6// std
5use std::fmt; 7use std::fmt;
6use std::path::Path; 8use std::path::Path;
7 9
8pub fn init_db<P: AsRef<Path> + fmt::Display>(p: P) -> Result<Connection> { 10pub fn open_connection<P>(p: P) -> Result<Connection>
9 debug!("Looking for database at `{}`", p); 11where
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
21pub 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}