diff --git a/src/rows.rs b/src/rows.rs index 36148db5..1728ff4c 100644 --- a/src/rows.rs +++ b/src/rows.rs @@ -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 { diff --git a/tests/test.rs b/tests/test.rs index 8018aee8..e0fbbb79 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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)); +}