feat: basic db connection
This commit is contained in:
@@ -1 +1 @@
|
||||
database_path localhost
|
||||
database_path /home/dqnid/.config/wordsmith/ws.db
|
||||
|
||||
50
src/main.rs
50
src/main.rs
@@ -1,8 +1,12 @@
|
||||
use std::*;
|
||||
|
||||
use rusqlite::{Connection, OpenFlags};
|
||||
|
||||
mod modules;
|
||||
use modules::config::{Config, read_config};
|
||||
|
||||
use crate::modules::source::{FileDatabase, WordSource};
|
||||
|
||||
const DEFAULT_CONFIG_PATH: &'static str = "./src/assets/wordsmith_default.conf";
|
||||
|
||||
fn main() {
|
||||
@@ -28,6 +32,50 @@ fn main() {
|
||||
|
||||
let config = read_config(DEFAULT_CONFIG_PATH, default_config); // TODO: read config from param
|
||||
|
||||
// TODO: CREATE FILE.db IF DOES NOT EXISTS
|
||||
// TODO: CREATE TABLE IF DOES NOT EXISTS
|
||||
let connection = Connection::open(&config.database_path).unwrap();
|
||||
let word_source = FileDatabase {};
|
||||
|
||||
// NOTE: testing init
|
||||
// connection
|
||||
// .execute(
|
||||
// "CREATE TABLE word (
|
||||
// id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
// key TEXT NOT NULL,
|
||||
// description TEXT NOT NULL
|
||||
// )",
|
||||
// (), // empty list of parameters.
|
||||
// )
|
||||
// .unwrap();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO word (key, description) VALUES (?1, ?2)",
|
||||
("test", "a description"),
|
||||
)
|
||||
.unwrap();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO word (key, description) VALUES (?1, ?2)",
|
||||
("another", "a description"),
|
||||
)
|
||||
.unwrap();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO word (key, description) VALUES (?1, ?2)",
|
||||
("test yet", "a description"),
|
||||
)
|
||||
.unwrap();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO word (key, description) VALUES (?1, ?2)",
|
||||
("yes yes", "a description"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
if let Ok(w_list) = word_source.find_by_word("test", &connection) {
|
||||
println!("Words! {:?}", w_list);
|
||||
}
|
||||
|
||||
println!("Hello, world! {:?}", config);
|
||||
// CREATE DB IF DOES NOT EXISTS
|
||||
}
|
||||
|
||||
@@ -1,60 +1,59 @@
|
||||
#[derive(Clone, Copy)]
|
||||
use rusqlite::Connection;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Word(String, String);
|
||||
|
||||
pub struct FileDatabase {
|
||||
path: String,
|
||||
cached_words: Option<Vec<Word>>,
|
||||
}
|
||||
pub struct FileDatabase {}
|
||||
|
||||
// TruthSource
|
||||
pub trait WordSource {
|
||||
fn load(&self) -> Result<(), ()>;
|
||||
fn find_word(&self, word: &str) -> Result<Word, ()>;
|
||||
fn find_by_word(&self, word: &str, connection: &Connection) -> Result<Vec<Word>, ()>;
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn remove_word(&self, word: &str) -> Result<(), ()>;
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()>;
|
||||
fn get_all_words(&self) -> Vec<Word>;
|
||||
}
|
||||
|
||||
impl WordSource for FileDatabase {
|
||||
fn load(&self) -> Result<(), ()> {}
|
||||
fn fuzzy_compare(a: &str, b: &str) -> bool {
|
||||
if a.eq(b) {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn find_word(&self, word: &str) -> Result<Word, ()> {
|
||||
if let Some(words) = &self.cached_words {
|
||||
if let Some(w) = words.into_iter().find(|&w| word == w.0) {
|
||||
return Ok(w.clone());
|
||||
impl WordSource for FileDatabase {
|
||||
fn find_by_word(&self, word: &str, connection: &Connection) -> Result<Vec<Word>, ()> {
|
||||
let statement = connection.prepare("select key, description from word");
|
||||
let mut word_list: Vec<Word> = vec![];
|
||||
|
||||
if let Ok(mut query) = statement {
|
||||
let mut rows = query.query([]).unwrap();
|
||||
while let Some(row) = rows.next().unwrap() {
|
||||
let found_word: String = row.get(0).unwrap();
|
||||
if fuzzy_compare(word, &found_word) {
|
||||
let found_word_description: String = row.get(0).unwrap();
|
||||
word_list.push(Word(found_word, found_word_description));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(());
|
||||
}
|
||||
// TODO: implement db alternative access
|
||||
Err(())
|
||||
Ok(word_list)
|
||||
}
|
||||
|
||||
fn insert_word(&mut self, word: Word) -> Result<(), ()> {
|
||||
self.cached_words.get_or_insert(vec![]).push(word);
|
||||
// TODO: insert into db
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn edit_word(&mut self, word: Word) -> Result<(), ()> {
|
||||
if let Some(w) = self
|
||||
.cached_words
|
||||
.get_or_insert(vec![])
|
||||
.iter()
|
||||
.find(|&w| w.0 == word.0)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_word(&self, word: &str) -> Result<(), ()> {}
|
||||
fn remove_word(&self, word: &str) -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_all_words(&self) -> Vec<Word> {
|
||||
if let Some(words) = self.cached_words {
|
||||
words
|
||||
} else {
|
||||
// Check db
|
||||
()
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user