Skip to content

Commit caf3632

Browse files
authored
fix(mssql): escape special characters in passwords (#10437)
## Description of changes I found the error ``` 'IM002', '[IM002] [unixODBC][Driver Manager]Data source name not found and no default driver specified (0) (SQLDriverConnect)' ``` Because the password of mssql includes special characters like `{R;3G1/8Al2AniRye` that start with `{` or include `;`. It should be covered by `{` and `}` and replace `}` with`}}`. Reference: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-databases https://stackoverflow.com/questions/78531086/pyodbc-connection-string-correctly-escaping-password-with-special-characters/78532507#78532507
1 parent 4ea041f commit caf3632

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

ibis/backends/mssql/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,17 @@ def do_connect(
176176
self.con = pyodbc.connect(
177177
user=user,
178178
server=f"{host},{port}",
179-
password=password,
179+
password=self._escape_special_characters(password),
180180
driver=driver,
181181
**kwargs,
182182
)
183183

184184
self._post_connect()
185185

186+
@staticmethod
187+
def _escape_special_characters(value: str) -> str:
188+
return "{" + value.replace("}", "}}") + "}"
189+
186190
@util.experimental
187191
@classmethod
188192
def from_connection(cls, con: pyodbc.Connection) -> Backend:

ibis/backends/mssql/tests/test_client.py

+11
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,14 @@ def test_create_temp_table(con, temp):
300300
assert t.columns == ("a",)
301301
finally:
302302
con.drop_table(name)
303+
304+
305+
def test_escape_special_characters():
306+
test_func = ibis.backends.mssql.Backend._escape_special_characters
307+
assert test_func("1bis_Testing!") == "{1bis_Testing!}"
308+
assert test_func("{1bis_Testing!") == "{{1bis_Testing!}"
309+
assert test_func("1bis_Testing!}") == "{1bis_Testing!}}}"
310+
assert test_func("{1bis_Testing!}") == "{{1bis_Testing!}}}"
311+
assert test_func("1bis}Testing!") == "{1bis}}Testing!}"
312+
assert test_func("{R;3G1/8Al2AniRye") == "{{R;3G1/8Al2AniRye}"
313+
assert test_func("{R;3G1/8Al2AniRye}") == "{{R;3G1/8Al2AniRye}}}"

0 commit comments

Comments
 (0)