@@ -56,16 +56,9 @@ def __init__(self, connection_str: str = "", autocommit: bool = False, attrs_bef
5656 self .connection_str = self ._construct_connection_string (
5757 connection_str , ** kwargs
5858 )
59- # self._attrs_before = attrs_before
60- # self._autocommit = autocommit
61- # if self._attrs_before != {}:
62- # self._apply_attrs_before() # Apply pre-connection attributes
63- # if self._autocommit:
64- # self.setautocommit(autocommit)
65- print ("Connection string:" , self .connection_str )
66- self ._conn = ddbc_bindings .Connection (self .connection_str )
67- self ._conn .connect ()
68- print ("Connection established" )
59+ self ._attrs_before = attrs_before or {}
60+ self ._conn = ddbc_bindings .Connection (self .connection_str , autocommit )
61+ self ._conn .connect (self ._attrs_before )
6962 self ._autocommit = autocommit
7063 self .setautocommit (autocommit )
7164
@@ -107,90 +100,6 @@ def _construct_connection_string(self, connection_str: str = "", **kwargs) -> st
107100
108101 return conn_str
109102
110- # def _apply_attrs_before(self):
111- # """
112- # Apply specific pre-connection attributes.
113- # Currently, this method only processes an attribute with key 1256 (e.g., SQL_COPT_SS_ACCESS_TOKEN)
114- # if present in `self._attrs_before`. Other attributes are ignored.
115-
116- # Returns:
117- # bool: True.
118- # """
119-
120- # if ENABLE_LOGGING:
121- # logger.info("Attempting to apply pre-connection attributes (attrs_before): %s", self._attrs_before)
122-
123- # if not isinstance(self._attrs_before, dict):
124- # if self._attrs_before is not None and ENABLE_LOGGING:
125- # logger.warning(
126- # f"_attrs_before is of type {type(self._attrs_before).__name__}, "
127- # f"expected dict. Skipping attribute application."
128- # )
129- # elif self._attrs_before is None and ENABLE_LOGGING:
130- # logger.debug("_attrs_before is None. No pre-connection attributes to apply.")
131- # return True # Exit if _attrs_before is not a dictionary or is None
132-
133- # for key, value in self._attrs_before.items():
134- # ikey = None
135- # if isinstance(key, int):
136- # ikey = key
137- # elif isinstance(key, str) and key.isdigit():
138- # try:
139- # ikey = int(key)
140- # except ValueError:
141- # if ENABLE_LOGGING:
142- # logger.debug(
143- # f"Skipping attribute with key '{key}' in attrs_before: "
144- # f"could not convert string to int."
145- # )
146- # continue # Skip if string key is not a valid integer
147- # else:
148- # if ENABLE_LOGGING:
149- # logger.debug(
150- # f"Skipping attribute with key '{key}' in attrs_before due to "
151- # f"unsupported key type: {type(key).__name__}. Expected int or string representation of an int."
152- # )
153- # continue # Skip keys that are not int or string representation of an int
154-
155- # if ikey == ddbc_sql_const.SQL_COPT_SS_ACCESS_TOKEN.value:
156- # if ENABLE_LOGGING:
157- # logger.info(
158- # f"Found attribute {ddbc_sql_const.SQL_COPT_SS_ACCESS_TOKEN.value}. Attempting to set it."
159- # )
160- # self._conn.set_attribute(ikey, value)
161- # if ENABLE_LOGGING:
162- # logger.info(
163- # f"Call to set attribute {ddbc_sql_const.SQL_COPT_SS_ACCESS_TOKEN.value} with value '{value}' completed."
164- # )
165- # # If you expect only one such key, you could add 'break' here.
166- # else:
167- # if ENABLE_LOGGING:
168- # logger.debug(
169- # f"Ignoring attribute with key '{key}' (resolved to {ikey}) in attrs_before "
170- # f"as it is not the target attribute ({ddbc_sql_const.SQL_COPT_SS_ACCESS_TOKEN.value})."
171- # )
172- # return True
173-
174- # def _set_connection_attributes(self, ikey: int, ivalue: any) -> None:
175- # """
176- # Set the connection attributes before connecting.
177-
178- # Args:
179- # ikey (int): The attribute key to set.
180- # ivalue (Any): The value to set for the attribute. Can be bytes, bytearray, int, or unicode.
181- # vallen (int): The length of the value.
182-
183- # Raises:
184- # DatabaseError: If there is an error while setting the connection attribute.
185- # """
186-
187- # ret = ddbc_bindings.DDBCSQLSetConnectAttr(
188- # self.hdbc, # Connection handle
189- # ikey, # Attribute
190- # ivalue, # Value
191- # )
192- # check_error(ddbc_sql_const.SQL_HANDLE_DBC.value, self.hdbc, ret)
193-
194103 @property
195104 def autocommit (self ) -> bool :
196105 """
@@ -208,8 +117,6 @@ def autocommit(self, value: bool) -> None:
208117 value (bool): True to enable autocommit, False to disable it.
209118 Returns:
210119 None
211- Raises:
212- DatabaseError: If there is an error while setting the autocommit mode.
213120 """
214121 self .setautocommit (value )
215122 if ENABLE_LOGGING :
@@ -243,9 +150,6 @@ def cursor(self) -> Cursor:
243150 DatabaseError: If there is an error while creating the cursor.
244151 InterfaceError: If there is an error related to the database interface.
245152 """
246- # if self._is_closed():
247- # # Cannot create a cursor if the connection is closed
248- # raise Exception("Connection is closed. Cannot create cursor.")
249153 return Cursor (self )
250154
251155 def commit (self ) -> None :
@@ -260,10 +164,6 @@ def commit(self) -> None:
260164 Raises:
261165 DatabaseError: If there is an error while committing the transaction.
262166 """
263- if self ._is_closed ():
264- # Cannot commit if the connection is closed
265- raise Exception ("Connection is closed. Cannot commit." )
266-
267167 # Commit the current transaction
268168 self ._conn .commit ()
269169 if ENABLE_LOGGING :
@@ -280,10 +180,6 @@ def rollback(self) -> None:
280180 Raises:
281181 DatabaseError: If there is an error while rolling back the transaction.
282182 """
283- if self ._is_closed ():
284- # Cannot roll back if the connection is closed
285- raise Exception ("Connection is closed. Cannot roll back." )
286-
287183 # Roll back the current transaction
288184 self ._conn .rollback ()
289185 if ENABLE_LOGGING :
@@ -302,10 +198,7 @@ def close(self) -> None:
302198 Raises:
303199 DatabaseError: If there is an error while closing the connection.
304200 """
305- if self ._is_closed ():
306- # Connection is already closed
307- return
201+ # Close the connection
308202 self ._conn .close ()
309-
310203 if ENABLE_LOGGING :
311204 logger .info ("Connection closed successfully." )
0 commit comments