From 5c985cc49d8c32303744cc67ba2c8231ecfd7a6a Mon Sep 17 00:00:00 2001 From: threeoh6000 Date: Sun, 24 Nov 2024 17:04:40 +0000 Subject: [PATCH] Channel fetching. --- src/db/mod.rs | 11 ++++++++++- src/db/sqlite.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 8 +++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index c04fd2c..19559e3 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -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 { + 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 { diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs index a9db405..94d4dbd 100644 --- a/src/db/sqlite.rs +++ b/src/db/sqlite.rs @@ -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 { + 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::("uuid"); + let name = statement.read::("name"); + let widgets = statement.read::("widgets"); + + if uuid.is_ok() && name.is_ok() && widgets.is_ok() { + let uuid_result = Uuid::parse_str(&uuid.unwrap()); + let widgets_result: Vec> = 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(()); + } } diff --git a/src/main.rs b/src/main.rs index 35da21c..dabd515 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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!(); + } }