From 9f28112d4144bc5560eb70d62f5b717ee21d5771 Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Sun, 24 Nov 2024 14:17:22 +0000 Subject: [PATCH] Device fetching. --- src/db/mod.rs | 25 ++++++++++++++++++++++++- src/db/sqlite.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 26 ++++++++++++++++++++++---- src/schema.rs | 6 +++--- 4 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index dcf45d8..c04fd2c 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,6 +1,7 @@ // Database abstraction. -use crate::schema::DatabaseCredentials; +use crate::schema::{Device, DatabaseCredentials}; use std::fmt; +use uuid::Uuid; mod sqlite; #[non_exhaustive] @@ -35,6 +36,28 @@ impl DbAbstraction { } return Err("Invalid database source.".to_string()); } + + pub fn init(&mut self) -> Result<(), ()> { + if self.source == DbSource::SQLITE { + let db = self.sqlite.as_mut().unwrap(); + let result = db.create_schema(); + if result.is_ok() { + return Ok(()); + } else { + return Err(()); + } + } + return Err(()); + } + + pub fn get_device(&mut self, uuid: Uuid) -> Result { + if self.source == DbSource::SQLITE { + let db = self.sqlite.as_mut().unwrap(); + let result = db.get_device(uuid); + return result; + } + return Err(()); + } } impl fmt::Debug for DbAbstraction { diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs index 016b302..a9db405 100644 --- a/src/db/sqlite.rs +++ b/src/db/sqlite.rs @@ -1,6 +1,8 @@ // SQLite to Calamari DB abstraction. -use crate::schema::DatabaseCredentials; +use crate::schema::{Device, DatabaseCredentials}; use std::path::PathBuf; +use sqlite::State; +use uuid::Uuid; pub struct Database { connection: sqlite::Connection, @@ -25,7 +27,47 @@ impl Database { CREATE TABLE channels (uuid TEXT, name TEXT, widgets TEXT); CREATE TABLE widgets (uuid TEXT, name TEXT, location TEXT); CREATE TABLE users (uuid TEXT, username TEXT, password TEXT); + INSERT INTO devices (uuid, current_channel) VALUES('ffffffff-ffff-ffff-ffff-ffffffffffff', 'ffffffff-ffff-ffff-ffff-ffffffffffff'); "; return self.connection.execute(query); } + + pub fn get_device(&mut self, uuid: Uuid) -> Result { + let mut device = Device::new(); + let query = "SELECT * FROM devices WHERE uuid = ? LIMIT 1"; + let mut statement; + + let statement_res = self.connection.prepare(query); + if statement_res.is_err() { + return Err(()); + } else { + statement = statement_res.unwrap(); + } + + let result = statement.bind((1, uuid.to_string().as_str())); + if result.is_err() { + return Err(()); + } + + while let Ok(State::Row) = statement.next() { + let uuid = statement.read::("uuid"); + let channel = statement.read::("current_channel"); + + if uuid.is_ok() && channel.is_ok() { + let uuid_result = Uuid::parse_str(&uuid.unwrap()); + let channel_result = Uuid::parse_str(&channel.unwrap()); + if uuid_result.is_ok() && channel_result.is_ok() { + device.uuid = uuid_result.unwrap(); + device.current_channel = channel_result.unwrap(); + return Ok(device); + } else { + return Err(()); + } + } else { + return Err(()); + } + } + + return Err(()); + } } diff --git a/src/main.rs b/src/main.rs index 259c491..35da21c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,33 @@ // Calamari server main file. mod schema; mod db; +use uuid::Uuid; fn main() { let mut creds = schema::DatabaseCredentials::new(); creds.server = Some(":memory:".to_string()); - let db = db::DbAbstraction::open(creds.clone(), db::DbSource::SQLITE); + let db_res = db::DbAbstraction::open(creds, db::DbSource::SQLITE); + let mut db; - if db.is_ok() { - println!("Opened database with following credentials and source: {:?}, {:?}", creds, db); + if db_res.is_ok() { + println!("Opened database successfully."); + db = db_res.unwrap(); } else { - println!("{}", db.unwrap_err()); + panic!("{}", db_res.unwrap_err()); + } + + let res = db.init(); + if res.is_ok() { + println!("Initialised database."); + } else { + panic!(); + } + + let dev = db.get_device(Uuid::max()); + if dev.is_ok() { + println!("Got device."); + println!("{:#?}", dev.unwrap()); + } else { + panic!(); } } diff --git a/src/schema.rs b/src/schema.rs index 76e683a..d7f783c 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -20,12 +20,12 @@ impl DatabaseCredentials { #[derive(Debug, Clone)] pub struct Device { pub uuid: Uuid, - pub current_channel: Option + pub current_channel: Uuid, } impl Device { pub fn new() -> Device { - return Device { uuid: Uuid::nil(), current_channel: None }; + return Device { uuid: Uuid::nil(), current_channel: Uuid::nil() }; } } @@ -33,7 +33,7 @@ impl Device { pub struct Channel { pub uuid: Uuid, pub name: String, - pub widgets: Vec, + pub widgets: Vec, } impl Channel {