pub trait Dialect: Send + Sync {
fn name(&self) -> &'static str;
fn quote_identifier(&self, ident: &str) -> String;
fn bind_parameter(&self, index: usize) -> String;
fn supports_returning(&self) -> bool;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct SqliteDialect;
impl Dialect for SqliteDialect {
fn name(&self) -> &'static str {
"sqlite"
}
fn quote_identifier(&self, ident: &str) -> String {
format!("\"{}\"", ident.replace('"', "\"\""))
}
fn bind_parameter(&self, _index: usize) -> String {
"?".to_owned()
}
fn supports_returning(&self) -> bool {
true
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct PostgresDialect;
impl Dialect for PostgresDialect {
fn name(&self) -> &'static str {
"postgres"
}
fn quote_identifier(&self, ident: &str) -> String {
format!("\"{}\"", ident.replace('"', "\"\""))
}
fn bind_parameter(&self, index: usize) -> String {
format!("${index}")
}
fn supports_returning(&self) -> bool {
true
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct MySqlDialect;
impl Dialect for MySqlDialect {
fn name(&self) -> &'static str {
"mysql"
}
fn quote_identifier(&self, ident: &str) -> String {
format!("`{}`", ident.replace('`', "``"))
}
fn bind_parameter(&self, _index: usize) -> String {
"?".to_owned()
}
fn supports_returning(&self) -> bool {
false
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sqlite_quote_identifier() {
let d = SqliteDialect;
assert_eq!(d.quote_identifier("users"), "\"users\"");
assert_eq!(d.quote_identifier("user\"name"), "\"user\"\"name\"");
}
#[test]
fn sqlite_bind_parameter() {
let d = SqliteDialect;
assert_eq!(d.bind_parameter(1), "?");
assert_eq!(d.bind_parameter(5), "?");
}
#[test]
fn sqlite_supports_returning() {
assert!(SqliteDialect.supports_returning());
}
#[test]
fn postgres_quote_identifier() {
let d = PostgresDialect;
assert_eq!(d.quote_identifier("users"), "\"users\"");
}
#[test]
fn postgres_bind_parameter() {
let d = PostgresDialect;
assert_eq!(d.bind_parameter(1), "$1");
assert_eq!(d.bind_parameter(3), "$3");
}
#[test]
fn postgres_supports_returning() {
assert!(PostgresDialect.supports_returning());
}
#[test]
fn mysql_quote_identifier() {
let d = MySqlDialect;
assert_eq!(d.quote_identifier("users"), "`users`");
assert_eq!(d.quote_identifier("user`name"), "`user``name`");
}
#[test]
fn mysql_bind_parameter() {
let d = MySqlDialect;
assert_eq!(d.bind_parameter(1), "?");
}
#[test]
fn mysql_does_not_support_returning() {
assert!(!MySqlDialect.supports_returning());
}
}