Allow direct indexing of Rows.

This commit is contained in:
Steven Fackler 2015-06-07 09:15:52 -07:00
parent 77649c5191
commit 0883918f06
2 changed files with 30 additions and 0 deletions

View File

@ -50,6 +50,23 @@ impl<'stmt> Rows<'stmt> {
self.stmt.columns()
}
/// Returns the number of rows present.
pub fn len(&self) -> usize {
self.data.len()
}
/// Returns a specific `Row`.
///
/// # Panics
///
/// Panics if `idx` is out of bounds.
pub fn get<'a>(&'a self, idx: usize) -> Row<'a> {
Row {
stmt: self.stmt,
data: Cow::Borrowed(&self.data[idx]),
}
}
/// Returns an iterator over the `Row`s.
pub fn iter<'a>(&'a self) -> Iter<'a> {
Iter {

View File

@ -892,3 +892,16 @@ fn test_transaction_isolation_level() {
or_panic!(conn.set_transaction_isolation(IsolationLevel::ReadCommitted));
assert_eq!(IsolationLevel::ReadCommitted, or_panic!(conn.transaction_isolation()));
}
#[test]
fn test_rows_index() {
let conn = Connection::connect("postgres://postgres@localhost", &SslMode::None).unwrap();
conn.batch_execute("
CREATE TEMPORARY TABLE foo (id INT PRIMARY KEY);
INSERT INTO foo (id) VALUES (1), (2), (3);
").unwrap();
let stmt = conn.prepare("SELECT id FROM foo ORDER BY id").unwrap();
let rows = stmt.query(&[]).unwrap();
assert_eq!(3, rows.len());
assert_eq!(2i32, rows.get(1).get(0));
}