drizzle_core/traits/
index.rs1use core::any::Any;
2
3use crate::{SQLParam, SQLSchemaType, SQLTable, TableRef, ToSQL};
4
5pub trait DrizzleIndex: Send + Sync + 'static {
10 const INDEX_NAME: &'static str;
12
13 const COLUMN_NAMES: &'static [&'static str];
15
16 const IS_UNIQUE: bool = false;
18
19 const WHERE_CLAUSE: Option<&'static str> = None;
21
22 fn table_ref() -> &'static TableRef;
24}
25
26impl<T: DrizzleIndex> SQLIndexInfo for T {
28 fn table(&self) -> &'static TableRef {
29 T::table_ref()
30 }
31
32 fn name(&self) -> &'static str {
33 T::INDEX_NAME
34 }
35
36 fn columns(&self) -> &'static [&'static str] {
37 T::COLUMN_NAMES
38 }
39
40 fn is_unique(&self) -> bool {
41 T::IS_UNIQUE
42 }
43
44 fn where_clause(&self) -> Option<&'static str> {
45 T::WHERE_CLAUSE
46 }
47}
48
49pub trait SQLIndexInfo: Any + Send + Sync {
51 fn table(&self) -> &'static TableRef;
53 fn name(&self) -> &'static str;
55
56 fn columns(&self) -> &'static [&'static str];
58
59 fn is_unique(&self) -> bool {
61 false
62 }
63
64 fn where_clause(&self) -> Option<&'static str> {
66 None
67 }
68}
69
70impl core::fmt::Debug for dyn SQLIndexInfo {
71 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
72 f.debug_struct("SQLIndexInfo")
73 .field("name", &self.name())
74 .field("is_unique", &self.is_unique())
75 .field("where_clause", &self.where_clause())
76 .field("columns", &self.columns())
77 .field("table", &self.table().name)
78 .finish()
79 }
80}
81
82#[diagnostic::on_unimplemented(
89 message = "`{Self}` is not a SQL index for this dialect",
90 label = "ensure this type was derived with #[SQLiteIndex], #[PostgresIndex], or #[MySQLIndex]"
91)]
92pub trait SQLIndex<'a, Type: SQLSchemaType, Value: SQLParam + 'a>:
93 SQLIndexInfo + ToSQL<'a, Value>
94{
95 type Table: SQLTable<'a, Type, Value>;
97}