Skip to content

Commit ef6072e

Browse files
author
James William Pye
committed
Driver parameter support.
1 parent b4c3b20 commit ef6072e

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

postgresql/iri.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
1616
Driver Parameters:
1717
18-
pq://user@host/?[driver_param]=value&[other_param]=value
18+
pq://user@host/?[driver_param]=value&[other_param]=value?setting=val
1919
"""
2020
from .resolved import riparse as ri
2121
from . import string as pg_str
22+
import operator
2223
import re
2324

2425
escape_path_re = re.compile('[%s]' %(re.escape(ri.unescaped + ','),))
@@ -134,6 +135,15 @@ def construct(x, obscure_password = False):
134135
password = x.get('password')
135136
if obscure_password and password is not None:
136137
password = '***'
138+
driver_params = list({
139+
'[' + k + ']' : str(v) for k,v in x.items()
140+
if k not in (
141+
'user', 'password', 'port', 'database', 'ssl',
142+
'path', 'host', 'unix', 'ipv','settings'
143+
)
144+
}.items())
145+
driver_params.sort(key=operator.itemgetter(0))
146+
137147
return (
138148
'pqs' if x.get('ssl', False) is True else 'pq',
139149
# netloc: user:pass@host[:port]
@@ -148,7 +158,9 @@ def construct(x, obscure_password = False):
148158
for path_comp in path
149159
]),
150160
None if no_path_settings is None else (
151-
ri.construct_query(no_path_settings)
161+
ri.construct_query(
162+
driver_params + no_path_settings
163+
)
152164
),
153165
None if search_path is None else construct_path(search_path),
154166
)

postgresql/test/test_iri.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
'pq://fæm.com:123/õéf/á?param=val',
2424
'pq://l»»@fæm.com:123/õéf/á?param=val',
2525
'pq://fæᎱᏋm.com/õéf/á?param=val',
26+
'pq://fæᎱᏋm.com/õéf/á?param=val&[setting]=value',
2627
)
2728

2829
sample_structured_parameters = [
@@ -68,6 +69,19 @@
6869
'set2' : 'val2',
6970
},
7071
},
72+
{
73+
'user' : 'user',
74+
'password' : 'secret',
75+
'host' : '',
76+
'port' : 'ssh',
77+
'database' : 'database_name',
78+
'settings' : {
79+
'set1' : 'val1',
80+
'set2' : 'val2',
81+
},
82+
'connect_timeout' : '10',
83+
'sslmode' : 'prefer',
84+
},
7185
]
7286

7387
class test_iri(unittest.TestCase):

0 commit comments

Comments
 (0)