1616# normalize_architecture import removed as it's unused
1717
1818
19- def add_driver_to_connection_str (connection_str : str ) -> str :
20- """
21- Add the DDBC driver to the connection string if not present.
22-
23- Args:
24- connection_str (str): The original connection string.
25-
26-
27- Returns:
28- str: The connection string with the DDBC driver added.
29-
30- Raises:
31- Exception: If the connection string is invalid.
32- """
33- logger .debug (
34- "add_driver_to_connection_str: Processing connection string (length=%d)" ,
35- len (connection_str ),
36- )
37- driver_name = "Driver={ODBC Driver 18 for SQL Server}"
38- try :
39- # Strip any leading or trailing whitespace from the connection string
40- connection_str = connection_str .strip ()
41- connection_str = add_driver_name_to_app_parameter (connection_str )
42-
43- # Split the connection string into individual attributes
44- connection_attributes = connection_str .split (";" )
45- final_connection_attributes = []
46-
47- # Iterate through the attributes and exclude any existing driver attribute
48- driver_found = False
49- for attribute in connection_attributes :
50- if attribute .lower ().split ("=" )[0 ] == "driver" :
51- driver_found = True
52- logger .debug (
53- "add_driver_to_connection_str: Existing driver attribute found, removing"
54- )
55- continue
56- final_connection_attributes .append (attribute )
57-
58- # Join the remaining attributes back into a connection string
59- connection_str = ";" .join (final_connection_attributes )
60-
61- # Insert the driver attribute at the beginning of the connection string
62- final_connection_attributes .insert (0 , driver_name )
63- connection_str = ";" .join (final_connection_attributes )
64- logger .debug (
65- "add_driver_to_connection_str: Driver added (had_existing=%s, attr_count=%d)" ,
66- str (driver_found ),
67- len (final_connection_attributes ),
68- )
69-
70- except Exception as e :
71- logger .debug (
72- "add_driver_to_connection_str: Failed to process connection string - %s" , str (e )
73- )
74- raise ValueError (
75- "Invalid connection string, Please follow the format: "
76- "Server=server_name;Database=database_name;UID=user_name;PWD=password"
77- ) from e
78-
79- return connection_str
80-
81-
8219def check_error (handle_type : int , handle : Any , ret : int ) -> None :
8320 """
8421 Check for errors and raise an exception if an error is found.
@@ -101,45 +38,6 @@ def check_error(handle_type: int, handle: Any, ret: int) -> None:
10138 raise_exception (error_info .sqlState , error_info .ddbcErrorMsg )
10239
10340
104- def add_driver_name_to_app_parameter (connection_string : str ) -> str :
105- """
106- Modifies the input connection string by appending the APP name.
107-
108- Args:
109- connection_string (str): The input connection string.
110-
111- Returns:
112- str: The modified connection string.
113- """
114- logger .debug ("add_driver_name_to_app_parameter: Processing connection string" )
115- # Split the input string into key-value pairs
116- parameters = connection_string .split (";" )
117-
118- # Initialize variables
119- app_found = False
120- modified_parameters = []
121-
122- # Iterate through the key-value pairs
123- for param in parameters :
124- if param .lower ().startswith ("app=" ):
125- # Overwrite the value with 'MSSQL-Python'
126- app_found = True
127- key , _ = param .split ("=" , 1 )
128- modified_parameters .append (f"{ key } =MSSQL-Python" )
129- logger .debug ("add_driver_name_to_app_parameter: Existing APP parameter overwritten" )
130- else :
131- # Keep other parameters as is
132- modified_parameters .append (param )
133-
134- # If APP key is not found, append it
135- if not app_found :
136- modified_parameters .append ("APP=MSSQL-Python" )
137- logger .debug ("add_driver_name_to_app_parameter: APP parameter added" )
138-
139- # Join the parameters back into a connection string
140- return ";" .join (modified_parameters ) + ";"
141-
142-
14341def sanitize_connection_string (conn_str : str ) -> str :
14442 """
14543 Sanitize the connection string by removing sensitive information.
0 commit comments