sql_middleware/query.rs
1#[cfg(feature = "mssql")]
2use tiberius::Client as TiberiusClient;
3#[cfg(feature = "mssql")]
4use tokio::net::TcpStream;
5#[cfg(feature = "mssql")]
6use tokio_util::compat::Compat;
7
8#[cfg(feature = "sqlite")]
9use deadpool_sqlite::rusqlite::Connection as SqliteConnectionType;
10
11use crate::types::RowValues;
12
13/// Wrapper around a database connection for generic code
14///
15/// This enum allows code to handle `PostgreSQL`, `SQLite`, SQL Server, or `LibSQL`
16/// connections in a generic way.
17pub enum AnyConnWrapper<'a> {
18 /// `PostgreSQL` client connection
19 #[cfg(feature = "postgres")]
20 Postgres(&'a mut tokio_postgres::Client),
21 /// `SQLite` database connection
22 #[cfg(feature = "sqlite")]
23 Sqlite(&'a mut SqliteConnectionType),
24 /// SQL Server client connection
25 #[cfg(feature = "mssql")]
26 Mssql(&'a mut TiberiusClient<Compat<TcpStream>>),
27 /// `LibSQL` database connection
28 #[cfg(feature = "libsql")]
29 Libsql(&'a deadpool_libsql::Object),
30 /// Turso database connection
31 #[cfg(feature = "turso")]
32 Turso(&'a turso::Connection),
33}
34
35/// A query and its parameters bundled together
36///
37/// This type makes it easier to pass around a SQL query and its
38/// parameters as a single unit.
39#[derive(Debug, Clone)]
40pub struct QueryAndParams {
41 /// The SQL query string
42 pub query: String,
43 /// The parameters to be bound to the query
44 pub params: Vec<RowValues>,
45}
46
47impl QueryAndParams {
48 /// Create a new `QueryAndParams` with the given query string and parameters
49 ///
50 /// # Arguments
51 ///
52 /// * `query` - The SQL query string
53 /// * `params` - The parameters to bind to the query
54 ///
55 /// # Returns
56 ///
57 /// A new `QueryAndParams` instance
58 pub fn new(query: impl Into<String>, params: Vec<RowValues>) -> Self {
59 Self {
60 query: query.into(),
61 params,
62 }
63 }
64
65 /// Create a new `QueryAndParams` with no parameters
66 ///
67 /// # Arguments
68 ///
69 /// * `query` - The SQL query string
70 ///
71 /// # Returns
72 ///
73 /// A new `QueryAndParams` instance with an empty parameter list
74 pub fn new_without_params(query: impl Into<String>) -> Self {
75 Self {
76 query: query.into(),
77 params: Vec::new(),
78 }
79 }
80}