use facet::{DefaultSource, Facet, Type, UserType};
use rusqlite::{Statement, types::FromSql, types::ToSql};
#[derive(thiserror::Error, Debug)]
pub enum OrmErrors {
#[error(transparent)]
StdIo(#[from] std::io::Error),
#[error("{0}")]
Custom(String),
}
impl From<String> for OrmErrors {
fn from(value: String) -> Self {
Self::Custom(value)
}
}
impl From<&str> for OrmErrors {
fn from(value: &str) -> Self {
Self::from(value.to_string())
}
}
pub type Res<T> = Result<T, OrmErrors>;
pub trait Database {
fn get_table<'d, T: Table<'d>>(&self, name: &str) -> Option<T>;
fn statement<'d>(&self) -> Statement;
}
pub struct Sqlite {}
impl Database for Sqlite {
fn get_table<'d, T: Table<'d>>(&self, name: &str) -> Option<T> {
todo!()
}
fn statement<'d>(&self) -> Statement {
todo!()
}
}
pub trait Table<'d>
where
Self: Facet<'d>,
{
const NAME: &'d str;
fn create<D: Database>(db: &'d D) -> String;
fn select_all_query<R: Facet<'d>>() -> String {
let t = R::SHAPE.ty;
if let Type::User(ut) = t {
if let UserType::Struct(s) = ut {
let mut o = String::new();
for field in s.fields {
let name = if let Some(rename) = field.rename {
rename
} else {
field.name
};
let typ = { field.shape().def };
if let Some(DefaultSource::FromTrait) = field.default {
format!("DEFAULT ");
} else if let Some(DefaultSource::Custom(c)) = field.default {
format!("DEFAULT ");
} else {
todo!()
};
format!("{name} ,");
}
return o;
} else if let UserType::Enum(e) = ut {
} else {
}
todo!()
} else {
todo!()
}
}
}
pub trait Insert<'d, T>
where
Self: Table<'d>,
{
}
pub trait Select<'d, T: Facet<'d>>
where
Self: Table<'d>,
{
}