Skip to main content

drizzle_core/traits/
view.rs

1use crate::prelude::Cow;
2use crate::traits::SQLSchemaType;
3use crate::{SQL, SQLParam, SQLTable, SQLTableInfo};
4use core::any::Any;
5
6/// Trait for database views.
7///
8/// A View is essentially a named SQL query that can be queried like a table.
9#[diagnostic::on_unimplemented(
10    message = "`{Self}` is not a SQL view for this dialect",
11    label = "ensure this type was derived with a drizzle view macro"
12)]
13pub trait SQLView<'a, Type: SQLSchemaType, Value: SQLParam + 'a>:
14    SQLTable<'a, Type, Value>
15{
16    /// Returns the SQL definition of this view (the SELECT statement).
17    fn definition(&self) -> SQL<'a, Value>;
18
19    /// Returns true if this is an existing view in the database not managed by Drizzle.
20    fn is_existing(&self) -> bool {
21        false
22    }
23}
24
25/// Metadata information about a database view.
26pub trait SQLViewInfo: SQLTableInfo + Any {
27    /// Returns the SQL definition of this view.
28    fn definition_sql(&self) -> Cow<'static, str>;
29
30    /// Returns true if this is an existing view in the database not managed by Drizzle.
31    fn is_existing(&self) -> bool {
32        false
33    }
34
35    /// Returns true if this is a materialized view.
36    fn is_materialized(&self) -> bool {
37        false
38    }
39
40    /// Returns WITH NO DATA flag for materialized views.
41    fn with_no_data(&self) -> Option<bool> {
42        None
43    }
44
45    /// Returns USING clause for materialized views.
46    fn using_clause(&self) -> Option<&'static str> {
47        None
48    }
49
50    /// Returns TABLESPACE for materialized views.
51    fn tablespace(&self) -> Option<&'static str> {
52        None
53    }
54}
55
56impl core::fmt::Debug for dyn SQLViewInfo {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        f.debug_struct("SQLViewInfo")
59            .field("name", &self.name())
60            .field("schema", &SQLTableInfo::schema(self))
61            .field("qualified_name", &SQLTableInfo::qualified_name(self))
62            .field("definition", &self.definition_sql())
63            .field("existing", &self.is_existing())
64            .field("materialized", &self.is_materialized())
65            .finish()
66    }
67}