forked from snowflakedb/snowflake-sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
74 lines (62 loc) · 2.83 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
# Copyright (c) 2012-2022 Snowflake Computing Inc. All rights reserved.
#
import re
from urllib.parse import quote_plus
from sqlalchemy import exc
from snowflake.connector.compat import IS_STR
def _rfc_1738_quote(text):
return re.sub(r"[:@/]", lambda m: "%%%X" % ord(m.group(0)), text)
def _url(**db_parameters):
"""
Composes a SQLAlchemy connect string from the given database connection
parameters.
Password containing special characters (e.g., '@', '%') need to be encoded to be parsed correctly.
Unescaped password containing special characters might lead to authentication failure.
Please follow the instructions to encode the password:
https://github.com./snowflakedb/snowflake-sqlalchemy#escaping-special-characters-such-as---signs-in-passwords
"""
specified_parameters = []
if "account" not in db_parameters:
raise exc.ArgumentError("account parameter must be specified.")
if "host" in db_parameters:
ret = "snowflake://{user}:{password}@{host}:{port}/".format(
user=db_parameters.get("user", ""),
password=_rfc_1738_quote(db_parameters.get("password", "")),
host=db_parameters["host"],
port=db_parameters["port"] if "port" in db_parameters else 443,
)
specified_parameters += ["user", "password", "host", "port"]
elif "region" not in db_parameters:
ret = "snowflake://{user}:{password}@{account}/".format(
account=db_parameters["account"],
user=db_parameters.get("user", ""),
password=_rfc_1738_quote(db_parameters.get("password", "")),
)
specified_parameters += ["user", "password", "account"]
else:
ret = "snowflake://{user}:{password}@{account}.{region}/".format(
account=db_parameters["account"],
user=db_parameters.get("user", ""),
password=_rfc_1738_quote(db_parameters.get("password", "")),
region=db_parameters["region"],
)
specified_parameters += ["user", "password", "account", "region"]
if "database" in db_parameters:
ret += quote_plus(db_parameters["database"])
specified_parameters += ["database"]
if "schema" in db_parameters:
ret += "/" + quote_plus(db_parameters["schema"])
specified_parameters += ["schema"]
elif "schema" in db_parameters:
raise exc.ArgumentError("schema cannot be specified without database")
def sep(is_first_parameter):
return "?" if is_first_parameter else "&"
is_first_parameter = True
for p in sorted(db_parameters.keys()):
v = db_parameters[p]
if p not in specified_parameters:
encoded_value = quote_plus(v) if IS_STR(v) else str(v)
ret += sep(is_first_parameter) + p + "=" + encoded_value
is_first_parameter = False
return ret