Skip to content

Commit 3a9beb7

Browse files
committed
Add Row::get_bytes
Useful for things like postgres_large_object to avoid some allocation
1 parent e67b5b3 commit 3a9beb7

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,6 +1693,18 @@ impl<'stmt> Row<'stmt> {
16931693
Err(err) => panic!("error retrieving column {:?}: {:?}", idx, err)
16941694
}
16951695
}
1696+
1697+
/// Retrieves the specified field as a raw buffer of Postgres data.
1698+
///
1699+
/// ## Panics
1700+
///
1701+
/// Panics if the index does not references a column.
1702+
pub fn get_bytes<I>(&self, idx: I) -> Option<&[u8]> where I: RowIndex + fmt::Debug {
1703+
match idx.idx(self.stmt) {
1704+
Some(idx) => self.data[idx].as_ref().map(|e| &**e),
1705+
None => panic!("invalid index {:?}", idx),
1706+
}
1707+
}
16961708
}
16971709

16981710
/// A trait implemented by types that can index into columns of a row.

tests/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,3 +917,11 @@ fn test_parameter() {
917917
assert_eq!(Some("UTF8".to_string()), conn.parameter("client_encoding"));
918918
assert_eq!(None, conn.parameter("asdf"));
919919
}
920+
921+
#[test]
922+
fn test_get_bytes() {
923+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
924+
let stmt = or_panic!(conn.prepare("SELECT '\\x00010203'::BYTEA"));
925+
let mut result = or_panic!(stmt.query(&[]));
926+
assert_eq!(b"\x00\x01\x02\x03", result.next().unwrap().get_bytes(0).unwrap());
927+
}

0 commit comments

Comments
 (0)