Skip to content

Commit f3cee92

Browse files
committed
Fix special characters in URLs
1 parent 0e4c7d3 commit f3cee92

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

sqlalchemy_singlestoredb/base.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from typing import List
99
from typing import Optional
1010
from typing import Type
11+
from urllib.parse import quote
12+
from urllib.parse import quote_plus
1113

1214
import sqlalchemy.sql.elements as se
1315
from sqlalchemy import util
@@ -318,6 +320,33 @@ def result_processor(self, dialect: Any, coltype: Any) -> Any:
318320
return None
319321

320322

323+
def render_as_string(url: URL) -> str:
324+
s = url.drivername + '://'
325+
if url.username is not None:
326+
s += quote(url.username)
327+
if url.password is not None:
328+
s += ':' + quote(str(url.password))
329+
s += '@'
330+
if url.host is not None:
331+
if ':' in url.host:
332+
s += f'[{url.host}]'
333+
else:
334+
s += url.host
335+
if url.port is not None:
336+
s += ':' + str(url.port)
337+
if url.database is not None:
338+
s += '/' + url.database
339+
if url.query:
340+
keys = list(url.query)
341+
keys.sort()
342+
s += '?' + '&'.join(
343+
f'{quote_plus(k)}={quote_plus(element)}'
344+
for k in keys
345+
for element in util.to_list(url.query[k])
346+
)
347+
return s
348+
349+
321350
class SingleStoreDBDialect(MySQLDialect):
322351
"""SingleStoreDB SQLAlchemy dialect."""
323352

@@ -383,7 +412,7 @@ def initialize(self, connection: Any) -> Any:
383412

384413
def create_connect_args(self, url: URL) -> List[Any]:
385414
from singlestoredb.connection import build_params
386-
return [[], build_params(host=url.render_as_string(hide_password=False))]
415+
return [[], build_params(host=render_as_string(url))]
387416

388417
def _extract_error_code(self, exception: Exception) -> int:
389418
return getattr(exception, 'errno', -1)

0 commit comments

Comments
 (0)