drizzle-postgres 0.3.1

A type-safe SQL query builder for Rust
Documentation
//! Shared `PostgreSQL` types: schema object kinds, a number helper, the legacy
//! isolation-level enum, and the join types re-exported from core.

use drizzle_core::schema::SQLEnumInfo;
use drizzle_core::traits::SQLViewInfo;
use drizzle_core::{SQLIndexInfo, SQLPolicyInfo, SQLSchemaType};

/// A schema object that a `PostgreSQL` schema can contain.
///
/// Each table, view, index, policy and enum that the `PostgreSQL` macros
/// generate reports its kind as one of these (its `SQLSchema::TYPE`). The
/// type also serves as the dialect marker that keeps tables of other
/// dialects out of a `PostgreSQL` schema.
#[derive(Debug, Clone)]
pub enum PostgresSchemaType {
    /// A regular table
    Table(&'static drizzle_core::TableRef),
    /// A view
    View(&'static dyn SQLViewInfo),
    /// An index
    Index(&'static dyn SQLIndexInfo),
    /// A row-level security policy
    Policy(&'static dyn SQLPolicyInfo),
    /// A trigger (reserved; not generated yet)
    Trigger,
    /// A database enum type (`PostgreSQL`)
    Enum(&'static dyn SQLEnumInfo),
}

impl SQLSchemaType for PostgresSchemaType {}

//------------------------------------------------------------------------------
// Number Type
//------------------------------------------------------------------------------

/// A number that is either an integer or a float.
///
/// Defaults to `Integer(0)`.
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub enum Number {
    /// Integer value
    Integer(i64),
    /// Floating point value
    Real(f64),
}

impl Default for Number {
    fn default() -> Self {
        Self::Integer(Default::default())
    }
}

impl From<i64> for Number {
    fn from(value: i64) -> Self {
        Self::Integer(value)
    }
}

impl From<f64> for Number {
    fn from(value: f64) -> Self {
        Self::Real(value)
    }
}

/// `PostgreSQL` transaction isolation levels (legacy form of
/// [`TransactionConfig`](crate::TransactionConfig)).
///
/// Converts into a [`TransactionConfig`](crate::TransactionConfig). For the
/// default, `ReadCommitted`, the `postgres` and `tokio-postgres` drivers keep
/// the server's default isolation level instead of setting `READ COMMITTED`
/// explicitly. `Display` renders the SQL keyword, such as `READ COMMITTED`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PostgresTransactionType {
    /// READ UNCOMMITTED isolation level
    ReadUncommitted,
    /// READ COMMITTED isolation level (`PostgreSQL` default)
    #[default]
    ReadCommitted,
    /// REPEATABLE READ isolation level
    RepeatableRead,
    /// SERIALIZABLE isolation level
    Serializable,
}

impl core::fmt::Display for PostgresTransactionType {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let level = match self {
            Self::ReadUncommitted => "READ UNCOMMITTED",
            Self::ReadCommitted => "READ COMMITTED",
            Self::RepeatableRead => "REPEATABLE READ",
            Self::Serializable => "SERIALIZABLE",
        };
        write!(f, "{level}")
    }
}

// Re-export Join from core
pub use drizzle_core::{Join, JoinType};