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 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
62 changes: 48 additions & 14 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 @@ -28,8 +53,26 @@ 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)
// note: decoding here is for the `"char"` type as Postgres does not have a native 1-byte integer type.
// https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/char.c#L58-L60
match value.format() {
PgValueFormat::Binary => int_decode(value)?.try_into().map_err(Into::into),
PgValueFormat::Text => {
let text = value.as_str()?;

// A value of 0 is represented with the empty string.
if text.is_empty() {
return Ok(0);
}

if text.starts_with('\\') {
// For values between 0x80 and 0xFF, it's encoded in octal.
return Ok(i8::from_str_radix(text.trim_start_matches('\\'), 8)?);
}

Ok(text.as_bytes()[0] as i8)
}
}
}
}

Expand All @@ -55,10 +98,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 +124,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 +150,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