Make rows 1-indexed

This commit is contained in:
Steven Fackler 2013-12-02 19:53:40 -08:00
parent a1da38b50e
commit c7d58e73c4
3 changed files with 30 additions and 28 deletions

View File

@ -52,10 +52,10 @@ fn main() {
let stmt = conn.prepare("SELECT id, name, time_created, data FROM person");
for row in stmt.query([]) {
let person = Person {
id: row[0],
name: row[1],
time_created: row[2],
data: row[3]
id: row[1],
name: row[2],
time_created: row[3],
data: row[4]
};
println!("Found person {}", person.name);
}
@ -107,12 +107,13 @@ let updates = stmt.update([&1i32 as &ToSql, & &"biz" as &ToSql]);
println!("{} rows were updated", updates);
```
`query` returns an iterator over the rows returned from the database. The
fields in a row can be accessed either by their indices or their column names.
Unlike statement parameters, result columns are zero-indexed.
fields in a row can be accessed either by their indices or their column names,
though access by index is more efficient. Like statement parameters, result
columns are one-indexed.
```rust
let stmt = conn.prepare("SELECT bar, baz FROM foo");
for row in stmt.query([]) {
let bar: i32 = row[0];
let bar: i32 = row[1];
let baz: ~str = row["baz"];
println!("bar: {}, baz: {}", bar, baz);
}

21
lib.rs
View File

@ -44,10 +44,10 @@ fn main() {
let stmt = conn.prepare("SELECT id, name, time_created, data FROM person");
for row in stmt.query([]) {
let person = Person {
id: row[0],
name: row[1],
time_created: row[2],
data: row[3]
id: row[1],
name: row[2],
time_created: row[3],
data: row[4]
};
println!("Found person {}", person.name);
}
@ -1159,10 +1159,10 @@ impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresResult<'stmt> {
/// A single result row of a query.
///
/// A value can be accessed by the name or index of its column, though access
/// by index is more efficient.
/// by index is more efficient. Rows are 1-indexed.
///
/// ```rust
/// let foo: i32 = row[0];
/// let foo: i32 = row[1];
/// let bar: ~str = row["bar"];
/// ```
pub struct PostgresRow<'stmt> {
@ -1198,16 +1198,17 @@ pub trait RowIndex {
impl RowIndex for uint {
#[inline]
fn idx(&self, _stmt: &NormalPostgresStatement) -> uint {
*self
assert!(*self != 0, "out of bounds row access");
*self - 1
}
}
// This is a convenience as the 0 in get[0] resolves to int :(
// This is a convenience as the 1 in get[1] resolves to int :(
impl RowIndex for int {
#[inline]
fn idx(&self, _stmt: &NormalPostgresStatement) -> uint {
assert!(*self >= 0);
*self as uint
assert!(*self >= 1, "out of bounds row access");
(*self - 1) as uint
}
}

22
test.rs
View File

@ -106,7 +106,7 @@ fn test_transaction_commit() {
let stmt = conn.prepare("SELECT * FROM foo");
let result = stmt.query([]);
assert_eq!(~[1i32], result.map(|row| { row[0] }).collect());
assert_eq!(~[1i32], result.map(|row| { row[1] }).collect());
}
#[test]
@ -124,7 +124,7 @@ fn test_transaction_rollback() {
let stmt = conn.prepare("SELECT * FROM foo");
let result = stmt.query([]);
assert_eq!(~[1i32], result.map(|row| row[0]).collect());
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
}
#[test]
@ -163,7 +163,7 @@ fn test_nested_transactions() {
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
let result = stmt.query([]);
assert_eq!(~[1i32, 2, 4, 6], result.map(|row| row[0]).collect());
assert_eq!(~[1i32, 2, 4, 6], result.map(|row| row[1]).collect());
trans1.set_rollback();
}
@ -171,7 +171,7 @@ fn test_nested_transactions() {
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
let result = stmt.query([]);
assert_eq!(~[1i32], result.map(|row| row[0]).collect());
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
}
#[test]
@ -183,7 +183,7 @@ fn test_query() {
let stmt = conn.prepare("SELECT * from foo ORDER BY id");
let result = stmt.query([]);
assert_eq!(~[1i64, 2], result.map(|row| row[0]).collect());
assert_eq!(~[1i64, 2], result.map(|row| row[1]).collect());
}
#[test]
@ -201,7 +201,7 @@ fn test_lazy_query() {
let stmt = trans.prepare("SELECT id FROM foo ORDER BY id");
let result = stmt.lazy_query(2, []);
assert_eq!(values, result.map(|row| row[0]).collect());
assert_eq!(values, result.map(|row| row[1]).collect());
trans.set_rollback();
}
@ -227,11 +227,11 @@ fn test_type<T: Eq+FromSql+ToSql>(sql_type: &str, checks: &[(T, &str)]) {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
for &(ref val, ref repr) in checks.iter() {
let stmt = conn.prepare("SELECT " + *repr + "::" + sql_type);
let result = stmt.query([]).next().unwrap()[0];
let result = stmt.query([]).next().unwrap()[1];
assert_eq!(val, &result);
let stmt = conn.prepare("SELECT $1::" + sql_type);
let result = stmt.query([val as &ToSql]).next().unwrap()[0];
let result = stmt.query([val as &ToSql]).next().unwrap()[1];
assert_eq!(val, &result);
}
}
@ -309,7 +309,7 @@ fn test_bpchar_params() {
let res = stmt.query([]);
assert_eq!(~[Some(~"12345"), Some(~"123 "), None],
res.map(|row| row[0]).collect());
res.map(|row| row[1]).collect());
}
#[test]
@ -438,13 +438,13 @@ fn test_nan_param<T: Float+ToSql+FromSql>(sql_type: &str) {
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
let stmt = conn.prepare("SELECT 'NaN'::" + sql_type);
let mut result = stmt.query([]);
let val: T = result.next().unwrap()[0];
let val: T = result.next().unwrap()[1];
assert!(val.is_nan());
let nan: T = Float::nan();
let stmt = conn.prepare("SELECT $1::" + sql_type);
let mut result = stmt.query([&nan as &ToSql]);
let val: T = result.next().unwrap()[0];
let val: T = result.next().unwrap()[1];
assert!(val.is_nan())
}