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    // ODBC arguments placeholder
41    impl_statement_query!(crate::odbc::OdbcArguments);
42}
43
44impl ColumnIndex<OdbcStatement<'_>> for &'_ str {
45    fn index(&self, statement: &OdbcStatement<'_>) -> Result<usize, Error> {
46        statement
47            .metadata
48            .columns
49            .iter()
50            .position(|c| c.name == *self)
51            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
52    }
53}
54
55#[cfg(feature = "any")]
56impl<'q> From<OdbcStatement<'q>> for crate::any::AnyStatement<'q> {
57    fn from(stmt: OdbcStatement<'q>) -> Self {
58        let mut column_names = crate::HashMap::<crate::ext::ustr::UStr, usize>::default();
59
60        // First build the columns and collect names
61        let columns: Vec<_> = stmt
62            .metadata
63            .columns
64            .iter()
65            .enumerate()
66            .map(|(index, col)| {
67                column_names.insert(crate::ext::ustr::UStr::new(&col.name), index);
68                crate::any::AnyColumn {
69                    kind: crate::any::column::AnyColumnKind::Odbc(col.clone()),
70                    type_info: crate::any::AnyTypeInfo::from(col.type_info.clone()),
71                }
72            })
73            .collect();
74
75        crate::any::AnyStatement {
76            sql: stmt.sql,
77            parameters: Some(either::Either::Right(stmt.metadata.parameters)),
78            columns,
79            column_names: std::sync::Arc::new(column_names),
80        }
81    }
82}