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
//! Database driver interface for Toasty.
//!
//! This module defines the traits and types that database drivers must implement
//! to integrate with the Toasty query engine. The two core traits are [`Driver`]
//! (factory for connections and schema operations) and [`Connection`] (executes
//! operations against a live database session).
//!
//! The query planner inspects [`Capability`] to decide which [`Operation`]
//! variants to emit. SQL-based drivers receive [`Operation::QuerySql`] and
//! [`Operation::Insert`], while key-value drivers (e.g., DynamoDB) receive
//! [`Operation::GetByKey`], [`Operation::QueryPk`], etc. The
//! [`SchemaMutations`] sub-struct (`Capability::schema_mutations`) describes
//! what the database can do to its own schema — for example, whether
//! `ALTER COLUMN` can change a column's type — and the migration generator
//! consults it to decide between an in-place alter and a table rebuild.
//!
//! # Architecture
//!
//! ```text
//! Query Engine ──▶ Operation ──▶ Connection::exec() ──▶ ExecResponse
//! ▲
//! │
//! Driver::capability()
//! ```
//!
//! # Error classification
//!
//! The pool and the engine branch on the error variant returned from
//! [`Connection::exec`] and [`Connection::ping`]. Drivers MUST cooperate
//! with those branches:
//!
//! - A connection-level fault (closed socket, broken pipe, protocol
//! error, end-of-stream during handshake) MUST be classified as
//! [`crate::Error::connection_lost`]. The pool uses that signal to
//! evict the slot and to wake the background sweep, which then pings
//! the remaining idle connections and drops any that also fail. Any
//! other error variant for the same condition leaks a dead connection
//! back into the pool.
//!
//! - A retryable transaction conflict (PostgreSQL SQLSTATE `40001`,
//! MySQL error `1213`) SHOULD be classified as
//! [`crate::Error::serialization_failure`]. The engine does not retry
//! automatically; the classification is propagated to user code so
//! the caller can decide.
//!
//! - A write attempted against a read-only session (PostgreSQL
//! `25006`, MySQL `1792`) SHOULD be classified as
//! [`crate::Error::read_only_transaction`].
//!
//! Other backend errors are typically wrapped with
//! [`crate::Error::driver_operation_failed`].
pub use ;
pub use ;
pub use ;
use crate;
use async_trait;
use ;
/// Factory for database connections and provider of driver-level metadata.
///
/// Each database backend (SQLite, PostgreSQL, MySQL, DynamoDB) implements this
/// trait to tell Toasty what the backend supports ([`Capability`]) and to
/// create [`Connection`] instances on demand.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::driver::Driver;
///
/// // Drivers are typically constructed from a connection URL:
/// let driver: Box<dyn Driver> = make_driver("sqlite::memory:").await;
/// assert!(!driver.url().is_empty());
///
/// let capability = driver.capability();
/// assert!(capability.sql);
///
/// let conn = driver.connect().await.unwrap();
/// ```
/// A live database session that can execute [`Operation`]s.
///
/// Connections are obtained from [`Driver::connect`] and are managed by the
/// connection pool. All query execution flows through [`Connection::exec`],
/// which accepts an [`Operation`] and returns an [`ExecResponse`].
///
/// # Examples
///
/// ```ignore
/// use toasty_core::driver::{Connection, Operation, ExecResponse};
/// use toasty_core::driver::operation::Transaction;
///
/// // Execute a transaction start operation on a connection:
/// let response = conn.exec(&schema, Transaction::start().into()).await?;
/// ```