Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

wip: range to str decoding #45

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

Draft
lovasoa wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from better-ranges
Draft
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
7 changes: 7 additions & 0 deletions sqlx-core/src/postgres/types/range.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,13 @@ where
}
}

pub fn decode_as_string<T: Display>(value: PgValueRef<'_>) -> Result<String, BoxDynError>
where
T: Type<Postgres> + for<'a> Decode<'a, Postgres>,
{
Ok(<PgRange<T> as Decode<'_, Postgres>>::decode(value)?.to_string())
}

fn parse_bound<T>(ch: char, value: Option<T>) -> Result<Bound<T>, BoxDynError> {
Ok(if let Some(value) = value {
match ch {
Expand Down
18 changes: 18 additions & 0 deletions sqlx-core/src/postgres/types/str.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl Type<Postgres> for str {
PgTypeInfo::BPCHAR,
PgTypeInfo::VARCHAR,
PgTypeInfo::INTERVAL,
PgTypeInfo::INT4_RANGE,
PgTypeInfo::NUM_RANGE,
PgTypeInfo::TS_RANGE,
PgTypeInfo::TSTZ_RANGE,
PgTypeInfo::DATE_RANGE,
PgTypeInfo::INT8_RANGE,
PgTypeInfo::MONEY,
PgTypeInfo::UNKNOWN,
]
Expand Down Expand Up @@ -115,6 +121,18 @@ impl Decode<'_, Postgres> for String {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
match *value.type_info {
PgType::Interval => super::interval::decode_as_string(value),
PgType::Int4Range => super::range::decode_as_string::<i32>(value),
#[cfg(feature = "bigdecimal")]
PgType::NumRange => super::range::decode_as_string::<bigdecimal::BigDecimal>(value),
#[cfg(feature = "chrono")]
PgType::TsRange => super::range::decode_as_string::<chrono::NaiveDateTime>(value),
#[cfg(feature = "chrono")]
PgType::TstzRange => {
super::range::decode_as_string::<chrono::DateTime<chrono::Utc>>(value)
}
#[cfg(feature = "chrono")]
PgType::DateRange => super::range::decode_as_string::<chrono::NaiveDate>(value),
PgType::Int8Range => super::range::decode_as_string::<i64>(value),
_ => Ok(value.as_str()?.to_owned()),
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/postgres/types.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,15 @@ test_decode_type!(interval_string<String>(Postgres,
"'00:01:00'::INTERVAL * 60" == "01:00:00"
));

#[cfg(feature = "bigdecimal")]
#[cfg(feature = "chrono")]
test_decode_type!(range_string<String>(Postgres,
"'(1,2)'::int4range" == "(1,2)",
"'(1,2)'::numrange" == "(1,2)",
"'[\"2025年01月01日T00:00:00+01:00\", \"2025年01月02日T00:00:00+00:00\")'::tstzrange" == "[\"2025年01月01日T00:00:00+01:00\", \"2025年01月02日T00:00:00+00:00\")",
"'(1,2)'::int8range" == "(1,2)",
));

test_prepared_type!(money<PgMoney>(Postgres, "123.45::money" == PgMoney(12345)));

test_prepared_type!(money_vec<Vec<PgMoney>>(Postgres,
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /