From 8203410e3f9fda5e22dad74f480f8d9495fe8463 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 12 Feb 2021 01:06:53 -0800 Subject: [PATCH] reactor(core): remove Rt param from Database trait and remove Connection associated type - significantly cleans up the sqlx/ zero-prelude wrapping --- sqlx-core/src/acquire.rs | 28 ++++++-------- sqlx-core/src/blocking.rs | 4 +- sqlx-core/src/blocking/acquire.rs | 12 +++--- sqlx-core/src/blocking/executor.rs | 8 ++-- sqlx-core/src/connection.rs | 2 +- sqlx-core/src/database.rs | 16 +++----- sqlx-core/src/decode.rs | 9 ++--- sqlx-core/src/encode.rs | 4 +- sqlx-core/src/error.rs | 14 ++++++- sqlx-core/src/executor.rs | 10 ++--- sqlx-core/src/lib.rs | 4 +- sqlx-core/src/pool.rs | 17 --------- sqlx-core/src/row.rs | 19 ++++++++-- .../src/connection/executor/fetch_all.rs | 2 +- .../src/connection/executor/fetch_optional.rs | 2 +- sqlx-mysql/src/database.rs | 13 +++---- sqlx-mysql/src/protocol.rs | 12 +++--- sqlx-mysql/src/protocol/info.rs | 4 +- sqlx-mysql/src/row.rs | 38 +++++++++++++++---- sqlx-mysql/src/type_id.rs | 4 +- sqlx-mysql/src/type_info.rs | 1 - sqlx-mysql/src/types/bool.rs | 10 ++--- sqlx-mysql/src/types/uint.rs | 8 ++-- sqlx-mysql/src/value.rs | 8 +++- sqlx/src/lib.rs | 2 +- sqlx/src/mysql.rs | 6 +-- sqlx/src/mysql/connection.rs | 3 +- sqlx/src/mysql/database.rs | 19 ---------- 28 files changed, 137 insertions(+), 142 deletions(-) delete mode 100644 sqlx-core/src/pool.rs delete mode 100644 sqlx/src/mysql/database.rs diff --git a/sqlx-core/src/acquire.rs b/sqlx-core/src/acquire.rs index 384b3006..5ae760cf 100644 --- a/sqlx-core/src/acquire.rs +++ b/sqlx-core/src/acquire.rs @@ -1,11 +1,11 @@ #[cfg(feature = "async")] use futures_util::future::BoxFuture; -use crate::{Database, Runtime}; +use crate::{Connection, Database, Runtime}; #[allow(clippy::type_complexity)] pub trait Acquire { - type Database: Database; + type Connection: Connection; /// Get a connection from the pool, make a new connection, or wait for one to become /// available. @@ -13,33 +13,27 @@ pub trait Acquire { /// Takes exclusive use of the connection until it is released. /// #[cfg(feature = "async")] - fn acquire( - self, - ) -> BoxFuture<'static, crate::Result<>::Connection>> + fn acquire(self) -> BoxFuture<'static, crate::Result> where - >::Connection: Sized; + Self::Connection: Sized; /// Get a connection from the pool, if available. /// /// Returns `None` immediately if there are no connections available. - /// - fn try_acquire(self) -> Option<>::Connection> + /// + fn try_acquire(self) -> Option where - >::Connection: Sized; + Self::Connection: Sized; #[cfg(feature = "async")] - fn begin( - self, - ) -> BoxFuture<'static, crate::Result<>::Connection>> + fn begin(self) -> BoxFuture<'static, crate::Result> where - >::Connection: Sized; + Self::Connection: Sized; #[cfg(feature = "async")] - fn try_begin( - self, - ) -> BoxFuture<'static, crate::Result>::Connection>>> + fn try_begin(self) -> BoxFuture<'static, crate::Result>> where - >::Connection: Sized; + Self::Connection: Sized; } // TODO: impl Acquire for &Pool { ... } diff --git a/sqlx-core/src/blocking.rs b/sqlx-core/src/blocking.rs index 20d8e509..46de8e2a 100644 --- a/sqlx-core/src/blocking.rs +++ b/sqlx-core/src/blocking.rs @@ -6,14 +6,14 @@ mod acquire; mod close; mod connect; mod connection; -mod options; mod executor; +mod options; pub(crate) mod runtime; -pub use executor::Executor; pub use acquire::Acquire; pub use close::Close; pub use connect::Connect; pub use connection::Connection; +pub use executor::Executor; pub use options::ConnectOptions; pub use runtime::Runtime; diff --git a/sqlx-core/src/blocking/acquire.rs b/sqlx-core/src/blocking/acquire.rs index 39d8e1cd..22b5599a 100644 --- a/sqlx-core/src/blocking/acquire.rs +++ b/sqlx-core/src/blocking/acquire.rs @@ -13,17 +13,17 @@ where /// For detailed information, refer to the async version of /// this: [`acquire()`][crate::Acquire::acquire]. /// - fn acquire(self) -> crate::Result<>::Connection> + fn acquire(self) -> crate::Result where - >::Connection: Sized; + Self::Connection: Sized; - fn begin(self) -> crate::Result<>::Connection> + fn begin(self) -> crate::Result where - >::Connection: Sized; + Self::Connection: Sized; - fn try_begin(self) -> crate::Result>::Connection>> + fn try_begin(self) -> crate::Result> where - >::Connection: Sized; + Self::Connection: Sized; } // TODO: impl Acquire for &Pool { ... } diff --git a/sqlx-core/src/blocking/executor.rs b/sqlx-core/src/blocking/executor.rs index 651254a1..823ab795 100644 --- a/sqlx-core/src/blocking/executor.rs +++ b/sqlx-core/src/blocking/executor.rs @@ -3,12 +3,12 @@ use crate::Database; pub trait Executor: crate::Executor where - Self::Database: Database, + Self::Database: Database, { fn execute<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> crate::Result<>::QueryResult> + ) -> crate::Result<::QueryResult> where 'e: 'x, 'q: 'x; @@ -16,7 +16,7 @@ where fn fetch_all<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> crate::Result>::Row>> + ) -> crate::Result::Row>> where 'e: 'x, 'q: 'x; @@ -24,7 +24,7 @@ where fn fetch_optional<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> crate::Result>::Row>> + ) -> crate::Result::Row>> where 'e: 'x, 'q: 'x; diff --git a/sqlx-core/src/connection.rs b/sqlx-core/src/connection.rs index 5fb7191b..64575271 100644 --- a/sqlx-core/src/connection.rs +++ b/sqlx-core/src/connection.rs @@ -16,7 +16,7 @@ pub trait Connection: 'static + Send + Connect + Close where Rt: Runtime, { - type Database: Database; + type Database: Database; /// Checks if a connection to the database is still valid. /// diff --git a/sqlx-core/src/database.rs b/sqlx-core/src/database.rs index 559d5d1e..4abdeed6 100644 --- a/sqlx-core/src/database.rs +++ b/sqlx-core/src/database.rs @@ -1,25 +1,21 @@ use std::fmt::Debug; -use crate::{Column, Connection, QueryResult, Row, Runtime}; +use crate::{Column, QueryResult, Row}; /// A database driver. /// -/// This trait encapsulates a complete set of traits that implement a driver for a -/// specific database (e.g., MySQL, PostgreSQL). +/// Represents a family of traits for interacting with a database. This is +/// separate from [`Connection`][crate::Connection]. One database driver may +/// have multiple concrete `Connection` implementations. /// -pub trait Database: +pub trait Database: 'static + Sized + Debug + for<'x> HasOutput<'x> + for<'r> HasRawValue<'r> -where - Rt: Runtime, { - /// The concrete [`Connection`] implementation for this database. - type Connection: Connection + ?Sized; - /// The concrete [`Column`] implementation for this database. type Column: Column; /// The concrete [`Row`] implementation for this database. - type Row: Row; + type Row: Row; /// The concrete [`QueryResult`] implementation for this database. type QueryResult: QueryResult; diff --git a/sqlx-core/src/decode.rs b/sqlx-core/src/decode.rs index 0f934ae0..25d03d17 100644 --- a/sqlx-core/src/decode.rs +++ b/sqlx-core/src/decode.rs @@ -6,18 +6,15 @@ use crate::database::HasRawValue; use crate::{Database, Runtime}; /// A type that can be decoded from a SQL value. -pub trait Decode<'r, Db: Database, Rt: Runtime>: Sized + Send + Sync { +pub trait Decode<'r, Db: Database>: Sized + Send + Sync { fn decode(value: >::RawValue) -> Result; } /// A type that can be decoded from a SQL value, without borrowing any data /// from the row. -pub trait DecodeOwned, Rt: Runtime>: for<'de> Decode<'de, Db, Rt> {} +pub trait DecodeOwned: for<'r> Decode<'r, Db> {} -impl, Rt: Runtime> DecodeOwned for T where - T: for<'de> Decode<'de, Db, Rt> -{ -} +impl DecodeOwned for T where T: for<'r> Decode<'r, Db> {} /// Errors which can occur while decoding a SQL value. #[derive(Debug)] diff --git a/sqlx-core/src/encode.rs b/sqlx-core/src/encode.rs index 274b4eae..fbc870e9 100644 --- a/sqlx-core/src/encode.rs +++ b/sqlx-core/src/encode.rs @@ -5,7 +5,7 @@ use crate::database::{HasOutput, HasRawValue}; use crate::{Database, Runtime}; /// A type that can be encoded into a SQL value. -pub trait Encode, Rt: Runtime>: Send + Sync { +pub trait Encode: Send + Sync { /// Encode this value into a SQL value. fn encode(&self, ty: &Db::TypeInfo, out: &mut >::Output) -> Result<()>; @@ -16,7 +16,7 @@ pub trait Encode, Rt: Runtime>: Send + Sync { } } -impl, Db: Database, Rt: Runtime> Encode for &T { +impl, Db: Database> Encode for &T { #[inline] fn encode(&self, ty: &Db::TypeInfo, out: &mut >::Output) -> Result<()> { (*self).encode(ty, out) diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index a399bf76..b445c8c5 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -33,6 +33,11 @@ pub enum Error { Decode(DecodeError), Encode(EncodeError), + + ColumnIndexOutOfBounds { + index: usize, + len: usize, + }, } impl Error { @@ -84,6 +89,14 @@ impl Display for Error { Self::Encode(error) => { write!(f, "{}", error) } + + Self::ColumnIndexOutOfBounds { index, len } => { + write!( + f, + "column index out of bounds: the len is {}, but the index is {}", + len, index + ) + } } } } @@ -92,7 +105,6 @@ impl StdError for Error { fn source(&self) -> Option<&(dyn StdError + 'static)> { match self { Self::Configuration { source: Some(source), .. } => Some(&**source), - Self::Network(source) => Some(source), _ => None, diff --git a/sqlx-core/src/executor.rs b/sqlx-core/src/executor.rs index deb0527b..719f8e68 100644 --- a/sqlx-core/src/executor.rs +++ b/sqlx-core/src/executor.rs @@ -13,7 +13,7 @@ use crate::{Database, Error, Result, Runtime}; /// #[allow(clippy::type_complexity)] pub trait Executor { - type Database: Database; + type Database: Database; /// Execute the SQL query and return information about the result, including /// the number of rows affected, if any. @@ -21,7 +21,7 @@ pub trait Executor { fn execute<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> BoxFuture<'x, Result<>::QueryResult>> + ) -> BoxFuture<'x, Result<::QueryResult>> where Rt: crate::Async, 'e: 'x, @@ -31,7 +31,7 @@ pub trait Executor { fn fetch_all<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> BoxFuture<'x, Result>::Row>>> + ) -> BoxFuture<'x, Result::Row>>> where Rt: crate::Async, 'e: 'x, @@ -41,7 +41,7 @@ pub trait Executor { fn fetch_optional<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> BoxFuture<'x, Result>::Row>>> + ) -> BoxFuture<'x, Result::Row>>> where Rt: crate::Async, 'e: 'x, @@ -51,7 +51,7 @@ pub trait Executor { fn fetch_one<'x, 'e, 'q>( &'e mut self, sql: &'q str, - ) -> BoxFuture<'x, Result<>::Row>> + ) -> BoxFuture<'x, Result<::Row>> where Rt: crate::Async, 'e: 'x, diff --git a/sqlx-core/src/lib.rs b/sqlx-core/src/lib.rs index 58733c00..8bd02687 100644 --- a/sqlx-core/src/lib.rs +++ b/sqlx-core/src/lib.rs @@ -29,9 +29,8 @@ pub mod encode; mod error; mod executor; mod options; -mod pool; mod query_result; -mod row; +pub mod row; mod runtime; #[doc(hidden)] @@ -60,7 +59,6 @@ pub use encode::Encode; pub use error::{DatabaseError, Error, Result}; pub use executor::Executor; pub use options::ConnectOptions; -pub use pool::Pool; pub use query_result::QueryResult; pub use row::Row; #[cfg(feature = "actix")] diff --git a/sqlx-core/src/pool.rs b/sqlx-core/src/pool.rs deleted file mode 100644 index 4e62a165..00000000 --- a/sqlx-core/src/pool.rs +++ /dev/null @@ -1,17 +0,0 @@ -use std::marker::PhantomData; - -use crate::{Database, Runtime}; - -/// A connection pool to enable the efficient reuse of a managed pool of SQL connections. -pub struct Pool -where - Rt: Runtime, - Db: Database, -{ - runtime: PhantomData, - database: PhantomData, -} - -// TODO: impl Acquire for &Pool -// TODO: impl Connect for Pool -// TODO: impl Close for Pool diff --git a/sqlx-core/src/row.rs b/sqlx-core/src/row.rs index 5631bedb..52f2ca31 100644 --- a/sqlx-core/src/row.rs +++ b/sqlx-core/src/row.rs @@ -1,7 +1,8 @@ -use crate::{Column, Database, Runtime}; +use crate::database::HasRawValue; +use crate::{Column, Database, Decode, Runtime}; pub trait Row: 'static + Send + Sync { - type Column: Column; + type Database: Database; /// Returns `true` if the row contains only `NULL` values. fn is_null(&self) -> bool; @@ -15,7 +16,7 @@ pub trait Row: 'static + Send + Sync { } /// Returns a reference to the columns in the row. - fn columns(&self) -> &[Self::Column]; + fn columns(&self) -> &[::Column]; /// Returns the column name, given the ordinal (also known as index) of the column. fn column_name_of(&self, ordinal: usize) -> &str; @@ -29,7 +30,17 @@ pub trait Row: 'static + Send + Sync { /// Returns the column ordinal, given the name of the column. fn try_ordinal_of(&self, name: &str) -> crate::Result; - fn try_get_raw(&self) -> crate::Result<&[u8]>; + /// Returns the decoded value at the index. + fn try_get<'r, T>(&'r self, index: usize) -> crate::Result + where + T: Decode<'r, Self::Database>; + + /// Returns the raw representation of the value at the index. + // noinspection RsNeedlessLifetimes + fn try_get_raw<'r>( + &'r self, + index: usize, + ) -> crate::Result<>::RawValue>; } // TODO: fn type_info_of(index) diff --git a/sqlx-mysql/src/connection/executor/fetch_all.rs b/sqlx-mysql/src/connection/executor/fetch_all.rs index 7763db39..8a9bf3e5 100644 --- a/sqlx-mysql/src/connection/executor/fetch_all.rs +++ b/sqlx-mysql/src/connection/executor/fetch_all.rs @@ -31,7 +31,7 @@ macro_rules! impl_fetch_all { // execute ignores any rows returned // but we do increment affected rows QueryStep::End(res) => break 'result res.into_result()?, - QueryStep::Row(row) => rows.push(MySqlRow(row.deserialize_with(&columns[..])?)), + QueryStep::Row(row) => rows.push(MySqlRow::new(row.deserialize_with(&columns[..])?)), } } } diff --git a/sqlx-mysql/src/connection/executor/fetch_optional.rs b/sqlx-mysql/src/connection/executor/fetch_optional.rs index 510a4cdd..15dbbb44 100644 --- a/sqlx-mysql/src/connection/executor/fetch_optional.rs +++ b/sqlx-mysql/src/connection/executor/fetch_optional.rs @@ -33,7 +33,7 @@ macro_rules! impl_fetch_optional { // but we do increment affected rows QueryStep::End(res) => break 'result res.into_result()?, QueryStep::Row(row) => { - first_row = Some(MySqlRow(row.deserialize_with(&columns[..])?)); + first_row = Some(MySqlRow::new(row.deserialize_with(&columns[..])?)); // get out as soon as possible after finding our one row break 'results; diff --git a/sqlx-mysql/src/database.rs b/sqlx-mysql/src/database.rs index 8b6f3ad8..d6af045f 100644 --- a/sqlx-mysql/src/database.rs +++ b/sqlx-mysql/src/database.rs @@ -1,26 +1,23 @@ use sqlx_core::database::{HasOutput, HasRawValue}; -use sqlx_core::{Database, Runtime}; +use sqlx_core::Database; use super::{ - MySqlColumn, MySqlConnection, MySqlOutput, MySqlQueryResult, MySqlRawValue, MySqlRow, - MySqlTypeId, MySqlTypeInfo, + MySqlColumn, MySqlOutput, MySqlQueryResult, MySqlRawValue, MySqlRow, MySqlTypeId, MySqlTypeInfo, }; #[derive(Debug)] pub struct MySql; -impl Database for MySql { - type Connection = MySqlConnection; - +impl Database for MySql { type Column = MySqlColumn; type Row = MySqlRow; type QueryResult = MySqlQueryResult; - type TypeId = MySqlTypeId; - type TypeInfo = MySqlTypeInfo; + + type TypeId = MySqlTypeId; } impl<'x> HasOutput<'x> for MySql { diff --git a/sqlx-mysql/src/protocol.rs b/sqlx-mysql/src/protocol.rs index fd3c9c44..9de432a1 100644 --- a/sqlx-mysql/src/protocol.rs +++ b/sqlx-mysql/src/protocol.rs @@ -8,20 +8,18 @@ mod eof; mod err; mod handshake; mod handshake_response; +mod info; mod ok; +mod packet; mod ping; mod query; mod query_response; -mod info; -mod result; mod query_step; -mod packet; mod quit; +mod result; mod row; mod status; -pub(crate) use info::Info; -pub(crate) use packet::Packet; pub(crate) use auth_plugin::AuthPlugin; pub(crate) use auth_response::AuthResponse; pub(crate) use auth_switch::AuthSwitch; @@ -30,14 +28,16 @@ pub(crate) use column_def::ColumnDefinition; pub(crate) use command::{Command, MaybeCommand}; pub(crate) use eof::EofPacket; pub(crate) use err::ErrPacket; -pub(crate) use result::ResultPacket; pub(crate) use handshake::Handshake; pub(crate) use handshake_response::HandshakeResponse; +pub(crate) use info::Info; pub(crate) use ok::OkPacket; +pub(crate) use packet::Packet; pub(crate) use ping::Ping; pub(crate) use query::Query; pub(crate) use query_response::QueryResponse; pub(crate) use query_step::QueryStep; pub(crate) use quit::Quit; +pub(crate) use result::ResultPacket; pub(crate) use row::Row; pub(crate) use status::Status; diff --git a/sqlx-mysql/src/protocol/info.rs b/sqlx-mysql/src/protocol/info.rs index d88871a1..28d2f350 100644 --- a/sqlx-mysql/src/protocol/info.rs +++ b/sqlx-mysql/src/protocol/info.rs @@ -35,11 +35,11 @@ impl Info { // ignore records changed // this is "rows affected" for UPDATE - "Changed" => {}, + "Changed" => {} // ignore warnings in info // these are passed back differently - "Warnings" => {}, + "Warnings" => {} // unknown key _ => failed = true, diff --git a/sqlx-mysql/src/row.rs b/sqlx-mysql/src/row.rs index 5180bf5b..92bf6193 100644 --- a/sqlx-mysql/src/row.rs +++ b/sqlx-mysql/src/row.rs @@ -1,14 +1,23 @@ -use sqlx_core::Row; +use std::marker::PhantomData; -use crate::{protocol, MySqlColumn}; +use bytes::Bytes; +use sqlx_core::{Decode, Error, Row, Runtime}; + +use crate::{protocol, MySql, MySqlColumn, MySqlRawValue, MySqlRawValueFormat}; #[allow(clippy::module_name_repetitions)] -pub struct MySqlRow(pub(crate) protocol::Row); +pub struct MySqlRow { + values: Vec>, +} impl MySqlRow { + pub(crate) fn new(row: protocol::Row) -> Self { + Self { values: row.values } + } + #[must_use] pub fn len(&self) -> usize { - self.0.values.len() + self.values.len() } #[must_use] @@ -18,7 +27,7 @@ impl MySqlRow { } impl Row for MySqlRow { - type Column = MySqlColumn; + type Database = MySql; fn is_null(&self) -> bool { todo!() @@ -48,7 +57,22 @@ impl Row for MySqlRow { todo!() } - fn try_get_raw(&self) -> sqlx_core::Result<&[u8]> { - todo!() + fn try_get<'r, T>(&'r self, index: usize) -> sqlx_core::Result + where + T: Decode<'r, Self::Database>, + { + Ok(self.try_get_raw(index)?.decode()?) + } + + // noinspection RsNeedlessLifetimes + fn try_get_raw<'r>(&'r self, index: usize) -> sqlx_core::Result> { + let format = MySqlRawValueFormat::Text; + + let value = self + .values + .get(index) + .ok_or_else(|| Error::ColumnIndexOutOfBounds { len: self.len(), index })?; + + Ok(MySqlRawValue::new(value, format)) } } diff --git a/sqlx-mysql/src/type_id.rs b/sqlx-mysql/src/type_id.rs index c4241777..fe68bca6 100644 --- a/sqlx-mysql/src/type_id.rs +++ b/sqlx-mysql/src/type_id.rs @@ -16,13 +16,13 @@ impl MySqlTypeId { /// directly used in an expression by itself, such as `SELECT NULL`. /// pub const fn is_null(&self) -> bool { - matches!(self, MySqlTypeId::NULL) + matches!(*self, MySqlTypeId::NULL) } /// Returns `true` if this is an integer data type. pub const fn is_integer(&self) -> bool { matches!( - self, + *self, MySqlTypeId::TINYINT | MySqlTypeId::TINYINT_UNSIGNED | MySqlTypeId::SMALLINT diff --git a/sqlx-mysql/src/type_info.rs b/sqlx-mysql/src/type_info.rs index fbe5fbf9..5906b7a4 100644 --- a/sqlx-mysql/src/type_info.rs +++ b/sqlx-mysql/src/type_info.rs @@ -35,4 +35,3 @@ impl MySqlTypeInfo { self.id().name() } } -o diff --git a/sqlx-mysql/src/types/bool.rs b/sqlx-mysql/src/types/bool.rs index 322c989b..c2d88539 100644 --- a/sqlx-mysql/src/types/bool.rs +++ b/sqlx-mysql/src/types/bool.rs @@ -1,6 +1,6 @@ use bytes::BufMut; use sqlx_core::{decode, encode}; -use sqlx_core::{Decode, Encode, Runtime}; +use sqlx_core::{Decode, Encode}; use crate::{MySql, MySqlOutput, MySqlRawValue, MySqlTypeId, MySqlTypeInfo}; @@ -10,14 +10,14 @@ use crate::{MySql, MySqlOutput, MySqlRawValue, MySqlTypeId, MySqlTypeInfo}; // TODO: accepts(ty) -> ty.is_integer() // TODO: compatible(ty) -> ty.is_integer() -impl Encode for bool { +impl Encode for bool { fn encode(&self, ty: &MySqlTypeInfo, out: &mut MySqlOutput<'_>) -> encode::Result<()> { - >::encode(&(*self as u8), ty, out) + >::encode(&(*self as u8), ty, out) } } -impl<'r, Rt: Runtime> Decode<'r, MySql, Rt> for bool { +impl<'r> Decode<'r, MySql> for bool { fn decode(raw: MySqlRawValue<'r>) -> decode::Result { - Ok(raw.decode::()? != 0) + Ok(raw.decode::()? != 0) } } diff --git a/sqlx-mysql/src/types/uint.rs b/sqlx-mysql/src/types/uint.rs index 5aac2697..eeeaf6c0 100644 --- a/sqlx-mysql/src/types/uint.rs +++ b/sqlx-mysql/src/types/uint.rs @@ -1,7 +1,7 @@ use bytes::Buf; use sqlx_core::database::HasOutput; use sqlx_core::{decode, encode}; -use sqlx_core::{Database, Decode, Encode, Runtime}; +use sqlx_core::{Database, Decode, Encode}; use crate::type_info::MySqlTypeInfo; use crate::MySqlRawValueFormat::*; @@ -12,7 +12,7 @@ use crate::{MySql, MySqlOutput, MySqlRawValue, MySqlTypeId}; // TODO: accepts(ty) -> ty.is_integer() // TODO: compatible(ty) -> ty.is_integer() -impl Encode for u8 { +impl Encode for u8 { fn encode(&self, _: &MySqlTypeInfo, out: &mut MySqlOutput<'_>) -> encode::Result<()> { out.buffer().push(*self); @@ -20,8 +20,10 @@ impl Encode for u8 { } } -impl<'r, Rt: Runtime> Decode<'r, MySql, Rt> for u8 { +impl<'r> Decode<'r, MySql> for u8 { fn decode(value: MySqlRawValue<'r>) -> decode::Result { + // FIXME: ensure that the SQL value fits within u8 + Ok(match value.format() { Binary => value.as_bytes()?.get_u8(), Text => value.as_str()?.parse()?, diff --git a/sqlx-mysql/src/value.rs b/sqlx-mysql/src/value.rs index d7e5951e..61f1f0d0 100644 --- a/sqlx-mysql/src/value.rs +++ b/sqlx-mysql/src/value.rs @@ -26,6 +26,10 @@ pub struct MySqlRawValue<'r> { // 'r: row impl<'r> MySqlRawValue<'r> { + pub(crate) fn new(value: &'r Option, format: MySqlRawValueFormat) -> Self { + Self { value: value.as_ref(), format } + } + /// Returns the format of this value. pub const fn format(&self) -> MySqlRawValueFormat { self.format @@ -43,7 +47,7 @@ impl<'r> MySqlRawValue<'r> { } /// Decode this value into the target type. - pub fn decode, Rt: Runtime>(self) -> DecodeResult { - >::decode(self) + pub fn decode>(self) -> DecodeResult { + >::decode(self) } } diff --git a/sqlx/src/lib.rs b/sqlx/src/lib.rs index b6ff782e..7507f6df 100644 --- a/sqlx/src/lib.rs +++ b/sqlx/src/lib.rs @@ -66,5 +66,5 @@ pub use sqlx_core::AsyncStd; #[cfg(feature = "tokio")] pub use sqlx_core::Tokio; pub use sqlx_core::{ - Acquire, Close, Connect, ConnectOptions, Connection, Database, Error, Result, Runtime, + Acquire, Close, Connect, ConnectOptions, Connection, Database, Error, Result, Row, Runtime, }; diff --git a/sqlx/src/mysql.rs b/sqlx/src/mysql.rs index c81bee99..cc69b0d5 100644 --- a/sqlx/src/mysql.rs +++ b/sqlx/src/mysql.rs @@ -4,7 +4,6 @@ //! mod connection; -mod database; mod options; #[cfg(feature = "blocking")] @@ -15,11 +14,10 @@ mod blocking; // this is to provide runtime-specialized inherent methods by taking advantage // of through crate-local negative reasoning pub use connection::MySqlConnection; -pub use database::MySql; pub use options::MySqlConnectOptions; // // re-export the remaining types from the driver pub use sqlx_mysql::{ - MySqlColumn, MySqlDatabaseError, MySqlQueryResult, MySqlRawValue, MySqlRawValueFormat, - MySqlRow, MySqlTypeId, + types, MySql, MySqlColumn, MySqlDatabaseError, MySqlQueryResult, MySqlRawValue, + MySqlRawValueFormat, MySqlRow, MySqlTypeId, }; diff --git a/sqlx/src/mysql/connection.rs b/sqlx/src/mysql/connection.rs index d140b125..8a3f6bd3 100644 --- a/sqlx/src/mysql/connection.rs +++ b/sqlx/src/mysql/connection.rs @@ -3,9 +3,8 @@ use std::fmt::{self, Debug, Formatter}; #[cfg(feature = "async")] use futures_util::future::{BoxFuture, FutureExt}; use sqlx_core::Executor; -use sqlx_mysql::{MySqlQueryResult, MySqlRow}; -use super::{MySql, MySqlConnectOptions}; +use super::{MySql, MySqlConnectOptions, MySqlQueryResult, MySqlRow}; #[cfg(feature = "async")] use crate::{Async, Result}; use crate::{Close, Connect, Connection, DefaultRuntime, Runtime}; diff --git a/sqlx/src/mysql/database.rs b/sqlx/src/mysql/database.rs deleted file mode 100644 index 3391bca8..00000000 --- a/sqlx/src/mysql/database.rs +++ /dev/null @@ -1,19 +0,0 @@ -use sqlx_core::HasOutput; -use sqlx_mysql::{MySqlColumn, MySqlQueryResult, MySqlRow}; - -use super::MySqlConnection; -use crate::{Database, Runtime}; - -#[derive(Debug)] -pub struct MySql; - -impl Database for MySql { - type Connection = MySqlConnection; - type Column = MySqlColumn; - type Row = MySqlRow; - type QueryResult = MySqlQueryResult; -} - -impl<'x> HasOutput<'x> for MySql { - type Output = &'x mut Vec; -}