SQL Microsoft¶
MsSqlHandler(connection_properties, max_retries=8, max_conn_retries=3, retry_wait_time=30, exponential_min_retry_wait_time=0.1, exponential_multiplier=1, skip_connect=True, **kwargs)
¶
Subclass of SqlHandler used for Microsoft SQL Server.
Source code in echo_connhandler/sql_core.py
def __init__(
self,
connection_properties: SqlConnProperties,
max_retries: int = 8,
max_conn_retries: int = 3,
retry_wait_time: float = 30,
exponential_min_retry_wait_time: float = 0.1,
exponential_multiplier: float = 1,
skip_connect: bool = True,
**kwargs, # pylint: disable=unused-argument # noqa
) -> None:
"""Method that initializes the SQL handler.
This already connects to the SQL server.
This handler implements a robust retry strategy using exponential backoff to gracefully manage connection or query failures. By default, operations are attempted up to 8 times; between failures, the wait time doubles exponentially—starting at 0.1 second (0.1s, 0.2s, 0.4s, 0.8s, 1.6s, 3.2s, 6.4s, 12.8s )—until it reaches a configured ceiling of 30 seconds. This approach allows for rapid recovery from momentary glitches while preventing server overload during persistent outages.
Using the default parameters the connection will fail after around 90 seconds if the server is not reachable.
Parameters
----------
connection_properties : SqlConnProperties
Object containing connection parameters.
max_retries : int, optional
Number of retries that will be attempted when doing queries. Will be used in `stop` parameter of tenacity.stop_after_attempt, by default 8
max_conn_retries : int, optional
Number of retries that will be attempted when reconnecting. Will be used in `stop` parameter of tenacity.stop_after_attempt, by default 3
exponential_min_retry_wait_time: float, optional
Min time to wait between retries when reconnecting or doing queries. Will be used in `min` parameter of tenacity.wait_exponential, by default 0.1
exponential_multiplier: float, optional
Multiplier to use when calculating wait time between retries when reconnecting or doing queries. Will be used in `multiplier` parameter of tenacity.wait_exponential, by default 1
skip_connect : bool, optional
If True, the connection will not be established when the object is created.
If this is set toTrue, the user will need to manually call the reconnect() method when they want to connect to the server.
By default True
**kwargs : dict, optional
Just kept here for compatibility.
"""
super().__init__(
connection_properties=connection_properties,
max_retries=max_retries,
max_conn_retries=max_conn_retries,
retry_wait_time=retry_wait_time,
exponential_min_retry_wait_time=exponential_min_retry_wait_time,
exponential_multiplier=exponential_multiplier,
skip_connect=skip_connect,
)
self._last_cursor: Any = None
self._adbc_connection: Any = None
self._suppress_auto_close: bool = False
table_exists(schema, table_name)
¶
Method to check if a table or view exists in the MS SQL Server database.
Parameters:
-
(schema¶str | None) –Name of the schema where the table should be located.
-
(table_name¶str) –Name of the table to check.
Returns:
-
bool–True if the table exists, False otherwise.
Source code in echo_connhandler/sql_ms.py
@validate_call
def table_exists(self, schema: str | None, table_name: str) -> bool:
"""Method to check if a table or view exists in the MS SQL Server database.
Parameters
----------
schema : str | None
Name of the schema where the table should be located.
table_name : str
Name of the table to check.
Returns
-------
bool
True if the table exists, False otherwise.
"""
if not isinstance(schema, str | type(None)):
raise TypeError(f"schema should be a string or None, not {type(schema)}")
if not isinstance(table_name, str):
raise TypeError(f"table_name should be a string, not {type(table_name)}")
_prev_suppress = self._suppress_auto_close
self._suppress_auto_close = True
try:
# Default schema in SQL Server is "dbo" when not specified
target_schema = schema or "dbo"
# Very fast query to check for table existence, should be much faster than fetching all table names from the schema
query = f"""
SELECT 1
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '{target_schema}'
AND TABLE_NAME = '{table_name}'
""" # noqa: S608
with self.cursor() as cur:
cur.execute(query)
result = cur.fetchone()
return result is not None
finally:
self._suppress_auto_close = _prev_suppress
if not _prev_suppress:
self._auto_close_if_needed()