Skip to content

Commit

Permalink
Support for rust_decimal::Decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius de Bruijn committed Jun 29, 2020
1 parent e4005bb commit 3e69903
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ offline = [ "sqlx-macros/offline", "sqlx-core/offline" ]
# intended mainly for CI and docs
all = [ "tls", "all-databases", "all-types" ]
all-databases = [ "mysql", "sqlite", "postgres", "mssql", "any" ]
all-types = [ "bigdecimal", "json", "time", "chrono", "ipnetwork", "uuid" ]
all-types = [ "bigdecimal", "decimal", "json", "time", "chrono", "ipnetwork", "uuid" ]

# runtime
runtime-async-std = [ "sqlx-core/runtime-async-std", "sqlx-macros/runtime-async-std" ]
Expand All @@ -65,6 +65,7 @@ mssql = [ "sqlx-core/mssql", "sqlx-macros/mssql" ]

# types
bigdecimal = ["sqlx-core/bigdecimal", "sqlx-macros/bigdecimal"]
decimal = ["sqlx-core/decimal"]
chrono = [ "sqlx-core/chrono", "sqlx-macros/chrono" ]
ipnetwork = [ "sqlx-core/ipnetwork", "sqlx-macros/ipnetwork" ]
uuid = [ "sqlx-core/uuid", "sqlx-macros/uuid" ]
Expand Down
5 changes: 4 additions & 1 deletion sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ mssql = [ "uuid", "encoding_rs", "regex" ]
any = []

# types
all-types = [ "chrono", "time", "bigdecimal", "ipnetwork", "json", "uuid" ]
all-types = [ "chrono", "time", "bigdecimal", "decimal", "ipnetwork", "json", "uuid" ]
bigdecimal = [ "bigdecimal_", "num-bigint" ]
decimal = [ "rust_decimal", "num-bigint", "num-traits" ]
json = [ "serde", "serde_json" ]

# runtimes
Expand All @@ -41,6 +42,8 @@ atoi = "0.3.2"
sqlx-rt = { path = "../sqlx-rt", version = "0.1.0-pre" }
base64 = { version = "0.12.1", default-features = false, optional = true, features = [ "std" ] }
bigdecimal_ = { version = "0.1.0", optional = true, package = "bigdecimal" }
rust_decimal = { version = "1.6.0", optional = true }
num-traits = { version = "0.2.12", optional = true }
bitflags = { version = "1.2.1", default-features = false }
bytes = "0.5.4"
byteorder = { version = "1.3.4", default-features = false, features = [ "std" ] }
Expand Down
29 changes: 29 additions & 0 deletions sqlx-core/src/mysql/types/decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use rust_decimal::Decimal;

use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::mysql::io::MySqlBufMutExt;
use crate::mysql::protocol::text::ColumnType;
use crate::mysql::{MySql, MySqlTypeInfo, MySqlValueRef};
use crate::types::Type;

impl Type<MySql> for Decimal {
fn type_info() -> MySqlTypeInfo {
MySqlTypeInfo::binary(ColumnType::NewDecimal)
}
}

impl Encode<'_, MySql> for Decimal {
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
buf.put_str_lenenc(&self.to_string());

IsNull::No
}
}

impl Decode<'_, MySql> for Decimal {
fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(value.as_str()?.parse()?)
}
}
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 @@ -48,6 +48,13 @@
//! |---------------------------------------|------------------------------------------------------|
//! | `bigdecimal::BigDecimal` | DECIMAL |
//!
//! ### [`decimal`](https://crates.io/crates/rust_decimal)
//! Requires the `decimal` Cargo feature flag.
//!
//! | Rust type | MySQL type(s) |
//! |---------------------------------------|------------------------------------------------------|
//! | `rust_decimal::Decimal` | DECIMAL |
//!
//! ### [`json`](https://crates.io/crates/json)
//!
//! Requires the `json` Cargo feature flag.
Expand All @@ -72,6 +79,9 @@ mod uint;
#[cfg(feature = "bigdecimal")]
mod bigdecimal;

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

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

Expand Down
Loading

0 comments on commit 3e69903

Please sign in to comment.