summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-11-06 02:28:18 -0500
committerJesse Luehrs <doy@tozt.net>2018-11-06 02:28:18 -0500
commitb657e862df6a90ad2f957f02b964d2576e2f03be (patch)
treef09e6f2a54764c67efb43d3d57dd8199b750d57a
parenta7cceb1aa5afb13de9f51c10670757d39d50e2a3 (diff)
downloadlastfm-query-b657e862df6a90ad2f957f02b964d2576e2f03be.tar.gz
lastfm-query-b657e862df6a90ad2f957f02b964d2576e2f03be.zip
no need to create the connection twice here
-rw-r--r--src/db.rs17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/db.rs b/src/db.rs
index ef5a58f..e9e2c62 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -15,16 +15,19 @@ pub struct DB {
impl DB {
pub fn new<P: AsRef<std::path::Path>>(path: &P) -> Result<DB> {
- if !path.as_ref().exists() {
- Self::create(path)?;
+ let conn = if path.as_ref().exists() {
+ rusqlite::Connection::open(path)?
}
+ else {
+ Self::create(path)?
+ };
- return Ok(DB {
- conn: rusqlite::Connection::open(path)?,
- })
+ return Ok(DB { conn })
}
- fn create<P: AsRef<std::path::Path>>(path: &P) -> Result<()> {
+ fn create<P: AsRef<std::path::Path>>(
+ path: &P
+ ) -> Result<rusqlite::Connection> {
println!(
"Initializing database at {}",
path.as_ref().to_string_lossy(),
@@ -34,7 +37,7 @@ impl DB {
std::fs::create_dir_all(parent)?;
let conn = rusqlite::Connection::open(path)?;
conn.execute(SCHEMA, rusqlite::NO_PARAMS)?;
- Ok(())
+ Ok(conn)
}
else {
unimplemented!();