sql_middleware/query.rs
1use crate::types::RowValues;
2
3/// A SQL string and its bound parameters bundled together.
4///
5/// Handy for helpers that need to return both query text and params without
6/// losing alignment with placeholder translation:
7/// ```rust
8/// use sql_middleware::prelude::*;
9///
10/// let qp = QueryAndParams::new(
11/// "INSERT INTO t (id, name) VALUES ($1, $2)",
12/// vec![RowValues::Int(1), RowValues::Text("alice".into())],
13/// );
14/// # let _ = qp;
15/// ```
16#[derive(Debug, Clone)]
17pub struct QueryAndParams {
18 /// The SQL query string
19 pub query: String,
20 /// The parameters to be bound to the query
21 pub params: Vec<RowValues>,
22}
23
24impl QueryAndParams {
25 /// Create a new `QueryAndParams` with the given query string and parameters
26 ///
27 /// # Arguments
28 ///
29 /// * `query` - The SQL query string
30 /// * `params` - The parameters to bind to the query
31 ///
32 /// # Returns
33 ///
34 /// A new `QueryAndParams` instance
35 pub fn new(query: impl Into<String>, params: Vec<RowValues>) -> Self {
36 Self {
37 query: query.into(),
38 params,
39 }
40 }
41
42 /// Create a new `QueryAndParams` with no parameters
43 ///
44 /// # Arguments
45 ///
46 /// * `query` - The SQL query string
47 ///
48 /// # Returns
49 ///
50 /// A new `QueryAndParams` instance with an empty parameter list
51 pub fn new_without_params(query: impl Into<String>) -> Self {
52 Self {
53 query: query.into(),
54 params: Vec::new(),
55 }
56 }
57}