use crate::prelude::*;
use crate::{ToSQL, sql::SQL, traits::SQLParam};
use core::any::Any;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SchemaName(pub &'static str);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct TableName(pub &'static str);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ColumnName(pub &'static str);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct IndexName(pub &'static str);
pub trait SQLEnumInfo: Any + Send + Sync {
fn name(&self) -> &'static str;
fn create_type_sql(&self) -> String;
fn variants(&self) -> &'static [&'static str];
}
pub trait AsEnumInfo: SQLEnumInfo {
fn as_enum(&self) -> &dyn SQLEnumInfo;
}
impl<T: SQLEnumInfo> AsEnumInfo for T {
fn as_enum(&self) -> &dyn SQLEnumInfo {
self
}
}
impl core::fmt::Debug for dyn SQLEnumInfo {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SQLEnumInfo")
.field("name", &self.name())
.field("variants", &self.variants())
.finish()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OrderBy {
Asc,
Desc,
}
impl OrderBy {
pub fn asc<'a, V, T>(column: T) -> SQL<'a, V>
where
V: SQLParam + 'a,
T: ToSQL<'a, V>,
{
column.to_sql().append(&Self::Asc)
}
pub fn desc<'a, V, T>(column: T) -> SQL<'a, V>
where
V: SQLParam + 'a,
T: ToSQL<'a, V>,
{
column.to_sql().append(&Self::Desc)
}
}
impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for OrderBy {
fn to_sql(&self) -> SQL<'a, V> {
let sql_str = match self {
OrderBy::Asc => "ASC",
OrderBy::Desc => "DESC",
};
SQL::raw(sql_str)
}
}