Skip to content

Commit b13de8f

Browse files
jahnvi480bewithgaurav
authored andcommitted
Merged PR 5213: Adding autocommit function
Adding autocommit function ---- #### AI description (iteration 1) #### PR Classification New feature #### PR Summary This pull request adds an autocommit function to the database connection class. - `mssql_python/connection.py`: Added `autocommit` property to get the current autocommit mode and `setautocommit` method to set the autocommit mode. Updated the constructor to accept an `autocommit` parameter and initialize it. Related work items: #33433
1 parent 3a71a3f commit b13de8f

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

mssql_python/connection.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Connection:
3232
close() -> None:
3333
"""
3434

35-
def __init__(self, connection_str: str) -> None:
35+
def __init__(self, connection_str: str, autocommit: bool = True) -> None:
3636
"""
3737
Initialize the connection object with the specified connection string.
3838
@@ -52,6 +52,8 @@ def __init__(self, connection_str: str) -> None:
5252
self.hdbc = ctypes.c_void_p()
5353
self.connection_str = add_driver_to_connection_str(connection_str)
5454
self._initializer()
55+
self._autocommit = autocommit
56+
self.setautocommit(autocommit)
5557

5658
def _initializer(self) -> None:
5759
"""
@@ -126,6 +128,49 @@ def _connect_to_db(self) -> None:
126128
logging.error("An error occurred while connecting to the database: %s", e)
127129
raise
128130

131+
@property
132+
def autocommit(self) -> bool:
133+
"""
134+
Return the current autocommit mode of the connection.
135+
Returns:
136+
bool: True if autocommit is enabled, False otherwise.
137+
"""
138+
try:
139+
autocommit_mode = ddbc_bindings.DDBCSQLGetConnectionAttr(
140+
self.hdbc.value, # Connection handle
141+
odbc_sql_const.SQL_ATTR_AUTOCOMMIT.value, # Attribute
142+
)
143+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, autocommit_mode)
144+
return autocommit_mode == odbc_sql_const.SQL_AUTOCOMMIT_ON.value
145+
except Exception as e:
146+
logging.error("An error occurred while getting autocommit mode: %s", e)
147+
raise Exception("DatabaseError: Failed to get autocommit mode") from e
148+
149+
def setautocommit(self, value: bool) -> None:
150+
"""
151+
Set the autocommit mode of the connection.
152+
Args:
153+
value (bool): True to enable autocommit, False to disable it.
154+
Returns:
155+
None
156+
Raises:
157+
DatabaseError: If there is an error while setting the autocommit mode.
158+
"""
159+
try:
160+
ret = ddbc_bindings.DDBCSQLSetConnectAttr(
161+
self.hdbc.value, # Connection handle
162+
odbc_sql_const.SQL_ATTR_AUTOCOMMIT.value, # Attribute
163+
odbc_sql_const.SQL_AUTOCOMMIT_ON.value if value else odbc_sql_const.SQL_AUTOCOMMIT_OFF.value, # Value
164+
0 # String length
165+
)
166+
check_error(odbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc.value, ret)
167+
self._autocommit = value
168+
logging.info("Autocommit mode set to %s.", value)
169+
except Exception as e:
170+
logging.error("An error occurred while setting autocommit mode: %s", e)
171+
raise Exception("DatabaseError: Failed to set autocommit mode") from e
172+
173+
129174
def cursor(self) -> Cursor:
130175
"""
131176
Return a new Cursor object using the connection.

mssql_python/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ class ConstantsODBC(Enum):
2323
SQL_OV_ODBC3 = 3
2424
SQL_COMMIT = 0
2525
SQL_ROLLBACK = 1
26+
SQL_ATTR_AUTOCOMMIT = 102
27+
SQL_AUTOCOMMIT_ON = 1
28+
SQL_AUTOCOMMIT_OFF = 0

tests/test_connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def test_commit(db_connection):
4040
# finally:
4141
# cursor.execute("DROP TABLE test_commit;")
4242

43+
def test_autocommit(db_connection):
44+
assert db_connection.autocommit is True, "Autocommit should be True by default"
45+
db_connection.setautocommit(False)
46+
assert db_connection.autocommit is False, "Autocommit failed: Autocommit should be False"
47+
4348
def test_rollback(db_connection):
4449
db_connection.rollback()
4550
# # Make a transaction and rollback

0 commit comments

Comments
 (0)