Skip to content

Commit c7d58e7

Browse files
committed
Make rows 1-indexed
1 parent a1da38b commit c7d58e7

3 files changed

Lines changed: 30 additions & 28 deletions

File tree

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ fn main() {
5252
let stmt = conn.prepare("SELECT id, name, time_created, data FROM person");
5353
for row in stmt.query([]) {
5454
let person = Person {
55-
id: row[0],
56-
name: row[1],
57-
time_created: row[2],
58-
data: row[3]
55+
id: row[1],
56+
name: row[2],
57+
time_created: row[3],
58+
data: row[4]
5959
};
6060
println!("Found person {}", person.name);
6161
}
@@ -107,12 +107,13 @@ let updates = stmt.update([&1i32 as &ToSql, & &"biz" as &ToSql]);
107107
println!("{} rows were updated", updates);
108108
```
109109
`query` returns an iterator over the rows returned from the database. The
110-
fields in a row can be accessed either by their indices or their column names.
111-
Unlike statement parameters, result columns are zero-indexed.
110+
fields in a row can be accessed either by their indices or their column names,
111+
though access by index is more efficient. Like statement parameters, result
112+
columns are one-indexed.
112113
```rust
113114
let stmt = conn.prepare("SELECT bar, baz FROM foo");
114115
for row in stmt.query([]) {
115-
let bar: i32 = row[0];
116+
let bar: i32 = row[1];
116117
let baz: ~str = row["baz"];
117118
println!("bar: {}, baz: {}", bar, baz);
118119
}

lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ fn main() {
4444
let stmt = conn.prepare("SELECT id, name, time_created, data FROM person");
4545
for row in stmt.query([]) {
4646
let person = Person {
47-
id: row[0],
48-
name: row[1],
49-
time_created: row[2],
50-
data: row[3]
47+
id: row[1],
48+
name: row[2],
49+
time_created: row[3],
50+
data: row[4]
5151
};
5252
println!("Found person {}", person.name);
5353
}
@@ -1159,10 +1159,10 @@ impl<'stmt> Iterator<PostgresRow<'stmt>> for PostgresResult<'stmt> {
11591159
/// A single result row of a query.
11601160
///
11611161
/// A value can be accessed by the name or index of its column, though access
1162-
/// by index is more efficient.
1162+
/// by index is more efficient. Rows are 1-indexed.
11631163
///
11641164
/// ```rust
1165-
/// let foo: i32 = row[0];
1165+
/// let foo: i32 = row[1];
11661166
/// let bar: ~str = row["bar"];
11671167
/// ```
11681168
pub struct PostgresRow<'stmt> {
@@ -1198,16 +1198,17 @@ pub trait RowIndex {
11981198
impl RowIndex for uint {
11991199
#[inline]
12001200
fn idx(&self, _stmt: &NormalPostgresStatement) -> uint {
1201-
*self
1201+
assert!(*self != 0, "out of bounds row access");
1202+
*self - 1
12021203
}
12031204
}
12041205

1205-
// This is a convenience as the 0 in get[0] resolves to int :(
1206+
// This is a convenience as the 1 in get[1] resolves to int :(
12061207
impl RowIndex for int {
12071208
#[inline]
12081209
fn idx(&self, _stmt: &NormalPostgresStatement) -> uint {
1209-
assert!(*self >= 0);
1210-
*self as uint
1210+
assert!(*self >= 1, "out of bounds row access");
1211+
(*self - 1) as uint
12111212
}
12121213
}
12131214

test.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ fn test_transaction_commit() {
106106
let stmt = conn.prepare("SELECT * FROM foo");
107107
let result = stmt.query([]);
108108

109-
assert_eq!(~[1i32], result.map(|row| { row[0] }).collect());
109+
assert_eq!(~[1i32], result.map(|row| { row[1] }).collect());
110110
}
111111

112112
#[test]
@@ -124,7 +124,7 @@ fn test_transaction_rollback() {
124124
let stmt = conn.prepare("SELECT * FROM foo");
125125
let result = stmt.query([]);
126126

127-
assert_eq!(~[1i32], result.map(|row| row[0]).collect());
127+
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
128128
}
129129

130130
#[test]
@@ -163,15 +163,15 @@ fn test_nested_transactions() {
163163
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
164164
let result = stmt.query([]);
165165

166-
assert_eq!(~[1i32, 2, 4, 6], result.map(|row| row[0]).collect());
166+
assert_eq!(~[1i32, 2, 4, 6], result.map(|row| row[1]).collect());
167167

168168
trans1.set_rollback();
169169
}
170170

171171
let stmt = conn.prepare("SELECT * FROM foo ORDER BY id");
172172
let result = stmt.query([]);
173173

174-
assert_eq!(~[1i32], result.map(|row| row[0]).collect());
174+
assert_eq!(~[1i32], result.map(|row| row[1]).collect());
175175
}
176176

177177
#[test]
@@ -183,7 +183,7 @@ fn test_query() {
183183
let stmt = conn.prepare("SELECT * from foo ORDER BY id");
184184
let result = stmt.query([]);
185185

186-
assert_eq!(~[1i64, 2], result.map(|row| row[0]).collect());
186+
assert_eq!(~[1i64, 2], result.map(|row| row[1]).collect());
187187
}
188188

189189
#[test]
@@ -201,7 +201,7 @@ fn test_lazy_query() {
201201

202202
let stmt = trans.prepare("SELECT id FROM foo ORDER BY id");
203203
let result = stmt.lazy_query(2, []);
204-
assert_eq!(values, result.map(|row| row[0]).collect());
204+
assert_eq!(values, result.map(|row| row[1]).collect());
205205

206206
trans.set_rollback();
207207
}
@@ -227,11 +227,11 @@ fn test_type<T: Eq+FromSql+ToSql>(sql_type: &str, checks: &[(T, &str)]) {
227227
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
228228
for &(ref val, ref repr) in checks.iter() {
229229
let stmt = conn.prepare("SELECT " + *repr + "::" + sql_type);
230-
let result = stmt.query([]).next().unwrap()[0];
230+
let result = stmt.query([]).next().unwrap()[1];
231231
assert_eq!(val, &result);
232232

233233
let stmt = conn.prepare("SELECT $1::" + sql_type);
234-
let result = stmt.query([val as &ToSql]).next().unwrap()[0];
234+
let result = stmt.query([val as &ToSql]).next().unwrap()[1];
235235
assert_eq!(val, &result);
236236
}
237237
}
@@ -309,7 +309,7 @@ fn test_bpchar_params() {
309309
let res = stmt.query([]);
310310

311311
assert_eq!(~[Some(~"12345"), Some(~"123 "), None],
312-
res.map(|row| row[0]).collect());
312+
res.map(|row| row[1]).collect());
313313
}
314314
315315
#[test]
@@ -438,13 +438,13 @@ fn test_nan_param<T: Float+ToSql+FromSql>(sql_type: &str) {
438438
let conn = PostgresConnection::connect("postgres://postgres@localhost", &NoSsl);
439439
let stmt = conn.prepare("SELECT 'NaN'::" + sql_type);
440440
let mut result = stmt.query([]);
441-
let val: T = result.next().unwrap()[0];
441+
let val: T = result.next().unwrap()[1];
442442
assert!(val.is_nan());
443443

444444
let nan: T = Float::nan();
445445
let stmt = conn.prepare("SELECT $1::" + sql_type);
446446
let mut result = stmt.query([&nan as &ToSql]);
447-
let val: T = result.next().unwrap()[0];
447+
let val: T = result.next().unwrap()[1];
448448
assert!(val.is_nan())
449449
}
450450

0 commit comments

Comments
 (0)