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

fix(postgres) : int type conversion while decoding #3173

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Changes from 2 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
46 changes: 33 additions & 13 deletions sqlx-postgres/src/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@ use crate::error::BoxDynError;
use crate::types::Type;
use crate::{PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef, Postgres};

fn int_decode(value: PgValueRef<'_>) -> Result<i64, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Text => value.as_str()?.parse()?,
PgValueFormat::Binary => {
let buf = value.as_bytes()?;

// Return error if buf is empty or is more than 8 bytes
match buf.len() {
0 => {
return Err("Value Buffer found empty while decoding to integer type".into());
}
buf_len @ 9.. => {
return Err(format!(
"Value Buffer exceeds 8 bytes while decoding to integer type. Buffer size = {} bytes ", buf_len
)
.into());
}
_ => {}
}

BigEndian::read_int(buf, buf.len())
abonander marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

impl Type<Postgres> for i8 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::CHAR
Expand All @@ -29,7 +54,11 @@ impl Encode<'_, Postgres> for i8 {
impl Decode<'_, Postgres> for i8 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
// note: in the TEXT encoding, a value of "0" here is encoded as an empty string
Ok(value.as_bytes()?.get(0).copied().unwrap_or_default() as i8)
if (value.format() == PgValueFormat::Text) && (value.as_bytes()?.is_empty()) {
return Ok(Default::default());
}

int_decode(value)?.try_into().map_err(Into::into)
abonander marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -55,10 +84,7 @@ impl Encode<'_, Postgres> for i16 {

impl Decode<'_, Postgres> for i16 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => BigEndian::read_i16(value.as_bytes()?),
PgValueFormat::Text => value.as_str()?.parse()?,
})
int_decode(value)?.try_into().map_err(Into::into)
}
}

Expand All @@ -84,10 +110,7 @@ impl Encode<'_, Postgres> for i32 {

impl Decode<'_, Postgres> for i32 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => BigEndian::read_i32(value.as_bytes()?),
PgValueFormat::Text => value.as_str()?.parse()?,
})
int_decode(value)?.try_into().map_err(Into::into)
}
}

Expand All @@ -113,9 +136,6 @@ impl Encode<'_, Postgres> for i64 {

impl Decode<'_, Postgres> for i64 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
Ok(match value.format() {
PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?),
PgValueFormat::Text => value.as_str()?.parse()?,
})
int_decode(value)
}
}
Loading