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