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/// A database view: a named query that can be selected from like a table.
7///
8/// Generated by the view macros.
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 view's defining query.
17    fn definition(&self) -> SQL<'a, Value>;
18
19    /// Returns `true` for a view that already exists in the database and is
20    /// not created or dropped by Drizzle.
21    fn is_existing(&self) -> bool {
22        false
23    }
24}
25
26/// Runtime metadata of a view, usable as a trait object.
27pub trait SQLViewInfo: SQLTableInfo + Any {
28    /// Returns the view's defining query as SQL text.
29    fn definition_sql(&self) -> Cow<'static, str>;
30
31    /// Returns `true` for a view that already exists in the database and is
32    /// not created or dropped by Drizzle.
33    fn is_existing(&self) -> bool {
34        false
35    }
36
37    /// Returns `true` for a materialized view.
38    fn is_materialized(&self) -> bool {
39        false
40    }
41
42    /// For a materialized view, whether it is created `WITH NO DATA`.
43    fn with_no_data(&self) -> Option<bool> {
44        None
45    }
46
47    /// For a materialized view, its `USING` access method.
48    fn using_clause(&self) -> Option<&'static str> {
49        None
50    }
51
52    /// For a materialized view, its `TABLESPACE`.
53    fn tablespace(&self) -> Option<&'static str> {
54        None
55    }
56}
57
58impl core::fmt::Debug for dyn SQLViewInfo {
59    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
60        f.debug_struct("SQLViewInfo")
61            .field("name", &self.name())
62            .field("schema", &SQLTableInfo::schema(self))
63            .field("qualified_name", &SQLTableInfo::qualified_name(self))
64            .field("definition", &self.definition_sql())
65            .field("existing", &self.is_existing())
66            .field("materialized", &self.is_materialized())
67            .finish()
68    }
69}