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

Implement DECIMAL type support for mysql #234

Merged
merged 7 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -203,56 +203,56 @@ jobs:
# -----------------------------------------------------

# integration test: async-std (chrono)
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros uuid chrono tls'
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros uuid chrono tls bigdecimal'
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# integration test: async-std (time)
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros uuid time tls'
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros uuid time tls bigdecimal'
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# integration test: async-std (time + chrono)
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros uuid time chrono tls'
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros uuid time chrono tls bigdecimal'
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# integration test: tokio (chrono)
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros uuid chrono tls'
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros uuid chrono tls bigdecimal'
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# integration test: tokio (time)
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros uuid time tls'
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros uuid time tls bigdecimal'
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# integration test: tokio (time + chrono)
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros uuid chrono time tls'
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros uuid chrono time tls bigdecimal'
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# UI feature gate tests: async-std
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros tls' --test ui-tests
- run: cargo test --no-default-features --features 'runtime-async-std mysql macros tls bigdecimal' --test ui-tests
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
DATABASE_URL: mysql://root:password@localhost:${{ job.services.mysql.ports[3306] }}/sqlx?ssl-mode=VERIFY_CA&ssl-ca=%2Fdata%2Fmysql%2Fca.pem

# UI feature gate tests: tokio
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros tls' --test ui-tests
- run: cargo test --no-default-features --features 'runtime-tokio mysql macros tls bigdecimal' --test ui-tests
env:
# pass the path to the CA that the MySQL service generated
# NOTE: Github Actions' YML parser doesn't handle multiline strings correctly
Expand Down
2 changes: 2 additions & 0 deletions sqlx-core/src/mysql/protocol/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ impl<'c> Row<'c> {
(len_size, len.unwrap_or_default())
}

TypeId::NEWDECIMAL => (0, 1 + buffer[index] as usize),

id => {
unimplemented!("encountered unknown field type id: {:?}", id);
}
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/mysql/protocol/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TypeId {
// Numeric: FLOAT, DOUBLE
pub const FLOAT: TypeId = TypeId(4);
pub const DOUBLE: TypeId = TypeId(5);
// pub const NEWDECIMAL: TypeId = TypeId(246);
pub const NEWDECIMAL: TypeId = TypeId(246);

// Date/Time: DATE, TIME, DATETIME, TIMESTAMP
pub const DATE: TypeId = TypeId(10);
Expand Down
92 changes: 92 additions & 0 deletions sqlx-core/src/mysql/types/bigdecimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use bigdecimal::BigDecimal;

use crate::decode::Decode;
use crate::encode::Encode;
use crate::io::Buf;
use crate::mysql::protocol::TypeId;
use crate::mysql::{MySql, MySqlData, MySqlTypeInfo, MySqlValue};
use crate::types::Type;
use crate::Error;
use std::str::FromStr;

impl Type<MySql> for BigDecimal {
fn type_info() -> MySqlTypeInfo {
MySqlTypeInfo::new(TypeId::NEWDECIMAL)
}
}

impl Encode<MySql> for BigDecimal {
fn encode(&self, buf: &mut Vec<u8>) {
let size = Encode::<MySql>::size_hint(self) - 1;
assert!(size <= std::u8::MAX as usize, "Too large size");
buf.push(size as u8);
let s = self.to_string();
buf.extend_from_slice(s.as_bytes());
}

fn size_hint(&self) -> usize {
let s = self.to_string();
s.as_bytes().len() + 1
}
}

impl Decode<'_, MySql> for BigDecimal {
fn decode(value: MySqlValue) -> crate::Result<Self> {
match value.try_get()? {
MySqlData::Binary(mut binary) => {
let _len = binary.get_u8()?;
let s = std::str::from_utf8(binary).map_err(Error::decode)?;
Ok(BigDecimal::from_str(s).map_err(Error::decode)?)
}
MySqlData::Text(s) => {
let s = std::str::from_utf8(s).map_err(Error::decode)?;
Ok(BigDecimal::from_str(s).map_err(Error::decode)?)
}
}
}
}

#[test]
fn test_encode_decimal() {
let v: BigDecimal = BigDecimal::from_str("-1.05").unwrap();
let mut buf: Vec<u8> = vec![];
<BigDecimal as Encode<MySql>>::encode(&v, &mut buf);
assert_eq!(buf, vec![0x05, b'-', b'1', b'.', b'0', b'5']);

let v: BigDecimal = BigDecimal::from_str("-105000").unwrap();
let mut buf: Vec<u8> = vec![];
<BigDecimal as Encode<MySql>>::encode(&v, &mut buf);
assert_eq!(buf, vec![0x07, b'-', b'1', b'0', b'5', b'0', b'0', b'0']);

let v: BigDecimal = BigDecimal::from_str("0.00105").unwrap();
let mut buf: Vec<u8> = vec![];
<BigDecimal as Encode<MySql>>::encode(&v, &mut buf);
assert_eq!(buf, vec![0x07, b'0', b'.', b'0', b'0', b'1', b'0', b'5']);
}

#[test]
fn test_decode_decimal() {
let buf: Vec<u8> = vec![0x05, b'-', b'1', b'.', b'0', b'5'];
let v = <BigDecimal as Decode<'_, MySql>>::decode(MySqlValue::binary(
MySqlTypeInfo::new(TypeId::NEWDECIMAL),
buf.as_slice(),
))
.unwrap();
assert_eq!(v.to_string(), "-1.05");

let buf: Vec<u8> = vec![0x04, b'0', b'.', b'0', b'5'];
let v = <BigDecimal as Decode<'_, MySql>>::decode(MySqlValue::binary(
MySqlTypeInfo::new(TypeId::NEWDECIMAL),
buf.as_slice(),
))
.unwrap();
assert_eq!(v.to_string(), "0.05");

let buf: Vec<u8> = vec![0x06, b'-', b'9', b'0', b'0', b'0', b'0'];
let v = <BigDecimal as Decode<'_, MySql>>::decode(MySqlValue::binary(
MySqlTypeInfo::new(TypeId::NEWDECIMAL),
buf.as_slice(),
))
.unwrap();
assert_eq!(v.to_string(), "-90000");
}
10 changes: 10 additions & 0 deletions sqlx-core/src/mysql/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
//! | `time::Date` | DATE |
//! | `time::Time` | TIME |
//!
//! ### [`bigdecimal`](https://crates.io/crates/bigdecimal)
//! Requires the `bigdecimal` Cargo feature flag.
//!
//! | Rust type | MySQL type(s) |
//! |---------------------------------------|------------------------------------------------------|
//! | `bigdecimal::BigDecimal` | DECIMAL |
//!
//! # Nullable
//!
//! In addition, `Option<T>` is supported where `T` implements `Type`. An `Option<T>` represents
Expand All @@ -54,6 +61,9 @@ mod int;
mod str;
mod uint;

#[cfg(feature = "bigdecimal")]
mod bigdecimal;

#[cfg(feature = "chrono")]
mod chrono;

Expand Down
3 changes: 3 additions & 0 deletions sqlx-macros/src/database/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ impl_database_ext! {

#[cfg(feature = "time")]
sqlx::types::time::OffsetDateTime,

#[cfg(feature = "bigdecimal")]
sqlx::types::BigDecimal,
},
ParamChecking::Weak,
feature-types: info => info.type_feature_gate(),
Expand Down
12 changes: 12 additions & 0 deletions tests/mysql-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ mod time_tests {
.assume_utc()
));
}

#[cfg(feature = "bigdecimal")]
test_type!(decimal(
MySql,
sqlx::types::BigDecimal,
"CAST(1 AS DECIMAL(1, 0))" == "1".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(10000 AS DECIMAL(5, 0))" == "10000".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(0.1 AS DECIMAL(2, 1))" == "0.1".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(0.01234 AS DECIMAL(6, 5))" == "0.01234".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(12.34 AS DECIMAL(4, 2))" == "12.34".parse::<sqlx::types::BigDecimal>().unwrap(),
"CAST(12345.6789 AS DECIMAL(9, 4))" == "12345.6789".parse::<sqlx::types::BigDecimal>().unwrap(),
));