Skip to main content

toasty_sql/serializer/
flavor.rs

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