Device fetching.
This commit is contained in:
parent
1c89aec6ff
commit
9f28112d41
4 changed files with 92 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
// Database abstraction.
|
// Database abstraction.
|
||||||
use crate::schema::DatabaseCredentials;
|
use crate::schema::{Device, DatabaseCredentials};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use uuid::Uuid;
|
||||||
mod sqlite;
|
mod sqlite;
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
@ -35,6 +36,28 @@ impl DbAbstraction {
|
||||||
}
|
}
|
||||||
return Err("Invalid database source.".to_string());
|
return Err("Invalid database source.".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn init(&mut self) -> Result<(), ()> {
|
||||||
|
if self.source == DbSource::SQLITE {
|
||||||
|
let db = self.sqlite.as_mut().unwrap();
|
||||||
|
let result = db.create_schema();
|
||||||
|
if result.is_ok() {
|
||||||
|
return Ok(());
|
||||||
|
} else {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_device(&mut self, uuid: Uuid) -> Result<Device, ()> {
|
||||||
|
if self.source == DbSource::SQLITE {
|
||||||
|
let db = self.sqlite.as_mut().unwrap();
|
||||||
|
let result = db.get_device(uuid);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for DbAbstraction {
|
impl fmt::Debug for DbAbstraction {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
// SQLite to Calamari DB abstraction.
|
// SQLite to Calamari DB abstraction.
|
||||||
use crate::schema::DatabaseCredentials;
|
use crate::schema::{Device, DatabaseCredentials};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use sqlite::State;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
connection: sqlite::Connection,
|
connection: sqlite::Connection,
|
||||||
|
@ -25,7 +27,47 @@ impl Database {
|
||||||
CREATE TABLE channels (uuid TEXT, name TEXT, widgets TEXT);
|
CREATE TABLE channels (uuid TEXT, name TEXT, widgets TEXT);
|
||||||
CREATE TABLE widgets (uuid TEXT, name TEXT, location TEXT);
|
CREATE TABLE widgets (uuid TEXT, name TEXT, location TEXT);
|
||||||
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');
|
||||||
";
|
";
|
||||||
return self.connection.execute(query);
|
return self.connection.execute(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_device(&mut self, uuid: Uuid) -> Result<Device, ()> {
|
||||||
|
let mut device = Device::new();
|
||||||
|
let query = "SELECT * FROM devices 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 channel = statement.read::<String, _>("current_channel");
|
||||||
|
|
||||||
|
if uuid.is_ok() && channel.is_ok() {
|
||||||
|
let uuid_result = Uuid::parse_str(&uuid.unwrap());
|
||||||
|
let channel_result = Uuid::parse_str(&channel.unwrap());
|
||||||
|
if uuid_result.is_ok() && channel_result.is_ok() {
|
||||||
|
device.uuid = uuid_result.unwrap();
|
||||||
|
device.current_channel = channel_result.unwrap();
|
||||||
|
return Ok(device);
|
||||||
|
} else {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
26
src/main.rs
26
src/main.rs
|
@ -1,15 +1,33 @@
|
||||||
// Calamari server main file.
|
// Calamari server main file.
|
||||||
mod schema;
|
mod schema;
|
||||||
mod db;
|
mod db;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut creds = schema::DatabaseCredentials::new();
|
let mut creds = schema::DatabaseCredentials::new();
|
||||||
creds.server = Some(":memory:".to_string());
|
creds.server = Some(":memory:".to_string());
|
||||||
let db = db::DbAbstraction::open(creds.clone(), db::DbSource::SQLITE);
|
let db_res = db::DbAbstraction::open(creds, db::DbSource::SQLITE);
|
||||||
|
let mut db;
|
||||||
|
|
||||||
if db.is_ok() {
|
if db_res.is_ok() {
|
||||||
println!("Opened database with following credentials and source: {:?}, {:?}", creds, db);
|
println!("Opened database successfully.");
|
||||||
|
db = db_res.unwrap();
|
||||||
} else {
|
} else {
|
||||||
println!("{}", db.unwrap_err());
|
panic!("{}", db_res.unwrap_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
let res = db.init();
|
||||||
|
if res.is_ok() {
|
||||||
|
println!("Initialised database.");
|
||||||
|
} else {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
|
||||||
|
let dev = db.get_device(Uuid::max());
|
||||||
|
if dev.is_ok() {
|
||||||
|
println!("Got device.");
|
||||||
|
println!("{:#?}", dev.unwrap());
|
||||||
|
} else {
|
||||||
|
panic!();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,12 @@ impl DatabaseCredentials {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Device {
|
pub struct Device {
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
pub current_channel: Option<Channel>
|
pub current_channel: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Device {
|
impl Device {
|
||||||
pub fn new() -> Device {
|
pub fn new() -> Device {
|
||||||
return Device { uuid: Uuid::nil(), current_channel: None };
|
return Device { uuid: Uuid::nil(), current_channel: Uuid::nil() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ impl Device {
|
||||||
pub struct Channel {
|
pub struct Channel {
|
||||||
pub uuid: Uuid,
|
pub uuid: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub widgets: Vec<Widget>,
|
pub widgets: Vec<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Channel {
|
impl Channel {
|
||||||
|
|
Loading…
Reference in a new issue