use std::{any::type_name, fmt};
use crate::{Arguments, Delete, Driver, Execute, Insert, Select, Table, Update};
#[derive(Clone)]
pub struct Database<D: Driver>(D);
impl<D: Driver> fmt::Debug for Database<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Database")
.field(&type_name::<D>())
.field(&type_name::<D::Dialect>())
.finish()
}
}
impl<D: Driver> Database<D> {
pub fn new(driver: D) -> Self {
Self(driver)
}
pub fn driver(&self) -> &D {
&self.0
}
pub fn select<T: Table>(&self, table: T) -> Select<D, T> {
let _ = table;
Select::new(self.0.clone())
}
pub fn insert<T: Table>(&self, table: T) -> Insert<D, T> {
let _ = table;
Insert::new(self.0.clone())
}
pub fn update<T: Table>(&self, table: T) -> Update<D, T> {
let _ = table;
Update::new(self.0.clone())
}
pub fn delete<T: Table>(&self, table: T) -> Delete<D, T> {
let _ = table;
Delete::new(self.0.clone())
}
pub fn execute<'a, T: Arguments<'a, D>>(
&self,
(sql, arguments): (&'static str, T),
) -> Execute<'a, D> {
Execute::new(self.0.clone(), sql, arguments.into_driver_arguments())
}
pub fn execute_unsafe<'a, T: Arguments<'a, D>>(
&self,
(sql, arguments): (&'a str, T),
) -> Execute<'a, D> {
Execute::new(self.0.clone(), sql, arguments.into_driver_arguments())
}
}