Ahojte Kolegovia, ozivujem temu, nakolko pribudli skusenosti ohladom Rustu, tak davam na zretel lepsi navrh

za pozornost stoji "create_table" v "impl Database" (1)
pripadne "pub fn push<T: CreateTable + ToDatabase>(&mut self, data: &T) " v (2)
Samozrejme je mozne nahradit Vec -> HashSet - to je kazdeho volba

/* temp */
/* use MYSQL::Connection; */
pub struct Connection {}
impl Connection {
pub fn execute(/* ... */){/* ... */}
}
pub enum DatabaseMembers {
X,
Y,
Z
}
// Various Structs
pub struct X {
pub name: String
}
pub struct Y {
pub text: String
}
pub struct Z {
pub value: u64
}
/* Traits */
pub trait CreateTable {
fn create_table(c: &Connection) -> Result<(), Error>;
fn table_type() -> DatabaseMembers;
}
pub trait FromDatabase : Sized {
fn get(c: &Connection) -> Result<Self, Error>;
}
pub trait ToDatabase {
fn push(c: &Connection, data: &Self) -> Result<(), Error>;
}
/* Trait implementation */
/* Impl for X */
impl CreateTable for X {
fn table_type() -> DatabaseMembers{
DatabaseMembers::X;
}
fn create_table(c: &Connection) -> Result<(), Error>{
let query =
"CREATE TABLE IF NOT EXISTS X (
id INTEGER PRIMARY KEY,
text TEXT NOT NULL,
);";
Ok(())
}
}
impl ToDatabase for X {
fn push(c: &Connection, data: &Self) -> Result<(), PError> {
let query =
"INSERT INTO ...
(name, ...)
VALUES
(?1, ...)
";
}
}
impl FromDatabase for X {
fn get(c: &Connection, id: usize) -> Result<Self, Error>{
let query =
"SELECT *
FROM ...
WHERE ... id=?1
";
/* ... */
Ok(X{ /*... */})
}
}
/* Impl for Y */
impl CreateTable for Y {
fn table_type() -> DatabaseMembers{
DatabaseMembers::Y;
}
fn create_table(c: &Connection) -> Result<(), Error>{
let query =
"CREATE TABLE IF NOT EXISTS Y (
id INTEGER PRIMARY KEY,
text TEXT NOT NULL,
);";
Ok(())
}
}
impl ToDatabase for Y {
fn push(c: &Connection, data: &Self) -> Result<(), PError> {
let query =
"INSERT INTO ...
(name, ...)
VALUES
(?1, ...)
";
}
}
impl FromDatabase for Y {
fn get(c: &Connection, id: usize) -> Result<Self, Error>{
let query =
"SELECT *
FROM ...
WHERE ... id=?1
";
/* ... */
Ok(Y{ /*... */})
}
}
/* Impl for Z */
/* ... */
pub struct Database {
c: Connection,
initialized_tables: Vec<DatabaseTables>,
}
impl Database {
pub fn open_temporary() -> Database {
let mut db = Database {
c: Connection::open_in_memory(),
}
/* ... */
Ok(db)
}
/* (1) - Explicitne vytvorim tabulku ...*/
pub fn create_table<T: CreateTable>(&mut self) -> Result<(), PError> {
if !self.initialized_tables.contains(&T::table_type()) {
let dbtable = T::create_table(&self.conn)?;
self.initialized_tables.push(dbtable);
}
Ok(())
}
pub fn push<T: CreateTable + ToDatabase>(&mut self, data: &T) {
/* (2) - alebo implicitne vytvorim tabulku */
if !self.initialized_tables.contains(&T::table_type()) {
T::create_table(&self.conn)?;
self.initialized_tables.push(&T::table_type());
}
ToDatabase::push(&self.conn, data)
}
pub fn get<T: FromDatabase>(&self, id: usize) -> Result<T, PError>{
FromDatabase::get(&self.conn, uid)
}
}
fn main() {
let mut db: Database = Database::open_temporary();
/* (1) - explicitne vytvorim tabulku pre X */
db.create_table::<X>();
let x = X {name: String::from("Ahoj")};
db.push(&x);
// (2) - implicitne vytvorim tabulku pre Y */
let y = Y {text: String::from("Ahoj")};
db.push(&y);
}
Pekny vecer