summaryrefslogtreecommitdiffstats
path: root/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.rs')
-rw-r--r--src/db.rs43
1 files changed, 43 insertions, 0 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!();
+ }
+ }
+}