drizzle_core/traits/
index.rs

1use core::any::Any;
2
3use crate::{SQLParam, SQLSchemaType, SQLTable, SQLTableInfo, ToSQL};
4
5pub trait SQLIndexInfo: Any + Send + Sync {
6    fn table(&self) -> &dyn SQLTableInfo;
7    /// The name of this index (for DROP INDEX statements)
8    fn name(&self) -> &'static str;
9
10    /// Whether this is a unique index
11    fn is_unique(&self) -> bool {
12        false
13    }
14}
15
16pub trait AsIndexInfo: Sized + SQLIndexInfo {
17    fn as_index(&self) -> &dyn SQLIndexInfo {
18        self
19    }
20}
21
22impl core::fmt::Debug for dyn SQLIndexInfo {
23    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24        f.debug_struct("SQLIndexInfo")
25            .field("name", &self.name())
26            .field("is_unique", &self.is_unique())
27            .field("table", &self.table())
28            .finish()
29    }
30}
31/// Trait for types that represent database indexes.
32/// Implemented by tuple structs like `struct UserEmailIdx(User::email);`
33pub trait SQLIndex<'a, Type: SQLSchemaType, Value: SQLParam + 'a>:
34    SQLIndexInfo + ToSQL<'a, Value>
35{
36    /// The table type this index is associated with
37    type Table: SQLTable<'a, Type, Value>;
38}