diff --git a/sqlx-core/src/postgres/types/record.rs b/sqlx-core/src/postgres/types/record.rs index 0e40f1ef..c4955704 100644 --- a/sqlx-core/src/postgres/types/record.rs +++ b/sqlx-core/src/postgres/types/record.rs @@ -1,5 +1,5 @@ use crate::decode::Decode; -use crate::encode::Encode; +use crate::encode::{Encode, IsNull}; use crate::io::Buf; use crate::postgres::protocol::TypeId; use crate::postgres::{PgTypeInfo, PgValue, Postgres}; @@ -42,13 +42,16 @@ impl<'a> PgRecordEncoder<'a> { self.buf.extend(&[0; 4]); let start = self.buf.len(); - value.encode(self.buf); + if let IsNull::Yes = value.encode_nullable(self.buf) { + // replaces zeros with actual length + self.buf[start - 4..start].copy_from_slice(&(-1_i32).to_be_bytes()); + } else { + let end = self.buf.len(); + let size = end - start; - let end = self.buf.len(); - let size = end - start; - - // replaces zeros with actual length - self.buf[start - 4..start].copy_from_slice(&(size as u32).to_be_bytes()); + // replaces zeros with actual length + self.buf[start - 4..start].copy_from_slice(&(size as u32).to_be_bytes()); + } // keep track of count self.num += 1; @@ -57,28 +60,6 @@ impl<'a> PgRecordEncoder<'a> { } } -// impl Encode for (bool, i32, i64, f64, String) { -// fn encode(&self, buf: &mut Vec) { -// PgRecordEncoder::new(buf) -// .encode(self.0) -// .encode(self.1) -// .encode(self.2) -// .encode(self.3) -// .encode(&self.4) -// .finish() -// } -// -// fn size_hint(&self) -> usize { -// // for each field; oid, length, value -// 5 * (4 + 4) -// + (>::size_hint(&self.0) -// + >::size_hint(&self.1) -// + >::size_hint(&self.2) -// + >::size_hint(&self.3) -// + >::size_hint(&self.4)) -// } -// } - pub struct PgRecordDecoder<'de> { value: PgValue<'de>, }