mssql_client/
query.rs

1//! Query builder and prepared statement support.
2
3use mssql_types::ToSql;
4
5/// A prepared query builder.
6///
7/// Queries can be built incrementally and reused with different parameters.
8#[derive(Debug, Clone)]
9pub struct Query {
10    sql: String,
11    // Placeholder for prepared statement handle and metadata
12}
13
14impl Query {
15    /// Create a new query from SQL text.
16    #[must_use]
17    pub fn new(sql: impl Into<String>) -> Self {
18        Self { sql: sql.into() }
19    }
20
21    /// Get the SQL text.
22    #[must_use]
23    pub fn sql(&self) -> &str {
24        &self.sql
25    }
26}
27
28/// Extension trait for building parameterized queries.
29pub trait QueryExt {
30    /// Add a parameter to the query.
31    fn bind<T: ToSql>(self, value: &T) -> BoundQuery<'_>;
32}
33
34/// A query with bound parameters.
35pub struct BoundQuery<'a> {
36    sql: &'a str,
37    params: Vec<&'a dyn ToSql>,
38}
39
40impl<'a> BoundQuery<'a> {
41    /// Create a new bound query.
42    pub fn new(sql: &'a str) -> Self {
43        Self {
44            sql,
45            params: Vec::new(),
46        }
47    }
48
49    /// Add another parameter.
50    pub fn bind<T: ToSql>(mut self, value: &'a T) -> Self {
51        self.params.push(value);
52        self
53    }
54
55    /// Get the SQL text.
56    #[must_use]
57    pub fn sql(&self) -> &str {
58        self.sql
59    }
60
61    /// Get the bound parameters.
62    #[must_use]
63    pub fn params(&self) -> &[&dyn ToSql] {
64        &self.params
65    }
66}