Skip to content

Commit 5cce517

Browse files
committed
Move lazy_query to Statement
Symmetry is good
1 parent ee7f9ee commit 5cce517

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

src/lib.rs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,33 +1227,14 @@ impl<'conn> Transaction<'conn> {
12271227
})
12281228
}
12291229

1230-
/// Executes a prepared statement, returning a lazily loaded iterator over
1231-
/// the resulting rows.
1232-
///
1233-
/// No more than `row_limit` rows will be stored in memory at a time. Rows
1234-
/// will be pulled from the database in batches of `row_limit` as needed.
1235-
/// If `row_limit` is less than or equal to 0, `lazy_query` is equivalent
1236-
/// to `query`.
1230+
#[deprecated = "call `lazy_query` on Statement instead"]
1231+
#[allow(missing_docs)]
12371232
pub fn lazy_query<'trans, 'stmt>(&'trans self,
12381233
stmt: &'stmt Statement,
12391234
params: &[&ToSql],
12401235
row_limit: i32)
12411236
-> Result<LazyRows<'trans, 'stmt>> {
1242-
if self.conn as *const _ != stmt.conn as *const _ {
1243-
return Err(Error::WrongConnection);
1244-
}
1245-
let conn = self.conn.conn.borrow();
1246-
check_desync!(conn);
1247-
if conn.trans_depth != self.depth {
1248-
return Err(Error::WrongTransaction);
1249-
}
1250-
drop(conn);
1251-
stmt.lazy_query(row_limit, params).map(|result| {
1252-
LazyRows {
1253-
_trans: self,
1254-
result: result
1255-
}
1256-
})
1237+
stmt.lazy_query(self, params, row_limit)
12571238
}
12581239

12591240
/// Determines if the transaction is currently set to commit or roll back.
@@ -1368,7 +1349,7 @@ impl<'conn> Statement<'conn> {
13681349
}
13691350
}
13701351

1371-
fn lazy_query<'a>(&'a self, row_limit: i32, params: &[&ToSql]) -> Result<Rows<'a>> {
1352+
fn inner_lazy_query<'a>(&'a self, row_limit: i32, params: &[&ToSql]) -> Result<Rows<'a>> {
13721353
let id = self.next_portal_id.get();
13731354
self.next_portal_id.set(id + 1);
13741355
let portal_name = format!("{}p{}", self.name, id);
@@ -1475,7 +1456,40 @@ impl<'conn> Statement<'conn> {
14751456
/// ```
14761457
pub fn query<'a>(&'a self, params: &[&ToSql]) -> Result<Rows<'a>> {
14771458
check_desync!(self.conn);
1478-
self.lazy_query(0, params)
1459+
self.inner_lazy_query(0, params)
1460+
}
1461+
1462+
/// Executes the prepared statement, returning a lazily loaded iterator
1463+
/// over the resulting rows.
1464+
///
1465+
/// No more than `row_limit` rows will be stored in memory at a time. Rows
1466+
/// will be pulled from the database in batches of `row_limit` as needed.
1467+
/// If `row_limit` is less than or equal to 0, `lazy_query` is equivalent
1468+
/// to `query`.
1469+
///
1470+
/// This can only be called inside of a transaction, and the `Transaction`
1471+
/// object representing the active transaction must be passed to
1472+
/// `lazy_query`.
1473+
pub fn lazy_query<'trans, 'stmt>(&'stmt self,
1474+
trans: &'trans Transaction,
1475+
params: &[&ToSql],
1476+
row_limit: i32)
1477+
-> Result<LazyRows<'trans, 'stmt>> {
1478+
if self.conn as *const _ != trans.conn as *const _ {
1479+
return Err(Error::WrongConnection);
1480+
}
1481+
let conn = self.conn.conn.borrow();
1482+
check_desync!(conn);
1483+
if conn.trans_depth != trans.depth {
1484+
return Err(Error::WrongTransaction);
1485+
}
1486+
drop(conn);
1487+
self.inner_lazy_query(row_limit, params).map(|result| {
1488+
LazyRows {
1489+
_trans: trans,
1490+
result: result
1491+
}
1492+
})
14791493
}
14801494

14811495
/// Consumes the statement, clearing it from the Postgres session.

tests/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ fn test_trans_prepare_with_nested_trans() {
297297
let trans = or_panic!(conn.transaction());
298298
let _trans2 = or_panic!(trans.transaction());
299299
let stmt = or_panic!(trans.prepare("SELECT 1"));
300-
match trans.lazy_query(&stmt, &[], 10) {
300+
match stmt.lazy_query(&trans, &[], 10) {
301301
Err(Error::WrongTransaction) => {}
302302
Err(r) => panic!("Unexpected error {:?}", r),
303303
Ok(_) => panic!("Unexpected success"),
@@ -420,7 +420,7 @@ fn test_lazy_query() {
420420
or_panic!(stmt.execute(&[value]));
421421
}
422422
let stmt = or_panic!(trans.prepare("SELECT id FROM foo ORDER BY id"));
423-
let result = or_panic!(trans.lazy_query(&stmt, &[], 2));
423+
let result = or_panic!(stmt.lazy_query(&trans, &[], 2));
424424
assert_eq!(values, result.map(|row| row.unwrap().get(0)).collect::<Vec<_>>());
425425
}
426426

@@ -431,7 +431,7 @@ fn test_lazy_query_wrong_conn() {
431431

432432
let trans = or_panic!(conn1.transaction());
433433
let stmt = or_panic!(conn2.prepare("SELECT 1::INT"));
434-
match trans.lazy_query(&stmt, &[], 1) {
434+
match stmt.lazy_query(&trans, &[], 1) {
435435
Err(Error::WrongConnection) => {}
436436
Err(err) => panic!("Unexpected error {:?}", err),
437437
Ok(_) => panic!("Expected failure")

0 commit comments

Comments
 (0)