sql_middleware/executor/
targets.rs1use crate::pool::MiddlewarePoolConnection;
2
3#[cfg(feature = "mssql")]
4use crate::mssql;
5#[cfg(feature = "postgres")]
6use crate::postgres;
7#[cfg(feature = "postgres")]
8use crate::postgres::typed::PgManager;
9#[cfg(feature = "sqlite")]
10use crate::sqlite::config::SqliteManager;
11#[cfg(feature = "turso")]
12use crate::turso;
13#[cfg(feature = "turso")]
14use crate::typed_turso::TursoManager;
15#[cfg(any(feature = "postgres", feature = "turso", feature = "sqlite"))]
16use bb8::PooledConnection;
17
18pub enum BatchTarget<'a> {
20 Connection(&'a mut MiddlewarePoolConnection),
21 #[cfg(feature = "postgres")]
22 PostgresTx(&'a postgres::transaction::Tx<'a>),
23 #[cfg(feature = "mssql")]
24 MssqlTx(&'a mut mssql::transaction::Tx<'a>),
25 #[cfg(feature = "turso")]
26 TursoTx(&'a turso::transaction::Tx<'a>),
27 #[cfg(feature = "turso")]
28 TypedTurso {
29 conn: &'a mut PooledConnection<'static, TursoManager>,
30 },
31 #[cfg(feature = "turso")]
32 TypedTursoTx {
33 conn: &'a mut PooledConnection<'static, TursoManager>,
34 },
35}
36
37pub struct QueryTarget<'a> {
39 pub(crate) kind: QueryTargetKind<'a>,
40 pub(crate) translation_default: bool,
41}
42
43pub(crate) enum QueryTargetKind<'a> {
44 Connection(&'a mut MiddlewarePoolConnection),
45 #[cfg(feature = "postgres")]
46 PostgresTx(&'a postgres::transaction::Tx<'a>),
47 #[cfg(feature = "sqlite")]
48 TypedSqlite {
49 conn: &'a mut PooledConnection<'static, SqliteManager>,
50 },
51 #[cfg(feature = "sqlite")]
52 TypedSqliteTx {
53 conn: &'a mut PooledConnection<'static, SqliteManager>,
54 },
55 #[cfg(feature = "postgres")]
56 TypedPostgres {
57 conn: &'a mut PooledConnection<'static, PgManager>,
58 },
59 #[cfg(feature = "postgres")]
60 TypedPostgresTx {
61 conn: &'a mut PooledConnection<'static, PgManager>,
62 },
63 #[cfg(feature = "turso")]
64 TypedTurso {
65 conn: &'a mut PooledConnection<'static, TursoManager>,
66 },
67 #[cfg(feature = "turso")]
68 TypedTursoTx {
69 conn: &'a mut PooledConnection<'static, TursoManager>,
70 },
71 #[cfg(feature = "mssql")]
72 MssqlTx(&'a mut mssql::transaction::Tx<'a>),
73 #[cfg(feature = "turso")]
74 TursoTx(&'a turso::transaction::Tx<'a>),
75}
76
77impl<'a> From<&'a mut MiddlewarePoolConnection> for BatchTarget<'a> {
78 fn from(conn: &'a mut MiddlewarePoolConnection) -> Self {
79 BatchTarget::Connection(conn)
80 }
81}
82
83#[cfg(feature = "postgres")]
84impl<'a> From<&'a postgres::transaction::Tx<'a>> for BatchTarget<'a> {
85 fn from(tx: &'a postgres::transaction::Tx<'a>) -> Self {
86 BatchTarget::PostgresTx(tx)
87 }
88}
89
90#[cfg(feature = "mssql")]
91impl<'a> From<&'a mut mssql::transaction::Tx<'a>> for BatchTarget<'a> {
92 fn from(tx: &'a mut mssql::transaction::Tx<'a>) -> Self {
93 BatchTarget::MssqlTx(tx)
94 }
95}
96
97#[cfg(feature = "turso")]
98impl<'a> From<&'a turso::transaction::Tx<'a>> for BatchTarget<'a> {
99 fn from(tx: &'a turso::transaction::Tx<'a>) -> Self {
100 BatchTarget::TursoTx(tx)
101 }
102}
103
104impl<'a> From<&'a mut MiddlewarePoolConnection> for QueryTarget<'a> {
105 fn from(conn: &'a mut MiddlewarePoolConnection) -> Self {
106 QueryTarget {
107 translation_default: conn.translation_default(),
108 kind: QueryTargetKind::Connection(conn),
109 }
110 }
111}
112
113#[cfg(feature = "sqlite")]
114impl<'a> QueryTarget<'a> {
115 pub(crate) fn from_typed_sqlite(
116 conn: &'a mut PooledConnection<'static, SqliteManager>,
117 in_tx: bool,
118 ) -> Self {
119 let kind = if in_tx {
120 QueryTargetKind::TypedSqliteTx { conn }
121 } else {
122 QueryTargetKind::TypedSqlite { conn }
123 };
124 QueryTarget {
125 translation_default: false,
126 kind,
127 }
128 }
129}
130
131#[cfg(feature = "postgres")]
132impl<'a> QueryTarget<'a> {
133 pub(crate) fn from_typed_postgres(
134 conn: &'a mut PooledConnection<'static, PgManager>,
135 in_tx: bool,
136 ) -> Self {
137 let kind = if in_tx {
138 QueryTargetKind::TypedPostgresTx { conn }
139 } else {
140 QueryTargetKind::TypedPostgres { conn }
141 };
142 QueryTarget {
143 translation_default: false,
144 kind,
145 }
146 }
147}
148
149#[cfg(feature = "postgres")]
150impl<'a> From<&'a postgres::transaction::Tx<'a>> for QueryTarget<'a> {
151 fn from(tx: &'a postgres::transaction::Tx<'a>) -> Self {
152 QueryTarget {
153 translation_default: false,
154 kind: QueryTargetKind::PostgresTx(tx),
155 }
156 }
157}
158
159#[cfg(feature = "mssql")]
160impl<'a> From<&'a mut mssql::transaction::Tx<'a>> for QueryTarget<'a> {
161 fn from(tx: &'a mut mssql::transaction::Tx<'a>) -> Self {
162 QueryTarget {
163 translation_default: false,
164 kind: QueryTargetKind::MssqlTx(tx),
165 }
166 }
167}
168
169#[cfg(feature = "turso")]
170impl<'a> From<&'a turso::transaction::Tx<'a>> for QueryTarget<'a> {
171 fn from(tx: &'a turso::transaction::Tx<'a>) -> Self {
172 QueryTarget {
173 translation_default: false,
174 kind: QueryTargetKind::TursoTx(tx),
175 }
176 }
177}
178
179#[cfg(feature = "turso")]
180impl<'a> QueryTarget<'a> {
181 pub(crate) fn from_typed_turso(
182 conn: &'a mut PooledConnection<'static, TursoManager>,
183 in_tx: bool,
184 ) -> Self {
185 let kind = if in_tx {
186 QueryTargetKind::TypedTursoTx { conn }
187 } else {
188 QueryTargetKind::TypedTurso { conn }
189 };
190 QueryTarget {
191 translation_default: true,
192 kind,
193 }
194 }
195}