Skip to main content

sentinel_driver/
statement.rs

1use std::sync::Arc;
2
3use crate::protocol::backend::FieldDescription;
4use crate::types::Oid;
5
6/// A prepared statement with parameter types and result column descriptions.
7#[derive(Debug, Clone)]
8pub struct Statement {
9    /// Server-assigned statement name (empty string = unnamed).
10    name: String,
11    /// SQL query text.
12    sql: String,
13    /// Parameter type OIDs (from ParameterDescription).
14    param_types: Vec<Oid>,
15    /// Result column descriptions (from RowDescription). `None` for non-SELECT.
16    columns: Option<Arc<Vec<FieldDescription>>>,
17}
18
19impl Statement {
20    pub fn new(
21        name: String,
22        sql: String,
23        param_types: Vec<Oid>,
24        columns: Option<Vec<FieldDescription>>,
25    ) -> Self {
26        Self {
27            name,
28            sql,
29            param_types,
30            columns: columns.map(Arc::new),
31        }
32    }
33
34    /// The server-assigned statement name.
35    pub fn name(&self) -> &str {
36        &self.name
37    }
38
39    /// The SQL query text.
40    pub fn sql(&self) -> &str {
41        &self.sql
42    }
43
44    /// Parameter type OIDs.
45    pub fn param_types(&self) -> &[Oid] {
46        &self.param_types
47    }
48
49    /// Number of parameters.
50    pub fn param_count(&self) -> usize {
51        self.param_types.len()
52    }
53
54    /// Result column descriptions. `None` for statements that don't return rows.
55    pub fn columns(&self) -> Option<&[FieldDescription]> {
56        self.columns.as_ref().map(|c| c.as_slice())
57    }
58
59    /// Number of result columns. 0 if the statement doesn't return rows.
60    pub fn column_count(&self) -> usize {
61        self.columns.as_ref().map_or(0, |c| c.len())
62    }
63}