use core::any::Any;
use crate::{SQLParam, SQLSchemaType, SQLTable, TableRef, ToSQL};
pub trait DrizzleIndex: Send + Sync + 'static {
const INDEX_NAME: &'static str;
const COLUMN_NAMES: &'static [&'static str];
const IS_UNIQUE: bool = false;
fn table_ref() -> &'static TableRef;
}
impl<T: DrizzleIndex> SQLIndexInfo for T {
fn table(&self) -> &'static TableRef {
T::table_ref()
}
fn name(&self) -> &'static str {
T::INDEX_NAME
}
fn columns(&self) -> &'static [&'static str] {
T::COLUMN_NAMES
}
fn is_unique(&self) -> bool {
T::IS_UNIQUE
}
}
pub trait SQLIndexInfo: Any + Send + Sync {
fn table(&self) -> &'static TableRef;
fn name(&self) -> &'static str;
fn columns(&self) -> &'static [&'static str];
fn is_unique(&self) -> bool {
false
}
}
impl core::fmt::Debug for dyn SQLIndexInfo {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("SQLIndexInfo")
.field("name", &self.name())
.field("is_unique", &self.is_unique())
.field("columns", &self.columns())
.field("table", &self.table().name)
.finish()
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a SQL index for this dialect",
label = "ensure this type was derived with #[SQLiteIndex] or #[PostgresIndex]"
)]
pub trait SQLIndex<'a, Type: SQLSchemaType, Value: SQLParam + 'a>:
SQLIndexInfo + ToSQL<'a, Value>
{
type Table: SQLTable<'a, Type, Value>;
}