Skip to main content

rbdc_pg/
statement.rs

1use crate::column::PgColumn;
2use crate::type_info::PgTypeInfo;
3use either::Either;
4use rbdc::ext::ustr::UStr;
5use rbdc::Error;
6use std::collections::HashMap;
7use std::sync::Arc;
8
9#[derive(Debug, Clone)]
10pub struct PgStatement {
11    pub(crate) sql: String,
12    pub(crate) metadata: Arc<PgStatementMetadata>,
13}
14
15#[derive(Debug, Default)]
16pub struct PgStatementMetadata {
17    pub(crate) columns: Vec<PgColumn>,
18    pub(crate) column_names: HashMap<UStr, usize>,
19    pub(crate) parameters: Vec<PgTypeInfo>,
20}
21
22impl PgStatement {
23    pub fn to_owned(&self) -> PgStatement {
24        PgStatement {
25            sql: self.sql.to_string(),
26            metadata: self.metadata.clone(),
27        }
28    }
29
30    pub fn sql(&self) -> &str {
31        &self.sql
32    }
33
34    pub fn parameters(&self) -> Option<Either<&[PgTypeInfo], usize>> {
35        Some(Either::Left(&self.metadata.parameters))
36    }
37
38    pub fn columns(&self) -> &[PgColumn] {
39        &self.metadata.columns
40    }
41}
42
43impl PgStatement {
44    pub fn index(&self, index: &str) -> Result<usize, Error> {
45        self.metadata
46            .column_names
47            .get(index)
48            .ok_or_else(|| Error::from(format!("ColumnNotFound {}", index)))
49            .map(|v| *v)
50    }
51}