[psycopg] pass extra_args down to connect()

Lets the user pass things like 'service' or 'sslmode'. Closes #74171

authorJulien Cristau <julien.cristau@logilab.fr>
changeset102dd17a1f69
branchdefault
phasedraft
hiddenyes
parent revision#a6706284f282 [numrange] add a new type of sequence that can deliver whole ranges at a time
child revision<not specified>
files modified by this revision
postgres.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1394200964 -3600
# Fri Mar 07 15:02:44 2014 +0100
# Node ID 102dd17a1f69b4fee1345ea01014bef496a17159
# Parent a6706284f2826a97209ef0e63c9b9847f1dcd238
[psycopg] pass extra_args down to connect()

Lets the user pass things like 'service' or 'sslmode'.
Closes #74171

diff --git a/postgres.py b/postgres.py
@@ -72,18 +72,24 @@
1  class _PsycopgAdapter(db.DBAPIAdapter):
2      """Simple Psycopg Adapter to DBAPI (cnx_string differs from classical ones)
3      """
4      def connect(self, host='', database='', user='', password='', port='', extra_args=None):
5          """Handles psycopg connection format"""
6 +        args = {}
7          if host:
8 -            cnx_string = 'host=%s  dbname=%s  user=%s' % (host, database, user)
9 -        else:
10 -            cnx_string = 'dbname=%s  user=%s' % (database, user)
11 +            args.setdefault('host', host)
12 +        if database:
13 +            args.setdefault('dbname', database)
14 +        if user:
15 +            args.setdefault('user', user)
16          if port:
17 -            cnx_string += ' port=%s' % port
18 +            args.setdefault('port', port)
19          if password:
20 -            cnx_string = '%s password=%s' % (cnx_string, password)
21 +            args.setdefault('password', password)
22 +        cnx_string = ' '.join('%s=%s' % item for item in args.iteritems())
23 +        if extra_args is not None:
24 +            cnx_string += ' ' + extra_args
25          cnx = self._native_module.connect(cnx_string)
26          cnx.set_isolation_level(1)
27          return self._wrap_if_needed(cnx)
28 
29