Use parse_dsn in ReplicationConnectionBase

This commit is contained in:
Oleksandr Shulgin 2015-10-01 16:04:19 +02:00
parent 95ee218c6d
commit cac83da5db

View File

@ -449,7 +449,7 @@ class ReplicationConnectionBase(_connection):
classes. Uses `ReplicationCursor` automatically. classes. Uses `ReplicationCursor` automatically.
""" """
def __init__(self, *args, **kwargs): def __init__(self, dsn, **kwargs):
""" """
Initializes a replication connection by adding appropriate Initializes a replication connection by adding appropriate
parameters to the provided DSN and tweaking the connection parameters to the provided DSN and tweaking the connection
@ -466,21 +466,16 @@ class ReplicationConnectionBase(_connection):
else: else:
raise psycopg2.ProgrammingError("unrecognized replication type: %s" % self.replication_type) raise psycopg2.ProgrammingError("unrecognized replication type: %s" % self.replication_type)
# FIXME: could really use parse_dsn here items = _ext.parse_dsn(dsn)
dsn = args[0]
if dsn.startswith('postgres://') or dsn.startswith('postgresql://'):
# poor man's url parsing
if dsn.rfind('?') > 0:
if not dsn.endswith('?'):
dsn += '&'
else:
dsn += '?'
else:
dsn += ' '
dsn += 'replication=%s' % replication
args = [dsn] + list(args[1:])
super(ReplicationConnectionBase, self).__init__(*args, **kwargs) # we add an appropriate replication keyword parameter, unless
# user has specified one explicitly in the DSN
items.setdefault('replication', replication)
dsn = " ".join(["%s=%s" % (k, psycopg2._param_escape(str(v)))
for (k, v) in items.iteritems()])
super(ReplicationConnectionBase, self).__init__(dsn, **kwargs)
# prevent auto-issued BEGIN statements # prevent auto-issued BEGIN statements
if not self.async: if not self.async: