Add widget fetching.

This commit is contained in:
abbie 2024-11-24 18:21:40 +00:00
parent 5c985cc49d
commit 1b25a8fcc6
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
3 changed files with 59 additions and 2 deletions

View file

@ -1,5 +1,5 @@
// Database abstraction. // Database abstraction.
use crate::schema::{Channel, Device, DatabaseCredentials}; use crate::schema::{Widget, Channel, Device, DatabaseCredentials};
use std::fmt; use std::fmt;
use uuid::Uuid; use uuid::Uuid;
mod sqlite; mod sqlite;
@ -67,6 +67,15 @@ impl DbAbstraction {
} }
return Err(()); return Err(());
} }
pub fn get_widget(&mut self, uuid: Uuid) -> Result<Widget, ()> {
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 { impl fmt::Debug for DbAbstraction {

View file

@ -1,5 +1,5 @@
// SQLite to Calamari DB abstraction. // SQLite to Calamari DB abstraction.
use crate::schema::{Channel, Device, DatabaseCredentials}; use crate::schema::{Widget, Channel, Device, DatabaseCredentials};
use std::path::PathBuf; use std::path::PathBuf;
use sqlite::State; use sqlite::State;
use uuid::Uuid; use uuid::Uuid;
@ -29,6 +29,7 @@ impl Database {
CREATE TABLE users (uuid TEXT, username TEXT, password 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'); 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 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); return self.connection.execute(query);
} }
@ -118,4 +119,44 @@ impl Database {
return Err(()); return Err(());
} }
pub fn get_widget(&mut self, uuid: Uuid) -> Result<Widget, ()> {
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::<String, _>("uuid");
let name = statement.read::<String, _>("name");
let location = statement.read::<String, _>("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(());
}
} }

View file

@ -36,4 +36,11 @@ fn main() {
} else { } else {
panic!(); panic!();
} }
let wid = db.get_widget(Uuid::max());
if wid.is_ok() {
println!("{:#?}", wid.unwrap());
} else {
panic!();
}
} }