Skip to content

Instantly share code, notes, and snippets.

@pimeys
Created October 17, 2019 14:58
Show Gist options
  • Save pimeys/edb4473daf8d5620a4cf817c33a6de1f to your computer and use it in GitHub Desktop.
Save pimeys/edb4473daf8d5620a4cf817c33a6de1f to your computer and use it in GitHub Desktop.
diff --git a/src/postgres.rs b/src/postgres.rs
index c36c165..7d14b27 100644
--- a/src/postgres.rs
+++ b/src/postgres.rs
@@ -1,4 +1,4 @@
-use byteorder::{BigEndian, ReadBytesExt};
+use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use bytes::{BytesMut, BufMut};
use num::Zero;
use crate::Decimal;
@@ -156,10 +156,10 @@ impl<'a> FromSql<'a> for Decimal {
}
impl ToSql for Decimal {
- fn to_sql(&self, _: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<dyn error::Error + 'static + Sync + Send>> {
+ fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn error::Error + 'static + Sync + Send>> {
// If it's zero we can short cut with a u64
if self.is_zero() {
- out.write_u64::<BigEndian>(0)?;
+ out.put_u64_be(0);
return Ok(IsNull::No);
}
let sign = if self.is_sign_negative() { 0x4000 } else { 0x0000 };
@@ -195,19 +195,24 @@ impl ToSql for Decimal {
whole_portion_len as i16 - 1
};
+ let mut v = Vec::new();
+
// Number of groups
- out.write_u16::<BigEndian>(num_groups as u16)?;
+ v.write_u16::<BigEndian>(num_groups as u16)?;
// Weight of first group
- out.write_i16::<BigEndian>(weight)?;
+ v.write_i16::<BigEndian>(weight)?;
// Sign
- out.write_u16::<BigEndian>(sign)?;
+ v.write_u16::<BigEndian>(sign)?;
// DScale
- out.write_u16::<BigEndian>(scale)?;
+ v.write_u16::<BigEndian>(scale)?;
+
// Now process the number
for group in groups[0..num_groups].iter().rev() {
- out.write_u16::<BigEndian>(*group)?;
+ v.write_u16::<BigEndian>(*group)?;
}
+ out.extend_from_slice(v.as_slice());
+
Ok(IsNull::No)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment