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.
9pub trait SQLView<'a, Type: SQLSchemaType, Value: SQLParam + 'a>:
10    SQLTable<'a, Type, Value>
11{
12    /// Returns the SQL definition of this view (the SELECT statement).
13    fn definition(&self) -> SQL<'a, Value>;
14
15    /// Returns true if this is an existing view in the database not managed by Drizzle.
16    fn is_existing(&self) -> bool {
17        false
18    }
19}
20
21/// Metadata information about a database view.
22pub trait SQLViewInfo: SQLTableInfo + Any {
23    /// Returns the SQL definition of this view.
24    fn definition_sql(&self) -> Cow<'static, str>;
25
26    /// Returns the schema name for this view (default: public).
27    fn schema(&self) -> &'static str {
28        "public"
29    }
30
31    /// Returns true if this is an existing view in the database not managed by Drizzle.
32    fn is_existing(&self) -> bool {
33        false
34    }
35
36    /// Returns true if this is a materialized view.
37    fn is_materialized(&self) -> bool {
38        false
39    }
40
41    /// Returns WITH NO DATA flag for materialized views.
42    fn with_no_data(&self) -> Option<bool> {
43        None
44    }
45
46    /// Returns USING clause for materialized views.
47    fn using_clause(&self) -> Option<&'static str> {
48        None
49    }
50
51    /// Returns TABLESPACE for materialized views.
52    fn tablespace(&self) -> Option<&'static str> {
53        None
54    }
55
56    /// Erased access to the view info.
57    fn as_view_info(&self) -> &dyn SQLViewInfo
58    where
59        Self: Sized,
60    {
61        self
62    }
63}
64
65impl core::fmt::Debug for dyn SQLViewInfo {
66    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67        f.debug_struct("SQLViewInfo")
68            .field("name", &self.name())
69            .field("schema", &self.schema())
70            .field("definition", &self.definition_sql())
71            .field("existing", &self.is_existing())
72            .field("materialized", &self.is_materialized())
73            .finish()
74    }
75}