diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-11-05 01:48:16 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-11-05 01:48:16 -0500 |
commit | 93f6e7083af1db40b16db049b62025f8cdcbbc5a (patch) | |
tree | 5712629b46b4ce249f77b7d9a6472cf8cf0872af /src | |
parent | 0206a6ebbb41050730697e0d658f7893e46dca03 (diff) | |
download | lastfm-query-93f6e7083af1db40b16db049b62025f8cdcbbc5a.tar.gz lastfm-query-93f6e7083af1db40b16db049b62025f8cdcbbc5a.zip |
start sketching out some db stuff
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 43 | ||||
-rw-r--r-- | src/error.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/paths.rs | 8 |
4 files changed, 64 insertions, 1 deletions
diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..ef5a58f --- /dev/null +++ b/src/db.rs @@ -0,0 +1,43 @@ +use error::Result; + +const SCHEMA: &'static str = " + CREATE TABLE `tracks` ( + artist varchar(1024) NOT NULL, + album varchar(1024) DEFAULT NULL, + name varchar(1024) NOT NULL, + timestamp integer(11) NOT NULL + ); +"; + +pub struct DB { + conn: rusqlite::Connection, +} + +impl DB { + pub fn new<P: AsRef<std::path::Path>>(path: &P) -> Result<DB> { + if !path.as_ref().exists() { + Self::create(path)?; + } + + return Ok(DB { + conn: rusqlite::Connection::open(path)?, + }) + } + + fn create<P: AsRef<std::path::Path>>(path: &P) -> Result<()> { + println!( + "Initializing database at {}", + path.as_ref().to_string_lossy(), + ); + + if let Some(parent) = path.as_ref().parent() { + std::fs::create_dir_all(parent)?; + let conn = rusqlite::Connection::open(path)?; + conn.execute(SCHEMA, rusqlite::NO_PARAMS)?; + Ok(()) + } + else { + unimplemented!(); + } + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..b508e3e --- /dev/null +++ b/src/error.rs @@ -0,0 +1,3 @@ +use failure::Error; + +pub type Result<T> = std::result::Result<T, Error>; diff --git a/src/main.rs b/src/main.rs index e7a11a9..f1018d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,12 @@ +extern crate directories; +extern crate failure; +extern crate rusqlite; + +mod error; +mod paths; +mod db; + fn main() { - println!("Hello, world!"); + let db = db::DB::new(&paths::dbpath()) + .expect("failed to create db"); } diff --git a/src/paths.rs b/src/paths.rs new file mode 100644 index 0000000..2322cea --- /dev/null +++ b/src/paths.rs @@ -0,0 +1,8 @@ +use directories::ProjectDirs; + +pub fn dbpath() -> std::path::PathBuf { + ProjectDirs::from("", "", "lastfm-query") + .expect("coudln't determine data directory") + .data_dir() + .join("tracks.sqlite") +} |