Skip to content

Commit

Permalink
fix: text encoding for bytea (#224)
Browse files Browse the repository at this point in the history
* fix: text encoding for bytea

* test: add integration test
  • Loading branch information
sunng87 authored Dec 6, 2024
1 parent cd88ac5 commit 4064832
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl ToSqlText for &[u8] {
_ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
out.put_slice(b"\\x");
out.put_slice(hex::encode(self).as_bytes());
Ok(IsNull::No)
}
Expand Down
20 changes: 16 additions & 4 deletions tests-integration/test-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ impl DummyDatabase {
Type::BOOL,
format.format_for(3),
);
vec![f1, f2, f3, f4]
let f5 = FieldInfo::new("data".into(), None, None, Type::BYTEA, format.format_for(4));
vec![f1, f2, f3, f4, f5]
}
}

Expand All @@ -95,14 +96,16 @@ impl SimpleQueryHandler for DummyDatabase {
Some("Tom"),
Some("2023-02-01 22:27:25.042674"),
Some(true),
Some("tomcat".as_bytes()),
),
(
Some(1),
Some("Jerry"),
Some("2023-02-01 22:27:42.165585"),
Some(false),
Some("".as_bytes()),
),
(Some(2), None, None, None),
(Some(2), None, None, None, None),
];
let data_row_stream = stream::iter(data.into_iter()).map(move |r| {
let mut encoder = DataRowEncoder::new(schema_ref.clone());
Expand All @@ -111,6 +114,7 @@ impl SimpleQueryHandler for DummyDatabase {
encoder.encode_field(&r.1)?;
encoder.encode_field(&r.2)?;
encoder.encode_field(&r.3)?;
encoder.encode_field(&r.4)?;

encoder.finish()
});
Expand Down Expand Up @@ -147,14 +151,21 @@ impl ExtendedQueryHandler for DummyDatabase {
println!("extended query: {:?}", query);
if query.starts_with("SELECT") {
let data = vec![
(Some(0), Some("Tom"), Some(SystemTime::now()), Some(true)),
(
Some(0),
Some("Tom"),
Some(SystemTime::now()),
Some(true),
Some("tomcat".as_bytes()),
),
(
Some(1),
Some("Jerry"),
Some(SystemTime::UNIX_EPOCH + Duration::from_secs(86400 * 5000)),
Some(false),
Some("".as_bytes()),
),
(Some(2), None, None, None),
(Some(2), None, None, None, None),
];
let schema = Arc::new(self.schema(&portal.result_column_format));
let schema_ref = schema.clone();
Expand All @@ -165,6 +176,7 @@ impl ExtendedQueryHandler for DummyDatabase {
encoder.encode_field(&r.1)?;
encoder.encode_field(&r.2)?;
encoder.encode_field(&r.3)?;
encoder.encode_field(&r.4)?;

encoder.finish()
});
Expand Down

0 comments on commit 4064832

Please sign in to comment.