| 1 | use crate::protocol::text::ColumnType; |
| 2 | use crate::{ |
| 3 | MySql, MySqlColumn, MySqlConnectOptions, MySqlConnection, MySqlQueryResult, MySqlRow, |
| 4 | MySqlTransactionManager, MySqlTypeInfo, |
| 5 | }; |
| 6 | use either::Either; |
| 7 | use futures_core::future::BoxFuture; |
| 8 | use futures_core::stream::BoxStream; |
| 9 | use futures_util::{stream, StreamExt, TryFutureExt, TryStreamExt}; |
| 10 | use sqlx_core::any::{ |
| 11 | Any, AnyArguments, AnyColumn, AnyConnectOptions, AnyConnectionBackend, AnyQueryResult, AnyRow, |
| 12 | AnyStatement, AnyTypeInfo, AnyTypeInfoKind, |
| 13 | }; |
| 14 | use sqlx_core::connection::Connection; |
| 15 | use sqlx_core::database::Database; |
| 16 | use sqlx_core::describe::Describe; |
| 17 | use sqlx_core::executor::Executor; |
| 18 | use sqlx_core::transaction::TransactionManager; |
| 19 | use std::future; |
| 20 | |
| 21 | sqlx_core::declare_driver_with_optional_migrate!(DRIVER = MySql); |
| 22 | |
| 23 | impl AnyConnectionBackend for MySqlConnection { |
| 24 | fn name(&self) -> &str { |
| 25 | <MySql as Database>::NAME |
| 26 | } |
| 27 | |
| 28 | fn close(self: Box<Self>) -> BoxFuture<'static, sqlx_core::Result<()>> { |
| 29 | Connection::close(*self) |
| 30 | } |
| 31 | |
| 32 | fn close_hard(self: Box<Self>) -> BoxFuture<'static, sqlx_core::Result<()>> { |
| 33 | Connection::close_hard(*self) |
| 34 | } |
| 35 | |
| 36 | fn ping(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> { |
| 37 | Connection::ping(self) |
| 38 | } |
| 39 | |
| 40 | fn begin(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> { |
| 41 | MySqlTransactionManager::begin(self) |
| 42 | } |
| 43 | |
| 44 | fn commit(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> { |
| 45 | MySqlTransactionManager::commit(self) |
| 46 | } |
| 47 | |
| 48 | fn rollback(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> { |
| 49 | MySqlTransactionManager::rollback(self) |
| 50 | } |
| 51 | |
| 52 | fn start_rollback(&mut self) { |
| 53 | MySqlTransactionManager::start_rollback(self) |
| 54 | } |
| 55 | |
| 56 | fn shrink_buffers(&mut self) { |
| 57 | Connection::shrink_buffers(self); |
| 58 | } |
| 59 | |
| 60 | fn flush(&mut self) -> BoxFuture<'_, sqlx_core::Result<()>> { |
| 61 | Connection::flush(self) |
| 62 | } |
| 63 | |
| 64 | fn should_flush(&self) -> bool { |
| 65 | Connection::should_flush(self) |
| 66 | } |
| 67 | |
| 68 | #[cfg(feature = "migrate")] |
| 69 | fn as_migrate( |
| 70 | &mut self, |
| 71 | ) -> sqlx_core::Result<&mut (dyn sqlx_core::migrate::Migrate + Send + 'static)> { |
| 72 | Ok(self) |
| 73 | } |
| 74 | |
| 75 | fn fetch_many<'q>( |
| 76 | &'q mut self, |
| 77 | query: &'q str, |
| 78 | persistent: bool, |
| 79 | arguments: Option<AnyArguments<'q>>, |
| 80 | ) -> BoxStream<'q, sqlx_core::Result<Either<AnyQueryResult, AnyRow>>> { |
| 81 | let persistent = persistent && arguments.is_some(); |
| 82 | let arguments = match arguments.as_ref().map(AnyArguments::convert_to).transpose() { |
| 83 | Ok(arguments) => arguments, |
| 84 | Err(error) => { |
| 85 | return stream::once(future::ready(Err(sqlx_core::Error::Encode(error)))).boxed() |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | Box::pin( |
| 90 | self.run(query, arguments, persistent) |
| 91 | .try_flatten_stream() |
| 92 | .map(|res| { |
| 93 | Ok(match res? { |
| 94 | Either::Left(result) => Either::Left(map_result(result)), |
| 95 | Either::Right(row) => Either::Right(AnyRow::try_from(&row)?), |
| 96 | }) |
| 97 | }), |
| 98 | ) |
| 99 | } |
| 100 | |
| 101 | fn fetch_optional<'q>( |
| 102 | &'q mut self, |
| 103 | query: &'q str, |
| 104 | persistent: bool, |
| 105 | arguments: Option<AnyArguments<'q>>, |
| 106 | ) -> BoxFuture<'q, sqlx_core::Result<Option<AnyRow>>> { |
| 107 | let persistent = persistent && arguments.is_some(); |
| 108 | let arguments = arguments |
| 109 | .as_ref() |
| 110 | .map(AnyArguments::convert_to) |
| 111 | .transpose() |
| 112 | .map_err(sqlx_core::Error::Encode); |
| 113 | |
| 114 | Box::pin(async move { |
| 115 | let arguments = arguments?; |
| 116 | let stream = self.run(query, arguments, persistent).await?; |
| 117 | futures_util::pin_mut!(stream); |
| 118 | |
| 119 | while let Some(result) = stream.try_next().await? { |
| 120 | if let Either::Right(row) = result { |
| 121 | return Ok(Some(AnyRow::try_from(&row)?)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | Ok(None) |
| 126 | }) |
| 127 | } |
| 128 | |
| 129 | fn prepare_with<'c, 'q: 'c>( |
| 130 | &'c mut self, |
| 131 | sql: &'q str, |
| 132 | _parameters: &[AnyTypeInfo], |
| 133 | ) -> BoxFuture<'c, sqlx_core::Result<AnyStatement<'q>>> { |
| 134 | Box::pin(async move { |
| 135 | let statement = Executor::prepare_with(self, sql, &[]).await?; |
| 136 | AnyStatement::try_from_statement( |
| 137 | sql, |
| 138 | &statement, |
| 139 | statement.metadata.column_names.clone(), |
| 140 | ) |
| 141 | }) |
| 142 | } |
| 143 | |
| 144 | fn describe<'q>(&'q mut self, sql: &'q str) -> BoxFuture<'q, sqlx_core::Result<Describe<Any>>> { |
| 145 | Box::pin(async move { |
| 146 | let describe = Executor::describe(self, sql).await?; |
| 147 | describe.try_into_any() |
| 148 | }) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | impl<'a> TryFrom<&'a MySqlTypeInfo> for AnyTypeInfo { |
| 153 | type Error = sqlx_core::Error; |
| 154 | |
| 155 | fn try_from(type_info: &'a MySqlTypeInfo) -> Result<Self, Self::Error> { |
| 156 | Ok(AnyTypeInfo { |
| 157 | kind: match &type_info.r#type { |
| 158 | ColumnType::Null => AnyTypeInfoKind::Null, |
| 159 | ColumnType::Short => AnyTypeInfoKind::SmallInt, |
| 160 | ColumnType::Long => AnyTypeInfoKind::Integer, |
| 161 | ColumnType::LongLong => AnyTypeInfoKind::BigInt, |
| 162 | ColumnType::Float => AnyTypeInfoKind::Real, |
| 163 | ColumnType::Double => AnyTypeInfoKind::Double, |
| 164 | ColumnType::Blob |
| 165 | | ColumnType::TinyBlob |
| 166 | | ColumnType::MediumBlob |
| 167 | | ColumnType::LongBlob => AnyTypeInfoKind::Blob, |
| 168 | ColumnType::String | ColumnType::VarString | ColumnType::VarChar => { |
| 169 | AnyTypeInfoKind::Text |
| 170 | } |
| 171 | _ => { |
| 172 | return Err(sqlx_core::Error::AnyDriverError( |
| 173 | format!("Any driver does not support MySql type {type_info:?}").into(), |
| 174 | )) |
| 175 | } |
| 176 | }, |
| 177 | }) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | impl<'a> TryFrom<&'a MySqlColumn> for AnyColumn { |
| 182 | type Error = sqlx_core::Error; |
| 183 | |
| 184 | fn try_from(column: &'a MySqlColumn) -> Result<Self, Self::Error> { |
| 185 | let type_info = AnyTypeInfo::try_from(&column.type_info)?; |
| 186 | |
| 187 | Ok(AnyColumn { |
| 188 | ordinal: column.ordinal, |
| 189 | name: column.name.clone(), |
| 190 | type_info, |
| 191 | }) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | impl<'a> TryFrom<&'a MySqlRow> for AnyRow { |
| 196 | type Error = sqlx_core::Error; |
| 197 | |
| 198 | fn try_from(row: &'a MySqlRow) -> Result<Self, Self::Error> { |
| 199 | AnyRow::map_from(row, row.column_names.clone()) |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | impl<'a> TryFrom<&'a AnyConnectOptions> for MySqlConnectOptions { |
| 204 | type Error = sqlx_core::Error; |
| 205 | |
| 206 | fn try_from(any_opts: &'a AnyConnectOptions) -> Result<Self, Self::Error> { |
| 207 | let mut opts = Self::parse_from_url(&any_opts.database_url)?; |
| 208 | opts.log_settings = any_opts.log_settings.clone(); |
| 209 | Ok(opts) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | fn map_result(result: MySqlQueryResult) -> AnyQueryResult { |
| 214 | AnyQueryResult { |
| 215 | rows_affected: result.rows_affected, |
| 216 | // Don't expect this to be a problem |
| 217 | #[allow(clippy::cast_possible_wrap)] |
| 218 | last_insert_id: Some(result.last_insert_id as i64), |
| 219 | } |
| 220 | } |