Skip to content

Commit 0883918

Browse files
committed
Allow direct indexing of Rows.
1 parent 77649c5 commit 0883918

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/rows.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ impl<'stmt> Rows<'stmt> {
5050
self.stmt.columns()
5151
}
5252

53+
/// Returns the number of rows present.
54+
pub fn len(&self) -> usize {
55+
self.data.len()
56+
}
57+
58+
/// Returns a specific `Row`.
59+
///
60+
/// # Panics
61+
///
62+
/// Panics if `idx` is out of bounds.
63+
pub fn get<'a>(&'a self, idx: usize) -> Row<'a> {
64+
Row {
65+
stmt: self.stmt,
66+
data: Cow::Borrowed(&self.data[idx]),
67+
}
68+
}
69+
5370
/// Returns an iterator over the `Row`s.
5471
pub fn iter<'a>(&'a self) -> Iter<'a> {
5572
Iter {

tests/test.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,16 @@ fn test_transaction_isolation_level() {
892892
or_panic!(conn.set_transaction_isolation(IsolationLevel::ReadCommitted));
893893
assert_eq!(IsolationLevel::ReadCommitted, or_panic!(conn.transaction_isolation()));
894894
}
895+
896+
#[test]
897+
fn test_rows_index() {
898+
let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
899+
conn.batch_execute("
900+
CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY);
901+
INSERT INTO foo (id) VALUES (1), (2), (3);
902+
").unwrap();
903+
let stmt = conn.prepare("SELECT id FROM foo ORDER BY id").unwrap();
904+
let rows = stmt.query(&[]).unwrap();
905+
assert_eq!(3, rows.len());
906+
assert_eq!(2i32, rows.get(1).get(0));
907+
}

0 commit comments

Comments
 (0)