Skip to main content

sqlx_core_oldapi/odbc/
statement.rs

1use crate::column::ColumnIndex;
2use crate::error::Error;
3use crate::odbc::{Odbc, OdbcColumn, OdbcTypeInfo};
4use crate::statement::Statement;
5use either::Either;
6use std::borrow::Cow;
7
8#[derive(Debug, Clone)]
9pub struct OdbcStatement<'q> {
10    pub(crate) sql: Cow<'q, str>,
11    pub(crate) metadata: OdbcStatementMetadata,
12}
13
14#[derive(Debug, Clone)]
15pub struct OdbcStatementMetadata {
16    pub columns: Vec<OdbcColumn>,
17    pub parameters: usize,
18}
19
20impl<'q> Statement<'q> for OdbcStatement<'q> {
21    type Database = Odbc;
22
23    fn to_owned(&self) -> OdbcStatement<'static> {
24        OdbcStatement {
25            sql: Cow::Owned(self.sql.to_string()),
26            metadata: self.metadata.clone(),
27        }
28    }
29
30    fn sql(&self) -> &str {
31        &self.sql
32    }
33    fn parameters(&self) -> Option<Either<&[OdbcTypeInfo], usize>> {
34        Some(Either::Right(self.metadata.parameters))
35    }
36    fn columns(&self) -> &[OdbcColumn] {
37        &self.metadata.columns
38    }
39
40    impl_statement_query!(crate::odbc::OdbcArguments);
41}
42
43impl ColumnIndex<OdbcStatement<'_>> for &'_ str {
44    fn index(&self, statement: &OdbcStatement<'_>) -> Result<usize, Error> {
45        statement
46            .metadata
47            .columns
48            .iter()
49            .position(|c| c.name == *self)
50            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
51    }
52}
53
54#[cfg(feature = "any")]
55impl<'q> From<OdbcStatement<'q>> for crate::any::AnyStatement<'q> {
56    fn from(stmt: OdbcStatement<'q>) -> Self {
57        let mut column_names = crate::HashMap::<crate::ext::ustr::UStr, usize>::default();
58
59        // First build the columns and collect names
60        let columns: Vec<_> = stmt
61            .metadata
62            .columns
63            .iter()
64            .enumerate()
65            .map(|(index, col)| {
66                column_names.insert(crate::ext::ustr::UStr::new(&col.name), index);
67                crate::any::AnyColumn {
68                    kind: crate::any::column::AnyColumnKind::Odbc(col.clone()),
69                    type_info: crate::any::AnyTypeInfo::from(col.type_info.clone()),
70                }
71            })
72            .collect();
73
74        crate::any::AnyStatement {
75            sql: stmt.sql,
76            parameters: Some(either::Either::Right(stmt.metadata.parameters)),
77            columns,
78            column_names: std::sync::Arc::new(column_names),
79        }
80    }
81}