-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP moving mysql types.rs file to shared area #1905
Open
virome
wants to merge
1
commit into
diesel-rs:master
Choose a base branch
from
virome:split-mysql-and-mariadb-into-distinct-backends
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! The MySQL backend | ||
use byteorder::NativeEndian; | ||
|
||
use backend::*; | ||
use mysql::bind_collector::MysqlBindCollector; | ||
use mysql::query_builder::MysqlQueryBuilder; | ||
use mysql::MysqlType; | ||
use sql_types::TypeMetadata; | ||
|
||
pub trait MysqlLikeBackend: Backend + for<'a> HasRawValue<'a, RawValue = &'a [u8]> {} | ||
|
||
impl<DB: MysqlLikeBackend> Backend for DB { | ||
type QueryBuilder = MysqlQueryBuilder; | ||
type BindCollector = MysqlBindCollector; | ||
type ByteOrder = NativeEndian; | ||
} | ||
|
||
impl<'a, DB: MysqlLikeBackend> HasRawValue<'a> for DB { | ||
type RawValue = &'a [u8]; | ||
} | ||
|
||
impl<DB: MysqlLikeBackend> TypeMetadata for DB { | ||
type TypeMetadata = MysqlType; | ||
type MetadataLookup = (); | ||
} | ||
|
||
impl<DB: MysqlLikeBackend> SupportsDefaultKeyword for DB {} | ||
impl<DB: MysqlLikeBackend> UsesAnsiSavepointSyntax for DB {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mod backend; | ||
pub mod types; | ||
|
||
pub use self::backend::MysqlLikeBackend; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
//! MySQL specific types | ||
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<MysqlLike: MysqlLikeBackend> ToSql<TinyInt, MysqlLike> for i8 { | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, MysqlLike>) -> serialize::Result { | ||
out.write_i8(*self).map(|_| IsNull::No).map_err(Into::into) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> FromSql<TinyInt, MysqlLike> for i8 { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
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>(ST); | ||
|
||
impl<MysqlLike: MysqlLikeBackend> ToSql<Unsigned<TinyInt>, MysqlLike> for u8 { | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, MysqlLike>) -> serialize::Result { | ||
ToSql::<TinyInt, MysqlLike>::to_sql(&(*self as i8), out) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> FromSql<Unsigned<TinyInt>, MysqlLike> for u8 { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
let signed: i8 = FromSql::<TinyInt, MysqlLike>::from_sql(bytes)?; | ||
Ok(signed as u8) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> ToSql<Unsigned<SmallInt>, MysqlLike> for u16 { | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, MysqlLike>) -> serialize::Result { | ||
ToSql::<SmallInt, MysqlLike>::to_sql(&(*self as i16), out) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> FromSql<Unsigned<SmallInt>, MysqlLike> for u16 { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
let signed: i16 = FromSql::<SmallInt, MysqlLike>::from_sql(bytes)?; | ||
Ok(signed as u16) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> ToSql<Unsigned<Integer>, MysqlLike> for u32 { | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, MysqlLike>) -> serialize::Result { | ||
ToSql::<Integer, MysqlLike>::to_sql(&(*self as i32), out) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> FromSql<Unsigned<Integer>, MysqlLike> for u32 { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
let signed: i32 = FromSql::<Integer, MysqlLike>::from_sql(bytes)?; | ||
Ok(signed as u32) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> ToSql<Unsigned<BigInt>, MysqlLike> for u64 { | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, MysqlLike>) -> serialize::Result { | ||
ToSql::<BigInt, MysqlLike>::to_sql(&(*self as i64), out) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> FromSql<Unsigned<BigInt>, MysqlLike> for u64 { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
let signed: i64 = FromSql::<BigInt, MysqlLike>::from_sql(bytes)?; | ||
Ok(signed as u64) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> ToSql<Bool, MysqlLike> for bool { | ||
fn to_sql<W: Write>(&self, out: &mut Output<W, MysqlLike>) -> serialize::Result { | ||
let int_value = if *self { 1 } else { 0 }; | ||
<i32 as ToSql<Integer, MysqlLike>>::to_sql(&int_value, out) | ||
} | ||
} | ||
|
||
impl<MysqlLike: MysqlLikeBackend> FromSql<Bool, MysqlLike> for bool { | ||
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> { | ||
Ok(not_none!(bytes).iter().any(|x| *x != 0)) | ||
} | ||
} | ||
|
||
impl<MysqlLike, ST> HasSqlType<Unsigned<ST>> for MysqlLike | ||
where | ||
MysqlLike: MysqlLikeBackend + HasSqlType<ST>, | ||
{ | ||
fn metadata(lookup: &MysqlLike::MetadataLookup) -> <MysqlLike as TypeMetadata>::TypeMetadata { | ||
<MysqlLike as HasSqlType<ST>>::metadata(lookup) | ||
} | ||
|
||
fn is_signed() -> IsSigned { | ||
IsSigned::Unsigned | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rust is so cool. I'm surprised this self referential trait relationship even compiled