Skip to content

Commit 75641e1

Browse files
committed
Allow statements to be prepared on any object
Statements aren't cleaned up at the end of a transaction, so this is safe.
1 parent 348195b commit 75641e1

3 files changed

Lines changed: 30 additions & 25 deletions

File tree

src/error.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,8 @@ pub enum Error {
614614
InvalidColumn,
615615
/// A value was NULL but converted to a non-nullable Rust type
616616
WasNull,
617-
/// An attempt was made to prepare a statement or start a transaction on an
618-
/// object other than the active transaction
617+
/// An attempt was made to start a transaction or execute a lazy query on
618+
/// an object other than the active transaction
619619
WrongTransaction,
620620
/// The server returned an unexpected response
621621
BadResponse,
@@ -639,8 +639,8 @@ impl fmt::Show for Error {
639639
Error::InvalidColumn => write!(fmt, "Invalid column"),
640640
Error::WasNull => write!(fmt, "The value was NULL"),
641641
Error::WrongTransaction =>
642-
write!(fmt, "An attempt was made to prepare a statement or start a transaction on \
643-
an object other than the active transaction"),
642+
write!(fmt, "An attempt was made to start a transaction or execute a lazy query \
643+
on an object other than the active transaction"),
644644
Error::BadResponse =>
645645
write!(fmt, "The server returned an unexpected response"),
646646
Error::BadData =>
@@ -665,7 +665,8 @@ impl error::Error for Error {
665665
Error::InvalidColumn => "Invalid column",
666666
Error::WasNull => "The value was NULL",
667667
Error::WrongTransaction => {
668-
"An attempt was made to use an object other than the active transaction"
668+
"An attempt was made to start a transaction or execute a lazy query on an object \
669+
other than the active transaction"
669670
}
670671
Error::BadResponse => "The server returned an unexpected response",
671672
Error::BadData => "The server provided data that the client could not parse",

src/lib.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -979,22 +979,14 @@ impl<'conn> Transaction<'conn> {
979979
}
980980

981981
/// Like `Connection::prepare`.
982-
pub fn prepare<'a>(&'a self, query: &str) -> Result<Statement<'a>> {
983-
let mut conn = self.conn.conn.borrow_mut();
984-
if conn.trans_depth != self.depth {
985-
return Err(Error::WrongTransaction);
986-
}
987-
conn.prepare(query, self.conn)
982+
pub fn prepare(&self, query: &str) -> Result<Statement<'conn>> {
983+
self.conn.conn.borrow_mut().prepare(query, self.conn)
988984
}
989985

990986
/// Like `Connection::prepare_copy_in`.
991-
pub fn prepare_copy_in<'a>(&'a self, table: &str, cols: &[&str])
992-
-> Result<CopyInStatement<'a>> {
993-
let mut conn = self.conn.conn.borrow_mut();
994-
if conn.trans_depth != self.depth {
995-
return Err(Error::WrongTransaction);
996-
}
997-
conn.prepare_copy_in(table, cols, self.conn)
987+
pub fn prepare_copy_in(&self, table: &str, cols: &[&str])
988+
-> Result<CopyInStatement<'conn>> {
989+
self.conn.conn.borrow_mut().prepare_copy_in(table, cols, self.conn)
998990
}
999991

1000992
/// Like `Connection::execute`.
@@ -1004,11 +996,7 @@ impl<'conn> Transaction<'conn> {
1004996

1005997
/// Like `Connection::batch_execute`.
1006998
pub fn batch_execute(&self, query: &str) -> Result<()> {
1007-
let mut conn = self.conn.conn.borrow_mut();
1008-
if conn.trans_depth != self.depth {
1009-
return Err(Error::WrongTransaction);
1010-
}
1011-
conn.quick_query(query).map(|_| ())
999+
self.conn.conn.borrow_mut().quick_query(query).map(|_| ())
10121000
}
10131001

10141002
/// Like `Connection::transaction`.
@@ -1043,7 +1031,12 @@ impl<'conn> Transaction<'conn> {
10431031
if self.conn as *const _ != stmt.conn as *const _ {
10441032
return Err(Error::WrongConnection);
10451033
}
1046-
check_desync!(self.conn);
1034+
let conn = self.conn.conn.borrow();
1035+
check_desync!(conn);
1036+
if conn.trans_depth != self.depth {
1037+
return Err(Error::WrongTransaction);
1038+
}
1039+
drop(conn);
10471040
stmt.lazy_query(row_limit, params).map(|result| {
10481041
LazyRows {
10491042
_trans: self,

tests/test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ fn test_trans_prepare_with_nested_trans() {
298298
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
299299
let trans = or_panic!(conn.transaction());
300300
let _trans2 = or_panic!(trans.transaction());
301-
match trans.prepare("") {
301+
let stmt = or_panic!(trans.prepare("SELECT 1"));
302+
match trans.lazy_query(&stmt, &[], 10) {
302303
Err(Error::WrongTransaction) => {}
303304
Err(r) => panic!("Unexpected error {}", r),
304305
Ok(_) => panic!("Unexpected success"),
@@ -310,6 +311,16 @@ fn test_trans_prepare_with_nested_trans() {
310311
}
311312
}
312313

314+
#[test]
315+
fn test_stmt_execute_after_transaction() {
316+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
317+
let trans = or_panic!(conn.transaction());
318+
let stmt = or_panic!(trans.prepare("SELECT 1"));
319+
or_panic!(trans.finish());
320+
let mut result = or_panic!(stmt.query(&[]));
321+
assert_eq!(1i32, result.next().unwrap().get(0));
322+
}
323+
313324
#[test]
314325
fn test_stmt_finish() {
315326
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));

0 commit comments

Comments
 (0)