Skip to content
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

enable Decimal support for Mysql #1019

Merged
merged 1 commit into from
Aug 3, 2017

Conversation

Fiedzia
Copy link
Contributor

@Fiedzia Fiedzia commented Jul 12, 2017

Whole decimal handling was really confusing, so I have no idea if I got this right, but seems to work.
I am still looking on how to write tests for it.

@killercup
Copy link
Member

cc @rubdos who added BigDecimal support for Postgres

impl ToSql<types::Numeric, Mysql> for BigDecimal {
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error + Send + Sync>> {
// Mysql representats decimal type as char[]
out.write(&format!("{}", *self).into_bytes()).map(|_| IsNull::No).map_err(|e| e.into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about write!(out, "{}", self)

pub mod bigdecimal {
extern crate num_traits;
extern crate num_bigint;
extern crate num_integer;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need any of these besides bigdecimal?

Copy link
Member

@Eijebong Eijebong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests ?
Also some documentation about the fact that MySQL supports the BigDecimal type would be great


use byteorder::WriteBytesExt;
use mysql::{Mysql, MysqlType};
pub use mysql::{Mysql, MysqlType};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed ?

use std::error::Error as StdError;
use std::io::Write;
use types::{ToSql, IsNull, FromSql, HasSqlType};

pub mod sql_types {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't reexport BigDecimal for postgres, why should we for MySQL ?

I think you can remove the whole sql_types module :)

@@ -58,3 +65,18 @@ impl HasSqlType<::types::Timestamp> for Mysql {
MysqlType::Timestamp
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️

MysqlType::String
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️


use mysql::Mysql;

pub use self::bigdecimal::BigDecimal;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be pub use ?

pub use self::bigdecimal::BigDecimal;

use types::{self, FromSql, ToSql, IsNull};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️

@Fiedzia
Copy link
Contributor Author

Fiedzia commented Jul 13, 2017

I've updated docs and I am not reexporting BigDecimal anymore. I'll will still need to add tests.


impl ToSql<types::Numeric, Mysql> for BigDecimal {
fn to_sql<W: Write>(&self, out: &mut W) -> Result<IsNull, Box<Error + Send + Sync>> {
// Mysql representats decimal type as char[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment is adding any value.


impl FromSql<types::Numeric, Mysql> for BigDecimal {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
//_ numeric
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this comment is adding any value.

impl FromSql<types::Numeric, Mysql> for BigDecimal {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> {
//_ numeric
match str::from_utf8(not_none!(bytes)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to do string conversion here? How about just BigDecimal::parse_bytes?

@Fiedzia
Copy link
Contributor Author

Fiedzia commented Jul 14, 2017

I've enabled pg tests for mysql

@killercup
Copy link
Member

@Fiedzia CI fails with

error[E0119]: conflicting implementations of trait `query_builder::query_id::QueryId` for type `types::Numeric`:
   --> /home/travis/build/diesel-rs/diesel/diesel/src/types/impls/mod.rs:160:9

Can you have a look at this?

@Eijebong
Copy link
Member

Oh, you also need to rebase, it should be easy, you have to change the mysql metadata function so it takes _: &()

@sgrif
Copy link
Member

sgrif commented Jul 18, 2017

Looks like something went wrong with the rebase (or with Github), this is showing all of #996 as part of this PR's changed files.

@Eijebong
Copy link
Member

Hmm this is a git usage mistake. The first different commit is d9778d4 which is
9f4e9d3 in this PR.

I don't know why, but the parent commit isn't the same.

@Fiedzia How did you rebase ? :p

@Fiedzia
Copy link
Contributor Author

Fiedzia commented Jul 18, 2017

Its me. I did git pull --rebase upstream master

@Eijebong
Copy link
Member

Mhhh weird that should have worked.

Did you have any conflict ? Did you resolve then properly ?

@Fiedzia
Copy link
Contributor Author

Fiedzia commented Jul 18, 2017

I had conflicts and I resolved them with no particular issues.

@Fiedzia
Copy link
Contributor Author

Fiedzia commented Jul 18, 2017

I believe I resolved all issues, builds are failing due to problem with rust nightly:
rust-lang/rust#43153

@sgrif
Copy link
Member

sgrif commented Jul 31, 2017

r? @Eijebong

Copy link
Member

@Eijebong Eijebong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're good to go minus those two comments nits :)

"1.0",
"141.0",
"-1.0",
// Powers of 10k (numeric is represented in base 10k)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true for MySQL IIRC


// Some non standard values:
let query = "cast(18446744073709551616 as decimal)"; // 2^64; doesn't fit in u64
//it is mysql, it will trim it even in strict mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "// It is"

@Eijebong
Copy link
Member

Eijebong commented Aug 3, 2017

Took the liberty to rebase and add a changelog entry, I'll merge once tests are green.

@Fiedzia
Copy link
Contributor Author

Fiedzia commented Aug 3, 2017

Linking the original issue here: #830

@Eijebong Eijebong merged commit 93de941 into diesel-rs:master Aug 3, 2017
@Eijebong
Copy link
Member

Eijebong commented Aug 3, 2017

Thanks !

@Fiedzia Fiedzia deleted the mysql_decimal_type branch August 3, 2017 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants