Skip to content

Commit af60848

Browse files
committed
Add an is_active method
Will be needed when Error::WrongTransaction is removed
1 parent 309b7b0 commit af60848

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,15 @@ impl Connection {
11091109
self.conn.borrow().is_desynchronized()
11101110
}
11111111

1112+
/// Determines if the `Connection` is currently "active", that is, if there
1113+
/// are no active transactions.
1114+
///
1115+
/// The `transaction` method can only be called on the active `Connection`
1116+
/// or `Transaction`.
1117+
pub fn is_active(&self) -> bool {
1118+
self.conn.borrow().trans_depth == 0
1119+
}
1120+
11121121
/// Consumes the connection, closing it.
11131122
///
11141123
/// Functionally equivalent to the `Drop` implementation for `Connection`
@@ -1234,6 +1243,11 @@ impl<'conn> Transaction<'conn> {
12341243
stmt.lazy_query(self, params, row_limit)
12351244
}
12361245

1246+
/// Like `Connection::is_active`.
1247+
pub fn is_active(&self) -> bool {
1248+
self.conn.conn.borrow().trans_depth == self.depth
1249+
}
1250+
12371251
/// Determines if the transaction is currently set to commit or roll back.
12381252
pub fn will_commit(&self) -> bool {
12391253
self.commit.get()
@@ -1989,6 +2003,9 @@ pub trait GenericConnection {
19892003

19902004
/// Like `Connection::batch_execute`.
19912005
fn batch_execute(&self, query: &str) -> Result<()>;
2006+
2007+
/// Like `Connection::is_active`.
2008+
fn is_active(&self) -> bool;
19922009
}
19932010

19942011
impl GenericConnection for Connection {
@@ -2016,6 +2033,10 @@ impl GenericConnection for Connection {
20162033
fn batch_execute(&self, query: &str) -> Result<()> {
20172034
self.batch_execute(query)
20182035
}
2036+
2037+
fn is_active(&self) -> bool {
2038+
self.is_active()
2039+
}
20192040
}
20202041

20212042
impl<'a> GenericConnection for Transaction<'a> {
@@ -2043,4 +2064,8 @@ impl<'a> GenericConnection for Transaction<'a> {
20432064
fn batch_execute(&self, query: &str) -> Result<()> {
20442065
self.batch_execute(query)
20452066
}
2067+
2068+
fn is_active(&self) -> bool {
2069+
self.is_active()
2070+
}
20462071
}

tests/test.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,3 +916,23 @@ fn test_prepare_cached() {
916916
assert_eq!(&[2, 1][], or_panic!(stmt.query(&[])).map(|r| r.get(0)).collect::<Vec<i32>>());
917917
or_panic!(stmt.finish());
918918
}
919+
920+
#[test]
921+
fn test_is_active() {
922+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
923+
assert!(conn.is_active());
924+
let trans = or_panic!(conn.transaction());
925+
assert!(!conn.is_active());
926+
assert!(trans.is_active());
927+
{
928+
let trans2 = or_panic!(trans.transaction());
929+
assert!(!conn.is_active());
930+
assert!(!trans.is_active());
931+
assert!(trans2.is_active());
932+
or_panic!(trans2.finish());
933+
}
934+
assert!(!conn.is_active());
935+
assert!(trans.is_active());
936+
or_panic!(trans.finish());
937+
assert!(conn.is_active());
938+
}

0 commit comments

Comments
 (0)