Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions odbc-api/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,16 @@ impl<'env> Connection<'env> {
.into_result(&self.connection)
}

/// `true` if the connection is in autocommit mode, `false` if it is in manual-commit mode. New
/// connections start in autocommit mode. See [`Connection::set_autocommit`].
///
/// This corresponds to the `SQL_ATTR_AUTOCOMMIT` attribute in the ODBC specification.
pub fn is_autocommit(&self) -> Result<bool, Error> {
self.connection
.is_autocommit()
.into_result(&self.connection)
}

/// To commit a transaction in manual-commit mode.
pub fn commit(&self) -> Result<(), Error> {
self.connection.commit().into_result(&self.connection)
Expand Down
20 changes: 19 additions & 1 deletion odbc-api/src/handles/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,25 @@ impl Connection<'_> {
.map(|v| match v {
0 => false,
1 => true,
other => panic!("Unexpected result value from SQLGetConnectAttr: {other}"),
other => panic!(
"Unexpected result value retrieving SQL_ATTR_CONNECTION_DEAD via \
SQLGetConnectAttr: {other}"
),
})
}
}

/// `true` if the connection is in autocommit mode, `false` if it is in manual-commit mode.
pub fn is_autocommit(&self) -> SqlResult<bool> {
unsafe {
self.attribute_u32(ConnectionAttribute::AUTOCOMMIT)
.map(|v| match v {
0 => false,
1 => true,
other => panic!(
"Unexpected result value retrieving SQL_ATTR_AUTOCOMMIT via \
SQLGetConnectAttr: {other}"
),
})
}
}
Expand Down
21 changes: 21 additions & 0 deletions odbc-api/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,27 @@ fn manual_commit_mode(profile: &Profile) {
conn.commit().unwrap();
}

#[test_case(MSSQL; "Microsoft SQL Server")]
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
#[test_case(POSTGRES; "PostgreSQL")]
fn is_autocommit_is_true_for_new_connection(profile: &Profile) {
let conn = profile.connection().unwrap();

assert!(conn.is_autocommit().unwrap());
}

#[test_case(MSSQL; "Microsoft SQL Server")]
#[test_case(MARIADB; "Maria DB")]
#[test_case(SQLITE_3; "SQLite 3")]
#[test_case(POSTGRES; "PostgreSQL")]
fn is_autocommit_is_false_after_disabling_autocommit(profile: &Profile) {
let conn = profile.connection().unwrap();
conn.set_autocommit(false).unwrap();

assert!(!conn.is_autocommit().unwrap());
}

/// This test checks the behaviour if a connections goes out of scope with a transaction still
/// open.
#[test_case(MSSQL; "Microsoft SQL Server")]
Expand Down
Loading