Add widget fetching.
This commit is contained in:
parent
5c985cc49d
commit
1b25a8fcc6
3 changed files with 59 additions and 2 deletions
|
@ -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 {
|
||||||
|
|
|
@ -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(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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!();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue