forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::decode::Decode; | ||
use crate::encode::{Encode, IsNull}; | ||
use crate::error::BoxDynError; | ||
use crate::sqlite::{ | ||
type_info::DataType, Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef, | ||
}; | ||
use crate::types::Type; | ||
use bigdecimal::BigDecimal; | ||
|
||
impl Type<Sqlite> for BigDecimal { | ||
fn type_info() -> SqliteTypeInfo { | ||
SqliteTypeInfo(DataType::Text) | ||
} | ||
|
||
fn compatible(ty: &SqliteTypeInfo) -> bool { | ||
<&str as Type<Sqlite>>::compatible(ty) | ||
} | ||
} | ||
|
||
impl Encode<'_, Sqlite> for BigDecimal { | ||
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull { | ||
let string_value = self.to_string(); | ||
Encode::<Sqlite>::encode(string_value, buf) | ||
} | ||
} | ||
|
||
impl Decode<'_, Sqlite> for BigDecimal { | ||
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> { | ||
let string_value = <&str as Decode<Sqlite>>::decode(value)?; | ||
|
||
string_value.parse().map_err(Into::into) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use bigdecimal_::FromPrimitive; | ||
use rust_decimal::Decimal; | ||
|
||
use crate::decode::Decode; | ||
use crate::encode::{Encode, IsNull}; | ||
use crate::error::BoxDynError; | ||
use crate::sqlite::{ | ||
type_info::DataType, Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef, | ||
}; | ||
use crate::types::Type; | ||
|
||
impl Type<Sqlite> for Decimal { | ||
fn type_info() -> SqliteTypeInfo { | ||
SqliteTypeInfo(DataType::Text) | ||
} | ||
|
||
fn compatible(ty: &SqliteTypeInfo) -> bool { | ||
<&str as Type<Sqlite>>::compatible(ty) | ||
|| <f64 as Type<Sqlite>>::compatible(ty) | ||
|| <i64 as Type<Sqlite>>::compatible(ty) | ||
} | ||
} | ||
|
||
impl Encode<'_, Sqlite> for Decimal { | ||
fn encode_by_ref(&self, buf: &mut Vec<SqliteArgumentValue<'_>>) -> IsNull { | ||
let string_value = self.to_string(); | ||
Encode::<Sqlite>::encode(string_value, buf) | ||
} | ||
} | ||
|
||
impl Decode<'_, Sqlite> for Decimal { | ||
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> { | ||
match value.type_info().0 { | ||
DataType::Float => Ok(Decimal::from_f64(value.double()).ok_or("bad float")?), | ||
DataType::Int | DataType::Int64 => Ok(Decimal::from(value.int64())), | ||
_ => { | ||
let string_value = <&str as Decode<Sqlite>>::decode(value)?; | ||
string_value.parse().map_err(Into::into) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
use crate::value::ValueRef; | ||
use crate::{ | ||
decode::Decode, | ||
encode::{Encode, IsNull}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters