#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DatabaseBackend {
Postgres,
MySQL,
MariaDB,
SQLite,
}
#[derive(thiserror::Error, Debug)]
pub enum QueryError {
#[error("Missing required clause: {clause}. Add .{clause}() to your query.")]
MissingClause { clause: String },
#[error("Feature not supported in {backend:?}: {feature}")]
UnsupportedFeature {
backend: DatabaseBackend,
feature: String,
},
#[error("Invalid syntax for {backend:?}: {message}")]
InvalidSyntax {
backend: DatabaseBackend,
message: String,
},
#[error("Database error: {0}")]
Database(String),
#[error("Not found")]
NotFound,
}
pub trait SqlDialect: Send + Sync {
fn quote_identifier(&self, identifier: &str) -> String;
fn placeholder(&self, position: usize) -> String;
fn limit_syntax(&self, limit: Option<i64>, offset: Option<i64>) -> String;
fn returning_syntax(&self, columns: &[String]) -> Option<String>;
fn upsert_syntax(&self, table: &str, columns: &[String], conflict_columns: &[String])
-> String;
fn current_timestamp(&self) -> &'static str;
fn auto_increment_syntax(&self) -> &'static str;
fn boolean_type(&self) -> &'static str;
fn as_any(&self) -> &dyn std::any::Any;
}
#[cfg(feature = "db-mysql")]
pub mod mysql;
#[cfg(feature = "db-postgres")]
pub mod postgres;
#[cfg(feature = "db-sqlite")]
pub mod sqlite;
#[cfg(feature = "db-mysql")]
pub use mysql::MySQLDialect;
#[cfg(feature = "db-postgres")]
pub use postgres::PostgresDialect;
#[cfg(feature = "db-sqlite")]
pub use sqlite::SQLiteDialect;
#[cfg_attr(not(test), allow(clippy::panic))]
pub fn create_dialect(backend: DatabaseBackend) -> Box<dyn SqlDialect> {
match backend {
#[cfg(feature = "db-postgres")]
DatabaseBackend::Postgres => Box::new(PostgresDialect::new()),
#[cfg(feature = "db-mysql")]
DatabaseBackend::MySQL | DatabaseBackend::MariaDB => Box::new(MySQLDialect::new()),
#[cfg(feature = "db-sqlite")]
DatabaseBackend::SQLite => Box::new(SQLiteDialect::new()),
#[allow(unreachable_patterns)]
other => panic!(
"Dialect for backend {:?} is unavailable: the corresponding database driver feature is not enabled",
other
),
}
}