use super::SqlDialect;
pub struct PostgresDialect {
pub(crate) schema_context: Option<PostgresSchemaContext>,
}
pub struct PostgresSchemaContext {
pub enum_fields: std::collections::HashMap<String, String>,
}
impl PostgresDialect {
pub fn new() -> Self {
Self {
schema_context: None,
}
}
pub fn with_schema_context(schema_context: PostgresSchemaContext) -> Self {
Self {
schema_context: Some(schema_context),
}
}
pub fn get_enum_type(&self, table: &str, column: &str) -> Option<&String> {
self.schema_context
.as_ref()?
.enum_fields
.get(&format!("{}.{}", table, column))
}
pub fn placeholder_with_enum_cast(&self, position: usize, enum_type: Option<&str>) -> String {
match enum_type {
Some(enum_name) => format!("${}::{}", position, enum_name),
None => format!("${}", position),
}
}
}
impl Default for PostgresDialect {
fn default() -> Self {
Self::new()
}
}
impl SqlDialect for PostgresDialect {
fn quote_identifier(&self, identifier: &str) -> String {
if identifier.contains('.') {
identifier.to_string()
} else {
format!("\"{}\"", identifier.replace("\"", "\"\""))
}
}
fn placeholder(&self, position: usize) -> String {
format!("${}", position)
}
fn limit_syntax(&self, limit: Option<i64>, offset: Option<i64>) -> String {
let mut sql = String::new();
if let Some(limit) = limit {
sql.push_str(&format!(" LIMIT {}", limit));
}
if let Some(offset) = offset {
sql.push_str(&format!(" OFFSET {}", offset));
}
sql
}
fn returning_syntax(&self, columns: &[String]) -> Option<String> {
Some(format!(" RETURNING {}", columns.join(", ")))
}
fn upsert_syntax(
&self,
table: &str,
columns: &[String],
conflict_columns: &[String],
) -> String {
format!(
"INSERT INTO {} ({}) VALUES ({}) ON CONFLICT ({}) DO UPDATE SET {}",
table,
columns.join(", "),
(1..=columns.len())
.map(|i| format!("${}", i))
.collect::<Vec<_>>()
.join(", "),
conflict_columns.join(", "),
columns
.iter()
.enumerate()
.map(|(i, col)| format!("{} = ${}", col, i + 1))
.collect::<Vec<_>>()
.join(", ")
)
}
fn current_timestamp(&self) -> &'static str {
"CURRENT_TIMESTAMP"
}
fn auto_increment_syntax(&self) -> &'static str {
"SERIAL PRIMARY KEY"
}
fn boolean_type(&self) -> &'static str {
"BOOLEAN"
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
}