sqlx_core_oldapi/any/
statement.rs1use crate::any::{Any, AnyArguments, AnyColumn, AnyColumnIndex, AnyTypeInfo};
2use crate::column::ColumnIndex;
3use crate::error::Error;
4use crate::ext::ustr::UStr;
5use crate::statement::Statement;
6use crate::HashMap;
7use either::Either;
8use std::borrow::Cow;
9use std::sync::Arc;
10
11pub struct AnyStatement<'q> {
12 pub(crate) sql: Cow<'q, str>,
13 pub(crate) parameters: Option<Either<Vec<AnyTypeInfo>, usize>>,
14 pub(crate) column_names: Arc<HashMap<UStr, usize>>,
15 pub(crate) columns: Vec<AnyColumn>,
16}
17
18impl<'q> Statement<'q> for AnyStatement<'q> {
19 type Database = Any;
20
21 fn to_owned(&self) -> AnyStatement<'static> {
22 AnyStatement::<'static> {
23 sql: Cow::Owned(self.sql.clone().into_owned()),
24 column_names: self.column_names.clone(),
25 parameters: self.parameters.clone(),
26 columns: self.columns.clone(),
27 }
28 }
29
30 fn sql(&self) -> &str {
31 &self.sql
32 }
33
34 fn parameters(&self) -> Option<Either<&[AnyTypeInfo], usize>> {
35 match &self.parameters {
36 Some(Either::Left(types)) => Some(Either::Left(&types)),
37 Some(Either::Right(count)) => Some(Either::Right(*count)),
38 None => None,
39 }
40 }
41
42 fn columns(&self) -> &[AnyColumn] {
43 &self.columns
44 }
45
46 impl_statement_query!(AnyArguments<'_>);
47}
48
49impl<'i> ColumnIndex<AnyStatement<'_>> for &'i str
50where
51 &'i str: AnyColumnIndex,
52{
53 fn index(&self, statement: &AnyStatement<'_>) -> Result<usize, Error> {
54 statement
55 .column_names
56 .get(*self)
57 .ok_or_else(|| Error::ColumnNotFound((*self).into()))
58 .map(|v| *v)
59 }
60}