From b69179cebbbf9a8498482d780b4f3623b2256ac3 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Wed, 10 Apr 2019 11:21:40 -0500 Subject: [PATCH] Remove Conn.Listen and Conn.Unlisten Use Conn.Exec instead to listen or unlisten. --- conn.go | 25 ------------------------- conn_test.go | 8 ++++---- doc.go | 2 +- examples/chat/main.go | 2 +- 4 files changed, 6 insertions(+), 31 deletions(-) diff --git a/conn.go b/conn.go index 29dc924f..0fd42a8d 100644 --- a/conn.go +++ b/conn.go @@ -66,7 +66,6 @@ type Conn struct { wbuf []byte config *ConnConfig // config used when establishing this connection preparedStatements map[string]*PreparedStatement - channels map[string]struct{} logger Logger logLevel LogLevel fp *fastpath @@ -191,7 +190,6 @@ func connect(ctx context.Context, config *ConnConfig, connInfo *pgtype.ConnInfo) } c.preparedStatements = make(map[string]*PreparedStatement) - c.channels = make(map[string]struct{}) c.cancelQueryCompleted = make(chan struct{}) close(c.cancelQueryCompleted) c.doneChan = make(chan struct{}) @@ -573,29 +571,6 @@ func (c *Conn) deallocateContext(ctx context.Context, name string) (err error) { return err } -// Listen establishes a PostgreSQL listen/notify to channel -func (c *Conn) Listen(channel string) error { - _, err := c.Exec(context.TODO(), "listen "+quoteIdentifier(channel)) - if err != nil { - return err - } - - c.channels[channel] = struct{}{} - - return nil -} - -// Unlisten unsubscribes from a listen channel -func (c *Conn) Unlisten(channel string) error { - _, err := c.Exec(context.TODO(), "unlisten "+quoteIdentifier(channel)) - if err != nil { - return err - } - - delete(c.channels, channel) - return nil -} - func (c *Conn) IsAlive() bool { c.mux.Lock() defer c.mux.Unlock() diff --git a/conn_test.go b/conn_test.go index db488f56..00698245 100644 --- a/conn_test.go +++ b/conn_test.go @@ -617,7 +617,7 @@ func TestSetLogger(t *testing.T) { t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", nil, oldLogger) } - if err := conn.Listen("foo"); err != nil { + if _, err := conn.Exec(context.Background(), "listen foo"); err != nil { t.Fatal(err) } @@ -631,7 +631,7 @@ func TestSetLogger(t *testing.T) { t.Fatalf("Expected conn.SetLogger to return %v, but it was %v", l1, oldLogger) } - if err := conn.Listen("bar"); err != nil { + if _, err := conn.Exec(context.Background(), "listen bar"); err != nil { t.Fatal(err) } @@ -657,7 +657,7 @@ func TestSetLogLevel(t *testing.T) { t.Fatal(err) } - if err := conn.Listen("foo"); err != nil { + if _, err := conn.Exec(context.Background(), "listen foo"); err != nil { t.Fatal(err) } @@ -669,7 +669,7 @@ func TestSetLogLevel(t *testing.T) { t.Fatal(err) } - if err := conn.Listen("bar"); err != nil { + if _, err := conn.Exec(context.Background(), "listen bar"); err != nil { t.Fatal(err) } diff --git a/doc.go b/doc.go index 5808c09d..7da1cd88 100644 --- a/doc.go +++ b/doc.go @@ -220,7 +220,7 @@ pgx can listen to the PostgreSQL notification system with the WaitForNotification function. It takes a maximum time to wait for a notification. - err := conn.Listen("channelname") + err := conn.Exec("listen channelname") if err != nil { return nil } diff --git a/examples/chat/main.go b/examples/chat/main.go index 83b16c02..35b73183 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -60,7 +60,7 @@ func listen() { } defer pool.Release(conn) - conn.Listen("chat") + conn.Exec(context.Background(), "listen chat") for { notification, err := conn.WaitForNotification(context.Background())