Channel fetching.

This commit is contained in:
abbie 2024-11-24 17:04:40 +00:00
parent 9f28112d41
commit 5c985cc49d
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
3 changed files with 66 additions and 3 deletions

View file

@ -1,5 +1,5 @@
// Database abstraction.
use crate::schema::{Device, DatabaseCredentials};
use crate::schema::{Channel, Device, DatabaseCredentials};
use std::fmt;
use uuid::Uuid;
mod sqlite;
@ -58,6 +58,15 @@ impl DbAbstraction {
}
return Err(());
}
pub fn get_channel(&mut self, uuid: Uuid) -> Result<Channel, ()> {
if self.source == DbSource::SQLITE {
let db = self.sqlite.as_mut().unwrap();
let result = db.get_channel(uuid);
return result;
}
return Err(());
}
}
impl fmt::Debug for DbAbstraction {

View file

@ -1,5 +1,5 @@
// SQLite to Calamari DB abstraction.
use crate::schema::{Device, DatabaseCredentials};
use crate::schema::{Channel, Device, DatabaseCredentials};
use std::path::PathBuf;
use sqlite::State;
use uuid::Uuid;
@ -28,6 +28,7 @@ impl Database {
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');
INSERT INTO channels (uuid, name, widgets) VALUES('ffffffff-ffff-ffff-ffff-ffffffffffff', 'Unsubscribed', 'ffffffff-ffff-ffff-ffff-ffffffffffff,ffffffff-ffff-ffff-ffff-fffffffffff0');
";
return self.connection.execute(query);
}
@ -70,4 +71,51 @@ impl Database {
return Err(());
}
pub fn get_channel(&mut self, uuid: Uuid) -> Result<Channel, ()> {
let mut channel = Channel::new();
let query = "SELECT * FROM channels 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 widgets = statement.read::<String, _>("widgets");
if uuid.is_ok() && name.is_ok() && widgets.is_ok() {
let uuid_result = Uuid::parse_str(&uuid.unwrap());
let widgets_result: Vec<Result<Uuid, _>> = widgets.unwrap().split(",").map(|x| Uuid::parse_str(&x)).collect();
if uuid_result.is_ok() {
for res in widgets_result {
if res.is_err() {
return Err(());
} else {
channel.widgets.push(res.unwrap());
}
}
channel.uuid = uuid_result.unwrap();
channel.name = name.unwrap();
return Ok(channel);
} else {
return Err(());
}
} else {
return Err(());
}
}
return Err(());
}
}

View file

@ -25,9 +25,15 @@ fn main() {
let dev = db.get_device(Uuid::max());
if dev.is_ok() {
println!("Got device.");
println!("{:#?}", dev.unwrap());
} else {
panic!();
}
let chn = db.get_channel(Uuid::max());
if chn.is_ok() {
println!("{:#?}", chn.unwrap());
} else {
panic!();
}
}