From 1b25a8fcc69ebbda54e61f0ca11a803c7867bff5 Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Sun, 24 Nov 2024 18:21:40 +0000 Subject: [PATCH] Add widget fetching. --- src/db/mod.rs | 11 ++++++++++- src/db/sqlite.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 7 +++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index 19559e3..b7e7b1d 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,5 +1,5 @@ // Database abstraction. -use crate::schema::{Channel, Device, DatabaseCredentials}; +use crate::schema::{Widget, Channel, Device, DatabaseCredentials}; use std::fmt; use uuid::Uuid; mod sqlite; @@ -67,6 +67,15 @@ impl DbAbstraction { } return Err(()); } + + pub fn get_widget(&mut self, uuid: Uuid) -> Result { + if self.source == DbSource::SQLITE { + let db = self.sqlite.as_mut().unwrap(); + let result = db.get_widget(uuid); + return result; + } + return Err(()); + } } impl fmt::Debug for DbAbstraction { diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs index 94d4dbd..7109b13 100644 --- a/src/db/sqlite.rs +++ b/src/db/sqlite.rs @@ -1,5 +1,5 @@ // SQLite to Calamari DB abstraction. -use crate::schema::{Channel, Device, DatabaseCredentials}; +use crate::schema::{Widget, Channel, Device, DatabaseCredentials}; use std::path::PathBuf; use sqlite::State; use uuid::Uuid; @@ -29,6 +29,7 @@ impl Database { 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'); INSERT INTO channels (uuid, name, widgets) VALUES('ffffffff-ffff-ffff-ffff-ffffffffffff', 'Unsubscribed', 'ffffffff-ffff-ffff-ffff-ffffffffffff,ffffffff-ffff-ffff-ffff-fffffffffff0'); + INSERT INTO widgets (uuid, name, location) VALUES('ffffffff-ffff-ffff-ffff-ffffffffffff', 'LCD Clock', 'widgets/ffffffff-ffff-ffff-ffff-ffffffffffff.swf'); "; return self.connection.execute(query); } @@ -118,4 +119,44 @@ impl Database { return Err(()); } + + pub fn get_widget(&mut self, uuid: Uuid) -> Result { + let mut widget = Widget::new(); + let query = "SELECT * FROM widgets 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 name = statement.read::("name"); + let location = statement.read::("location"); + + if uuid.is_ok() && name.is_ok() && location.is_ok() { + let uuid_result = Uuid::parse_str(&uuid.unwrap()); + if uuid_result.is_ok() { + widget.uuid = uuid_result.unwrap(); + widget.name = name.unwrap(); + widget.location = PathBuf::from(location.unwrap()); + return Ok(widget); + } else { + return Err(()); + } + } else { + return Err(()); + } + } + + return Err(()); + } } diff --git a/src/main.rs b/src/main.rs index dabd515..955cdd9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,4 +36,11 @@ fn main() { } else { panic!(); } + + let wid = db.get_widget(Uuid::max()); + if wid.is_ok() { + println!("{:#?}", wid.unwrap()); + } else { + panic!(); + } }