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

Support for rust_decimal::Decimal #456

Merged
merged 9 commits into from
Jul 9, 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
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.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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,7 +65,8 @@ sqlite = [ "sqlx-core/sqlite", "sqlx-macros/sqlite" ]
mssql = [ "sqlx-core/mssql", "sqlx-macros/mssql" ]

# types
bigdecimal = ["sqlx-core/bigdecimal", "sqlx-macros/bigdecimal"]
bigdecimal = [ "sqlx-core/bigdecimal", "sqlx-macros/bigdecimal" ]
decimal = [ "sqlx-core/decimal", "sqlx-macros/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