mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-01 06:52:19 +00:00
De-duplicate error impls and forward MySqlConnection/PgConnection to crate root
This commit is contained in:
parent
7d745f98ea
commit
017ee38725
@ -147,15 +147,25 @@ pub trait DatabaseError: Display + Debug + Send + Sync {
|
||||
/// The primary, human-readable error message.
|
||||
fn message(&self) -> &str;
|
||||
|
||||
fn details(&self) -> Option<&str>;
|
||||
fn details(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn hint(&self) -> Option<&str>;
|
||||
fn hint(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn table_name(&self) -> Option<&str>;
|
||||
fn table_name(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn column_name(&self) -> Option<&str>;
|
||||
fn column_name(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn constraint_name(&self) -> Option<&str>;
|
||||
fn constraint_name(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Used by the `protocol_error!()` macro for a lazily evaluated conversion to
|
||||
@ -170,3 +180,26 @@ macro_rules! protocol_err (
|
||||
$crate::error::ProtocolError { args: format_args!($($args)*) }
|
||||
}
|
||||
);
|
||||
|
||||
macro_rules! impl_fmt_error {
|
||||
($err:ty) => {
|
||||
impl std::fmt::Debug for $err {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.debug_struct("DatabaseError")
|
||||
.field("message", &self.message())
|
||||
.field("details", &self.details())
|
||||
.field("hint", &self.hint())
|
||||
.field("table_name", &self.table_name())
|
||||
.field("column_name", &self.column_name())
|
||||
.field("constraint_name", &self.constraint_name())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for $err {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.pad(self.message())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
use crate::error::DatabaseError;
|
||||
use crate::mysql::protocol::ErrPacket;
|
||||
|
||||
@ -9,45 +7,6 @@ impl DatabaseError for MySqlError {
|
||||
fn message(&self) -> &str {
|
||||
&*self.0.error_message
|
||||
}
|
||||
|
||||
fn details(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn hint(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn table_name(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn column_name(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
|
||||
fn constraint_name(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: De-duplicate these two impls with Postgres (macro?)
|
||||
|
||||
impl Debug for MySqlError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_struct("DatabaseError")
|
||||
.field("message", &self.message())
|
||||
.field("details", &self.details())
|
||||
.field("hint", &self.hint())
|
||||
.field("table_name", &self.table_name())
|
||||
.field("column_name", &self.column_name())
|
||||
.field("constraint_name", &self.constraint_name())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for MySqlError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.pad(self.message())
|
||||
}
|
||||
}
|
||||
impl_fmt_error!(MySqlError);
|
||||
|
@ -1,14 +1,5 @@
|
||||
//! **MySQL** database and connection types.
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
pub use arguments::MySqlArguments;
|
||||
pub use connection::MySqlConnection;
|
||||
pub use database::MySql;
|
||||
pub use row::MySqlRow;
|
||||
|
||||
use crate::url::Url;
|
||||
|
||||
mod arguments;
|
||||
mod connection;
|
||||
mod database;
|
||||
@ -19,6 +10,23 @@ mod protocol;
|
||||
mod row;
|
||||
mod types;
|
||||
|
||||
pub use database::MySql;
|
||||
|
||||
pub use arguments::MySqlArguments;
|
||||
|
||||
pub use connection::MySqlConnection;
|
||||
|
||||
pub use error::MySqlError;
|
||||
|
||||
pub use row::MySqlRow;
|
||||
|
||||
/// An alias for [`Pool`], specialized for **MySQL**.
|
||||
pub type MySqlPool = super::Pool<MySql>;
|
||||
|
||||
use std::convert::TryInto;
|
||||
|
||||
use crate::url::Url;
|
||||
|
||||
// used in tests and hidden code in examples
|
||||
#[doc(hidden)]
|
||||
pub async fn connect<T>(url: T) -> crate::Result<MySqlConnection>
|
||||
|
@ -1,9 +1,9 @@
|
||||
use crate::error::DatabaseError;
|
||||
use crate::postgres::protocol::Response;
|
||||
use std::fmt::{self, Debug, Display};
|
||||
|
||||
pub struct PgError(pub(super) Box<Response>);
|
||||
|
||||
impl crate::error::DatabaseError for PgError {
|
||||
impl DatabaseError for PgError {
|
||||
fn message(&self) -> &str {
|
||||
&self.0.message
|
||||
}
|
||||
@ -29,25 +29,4 @@ impl crate::error::DatabaseError for PgError {
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for PgError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use crate::error::DatabaseError;
|
||||
|
||||
f.debug_struct("DatabaseError")
|
||||
.field("message", &self.message())
|
||||
.field("details", &self.details())
|
||||
.field("hint", &self.hint())
|
||||
.field("table_name", &self.table_name())
|
||||
.field("column_name", &self.column_name())
|
||||
.field("constraint_name", &self.constraint_name())
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for PgError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use crate::error::DatabaseError;
|
||||
|
||||
f.pad(self.message())
|
||||
}
|
||||
}
|
||||
impl_fmt_error!(PgError);
|
||||
|
@ -13,10 +13,10 @@ pub use sqlx_core::{query, query_as};
|
||||
pub use sqlx_core::query_as_mapped;
|
||||
|
||||
#[cfg(feature = "mysql")]
|
||||
pub use sqlx_core::mysql::{self, MySql};
|
||||
pub use sqlx_core::mysql::{self, MySql, MySqlConnection, MySqlPool};
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
pub use sqlx_core::postgres::{self, Postgres};
|
||||
pub use sqlx_core::postgres::{self, Postgres, PgConnection, PgPool};
|
||||
|
||||
#[cfg(feature = "macros")]
|
||||
#[doc(hidden)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user