[psycopg] pass extra_args down to connect()

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

authorJulien Cristau <julien.cristau@logilab.fr>
changesete767d3e02ff2
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 e767d3e02ff2b0d4c0ee6736d8b5fa2e9a335aed
# 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 extra_args is not None:
8 +            args.update(extra_args)
9          if host:
10 -            cnx_string = 'host=%s  dbname=%s  user=%s' % (host, database, user)
11 -        else:
12 -            cnx_string = 'dbname=%s  user=%s' % (database, user)
13 +            args.setdefault('host', host)
14 +        if database:
15 +            args.setdefault('dbname', database)
16 +        if user:
17 +            args.setdefault('user', user)
18          if port:
19 -            cnx_string += ' port=%s' % port
20 +            args.setdefault('port', port)
21          if password:
22 -            cnx_string = '%s password=%s' % (cnx_string, password)
23 +            args.setdefault('password', password)
24 +        cnx_string = ' '.join('%s=%s' % item for item in args.iteritems())
25          cnx = self._native_module.connect(cnx_string)
26          cnx.set_isolation_level(1)
27          return self._wrap_if_needed(cnx)
28 
29