diff options
author | Gord Thompson <gord@gordthompson.com> | 2020-06-04 12:38:13 -0600 |
---|---|---|
committer | Gord Thompson <gord@gordthompson.com> | 2020-06-04 12:38:13 -0600 |
commit | 14bc09203a8b5b2bc001f764ad7cce6a184975cc (patch) | |
tree | 403520678d2904cbc89fb19fcadb643b8a62f6f7 | |
parent | a4de30c50eb660a08c8d025c158c54a0bd84f051 (diff) | |
download | sqlalchemy-14bc09203a8b5b2bc001f764ad7cce6a184975cc.tar.gz |
Fix connection string escaping for mssql+pyodbc
Fixes: #5373
Change-Id: Ia41e8f1ef8644c54d23ebfdf3f909c785adf0fb0
-rw-r--r-- | doc/build/changelog/unreleased_13/5373.rst | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/connectors/pyodbc.py | 2 | ||||
-rw-r--r-- | test/dialect/mssql/test_engine.py | 6 |
3 files changed, 12 insertions, 4 deletions
diff --git a/doc/build/changelog/unreleased_13/5373.rst b/doc/build/changelog/unreleased_13/5373.rst new file mode 100644 index 000000000..4396fbde3 --- /dev/null +++ b/doc/build/changelog/unreleased_13/5373.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, mssql + :tickets: 5373 + + Fixed issue with assembling the ODBC connection string for the pyodbc + DBAPI. Tokens containing semicolons and/or braces "{}" were not being + correctly escaped, causing the ODBC driver to misinterpret the + connection string attributes.
\ No newline at end of file diff --git a/lib/sqlalchemy/connectors/pyodbc.py b/lib/sqlalchemy/connectors/pyodbc.py index df1b2afdb..57a1c54fb 100644 --- a/lib/sqlalchemy/connectors/pyodbc.py +++ b/lib/sqlalchemy/connectors/pyodbc.py @@ -56,7 +56,7 @@ class PyODBCConnector(Connector): def check_quote(token): if ";" in str(token): - token = "'%s'" % token + token = "{%s}" % token.replace("}", "}}") return token keys = dict((k, check_quote(v)) for k, v in keys.items()) diff --git a/test/dialect/mssql/test_engine.py b/test/dialect/mssql/test_engine.py index 734224ed1..220941489 100644 --- a/test/dialect/mssql/test_engine.py +++ b/test/dialect/mssql/test_engine.py @@ -210,7 +210,7 @@ class ParseConnectTest(fixtures.TestBase): def test_pyodbc_token_injection(self): token1 = "someuser%3BPORT%3D50001" - token2 = "somepw%3BPORT%3D50001" + token2 = "some{strange}pw%3BPORT%3D50001" token3 = "somehost%3BPORT%3D50001" token4 = "somedb%3BPORT%3D50001" @@ -224,8 +224,8 @@ class ParseConnectTest(fixtures.TestBase): [ [ "DRIVER={foob};Server=somehost%3BPORT%3D50001;" - "Database=somedb%3BPORT%3D50001;UID='someuser;PORT=50001';" - "PWD='somepw;PORT=50001'" + "Database=somedb%3BPORT%3D50001;UID={someuser;PORT=50001};" + "PWD={some{strange}}pw;PORT=50001}" ], {}, ], |