#[derive(Debug, Clone)]
pub enum SqlDialect {
Postgres { table_name: String },
Sqlite,
}
impl SqlDialect {
pub fn postgres(table_name: &str) -> Self {
SqlDialect::Postgres {
table_name: table_name.to_string(),
}
}
pub fn sqlite() -> Self {
SqlDialect::Sqlite
}
pub fn table_name(&self) -> &str {
match self {
SqlDialect::Postgres { table_name } => table_name,
SqlDialect::Sqlite => "documents",
}
}
pub fn bool_true(&self) -> &str {
match self {
SqlDialect::Postgres { .. } => "true",
SqlDialect::Sqlite => "1",
}
}
pub fn bool_false(&self) -> &str {
match self {
SqlDialect::Postgres { .. } => "false",
SqlDialect::Sqlite => "0",
}
}
pub fn supports_array_params(&self) -> bool {
match self {
SqlDialect::Postgres { .. } => true,
SqlDialect::Sqlite => false,
}
}
pub fn mark_as_parents_sql(&self) -> String {
match self {
SqlDialect::Postgres { .. } => format!(
"UPDATE {} SET is_parent = true WHERE id = ANY($1) AND is_parent IS DISTINCT FROM true",
self.table_name()
),
SqlDialect::Sqlite => {
"UPDATE documents SET is_parent = 1 WHERE id = ?1 AND is_parent IS NOT 1"
.to_string()
}
}
}
pub fn mark_as_children_sql(&self) -> String {
match self {
SqlDialect::Postgres { .. } => format!(
"UPDATE {} SET is_parent = false WHERE id = ANY($1) AND is_parent IS DISTINCT FROM false",
self.table_name()
),
SqlDialect::Sqlite => {
"UPDATE documents SET is_parent = 0 WHERE id = ?1 AND is_parent IS NOT 0"
.to_string()
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_postgres_mark_as_parents_sql() {
let dialect = SqlDialect::postgres("my_table");
let sql = dialect.mark_as_parents_sql();
assert_eq!(
sql,
"UPDATE my_table SET is_parent = true WHERE id = ANY($1) AND is_parent IS DISTINCT FROM true"
);
assert!(
!sql.contains("IS NULL"),
"mark_as_parents must not filter by IS NULL"
);
}
#[test]
fn test_postgres_mark_as_children_sql() {
let dialect = SqlDialect::postgres("my_table");
let sql = dialect.mark_as_children_sql();
assert_eq!(
sql,
"UPDATE my_table SET is_parent = false WHERE id = ANY($1) AND is_parent IS DISTINCT FROM false"
);
assert!(
!sql.contains("IS NULL"),
"mark_as_children must not filter by IS NULL"
);
}
#[test]
fn test_sqlite_mark_as_parents_sql() {
let dialect = SqlDialect::sqlite();
let sql = dialect.mark_as_parents_sql();
assert_eq!(
sql,
"UPDATE documents SET is_parent = 1 WHERE id = ?1 AND is_parent IS NOT 1"
);
assert!(
!sql.contains("IS NULL"),
"mark_as_parents must not filter by IS NULL"
);
}
#[test]
fn test_sqlite_mark_as_children_sql() {
let dialect = SqlDialect::sqlite();
let sql = dialect.mark_as_children_sql();
assert_eq!(
sql,
"UPDATE documents SET is_parent = 0 WHERE id = ?1 AND is_parent IS NOT 0"
);
assert!(
!sql.contains("IS NULL"),
"mark_as_children must not filter by IS NULL"
);
}
#[test]
fn test_dialects_have_same_logic() {
let pg = SqlDialect::postgres("documents");
let sqlite = SqlDialect::sqlite();
let pg_parents = pg.mark_as_parents_sql();
let sqlite_parents = sqlite.mark_as_parents_sql();
assert!(pg_parents.starts_with("UPDATE documents SET is_parent = "));
assert!(sqlite_parents.starts_with("UPDATE documents SET is_parent = "));
assert!(!pg_parents.contains("NULL"));
assert!(!sqlite_parents.contains("NULL"));
}
#[test]
fn test_supports_array_params() {
assert!(SqlDialect::postgres("t").supports_array_params());
assert!(!SqlDialect::sqlite().supports_array_params());
}
}