Skip to main content

toasty_core/driver/operation/
query_sql.rs

1use super::{Operation, TypedValue};
2
3use crate::stmt;
4
5/// Executes a SQL statement against the database.
6///
7/// Only sent to SQL-capable drivers (those with [`Capability::sql`](super::super::Capability)
8/// set to `true`). The statement is a fully lowered [`stmt::Statement`] that
9/// the SQL serialization layer converts into a backend-specific SQL string.
10///
11/// # Examples
12///
13/// ```ignore
14/// use toasty_core::driver::operation::{QuerySql, Operation};
15///
16/// let op = QuerySql {
17///     stmt: sql_statement,
18///     ret: Some(vec![stmt::Type::String, stmt::Type::I64]),
19///     last_insert_id_hack: None,
20/// };
21/// let operation: Operation = op.into();
22/// assert!(operation.is_query_sql());
23/// ```
24#[derive(Debug, Clone)]
25pub struct QuerySql {
26    /// The SQL statement to execute. Scalar values that should be sent as
27    /// bind parameters have been replaced with `Expr::Arg(n)` where `n` is
28    /// the index into [`params`](Self::params).
29    pub stmt: stmt::Statement,
30
31    /// Typed bind parameters extracted from the statement. Each entry
32    /// corresponds to an `Expr::Arg(n)` placeholder in the statement.
33    pub params: Vec<TypedValue>,
34
35    /// The types of columns in the result set. When `Some`, the driver uses
36    /// these types to decode returned rows. When `None`, the statement does
37    /// not return rows (e.g., `DELETE` without `RETURNING`).
38    pub ret: Option<Vec<stmt::Type>>,
39
40    /// **Temporary MySQL workaround** for `RETURNING` from `INSERT`.
41    ///
42    /// When set, the driver should fetch `LAST_INSERT_ID()` to simulate
43    /// `RETURNING` behavior for the specified number of inserted rows.
44    /// Non-MySQL drivers should assert this is `None`.
45    pub last_insert_id_hack: Option<u64>,
46}
47
48impl Operation {
49    /// Returns `true` if this is a [`QuerySql`](Operation::QuerySql) operation.
50    pub fn is_query_sql(&self) -> bool {
51        matches!(self, Operation::QuerySql(_))
52    }
53}
54
55impl From<QuerySql> for Operation {
56    fn from(value: QuerySql) -> Self {
57        Self::QuerySql(value)
58    }
59}