From 1fd49b866455f91d869d1659bc2f9cbb25362585 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 16 Aug 2022 23:36:50 +0300 Subject: [PATCH] repo/sqlite: add base schema --- Cargo.lock | 85 +++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 7 +- src/repo.rs | 2 + src/repo/sqlite.rs | 60 +++++++++++++++ src/repo/sqlite/migrations/202208162308.sql | 25 ++++++ src/repo/sqlite/schema.sql | 24 ++++++ 7 files changed, 202 insertions(+), 2 deletions(-) create mode 100644 src/repo/sqlite.rs create mode 100644 src/repo/sqlite/migrations/202208162308.sql create mode 100644 src/repo/sqlite/schema.sql diff --git a/Cargo.lock b/Cargo.lock index 26cbaf5..69a7339 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -14,6 +25,12 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "cc" +version = "1.0.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" + [[package]] name = "cfg-if" version = "1.0.0" @@ -76,6 +93,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + [[package]] name = "getrandom" version = "0.2.7" @@ -92,6 +121,18 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashlink" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d452c155cb93fecdfb02a73dd57b5d8e442c2063bd7aac72f1bc5e4263a43086" +dependencies = [ + "hashbrown", +] [[package]] name = "heck" @@ -121,6 +162,17 @@ version = "0.2.127" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b" +[[package]] +name = "libsqlite3-sys" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f0455f2c1bc9a7caa792907026e469c1d91761fb0ea37cbb16427c77280cf35" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "log" version = "0.4.17" @@ -142,6 +194,12 @@ version = "6.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "648001efe5d5c0102d8cea768e348da85d90af8ba91f0bea908f157951493cd4" +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -204,6 +262,20 @@ dependencies = [ "thiserror", ] +[[package]] +name = "rusqlite" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01e213bc3ecb39ac32e81e51ebe31fd888a940515173e3a18a35f8c6e896422a" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + [[package]] name = "ryu" version = "1.0.11" @@ -241,6 +313,12 @@ dependencies = [ "serde", ] +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + [[package]] name = "syn" version = "1.0.99" @@ -258,6 +336,7 @@ version = "0.1.0" dependencies = [ "clap", "log", + "rusqlite", "serde", "serde_json", "xdg", @@ -295,6 +374,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 0c82a22..5811503 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ maintenance = { status = "experimental" } [dependencies] clap = { version = "3.2.16", default-features = false, features = ["derive", "std"] } log = "0.4.17" +rusqlite = { version = "0.28.0", features = ["bundled"] } serde = { version = "1.0.142", features = ["derive"] } serde_json = "1.0.83" xdg = "2.4.1" diff --git a/src/main.rs b/src/main.rs index 86c5c15..246456c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ //--------------------------------------------------------------------- use clap::Parser; -use repo::fs::FsRepo; +use repo::sqlite::SqliteRepo; use xdg::BaseDirectories; mod cli; @@ -38,7 +38,10 @@ fn main() { let args = cli::Args::parse(); let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); - let repo = FsRepo::new(xdg_dirs); + let repo = match SqliteRepo::new(xdg_dirs) { + Ok(repo) => repo, + Err(err) => return eprintln!("Cannot connect to repository: {}", err), + }; match args.command { cli::SubCommand::Add(args) => { diff --git a/src/repo.rs b/src/repo.rs index 6c6ca09..d27c6a3 100755 --- a/src/repo.rs +++ b/src/repo.rs @@ -14,6 +14,7 @@ //! along with tas. If not, see . //! pub mod fs; +pub mod sqlite; use std::path::PathBuf; @@ -21,6 +22,7 @@ use crate::domain; #[derive(Debug)] pub enum Error { + Connect, NotFound, InvalidData, InsertData, diff --git a/src/repo/sqlite.rs b/src/repo/sqlite.rs new file mode 100644 index 0000000..51b9aac --- /dev/null +++ b/src/repo/sqlite.rs @@ -0,0 +1,60 @@ +use rusqlite::Connection; +use xdg::BaseDirectories; + +use crate::domain; +use crate::repo::{Error, Repository}; + +const SCHEMA_FILE: &str = "schema.sql"; + +pub struct SqliteRepo { + conn: Connection, +} + +impl SqliteRepo { + pub fn new(xdg_dirs: BaseDirectories) -> Result { + let file_path = xdg_dirs.get_data_file(SCHEMA_FILE); + let conn = Connection::open(file_path).map_err(|_| Error::Connect)?; + Ok(Self { conn }) + } +} + +impl Repository for SqliteRepo { + fn get_current_task_opt(&self) -> Result, Error> { + todo!() + } + + fn get_task_opt(&self, id: domain::TaskId) -> Result, Error> { + todo!() + } + + fn get_tasks(&self) -> Result, Error> { + todo!() + } + fn remove_task(&self, id: domain::TaskId) -> Result { + todo!() + } + + fn insert_task(&self, insert_data: super::InsertTaskData) -> Result { + todo!() + } + + fn update_task( + &self, + id: domain::TaskId, + update_data: super::UpdateTaskData, + ) -> Result { + todo!() + } + + fn start_task(&self, id: domain::TaskId) -> Result { + todo!() + } + + fn stop_task(&self) -> Result { + todo!() + } + + fn finish_task(&self) -> Result { + todo!() + } +} diff --git a/src/repo/sqlite/migrations/202208162308.sql b/src/repo/sqlite/migrations/202208162308.sql new file mode 100644 index 0000000..ab812a6 --- /dev/null +++ b/src/repo/sqlite/migrations/202208162308.sql @@ -0,0 +1,25 @@ +CREATE TABLE _tas_info ( + version INTEGER PRIMARY KEY +); + +CREATE TABLE tasks ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + group TEXT NULL, + link TEXT NULL, + path TEXT NULL, + + current BOOLEAN NOT NULL DEFAULT false, + + created_at DATETIME NOT NULL DEFAULT datetime('now'), + finished_at DATETIME NULL +); + +CREATE VIEW active_tasks AS ( + SELECT * + FROM tasks + WHERE finished_at IS NULL + ORDER BY created_at +); + + diff --git a/src/repo/sqlite/schema.sql b/src/repo/sqlite/schema.sql new file mode 100644 index 0000000..6a5be81 --- /dev/null +++ b/src/repo/sqlite/schema.sql @@ -0,0 +1,24 @@ +CREATE TABLE _tas_info ( + version INTEGER PRIMARY KEY +); + +CREATE TABLE tasks ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + group TEXT NULL, + link TEXT NULL, + path TEXT NULL, + + current BOOLEAN NOT NULL DEFAULT false, + + created_at DATETIME NOT NULL DEFAULT datetime('now'), + finished_at DATETIME NULL +); + +CREATE VIEW active_tasks AS ( + SELECT * + FROM tasks + WHERE finished_at IS NULL + ORDER BY created_at +); +