mod column;
mod table;
mod value;
#[cfg(not(feature = "std"))]
use crate::prelude::*;
pub use column::*;
use core::any::Any;
use drizzle_core::error::DrizzleError;
pub use table::*;
pub use value::*;
use crate::values::{OwnedPostgresValue, PostgresValue};
#[allow(clippy::wrong_self_convention)]
pub trait PostgresEnum: Send + Sync + Any {
fn enum_type_name(&self) -> &'static str;
fn as_enum(&self) -> &dyn PostgresEnum;
fn variant_name(&self) -> &'static str;
fn into_boxed(&self) -> Box<dyn PostgresEnum>;
fn try_from_str(value: &str) -> Result<Self, DrizzleError>
where
Self: Sized;
}
impl core::fmt::Debug for &dyn PostgresEnum {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PostgresSQLEnum")
.field("type", &self.enum_type_name())
.field("variant", &self.variant_name())
.finish()
}
}
impl PartialEq for &dyn PostgresEnum {
fn eq(&self, other: &Self) -> bool {
self.enum_type_name() == other.enum_type_name()
&& self.variant_name() == other.variant_name()
}
}
impl core::fmt::Debug for Box<dyn PostgresEnum> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("PostgresSQLEnum")
.field("type", &self.enum_type_name())
.field("variant", &self.variant_name())
.finish()
}
}
impl Clone for Box<dyn PostgresEnum> {
fn clone(&self) -> Self {
self.into_boxed()
}
}
impl PartialEq for Box<dyn PostgresEnum> {
fn eq(&self, other: &Self) -> bool {
self.enum_type_name() == other.enum_type_name()
&& self.variant_name() == other.variant_name()
}
}
#[cfg(any(feature = "postgres-sync", feature = "tokio-postgres"))]
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be used as a PostgreSQL column type",
note = "add #[derive(PostgresEnum)] for enum types, or use a supported primitive type"
)]
pub trait DrizzlePostgresColumn: Sized {
type SQLType: drizzle_core::types::DataType;
const SQL_TYPE: &'static str;
const DDL_TYPE: &'static str = Self::SQL_TYPE;
const NEEDS_CREATE_TYPE: bool = false;
const SCHEMA: &'static str = "public";
fn decode(row: &crate::Row, idx: usize) -> Result<Self, DrizzleError>;
fn encode(&self) -> PostgresValue<'_>;
fn encode_owned(self) -> OwnedPostgresValue {
self.encode().into_owned()
}
}
impl<'a, T> From<T> for PostgresValue<'a>
where
T: DrizzlePostgresColumn,
{
fn from(value: T) -> Self {
value.encode_owned().into()
}
}
#[cfg(not(any(feature = "postgres-sync", feature = "tokio-postgres")))]
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be used as a PostgreSQL column type",
note = "add #[derive(PostgresEnum)] for enum types, or use a supported primitive type"
)]
pub trait DrizzlePostgresColumn: Sized {
type SQLType: drizzle_core::types::DataType;
const SQL_TYPE: &'static str;
const DDL_TYPE: &'static str = Self::SQL_TYPE;
const NEEDS_CREATE_TYPE: bool = false;
const SCHEMA: &'static str = "public";
fn encode(&self) -> PostgresValue<'_>;
fn encode_owned(self) -> OwnedPostgresValue {
self.encode().into_owned()
}
}