progress in database management

This commit is contained in:
Abraham Toriz 2021-06-21 11:12:30 -05:00
parent 1655803076
commit 47f4086c59
No known key found for this signature in database
GPG Key ID: D5B4A746DB5DD42A
8 changed files with 89 additions and 14 deletions

3
Cargo.lock generated
View File

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.7.4"
@ -256,6 +258,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57adcf67c8faaf96f3248c2a7b419a0dbc52ebe36ba83dd57fe83827c1ea4eb3"
dependencies = [
"bitflags",
"chrono",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",

View File

@ -9,7 +9,7 @@ edition = "2018"
[dependencies]
clap = "2"
chrono = "0.4"
rusqlite = "0.25.3"
rusqlite = { version = "0.25.3", features = ["chrono"] }
thiserror = "*"
dirs = "*"
serde = { version = "1.0", features = ["derive"] }

View File

@ -2,8 +2,13 @@ use std::convert::TryFrom;
use std::io::Write;
use clap::ArgMatches;
use chrono::{DateTime, Utc};
use crate::{database::Database, error, types::Time, editor, commands::Command};
use crate::database::Database;
use crate::error;
use crate::types::Time;
use crate::editor;
use crate::commands::Command;
pub struct Args {
at: Option<Time>,
@ -58,7 +63,7 @@ mod tests {
at: None,
note: Some("hola".into()),
};
let out = Vec::new();
let mut out = Vec::new();
assert!(false, "there are no entries");

View File

@ -1,30 +1,80 @@
use rusqlite::Connection;
use std::path::Path;
use crate::{types::Time, error};
use rusqlite::{Connection, ToSql};
use chrono::{DateTime, Utc};
use crate::error;
use crate::models::{Entry, Meta};
use crate::types::Time;
pub trait Database {
fn entry_insert(&mut self, at: Time, note: String) -> error::Result<()>;
/// This is used to create tables and insert rows
fn execute(&mut self, query: &str, params: &[&dyn ToSql]) -> error::Result<()>;
/// And this is used to retrieve data
fn entry_query(&self, query: &str, params: &[&dyn ToSql]) -> error::Result<Vec<Entry>>;
fn meta_query(&self, query: &str, params: &[&dyn ToSql]) -> error::Result<Vec<Meta>>;
fn entries_by_sheet(&mut self, sheet: String) -> error::Result<Vec<Entry>> {
self.entry_query("some".into(), &[])
}
fn entry_insert(&mut self, at: Time, note: String) -> error::Result<()> {
self.execute("", &[])
}
}
pub struct SqliteDatabase {
connection: Connection,
}
impl SqliteDatabase {
pub fn from_memory() -> error::Result<impl Database> {
Ok(SqliteDatabase {
connection: Connection::open_in_memory()?,
})
}
pub fn from_path(path: &str) -> error::Result<impl Database> {
let conn = Connection::open(path)?;
pub fn from_path<P: AsRef<Path>>(path: P) -> error::Result<impl Database> {
Ok(SqliteDatabase {
connection: Connection::open(path)?,
})
}
}
impl Database for SqliteDatabase {
fn entry_insert(&mut self, at: Time, note: String) -> error::Result<()> {
unimplemented!()
fn execute(&mut self, query: &str, params: &[&dyn ToSql]) -> error::Result<()> {
self.connection.execute(query, params)?;
Ok(())
}
fn entry_query(&self, query: &str, params: &[&dyn ToSql]) -> error::Result<Vec<Entry>> {
let mut stmt = self.connection.prepare(query)?;
let results = stmt.query_map([], |row| {
let x = Ok(Entry {
id: row.get("id")?,
note: row.get("note")?,
start: row.get("start")?,
end: row.get("end")?,
sheet: row.get("sheet")?,
});
x})?.filter_map(|r| r.ok()).collect();
Ok(results)
}
fn meta_query(&self, query: &str, params: &[&dyn ToSql]) -> error::Result<Vec<Meta>> {
let mut stmt = self.connection.prepare(query)?;
let results = stmt.query_map([], |row| Ok(Meta {
id: row.get("id")?,
key: row.get("key")?,
value: row.get("value")?,
}))?.filter_map(|r| r.ok()).collect();
Ok(results)
}
}

View File

@ -25,6 +25,9 @@ pub enum Error {
#[error("Couldn't parse yaml file at: {path}\nwith error: {error}")]
YamlError{ path: PathBuf, error: serde_yaml::Error},
#[error("Could not parse datetime: {0}")]
DateTimeParseError(#[from] chrono::format::ParseError),
}
pub type Result<T> = result::Result<T, Error>;

View File

@ -5,3 +5,4 @@ pub mod config;
pub mod editor;
pub mod formatters;
pub mod error;
pub mod models;

View File

@ -12,9 +12,7 @@ use tiempo::commands::{ Command, r#in::InCommand, display::DisplayCommand, };
fn error_trap(matches: ArgMatches) -> error::Result<()> {
let config = Config::read()?;
dbg!(config);
let mut conn = SqliteDatabase::from_path("db.sqlite3")?;
let mut conn = SqliteDatabase::from_path(config.database_file)?;
let mut out = io::stdout();
match matches.subcommand() {

15
src/models.rs Normal file
View File

@ -0,0 +1,15 @@
use chrono::{DateTime, Utc};
pub struct Entry {
pub id: u64,
pub note: String,
pub start: DateTime<Utc>,
pub end: DateTime<Utc>,
pub sheet: String,
}
pub struct Meta {
pub id: u64,
pub key: String,
pub value: String,
}