Skip to main content

toasty_sql/serializer/
dialect.rs

1use super::Serializer;
2
3use toasty_core::{
4    driver::{Dialect, SqlPlaceholder},
5    schema::db,
6};
7
8impl<'a> Serializer<'a> {
9    /// Creates a serializer that emits SQLite SQL.
10    pub fn sqlite(schema: &'a db::Schema) -> Self {
11        Self::sqlite_with_default_begin(schema, "BEGIN")
12    }
13
14    /// Creates a SQLite-dialect serializer with a custom SQL string for
15    /// [`TransactionMode::Default`].
16    ///
17    /// Used by SQLite-compatible engines whose preferred "no opinion" BEGIN
18    /// is not the classic deferred form — e.g. Turso with
19    /// `concurrent_writes()` enabled, where `Default` means `BEGIN
20    /// CONCURRENT`. The non-`Default` modes (`Deferred`, `Immediate`,
21    /// `Exclusive`) still map to their standard SQLite SQL.
22    pub fn sqlite_with_default_begin(schema: &'a db::Schema, default_begin: &'static str) -> Self {
23        Serializer {
24            schema,
25            dialect: Dialect::Sqlite,
26            sqlite_default_begin: default_begin,
27        }
28    }
29
30    /// Returns `true` if this serializer targets SQLite.
31    pub fn is_sqlite(&self) -> bool {
32        matches!(self.dialect, Dialect::Sqlite)
33    }
34
35    /// Creates a serializer that emits PostgreSQL SQL.
36    pub fn postgresql(schema: &'a db::Schema) -> Self {
37        Serializer {
38            schema,
39            dialect: Dialect::Postgresql,
40            sqlite_default_begin: "BEGIN",
41        }
42    }
43
44    /// Creates a serializer that emits MySQL SQL.
45    pub fn mysql(schema: &'a db::Schema) -> Self {
46        Serializer {
47            schema,
48            dialect: Dialect::Mysql,
49            sqlite_default_begin: "BEGIN",
50        }
51    }
52
53    pub(super) fn is_mysql(&self) -> bool {
54        matches!(self.dialect, Dialect::Mysql)
55    }
56}
57
58/// The placeholder syntax a dialect's bind layer accepts.
59pub(super) fn sql_placeholder(dialect: Dialect) -> SqlPlaceholder {
60    match dialect {
61        Dialect::Postgresql => SqlPlaceholder::DollarNumber,
62        Dialect::Sqlite => SqlPlaceholder::NumberedQuestionMark,
63        Dialect::Mysql => SqlPlaceholder::QuestionMark,
64    }
65}