drizzle-core 0.3.0

A type-safe SQL query builder for Rust
Documentation
use crate::prelude::Cow;
use crate::traits::SQLSchemaType;
use crate::{SQL, SQLParam, SQLTable, SQLTableInfo};
use core::any::Any;

/// A database view: a named query that can be selected from like a table.
///
/// Generated by the view macros.
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not a SQL view for this dialect",
    label = "ensure this type was derived with a drizzle view macro"
)]
pub trait SQLView<'a, Type: SQLSchemaType, Value: SQLParam + 'a>:
    SQLTable<'a, Type, Value>
{
    /// Returns the view's defining query.
    fn definition(&self) -> SQL<'a, Value>;

    /// Returns `true` for a view that already exists in the database and is
    /// not created or dropped by Drizzle.
    fn is_existing(&self) -> bool {
        false
    }
}

/// Runtime metadata of a view, usable as a trait object.
pub trait SQLViewInfo: SQLTableInfo + Any {
    /// Returns the view's defining query as SQL text.
    fn definition_sql(&self) -> Cow<'static, str>;

    /// Returns `true` for a view that already exists in the database and is
    /// not created or dropped by Drizzle.
    fn is_existing(&self) -> bool {
        false
    }

    /// Returns `true` for a materialized view.
    fn is_materialized(&self) -> bool {
        false
    }

    /// For a materialized view, whether it is created `WITH NO DATA`.
    fn with_no_data(&self) -> Option<bool> {
        None
    }

    /// For a materialized view, its `USING` access method.
    fn using_clause(&self) -> Option<&'static str> {
        None
    }

    /// For a materialized view, its `TABLESPACE`.
    fn tablespace(&self) -> Option<&'static str> {
        None
    }
}

impl core::fmt::Debug for dyn SQLViewInfo {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("SQLViewInfo")
            .field("name", &self.name())
            .field("schema", &SQLTableInfo::schema(self))
            .field("qualified_name", &SQLTableInfo::qualified_name(self))
            .field("definition", &self.definition_sql())
            .field("existing", &self.is_existing())
            .field("materialized", &self.is_materialized())
            .finish()
    }
}