From f9a10fbdd9e39af10e742bc97ce06344155e3728 Mon Sep 17 00:00:00 2001 From: Evan Weiler Date: Sun, 4 Nov 2018 16:18:17 -0600 Subject: [PATCH] WIP moving mysql types.rs file to shared area --- diesel/src/lib.rs | 6 +- diesel/src/mysql/backend.rs | 21 +--- diesel/src/mysql/mod.rs | 4 +- diesel/src/mysql/types/mod.rs | 104 ----------------- diesel/src/mysql_like/backend.rs | 25 +++++ diesel/src/mysql_like/mod.rs | 5 + diesel/src/mysql_like/types/mod.rs | 105 ++++++++++++++++++ .../{mysql => mysql_like}/types/numeric.rs | 14 +-- diesel/src/sql_types/mod.rs | 3 + 9 files changed, 154 insertions(+), 133 deletions(-) create mode 100644 diesel/src/mysql_like/backend.rs create mode 100644 diesel/src/mysql_like/mod.rs create mode 100644 diesel/src/mysql_like/types/mod.rs rename diesel/src/{mysql => mysql_like}/types/numeric.rs (62%) diff --git a/diesel/src/lib.rs b/diesel/src/lib.rs index 466a8b49d93f..61bad3a3e427 100644 --- a/diesel/src/lib.rs +++ b/diesel/src/lib.rs @@ -128,7 +128,7 @@ )] #![cfg_attr(feature = "unstable", feature(specialization, try_from))] // Built-in Lints -#![deny(warnings, missing_debug_implementations, missing_copy_implementations, missing_docs)] +#![deny(warnings, missing_debug_implementations, missing_copy_implementations)] // Clippy lints #![cfg_attr( feature = "cargo-clippy", @@ -192,6 +192,10 @@ pub mod migration; pub mod row; pub mod types; + +#[cfg(feature = "mysql")] +mod mysql_like; + #[cfg(feature = "mysql")] pub mod mysql; #[cfg(feature = "postgres")] diff --git a/diesel/src/mysql/backend.rs b/diesel/src/mysql/backend.rs index 91b32be5916b..90483e4bc89d 100644 --- a/diesel/src/mysql/backend.rs +++ b/diesel/src/mysql/backend.rs @@ -1,11 +1,6 @@ //! The MySQL backend -use byteorder::NativeEndian; - -use super::bind_collector::MysqlBindCollector; -use super::query_builder::MysqlQueryBuilder; -use backend::*; -use sql_types::TypeMetadata; +use mysql_like::MysqlLikeBackend; /// The MySQL backend #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -46,17 +41,5 @@ pub enum MysqlType { Blob, } -impl Backend for Mysql { - type QueryBuilder = MysqlQueryBuilder; - type BindCollector = MysqlBindCollector; - type RawValue = [u8]; - type ByteOrder = NativeEndian; -} - -impl TypeMetadata for Mysql { - type TypeMetadata = MysqlType; - type MetadataLookup = (); -} +impl MysqlLikeBackend for Mysql {} -impl SupportsDefaultKeyword for Mysql {} -impl UsesAnsiSavepointSyntax for Mysql {} diff --git a/diesel/src/mysql/mod.rs b/diesel/src/mysql/mod.rs index 745b584e018a..497f85d6d289 100644 --- a/diesel/src/mysql/mod.rs +++ b/diesel/src/mysql/mod.rs @@ -5,10 +5,10 @@ //! MySQL, you may need to work with this module directly. mod backend; -mod bind_collector; +pub mod bind_collector; mod connection; -mod query_builder; +pub mod query_builder; pub mod types; pub use self::backend::{Mysql, MysqlType}; diff --git a/diesel/src/mysql/types/mod.rs b/diesel/src/mysql/types/mod.rs index d684fee1b369..c80dac067a5d 100644 --- a/diesel/src/mysql/types/mod.rs +++ b/diesel/src/mysql/types/mod.rs @@ -2,110 +2,6 @@ #[cfg(feature = "chrono")] mod date_and_time; -mod numeric; - -use byteorder::WriteBytesExt; -use std::io::Write; - -use deserialize::{self, FromSql}; -use mysql::{Mysql, MysqlType}; -use serialize::{self, IsNull, Output, ToSql}; -use sql_types::*; - -impl ToSql for i8 { - fn to_sql(&self, out: &mut Output) -> serialize::Result { - out.write_i8(*self).map(|_| IsNull::No).map_err(Into::into) - } -} - -impl FromSql for i8 { - fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { - let bytes = not_none!(bytes); - Ok(bytes[0] as i8) - } -} - -/// Represents the MySQL unsigned type. -#[derive(Debug, Clone, Copy, Default, SqlType, QueryId)] -pub struct Unsigned(ST); - -impl ToSql, Mysql> for u8 { - fn to_sql(&self, out: &mut Output) -> serialize::Result { - ToSql::::to_sql(&(*self as i8), out) - } -} - -impl FromSql, Mysql> for u8 { - fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { - let signed: i8 = FromSql::::from_sql(bytes)?; - Ok(signed as u8) - } -} - -impl ToSql, Mysql> for u16 { - fn to_sql(&self, out: &mut Output) -> serialize::Result { - ToSql::::to_sql(&(*self as i16), out) - } -} - -impl FromSql, Mysql> for u16 { - fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { - let signed: i16 = FromSql::::from_sql(bytes)?; - Ok(signed as u16) - } -} - -impl ToSql, Mysql> for u32 { - fn to_sql(&self, out: &mut Output) -> serialize::Result { - ToSql::::to_sql(&(*self as i32), out) - } -} - -impl FromSql, Mysql> for u32 { - fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { - let signed: i32 = FromSql::::from_sql(bytes)?; - Ok(signed as u32) - } -} - -impl ToSql, Mysql> for u64 { - fn to_sql(&self, out: &mut Output) -> serialize::Result { - ToSql::::to_sql(&(*self as i64), out) - } -} - -impl FromSql, Mysql> for u64 { - fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { - let signed: i64 = FromSql::::from_sql(bytes)?; - Ok(signed as u64) - } -} - -impl ToSql for bool { - fn to_sql(&self, out: &mut Output) -> serialize::Result { - let int_value = if *self { 1 } else { 0 }; - >::to_sql(&int_value, out) - } -} - -impl FromSql for bool { - fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { - Ok(not_none!(bytes).iter().any(|x| *x != 0)) - } -} - -impl HasSqlType> for Mysql -where - Mysql: HasSqlType, -{ - fn metadata(lookup: &()) -> MysqlType { - >::metadata(lookup) - } - - fn is_signed() -> IsSigned { - IsSigned::Unsigned - } -} /// Represents the MySQL datetime type. /// diff --git a/diesel/src/mysql_like/backend.rs b/diesel/src/mysql_like/backend.rs new file mode 100644 index 000000000000..03ce49d2ba44 --- /dev/null +++ b/diesel/src/mysql_like/backend.rs @@ -0,0 +1,25 @@ +//! The MySQL backend +use byteorder::NativeEndian; + +use mysql::bind_collector::MysqlBindCollector; +use mysql::query_builder::MysqlQueryBuilder; +use mysql::MysqlType; +use backend::*; +use sql_types::TypeMetadata; + +pub trait MysqlLikeBackend: Backend {} + +impl Backend for DB { + type QueryBuilder = MysqlQueryBuilder; + type BindCollector = MysqlBindCollector; + type RawValue = [u8]; + type ByteOrder = NativeEndian; +} + +impl TypeMetadata for DB { + type TypeMetadata = MysqlType; + type MetadataLookup = (); +} + +impl SupportsDefaultKeyword for DB {} +impl UsesAnsiSavepointSyntax for DB {} diff --git a/diesel/src/mysql_like/mod.rs b/diesel/src/mysql_like/mod.rs new file mode 100644 index 000000000000..109af9671ad2 --- /dev/null +++ b/diesel/src/mysql_like/mod.rs @@ -0,0 +1,5 @@ + +mod backend; +pub mod types; + +pub use self::backend::{MysqlLikeBackend}; diff --git a/diesel/src/mysql_like/types/mod.rs b/diesel/src/mysql_like/types/mod.rs new file mode 100644 index 000000000000..29818de0419a --- /dev/null +++ b/diesel/src/mysql_like/types/mod.rs @@ -0,0 +1,105 @@ +//! MySQL specific types +mod numeric; + +use byteorder::WriteBytesExt; +use std::io::Write; + +use deserialize::{self, FromSql}; +use mysql_like::{MysqlLikeBackend}; +use serialize::{self, IsNull, Output, ToSql}; +use sql_types::*; + +impl ToSql for i8 { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + out.write_i8(*self).map(|_| IsNull::No).map_err(Into::into) + } +} + +impl FromSql for i8 { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let bytes = not_none!(bytes); + Ok(bytes[0] as i8) + } +} + +/// Represents the MySQL unsigned type. +#[derive(Debug, Clone, Copy, Default, SqlType, QueryId)] +pub struct Unsigned(ST); + +impl ToSql, MysqlLike> for u8 { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + ToSql::::to_sql(&(*self as i8), out) + } +} + +impl FromSql, MysqlLike> for u8 { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let signed: i8 = FromSql::::from_sql(bytes)?; + Ok(signed as u8) + } +} + +impl ToSql, MysqlLike> for u16 { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + ToSql::::to_sql(&(*self as i16), out) + } +} + +impl FromSql, MysqlLike> for u16 { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let signed: i16 = FromSql::::from_sql(bytes)?; + Ok(signed as u16) + } +} + +impl ToSql, MysqlLike> for u32 { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + ToSql::::to_sql(&(*self as i32), out) + } +} + +impl FromSql, MysqlLike> for u32 { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let signed: i32 = FromSql::::from_sql(bytes)?; + Ok(signed as u32) + } +} + +impl ToSql, MysqlLike> for u64 { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + ToSql::::to_sql(&(*self as i64), out) + } +} + +impl FromSql, MysqlLike> for u64 { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let signed: i64 = FromSql::::from_sql(bytes)?; + Ok(signed as u64) + } +} + +impl ToSql for bool { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + let int_value = if *self { 1 } else { 0 }; + >::to_sql(&int_value, out) + } +} + +impl FromSql for bool { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + Ok(not_none!(bytes).iter().any(|x| *x != 0)) + } +} + +impl HasSqlType> for MysqlLike +where + MysqlLike: MysqlLikeBackend + HasSqlType, +{ + fn metadata(lookup: &MysqlLike::MetadataLookup) -> ::TypeMetadata { + >::metadata(lookup) + } + + fn is_signed() -> IsSigned { + IsSigned::Unsigned + } +} diff --git a/diesel/src/mysql/types/numeric.rs b/diesel/src/mysql_like/types/numeric.rs similarity index 62% rename from diesel/src/mysql/types/numeric.rs rename to diesel/src/mysql_like/types/numeric.rs index 672bcb6ad88f..ec8af567275a 100644 --- a/diesel/src/mysql/types/numeric.rs +++ b/diesel/src/mysql_like/types/numeric.rs @@ -5,23 +5,23 @@ pub mod bigdecimal { use self::bigdecimal::BigDecimal; use std::io::prelude::*; - use backend::Backend; use deserialize::{self, FromSql}; - use mysql::Mysql; + use mysql_like::MysqlLikeBackend; use serialize::{self, IsNull, Output, ToSql}; use sql_types::{Binary, Numeric}; - impl ToSql for BigDecimal { - fn to_sql(&self, out: &mut Output) -> serialize::Result { + impl ToSql for BigDecimal { + fn to_sql(&self, out: &mut Output) -> serialize::Result { write!(out, "{}", *self) .map(|_| IsNull::No) .map_err(|e| e.into()) } } - impl FromSql for BigDecimal { - fn from_sql(bytes: Option<&::RawValue>) -> deserialize::Result { - let bytes_ptr = <*const [u8] as FromSql>::from_sql(bytes)?; + impl FromSql for BigDecimal + where MysqlLike: MysqlLikeBackend { + fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result { + let bytes_ptr = <*const [u8] as FromSql>::from_sql(bytes)?; let bytes = unsafe { &*bytes_ptr }; BigDecimal::parse_bytes(bytes, 10) .ok_or_else(|| Box::from(format!("{:?} is not valid decimal number ", bytes))) diff --git a/diesel/src/sql_types/mod.rs b/diesel/src/sql_types/mod.rs index 05b909c4db76..219d9de71bb3 100644 --- a/diesel/src/sql_types/mod.rs +++ b/diesel/src/sql_types/mod.rs @@ -361,6 +361,9 @@ pub use pg::types::sql_types::*; #[cfg(feature = "mysql")] pub use mysql::types::*; +#[cfg(feature = "mysql")] +pub use mysql_like::types::*; + /// Indicates that a SQL type exists for a backend. /// /// # Deriving