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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#[macro_use]
mod fmt;
use fmt::ToSql;
mod column;
use column::ColumnAlias;
mod cte;
mod delim;
use delim::{Comma, Delimited, Period};
mod flavor;
use flavor::Flavor;
mod ident;
use ident::Ident;
mod params;
pub use params::Placeholder;
// Fragment serializers
mod column_def;
mod expr;
mod name;
mod statement;
mod ty;
mod value;
use crate::stmt::Statement;
use toasty_core::{
driver::operation::{IsolationLevel, Transaction, TransactionMode},
schema::db::{self, Index, Table},
stmt::IntoExprTarget,
};
/// Serialize a statement to a SQL string
#[derive(Debug)]
pub struct Serializer<'a> {
/// Schema against which the statement is to be serialized
schema: &'a db::Schema,
/// The database flavor handles the differences between SQL dialects and
/// supported features.
flavor: Flavor,
/// SQL emitted for [`TransactionMode::Default`] under the SQLite flavor.
/// Constructors that don't override this leave it at `"BEGIN"`, which is
/// SQLite's natural default (DEFERRED). A driver that wants `Default` to
/// mean something engine-specific — Turso under `concurrent_writes()`
/// uses `"BEGIN CONCURRENT"` — sets this through
/// [`Self::sqlite_with_default_begin`]. The non-`Default` variants
/// (`Deferred`, `Immediate`, `Exclusive`) always emit fixed SQL.
sqlite_default_begin: &'static str,
}
struct Formatter<'a> {
/// Handle to the serializer
serializer: &'a Serializer<'a>,
/// Expression-resolution context for the current scope. Re-scoped (via
/// [`Formatter::scope`]) each time serialization descends into a new
/// query level, so it travels with the formatter rather than as a
/// separate argument.
cx: ExprContext<'a>,
/// Where to write the serialized SQL
dst: &'a mut String,
/// Current query depth. This is used to determine the nesting level when
/// generating names
depth: usize,
/// True when table names should be aliased.
alias: bool,
/// True when inside an INSERT statement. Used by MySQL to decide whether
/// VALUES rows need the ROW() wrapper (required in subqueries but not in
/// INSERT).
in_insert: bool,
/// Collects `Expr::Arg(n)` positions in the order they appear in the SQL.
/// Used by MySQL (which uses positional `?` without indices) to reorder
/// the params vec to match placeholder occurrence order. Borrowed so a
/// scoped child formatter writes through to the root's vec.
arg_positions: &'a mut Vec<usize>,
}
impl<'a> Formatter<'a> {
/// Descend into a new expression scope, returning a child formatter that
/// shares this one's output sink and arg collector (so writes flow back
/// to the root) but resolves references against `target`.
///
/// The child borrows `self`, so the parent scope stays live on the stack
/// for the child's lifetime — that is what keeps the `ExprContext` parent
/// chain valid for nested-reference resolution.
fn scope<'c>(&'c mut self, target: impl IntoExprTarget<'c, db::Schema>) -> Formatter<'c> {
Formatter {
serializer: self.serializer,
cx: self.cx.scope(target),
dst: &mut *self.dst,
depth: self.depth,
alias: self.alias,
in_insert: self.in_insert,
arg_positions: &mut *self.arg_positions,
}
}
}
/// Expression context bound to a database-level schema.
pub type ExprContext<'a> = toasty_core::stmt::ExprContext<'a, db::Schema>;
impl<'a> Serializer<'a> {
/// Serializes a [`Statement`] to a SQL string with all values inlined as
/// literals (no bind parameters). Appends a trailing semicolon.
///
/// Use this for DDL statements (`CREATE TABLE`, `CREATE TYPE`, etc.) where
/// bind parameters are not supported. DML statements should already have
/// their parameters extracted (as `Expr::Arg` placeholders) before reaching
/// the serializer.
pub fn serialize(&self, stmt: &Statement) -> String {
self.serialize_with_arg_order(stmt).0
}
/// Serializes a [`Statement`] and returns both the SQL string and the order
/// in which `Expr::Arg(n)` placeholders appear in the SQL.
///
/// The arg order is needed by MySQL which uses positional `?` without
/// indices — the caller must reorder its params vec to match the occurrence
/// order. PostgreSQL and SQLite use indexed placeholders (`$1`, `?1`) so
/// they can ignore the arg order.
pub fn serialize_with_arg_order(&self, stmt: &Statement) -> (String, Vec<usize>) {
let mut ret = String::new();
let mut arg_positions = Vec::new();
{
let mut fmt = Formatter {
serializer: self,
cx: ExprContext::new(self.schema),
dst: &mut ret,
depth: 0,
alias: false,
in_insert: false,
arg_positions: &mut arg_positions,
};
stmt.to_sql(&mut fmt);
}
ret.push(';');
(ret, arg_positions)
}
/// Serialize a transaction control operation to a SQL string.
///
/// The generated SQL is flavor-specific (e.g., MySQL uses `START TRANSACTION`
/// while other databases use `BEGIN`). Savepoints are named `sp_{id}`.
pub fn serialize_transaction(&self, op: &Transaction) -> String {
let mut ret = String::new();
let mut arg_positions = Vec::new();
{
let mut f = Formatter {
serializer: self,
cx: ExprContext::new(self.schema),
dst: &mut ret,
depth: 0,
alias: false,
in_insert: false,
arg_positions: &mut arg_positions,
};
match op {
Transaction::Start {
isolation,
read_only,
mode,
} => fmt!(
&mut f,
self.serialize_transaction_start(*isolation, *read_only, *mode)
),
Transaction::Commit => fmt!(&mut f, "COMMIT"),
Transaction::Rollback => fmt!(&mut f, "ROLLBACK"),
Transaction::Savepoint(name) => {
fmt!(&mut f, "SAVEPOINT " Ident(name))
}
Transaction::ReleaseSavepoint(name) => {
fmt!(&mut f, "RELEASE SAVEPOINT " Ident(name))
}
Transaction::RollbackToSavepoint(name) => {
fmt!(&mut f, "ROLLBACK TO SAVEPOINT " Ident(name))
}
};
}
ret.push(';');
ret
}
fn serialize_transaction_start(
&self,
isolation: Option<IsolationLevel>,
read_only: bool,
mode: TransactionMode,
) -> String {
fn isolation_level_str(level: IsolationLevel) -> &'static str {
match level {
IsolationLevel::ReadUncommitted => "READ UNCOMMITTED",
IsolationLevel::ReadCommitted => "READ COMMITTED",
IsolationLevel::RepeatableRead => "REPEATABLE READ",
IsolationLevel::Serializable => "SERIALIZABLE",
}
}
match self.flavor {
// MySQL has no SQLite-style lock-mode keyword; drivers
// reject non-Default `mode` before reaching the serializer.
Flavor::Mysql => {
let mut sql = String::new();
if let Some(level) = isolation {
sql.push_str("SET TRANSACTION ISOLATION LEVEL ");
sql.push_str(isolation_level_str(level));
sql.push_str("; ");
}
sql.push_str("START TRANSACTION");
if read_only {
sql.push_str(" READ ONLY");
}
sql
}
// PostgreSQL has no SQLite-style lock-mode keyword; drivers
// reject non-Default `mode` before reaching the serializer.
Flavor::Postgresql => {
let mut sql = String::from("BEGIN");
if let Some(level) = isolation {
sql.push_str(" ISOLATION LEVEL ");
sql.push_str(isolation_level_str(level));
}
if read_only {
sql.push_str(" READ ONLY");
}
sql
}
// SQLite has no per-transaction isolation level or read-only
// keyword; the lock-acquisition mode is the only knob. `Default`
// emits whatever the serializer was configured with at
// construction (`BEGIN` by default, or e.g. `BEGIN CONCURRENT`
// for Turso under MVCC). `Deferred`/`Immediate`/`Exclusive` are
// explicit caller requests with fixed SQL.
Flavor::Sqlite => match mode {
TransactionMode::Default => self.sqlite_default_begin.to_string(),
TransactionMode::Deferred => "BEGIN".to_string(),
TransactionMode::Immediate => "BEGIN IMMEDIATE".to_string(),
TransactionMode::Exclusive => "BEGIN EXCLUSIVE".to_string(),
},
}
}
fn table(&self, id: impl Into<db::TableId>) -> &'a Table {
self.schema.table(id.into())
}
fn index(&self, id: impl Into<db::IndexId>) -> &'a Index {
self.schema.index(id.into())
}
fn table_name(&self, id: impl Into<db::TableId>) -> Ident<&str> {
let table = self.schema.table(id.into());
Ident(&table.name)
}
fn column_name(&self, id: impl Into<db::ColumnId>) -> Ident<&str> {
let column = self.schema.column(id.into());
Ident(&column.name)
}
}