Skip to main content

drizzle_postgres/
common.rs

1//! Shared `PostgreSQL` types: schema object kinds, a number helper, the legacy
2//! isolation-level enum, and the join types re-exported from core.
3
4use drizzle_core::schema::SQLEnumInfo;
5use drizzle_core::traits::SQLViewInfo;
6use drizzle_core::{SQLIndexInfo, SQLPolicyInfo, SQLSchemaType};
7
8/// A schema object that a `PostgreSQL` schema can contain.
9///
10/// Each table, view, index, policy and enum that the `PostgreSQL` macros
11/// generate reports its kind as one of these (its `SQLSchema::TYPE`). The
12/// type also serves as the dialect marker that keeps tables of other
13/// dialects out of a `PostgreSQL` schema.
14#[derive(Debug, Clone)]
15pub enum PostgresSchemaType {
16    /// A regular table
17    Table(&'static drizzle_core::TableRef),
18    /// A view
19    View(&'static dyn SQLViewInfo),
20    /// An index
21    Index(&'static dyn SQLIndexInfo),
22    /// A row-level security policy
23    Policy(&'static dyn SQLPolicyInfo),
24    /// A trigger (reserved; not generated yet)
25    Trigger,
26    /// A database enum type (`PostgreSQL`)
27    Enum(&'static dyn SQLEnumInfo),
28}
29
30impl SQLSchemaType for PostgresSchemaType {}
31
32//------------------------------------------------------------------------------
33// Number Type
34//------------------------------------------------------------------------------
35
36/// A number that is either an integer or a float.
37///
38/// Defaults to `Integer(0)`.
39#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
40pub enum Number {
41    /// Integer value
42    Integer(i64),
43    /// Floating point value
44    Real(f64),
45}
46
47impl Default for Number {
48    fn default() -> Self {
49        Self::Integer(Default::default())
50    }
51}
52
53impl From<i64> for Number {
54    fn from(value: i64) -> Self {
55        Self::Integer(value)
56    }
57}
58
59impl From<f64> for Number {
60    fn from(value: f64) -> Self {
61        Self::Real(value)
62    }
63}
64
65/// `PostgreSQL` transaction isolation levels (legacy form of
66/// [`TransactionConfig`](crate::TransactionConfig)).
67///
68/// Converts into a [`TransactionConfig`](crate::TransactionConfig). For the
69/// default, `ReadCommitted`, the `postgres` and `tokio-postgres` drivers keep
70/// the server's default isolation level instead of setting `READ COMMITTED`
71/// explicitly. `Display` renders the SQL keyword, such as `READ COMMITTED`.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
73pub enum PostgresTransactionType {
74    /// READ UNCOMMITTED isolation level
75    ReadUncommitted,
76    /// READ COMMITTED isolation level (`PostgreSQL` default)
77    #[default]
78    ReadCommitted,
79    /// REPEATABLE READ isolation level
80    RepeatableRead,
81    /// SERIALIZABLE isolation level
82    Serializable,
83}
84
85impl core::fmt::Display for PostgresTransactionType {
86    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
87        let level = match self {
88            Self::ReadUncommitted => "READ UNCOMMITTED",
89            Self::ReadCommitted => "READ COMMITTED",
90            Self::RepeatableRead => "REPEATABLE READ",
91            Self::Serializable => "SERIALIZABLE",
92        };
93        write!(f, "{level}")
94    }
95}
96
97// Re-export Join from core
98pub use drizzle_core::{Join, JoinType};