feat(mysql): add initial Connection and Database types, hookup runtimes

This commit is contained in:
Ryan Leckey
2020-12-28 02:18:05 -08:00
parent 7a323f3471
commit 93bb9cceb0
23 changed files with 298 additions and 19 deletions

View File

@@ -1,11 +1,12 @@
use super::{ConnectOptions, Runtime};
use crate::DefaultRuntime;
/// A unique connection (session) with a specific database.
///
/// For detailed information, refer to the asynchronous version of
/// this: [`Connection`][crate::Connection].
///
pub trait Connection<Rt>: crate::Connection<Rt>
pub trait Connection<Rt = DefaultRuntime>: crate::Connection<Rt>
where
Rt: Runtime,
{

View File

@@ -1,20 +1,23 @@
use super::Runtime;
use super::{Connection, Runtime};
use crate::DefaultRuntime;
/// Options which can be used to configure how a SQL connection is opened.
///
/// For detailed information, refer to the asynchronous version of
/// this: [`ConnectOptions`][crate::ConnectOptions].
///
pub trait ConnectOptions<Rt>: crate::ConnectOptions<Rt>
pub trait ConnectOptions<Rt = DefaultRuntime>: crate::ConnectOptions<Rt>
where
Rt: Runtime,
{
type Connection: Connection<Rt> + ?Sized;
/// Establish a connection to the database.
///
/// For detailed information, refer to the asynchronous version of
/// this: [`connect()`][crate::ConnectOptions::connect].
///
fn connect(&self) -> crate::Result<Self::Connection>
fn connect(&self) -> crate::Result<<Self as ConnectOptions<Rt>>::Connection>
where
Self::Connection: Sized;
<Self as ConnectOptions<Rt>>::Connection: Sized;
}

View File

@@ -1,4 +1,4 @@
use crate::{ConnectOptions, Database, Runtime};
use crate::{ConnectOptions, Database, DefaultRuntime, Runtime};
#[cfg(feature = "async")]
use futures_util::future::BoxFuture;
@@ -11,7 +11,7 @@ use futures_util::future::BoxFuture;
/// SQL statements will be executed and results returned within the context
/// of this single SQL connection.
///
pub trait Connection<Rt>: 'static + Send
pub trait Connection<Rt = DefaultRuntime>: 'static + Send
where
Rt: Runtime,
{

View File

@@ -1,6 +1,6 @@
use std::fmt::Debug;
use crate::{Connection, Runtime};
use crate::{Connection, DefaultRuntime, Runtime};
/// A database driver.
///
@@ -8,7 +8,7 @@ use crate::{Connection, Runtime};
/// specific database (e.g., MySQL, PostgreSQL).
///
// 'x: execution
pub trait Database<Rt>: 'static + Sized + Debug + for<'x> HasOutput<'x>
pub trait Database<Rt = DefaultRuntime>: 'static + Sized + Debug + for<'x> HasOutput<'x>
where
Rt: Runtime,
{

View File

@@ -7,12 +7,14 @@ pub type Result<T> = std::result::Result<T, Error>;
#[non_exhaustive]
pub enum Error {
InvalidConnectionUrl(url::ParseError),
Network(std::io::Error),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidConnectionUrl(source) => write!(f, "invalid connection url: {}", source),
Self::Network(source) => write!(f, "network: {}", source),
}
}
}
@@ -21,6 +23,13 @@ impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::InvalidConnectionUrl(source) => Some(source),
Self::Network(source) => Some(source),
}
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::Network(error)
}
}

View File

@@ -37,7 +37,7 @@ mod runtime;
pub mod blocking;
pub use connection::Connection;
pub use database::Database;
pub use database::{Database, HasOutput};
pub use error::{Error, Result};
pub use options::ConnectOptions;
pub use runtime::Runtime;
@@ -52,7 +52,7 @@ pub use runtime::Tokio;
pub use runtime::Actix;
#[cfg(feature = "async")]
pub(crate) use runtime::Async;
pub use runtime::Async;
#[cfg(feature = "async-std")]
pub type DefaultRuntime = AsyncStd;

View File

@@ -1,10 +1,10 @@
use std::fmt::Debug;
use std::str::FromStr;
use crate::{Connection, Runtime};
use crate::{Connection, DefaultRuntime, Runtime};
/// Options which can be used to configure how a SQL connection is opened.
pub trait ConnectOptions<Rt>:
pub trait ConnectOptions<Rt = DefaultRuntime>:
'static + Send + Sync + Default + Debug + Clone + FromStr<Err = crate::Error>
where
Rt: Runtime,

View File

@@ -18,8 +18,8 @@ pub use self::actix::Actix;
/// Describes a set of types and functions used to open and manage
/// resources within SQLx.
pub trait Runtime {
type TcpStream;
pub trait Runtime: 'static + Send + Sync {
type TcpStream: Send;
/// Opens a TCP connection to a remote host at the specified port.
#[cfg(feature = "async")]