sqlx_core_oldapi/mssql/
statement.rs1use crate::column::ColumnIndex;
2use crate::error::Error;
3use crate::ext::ustr::UStr;
4use crate::mssql::{Mssql, MssqlArguments, MssqlColumn, MssqlTypeInfo};
5use crate::statement::Statement;
6use crate::HashMap;
7use either::Either;
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 .copied()
56 }
57}
58
59#[cfg(feature = "any")]
60impl<'q> From<MssqlStatement<'q>> for crate::any::AnyStatement<'q> {
61 #[inline]
62 fn from(statement: MssqlStatement<'q>) -> Self {
63 crate::any::AnyStatement::<'q> {
64 columns: statement
65 .metadata
66 .columns
67 .iter()
68 .map(|col| col.clone().into())
69 .collect(),
70 column_names: std::sync::Arc::new(statement.metadata.column_names.clone()),
71 parameters: None,
72 sql: statement.sql,
73 }
74 }
75}