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