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