use async_trait::async_trait;
use super::{catalog, columns, identifiers};
use crate::db::Db;
use crate::error::Result;
use crate::types::{ColumnDef, DialectCapabilities};
#[async_trait]
pub trait SqlDialect: Send + Sync {
fn name(&self) -> &str;
fn capabilities(&self) -> DialectCapabilities;
fn quote_ident(&self, name: &str) -> String {
identifiers::quote_ident(name)
}
fn quote_literal(&self, value: &str) -> String {
identifiers::quote_literal(value)
}
fn qualified(&self, schema: &str, name: &str) -> String {
identifiers::qualified(schema, name)
}
fn quote_ident_list(&self, names: &[String]) -> String {
identifiers::quote_ident_list(names)
}
fn render_column_def(&self, column: &ColumnDef) -> String {
columns::render_column_def(column)
}
fn render_column_list(&self, columns: &[ColumnDef]) -> String {
columns::render_column_list(columns)
}
fn create_index_sql(
&self,
schema: &str,
table: &str,
index_name: &str,
columns: &[String],
concurrently: bool,
) -> String;
fn table_exists_sql(&self, schema: &str, table: &str, negate: bool) -> String;
fn column_exists_sql(&self, schema: &str, table: &str, column: &str, negate: bool) -> String;
fn constraint_exists_sql(&self, schema: &str, table: &str, name: &str, negate: bool) -> String;
fn primary_key_exists_sql(&self, schema: &str, table: &str, negate: bool) -> String;
fn check_constraint_exists_sql(
&self,
schema: &str,
table: &str,
name: &str,
negate: bool,
) -> String;
fn schema_exists_sql(&self, schema: &str, negate: bool) -> String;
fn extension_exists_sql(&self, name: &str, negate: bool) -> String;
fn enum_type_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String;
fn enum_label_exists_sql(
&self,
schema: &str,
type_name: &str,
value: &str,
negate: bool,
) -> String;
fn view_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String;
fn matview_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String;
fn column_default_set_sql(
&self,
schema: &str,
table: &str,
column: &str,
negate: bool,
) -> String;
fn column_not_null_sql(&self, schema: &str, table: &str, column: &str, negate: bool) -> String;
fn create_history_table_sql(&self, name: &str) -> String;
async fn acquire_lock(&self, db: &dyn Db, key: i64) -> Result<()>;
async fn release_lock(&self, db: &dyn Db, key: i64) -> Result<()>;
}
const POSTGRES_CAPABILITIES: DialectCapabilities = DialectCapabilities {
enum_types: true,
sequences: true,
materialized_views: true,
extensions: true,
concurrent_indexes: true,
schemas: true,
advisory_locks: true,
add_check_constraints: true,
add_primary_key_constraints: true,
add_unique_constraints: true,
add_foreign_keys: true,
alter_column_default: true,
alter_column_not_null: true,
};
const SQLITE_CAPABILITIES: DialectCapabilities = DialectCapabilities {
enum_types: false,
sequences: false,
materialized_views: false,
extensions: false,
concurrent_indexes: false,
schemas: false,
advisory_locks: false,
add_check_constraints: false,
add_primary_key_constraints: false,
add_unique_constraints: false,
add_foreign_keys: false,
alter_column_default: false,
alter_column_not_null: false,
};
#[derive(Clone, Copy, Debug, Default)]
pub struct PostgresDialect;
#[async_trait]
impl SqlDialect for PostgresDialect {
fn name(&self) -> &str {
"postgres"
}
fn capabilities(&self) -> DialectCapabilities {
POSTGRES_CAPABILITIES
}
fn create_index_sql(
&self,
schema: &str,
table: &str,
index_name: &str,
columns: &[String],
concurrently: bool,
) -> String {
let concurrently_sql = if concurrently { " CONCURRENTLY" } else { "" };
format!(
"CREATE INDEX{} {} ON {} ({})",
concurrently_sql,
self.quote_ident(index_name),
self.qualified(schema, table),
self.quote_ident_list(columns)
)
}
fn table_exists_sql(&self, schema: &str, table: &str, negate: bool) -> String {
catalog::regclass_exists_sql(schema, table, negate)
}
fn column_exists_sql(&self, schema: &str, table: &str, column: &str, negate: bool) -> String {
catalog::column_exists_sql(schema, table, column, negate)
}
fn constraint_exists_sql(&self, schema: &str, table: &str, name: &str, negate: bool) -> String {
catalog::constraint_exists_sql(schema, table, name, negate)
}
fn primary_key_exists_sql(&self, schema: &str, table: &str, negate: bool) -> String {
catalog::primary_key_exists_sql(schema, table, negate)
}
fn check_constraint_exists_sql(
&self,
schema: &str,
table: &str,
name: &str,
negate: bool,
) -> String {
catalog::check_constraint_exists_sql(schema, table, name, negate)
}
fn schema_exists_sql(&self, schema: &str, negate: bool) -> String {
catalog::schema_exists_sql(schema, negate)
}
fn extension_exists_sql(&self, name: &str, negate: bool) -> String {
catalog::extension_exists_sql(name, negate)
}
fn enum_type_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String {
catalog::enum_type_exists_sql(schema, name, negate)
}
fn enum_label_exists_sql(
&self,
schema: &str,
type_name: &str,
value: &str,
negate: bool,
) -> String {
catalog::enum_label_exists_sql(schema, type_name, value, negate)
}
fn view_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String {
catalog::view_exists_sql(schema, name, negate)
}
fn matview_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String {
catalog::matview_exists_sql(schema, name, negate)
}
fn column_default_set_sql(
&self,
schema: &str,
table: &str,
column: &str,
negate: bool,
) -> String {
catalog::column_default_set_sql(schema, table, column, negate)
}
fn column_not_null_sql(&self, schema: &str, table: &str, column: &str, negate: bool) -> String {
catalog::column_not_null_sql(schema, table, column, negate)
}
fn create_history_table_sql(&self, name: &str) -> String {
format!(
"CREATE TABLE IF NOT EXISTS {} (
id bigint PRIMARY KEY,
parent_id bigint,
applied_at timestamptz NOT NULL DEFAULT now(),
operations text NOT NULL,
checksum text NOT NULL
)",
self.quote_ident(name)
)
}
async fn acquire_lock(&self, db: &dyn Db, key: i64) -> Result<()> {
db.execute(&format!("SELECT pg_advisory_lock({key})")).await
}
async fn release_lock(&self, db: &dyn Db, key: i64) -> Result<()> {
db.execute(&format!("SELECT pg_advisory_unlock({key})"))
.await
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SqliteDialect;
fn unsupported_probe(negate: bool) -> String {
if negate {
"SELECT 1".to_string()
} else {
"SELECT 0".to_string()
}
}
fn exists_select(exists: &str, negate: bool) -> String {
if negate {
format!("SELECT NOT {exists}")
} else {
format!("SELECT {exists}")
}
}
#[async_trait]
impl SqlDialect for SqliteDialect {
fn name(&self) -> &str {
"sqlite"
}
fn capabilities(&self) -> DialectCapabilities {
SQLITE_CAPABILITIES
}
fn create_index_sql(
&self,
schema: &str,
table: &str,
index_name: &str,
columns: &[String],
_concurrently: bool,
) -> String {
format!(
"CREATE INDEX {} ON {} ({})",
self.qualified(schema, index_name),
self.quote_ident(table),
self.quote_ident_list(columns)
)
}
fn table_exists_sql(&self, schema: &str, table: &str, negate: bool) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM {}.sqlite_master
WHERE name = {}
AND type IN ('table', 'index', 'view')
)",
self.quote_ident(schema),
self.quote_literal(table)
);
exists_select(&exists, negate)
}
fn column_exists_sql(&self, _schema: &str, table: &str, column: &str, negate: bool) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM pragma_table_info({})
WHERE name = {}
)",
self.quote_literal(table),
self.quote_literal(column)
);
exists_select(&exists, negate)
}
fn constraint_exists_sql(
&self,
_schema: &str,
_table: &str,
_name: &str,
negate: bool,
) -> String {
unsupported_probe(negate)
}
fn primary_key_exists_sql(&self, _schema: &str, table: &str, negate: bool) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM pragma_table_info({})
WHERE pk > 0
)",
self.quote_literal(table)
);
exists_select(&exists, negate)
}
fn check_constraint_exists_sql(
&self,
_schema: &str,
_table: &str,
_name: &str,
negate: bool,
) -> String {
unsupported_probe(negate)
}
fn schema_exists_sql(&self, schema: &str, negate: bool) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM pragma_database_list
WHERE name = {}
)",
self.quote_literal(schema)
);
exists_select(&exists, negate)
}
fn extension_exists_sql(&self, _name: &str, negate: bool) -> String {
unsupported_probe(negate)
}
fn enum_type_exists_sql(&self, _schema: &str, _name: &str, negate: bool) -> String {
unsupported_probe(negate)
}
fn enum_label_exists_sql(
&self,
_schema: &str,
_type_name: &str,
_value: &str,
negate: bool,
) -> String {
unsupported_probe(negate)
}
fn view_exists_sql(&self, schema: &str, name: &str, negate: bool) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM {}.sqlite_master
WHERE name = {}
AND type = 'view'
)",
self.quote_ident(schema),
self.quote_literal(name)
);
exists_select(&exists, negate)
}
fn matview_exists_sql(&self, _schema: &str, _name: &str, negate: bool) -> String {
unsupported_probe(negate)
}
fn column_default_set_sql(
&self,
_schema: &str,
table: &str,
column: &str,
negate: bool,
) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM pragma_table_info({})
WHERE name = {}
AND dflt_value IS NOT NULL
)",
self.quote_literal(table),
self.quote_literal(column)
);
exists_select(&exists, negate)
}
fn column_not_null_sql(
&self,
_schema: &str,
table: &str,
column: &str,
negate: bool,
) -> String {
let exists = format!(
"EXISTS (
SELECT 1 FROM pragma_table_info({})
WHERE name = {}
AND \"notnull\" = 1
)",
self.quote_literal(table),
self.quote_literal(column)
);
exists_select(&exists, negate)
}
fn create_history_table_sql(&self, name: &str) -> String {
format!(
"CREATE TABLE IF NOT EXISTS {} (
id INTEGER PRIMARY KEY,
parent_id INTEGER,
applied_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
operations TEXT NOT NULL,
checksum TEXT NOT NULL
)",
self.quote_ident(name)
)
}
async fn acquire_lock(&self, _db: &dyn Db, _key: i64) -> Result<()> {
Ok(())
}
async fn release_lock(&self, _db: &dyn Db, _key: i64) -> Result<()> {
Ok(())
}
}