sql_middleware/executor/
targets.rs

1use crate::pool::MiddlewarePoolConnection;
2use crate::translation::PlaceholderStyle;
3
4#[cfg(feature = "libsql")]
5use crate::libsql;
6#[cfg(feature = "mssql")]
7use crate::mssql;
8#[cfg(feature = "postgres")]
9use crate::postgres;
10#[cfg(feature = "postgres")]
11use crate::postgres::typed::PgManager;
12#[cfg(feature = "sqlite")]
13use crate::sqlite::config::SqliteManager;
14#[cfg(feature = "turso")]
15use crate::turso;
16#[cfg(feature = "turso")]
17use crate::typed_turso::TursoManager;
18#[cfg(any(feature = "postgres", feature = "turso", feature = "sqlite"))]
19use bb8::PooledConnection;
20
21/// Target for batch execution (connection or transaction).
22pub enum BatchTarget<'a> {
23    Connection(&'a mut MiddlewarePoolConnection),
24    #[cfg(feature = "postgres")]
25    PostgresTx(&'a postgres::transaction::Tx<'a>),
26    #[cfg(feature = "mssql")]
27    MssqlTx(&'a mut mssql::transaction::Tx<'a>),
28    #[cfg(feature = "libsql")]
29    LibsqlTx(&'a libsql::transaction::Tx<'a>),
30    #[cfg(feature = "turso")]
31    TursoTx(&'a turso::transaction::Tx<'a>),
32    #[cfg(feature = "turso")]
33    TypedTurso {
34        conn: &'a mut PooledConnection<'static, TursoManager>,
35    },
36    #[cfg(feature = "turso")]
37    TypedTursoTx {
38        conn: &'a mut PooledConnection<'static, TursoManager>,
39    },
40}
41
42/// Target for query builder dispatch (connection or transaction) with a translation default.
43pub struct QueryTarget<'a> {
44    pub(crate) kind: QueryTargetKind<'a>,
45    translation_default: bool,
46}
47
48pub(crate) enum QueryTargetKind<'a> {
49    Connection(&'a mut MiddlewarePoolConnection),
50    #[cfg(feature = "postgres")]
51    PostgresTx(&'a postgres::transaction::Tx<'a>),
52    #[cfg(feature = "sqlite")]
53    TypedSqlite {
54        conn: &'a mut PooledConnection<'static, SqliteManager>,
55    },
56    #[cfg(feature = "sqlite")]
57    TypedSqliteTx {
58        conn: &'a mut PooledConnection<'static, SqliteManager>,
59    },
60    #[cfg(feature = "postgres")]
61    TypedPostgres {
62        conn: &'a mut PooledConnection<'static, PgManager>,
63    },
64    #[cfg(feature = "postgres")]
65    TypedPostgresTx {
66        conn: &'a mut PooledConnection<'static, PgManager>,
67    },
68    #[cfg(feature = "turso")]
69    TypedTurso {
70        conn: &'a mut PooledConnection<'static, TursoManager>,
71    },
72    #[cfg(feature = "turso")]
73    TypedTursoTx {
74        conn: &'a mut PooledConnection<'static, TursoManager>,
75    },
76    #[cfg(feature = "mssql")]
77    MssqlTx(&'a mut mssql::transaction::Tx<'a>),
78    #[cfg(feature = "libsql")]
79    LibsqlTx(&'a libsql::transaction::Tx<'a>),
80    #[cfg(feature = "turso")]
81    TursoTx(&'a turso::transaction::Tx<'a>),
82}
83
84impl<'a> From<&'a mut MiddlewarePoolConnection> for BatchTarget<'a> {
85    fn from(conn: &'a mut MiddlewarePoolConnection) -> Self {
86        BatchTarget::Connection(conn)
87    }
88}
89
90#[cfg(feature = "postgres")]
91impl<'a> From<&'a postgres::transaction::Tx<'a>> for BatchTarget<'a> {
92    fn from(tx: &'a postgres::transaction::Tx<'a>) -> Self {
93        BatchTarget::PostgresTx(tx)
94    }
95}
96
97#[cfg(feature = "mssql")]
98impl<'a> From<&'a mut mssql::transaction::Tx<'a>> for BatchTarget<'a> {
99    fn from(tx: &'a mut mssql::transaction::Tx<'a>) -> Self {
100        BatchTarget::MssqlTx(tx)
101    }
102}
103
104#[cfg(feature = "libsql")]
105impl<'a> From<&'a libsql::transaction::Tx<'a>> for BatchTarget<'a> {
106    fn from(tx: &'a libsql::transaction::Tx<'a>) -> Self {
107        BatchTarget::LibsqlTx(tx)
108    }
109}
110
111#[cfg(feature = "turso")]
112impl<'a> From<&'a turso::transaction::Tx<'a>> for BatchTarget<'a> {
113    fn from(tx: &'a turso::transaction::Tx<'a>) -> Self {
114        BatchTarget::TursoTx(tx)
115    }
116}
117
118impl<'a> From<&'a mut MiddlewarePoolConnection> for QueryTarget<'a> {
119    fn from(conn: &'a mut MiddlewarePoolConnection) -> Self {
120        QueryTarget {
121            translation_default: conn.translation_default(),
122            kind: QueryTargetKind::Connection(conn),
123        }
124    }
125}
126
127#[cfg(feature = "sqlite")]
128impl<'a> QueryTarget<'a> {
129    pub(crate) fn from_typed_sqlite(
130        conn: &'a mut PooledConnection<'static, SqliteManager>,
131        in_tx: bool,
132    ) -> Self {
133        let kind = if in_tx {
134            QueryTargetKind::TypedSqliteTx { conn }
135        } else {
136            QueryTargetKind::TypedSqlite { conn }
137        };
138        QueryTarget {
139            translation_default: false,
140            kind,
141        }
142    }
143}
144
145#[cfg(feature = "postgres")]
146impl<'a> QueryTarget<'a> {
147    pub(crate) fn from_typed_postgres(
148        conn: &'a mut PooledConnection<'static, PgManager>,
149        in_tx: bool,
150    ) -> Self {
151        let kind = if in_tx {
152            QueryTargetKind::TypedPostgresTx { conn }
153        } else {
154            QueryTargetKind::TypedPostgres { conn }
155        };
156        QueryTarget {
157            translation_default: false,
158            kind,
159        }
160    }
161}
162
163#[cfg(feature = "postgres")]
164impl<'a> From<&'a postgres::transaction::Tx<'a>> for QueryTarget<'a> {
165    fn from(tx: &'a postgres::transaction::Tx<'a>) -> Self {
166        QueryTarget {
167            translation_default: false,
168            kind: QueryTargetKind::PostgresTx(tx),
169        }
170    }
171}
172
173#[cfg(feature = "mssql")]
174impl<'a> From<&'a mut mssql::transaction::Tx<'a>> for QueryTarget<'a> {
175    fn from(tx: &'a mut mssql::transaction::Tx<'a>) -> Self {
176        QueryTarget {
177            translation_default: false,
178            kind: QueryTargetKind::MssqlTx(tx),
179        }
180    }
181}
182
183#[cfg(feature = "libsql")]
184impl<'a> From<&'a libsql::transaction::Tx<'a>> for QueryTarget<'a> {
185    fn from(tx: &'a libsql::transaction::Tx<'a>) -> Self {
186        QueryTarget {
187            translation_default: false,
188            kind: QueryTargetKind::LibsqlTx(tx),
189        }
190    }
191}
192
193#[cfg(feature = "turso")]
194impl<'a> From<&'a turso::transaction::Tx<'a>> for QueryTarget<'a> {
195    fn from(tx: &'a turso::transaction::Tx<'a>) -> Self {
196        QueryTarget {
197            translation_default: false,
198            kind: QueryTargetKind::TursoTx(tx),
199        }
200    }
201}
202
203#[cfg(feature = "turso")]
204impl<'a> QueryTarget<'a> {
205    pub(crate) fn from_typed_turso(
206        conn: &'a mut PooledConnection<'static, TursoManager>,
207        in_tx: bool,
208    ) -> Self {
209        let kind = if in_tx {
210            QueryTargetKind::TypedTursoTx { conn }
211        } else {
212            QueryTargetKind::TypedTurso { conn }
213        };
214        QueryTarget {
215            translation_default: true,
216            kind,
217        }
218    }
219}
220
221impl QueryTarget<'_> {
222    #[must_use]
223    pub(crate) fn translation_default(&self) -> bool {
224        self.translation_default
225    }
226
227    #[must_use]
228    pub(crate) fn translation_target(&self) -> Option<PlaceholderStyle> {
229        match &self.kind {
230            QueryTargetKind::Connection(conn) => translation_target(conn),
231            #[cfg(feature = "postgres")]
232            QueryTargetKind::PostgresTx(_) => Some(PlaceholderStyle::Postgres),
233            #[cfg(feature = "sqlite")]
234            QueryTargetKind::TypedSqlite { .. } => Some(PlaceholderStyle::Sqlite),
235            #[cfg(feature = "sqlite")]
236            QueryTargetKind::TypedSqliteTx { .. } => Some(PlaceholderStyle::Sqlite),
237            #[cfg(feature = "postgres")]
238            QueryTargetKind::TypedPostgres { .. } => Some(PlaceholderStyle::Postgres),
239            #[cfg(feature = "postgres")]
240            QueryTargetKind::TypedPostgresTx { .. } => Some(PlaceholderStyle::Postgres),
241            #[cfg(feature = "libsql")]
242            QueryTargetKind::LibsqlTx(_) => Some(PlaceholderStyle::Sqlite),
243            #[cfg(feature = "turso")]
244            QueryTargetKind::TursoTx(_) => Some(PlaceholderStyle::Sqlite),
245            #[cfg(feature = "turso")]
246            QueryTargetKind::TypedTurso { .. } => Some(PlaceholderStyle::Sqlite),
247            #[cfg(feature = "turso")]
248            QueryTargetKind::TypedTursoTx { .. } => Some(PlaceholderStyle::Sqlite),
249            #[cfg(feature = "mssql")]
250            QueryTargetKind::MssqlTx(_) => None,
251            #[allow(unreachable_patterns)]
252            _ => None,
253        }
254    }
255}
256
257pub(crate) fn translation_target(conn: &MiddlewarePoolConnection) -> Option<PlaceholderStyle> {
258    match conn {
259        #[cfg(feature = "postgres")]
260        MiddlewarePoolConnection::Postgres { .. } => Some(PlaceholderStyle::Postgres),
261        #[cfg(feature = "sqlite")]
262        MiddlewarePoolConnection::Sqlite { .. } => Some(PlaceholderStyle::Sqlite),
263        #[cfg(feature = "libsql")]
264        MiddlewarePoolConnection::Libsql { .. } => Some(PlaceholderStyle::Sqlite),
265        #[cfg(feature = "turso")]
266        MiddlewarePoolConnection::Turso { .. } => Some(PlaceholderStyle::Sqlite),
267        #[cfg(feature = "mssql")]
268        MiddlewarePoolConnection::Mssql { .. } => None,
269        #[allow(unreachable_patterns)]
270        _ => None,
271    }
272}