welds_sqlx_mssql/
statement.rs

1use crate::error::Error;
2use crate::ext::ustr::UStr;
3use crate::HashMap;
4use crate::{Mssql, MssqlArguments, MssqlColumn, MssqlTypeInfo};
5use either::Either;
6use sqlx_core::column::ColumnIndex;
7use sqlx_core::statement::Statement;
8use std::borrow::Cow;
9use std::sync::Arc;
10
11#[derive(Debug, Clone)]
12pub struct MssqlStatement<'q> {
13    pub(crate) sql: Cow<'q, str>,
14    pub(crate) metadata: Arc<MssqlStatementMetadata>,
15}
16
17#[derive(Debug, Default, Clone)]
18pub(crate) struct MssqlStatementMetadata {
19    pub(crate) columns: Vec<MssqlColumn>,
20    pub(crate) column_names: HashMap<UStr, usize>,
21}
22
23impl<'q> Statement<'q> for MssqlStatement<'q> {
24    type Database = Mssql;
25
26    fn to_owned(&self) -> MssqlStatement<'static> {
27        MssqlStatement::<'static> {
28            sql: Cow::Owned(self.sql.clone().into_owned()),
29            metadata: self.metadata.clone(),
30        }
31    }
32
33    fn sql(&self) -> &str {
34        &self.sql
35    }
36
37    fn parameters(&self) -> Option<Either<&[MssqlTypeInfo], usize>> {
38        None
39    }
40
41    fn columns(&self) -> &[MssqlColumn] {
42        &self.metadata.columns
43    }
44
45    impl_statement_query!(MssqlArguments);
46}
47
48impl ColumnIndex<MssqlStatement<'_>> for &'_ str {
49    fn index(&self, statement: &MssqlStatement<'_>) -> Result<usize, Error> {
50        statement
51            .metadata
52            .column_names
53            .get(*self)
54            .ok_or_else(|| Error::ColumnNotFound((*self).into()))
55            .map(|v| *v)
56    }
57}