sql_middleware/executor/
targets.rs

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