-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
enable Decimal support for Mysql #1019
Conversation
cc @rubdos who added BigDecimal support for Postgres |
diesel/src/mysql/types/numeric.rs
Outdated
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()) |
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.
How about write!(out, "{}", self)
diesel/src/mysql/types/numeric.rs
Outdated
pub mod bigdecimal { | ||
extern crate num_traits; | ||
extern crate num_bigint; | ||
extern crate num_integer; |
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.
Do we need any of these besides bigdecimal
?
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.
Can you add some tests ?
Also some documentation about the fact that MySQL supports the BigDecimal
type would be great
diesel/src/mysql/types/mod.rs
Outdated
|
||
use byteorder::WriteBytesExt; | ||
use mysql::{Mysql, MysqlType}; | ||
pub use mysql::{Mysql, MysqlType}; |
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.
Why is this needed ?
diesel/src/mysql/types/mod.rs
Outdated
use std::error::Error as StdError; | ||
use std::io::Write; | ||
use types::{ToSql, IsNull, FromSql, HasSqlType}; | ||
|
||
pub mod sql_types { |
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.
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 | |||
} | |||
} | |||
|
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.
✂️
diesel/src/mysql/types/mod.rs
Outdated
MysqlType::String | ||
} | ||
} | ||
|
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.
✂️
diesel/src/mysql/types/numeric.rs
Outdated
|
||
use mysql::Mysql; | ||
|
||
pub use self::bigdecimal::BigDecimal; |
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.
Why does this need to be pub use
?
diesel/src/mysql/types/numeric.rs
Outdated
pub use self::bigdecimal::BigDecimal; | ||
|
||
use types::{self, FromSql, ToSql, IsNull}; | ||
|
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.
✂️
I've updated docs and I am not reexporting BigDecimal anymore. I'll will still need to add tests. |
diesel/src/mysql/types/numeric.rs
Outdated
|
||
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[] |
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.
I don't think this comment is adding any value.
diesel/src/mysql/types/numeric.rs
Outdated
|
||
impl FromSql<types::Numeric, Mysql> for BigDecimal { | ||
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error+Send+Sync>> { | ||
//_ numeric |
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.
I don't think this comment is adding any value.
diesel/src/mysql/types/numeric.rs
Outdated
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)) { |
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.
Why do we need to do string conversion here? How about just BigDecimal::parse_bytes
?
I've enabled pg tests for mysql |
@Fiedzia CI fails with
Can you have a look at this? |
Oh, you also need to rebase, it should be easy, you have to change the mysql metadata function so it takes |
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. |
Its me. I did git pull --rebase upstream master |
Mhhh weird that should have worked. Did you have any conflict ? Did you resolve then properly ? |
I had conflicts and I resolved them with no particular issues. |
I believe I resolved all issues, builds are failing due to problem with rust nightly: |
7f36814
to
fed235a
Compare
7898290
to
c202870
Compare
r? @Eijebong |
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.
I think we're good to go minus those two comments nits :)
diesel_tests/tests/types.rs
Outdated
"1.0", | ||
"141.0", | ||
"-1.0", | ||
// Powers of 10k (numeric is represented in base 10k) |
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.
This is not true for MySQL IIRC
diesel_tests/tests/types.rs
Outdated
|
||
// 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 |
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.
Nit: "// It is"
4daff32
to
8b069a1
Compare
Took the liberty to rebase and add a changelog entry, I'll merge once tests are green. |
Linking the original issue here: #830 |
Thanks ! |
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.