forked from snowflakedb/snowflake-sqlalchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
68 lines (58 loc) · 2.46 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019 Snowflake Computing Inc. All right reserved.
#
from sqlalchemy import exc
from snowflake.connector.compat import IS_STR
from urllib.parse import quote_plus
from sqlalchemy.engine.url import _rfc_1738_quote
def _url(**db_parameters):
"""
Composes a SQLAlchemy connect string from the given database connection
parameters.
"""
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