3939from psycopg2 import extensions as _ext
4040from psycopg2 .extensions import cursor as _cursor
4141from psycopg2 .extensions import connection as _connection
42+ from psycopg2 .extensions import REPLICATION_PHYSICAL , REPLICATION_LOGICAL
43+ from psycopg2 .extensions import ReplicationConnection as _replicationConnection
4244from psycopg2 .extensions import ReplicationCursor as _replicationCursor
4345from psycopg2 .extensions import ReplicationMessage
4446from psycopg2 .extensions import adapt as _A , quote_ident
@@ -439,65 +441,28 @@ def callproc(self, procname, vars=None):
439441 return LoggingCursor .callproc (self , procname , vars )
440442
441443
442- """Replication connection types."""
443- REPLICATION_LOGICAL = "LOGICAL"
444- REPLICATION_PHYSICAL = "PHYSICAL"
445-
446-
447- class ReplicationConnectionBase (_connection ):
444+ class ReplicationConnectionBase (_replicationConnection ):
448445 """
449446 Base class for Logical and Physical replication connection
450447 classes. Uses `ReplicationCursor` automatically.
451448 """
452449
453450 def __init__ (self , * args , ** kwargs ):
454- """
455- Initializes a replication connection by adding appropriate
456- parameters to the provided DSN and tweaking the connection
457- attributes.
458- """
459-
460- # replication_type is set in subclasses
461- if self .replication_type == REPLICATION_LOGICAL :
462- replication = 'database'
463-
464- elif self .replication_type == REPLICATION_PHYSICAL :
465- replication = 'true'
466-
467- else :
468- raise psycopg2 .ProgrammingError ("unrecognized replication type: %s" % self .replication_type )
469-
470- items = _ext .parse_dsn (args [0 ])
471-
472- # we add an appropriate replication keyword parameter, unless
473- # user has specified one explicitly in the DSN
474- items .setdefault ('replication' , replication )
475-
476- dsn = " " .join (["%s=%s" % (k , psycopg2 ._param_escape (str (v )))
477- for (k , v ) in items .iteritems ()])
478-
479- args = [dsn ] + list (args [1 :]) # async is the possible 2nd arg
480451 super (ReplicationConnectionBase , self ).__init__ (* args , ** kwargs )
481-
482- # prevent auto-issued BEGIN statements
483- if not self .async :
484- self .autocommit = True
485-
486- if self .cursor_factory is None :
487- self .cursor_factory = ReplicationCursor
452+ self .cursor_factory = ReplicationCursor
488453
489454
490455class LogicalReplicationConnection (ReplicationConnectionBase ):
491456
492457 def __init__ (self , * args , ** kwargs ):
493- self . replication_type = REPLICATION_LOGICAL
458+ kwargs [ ' replication_type' ] = REPLICATION_LOGICAL
494459 super (LogicalReplicationConnection , self ).__init__ (* args , ** kwargs )
495460
496461
497462class PhysicalReplicationConnection (ReplicationConnectionBase ):
498463
499464 def __init__ (self , * args , ** kwargs ):
500- self . replication_type = REPLICATION_PHYSICAL
465+ kwargs [ ' replication_type' ] = REPLICATION_PHYSICAL
501466 super (PhysicalReplicationConnection , self ).__init__ (* args , ** kwargs )
502467
503468
@@ -528,16 +493,16 @@ def create_replication_slot(self, slot_name, slot_type=None, output_plugin=None)
528493 if output_plugin is None :
529494 raise psycopg2 .ProgrammingError ("output plugin name is required to create logical replication slot" )
530495
531- command += "%s %s" % ( slot_type , quote_ident (output_plugin , self ) )
496+ command += "LOGICAL %s" % quote_ident (output_plugin , self )
532497
533498 elif slot_type == REPLICATION_PHYSICAL :
534499 if output_plugin is not None :
535500 raise psycopg2 .ProgrammingError ("cannot specify output plugin name when creating physical replication slot" )
536501
537- command += slot_type
502+ command += "PHYSICAL"
538503
539504 else :
540- raise psycopg2 .ProgrammingError ("unrecognized replication type: %s" % slot_type )
505+ raise psycopg2 .ProgrammingError ("unrecognized replication type: %s" % repr ( slot_type ) )
541506
542507 self .execute (command )
543508
@@ -562,15 +527,15 @@ def start_replication(self, slot_name=None, slot_type=None, start_lsn=0,
562527 else :
563528 raise psycopg2 .ProgrammingError ("slot name is required for logical replication" )
564529
565- command += "%s " % slot_type
530+ command += "LOGICAL "
566531
567532 elif slot_type == REPLICATION_PHYSICAL :
568533 if slot_name :
569534 command += "SLOT %s " % quote_ident (slot_name , self )
570535 # don't add "PHYSICAL", before 9.4 it was just START_REPLICATION XXX/XXX
571536
572537 else :
573- raise psycopg2 .ProgrammingError ("unrecognized replication type: %s" % slot_type )
538+ raise psycopg2 .ProgrammingError ("unrecognized replication type: %s" % repr ( slot_type ) )
574539
575540 if type (start_lsn ) is str :
576541 lsn = start_lsn .split ('/' )
0 commit comments