Fix Listen/Unlisten with special characters

fixes #132
pull/133/head
Jack Christensen 2016-03-24 15:26:44 -05:00
parent b8486d6749
commit 9ce1b2b16e
3 changed files with 23 additions and 2 deletions

View File

@ -8,6 +8,7 @@
* Fix compilation on 32-bit architecture
* Fix Tx.status not being set on error on Commit
* Fix Listen/Unlisten with special characters
# 2.8.0 (March 18, 2016)

View File

@ -657,7 +657,7 @@ func (c *Conn) Deallocate(name string) (err error) {
// Listen establishes a PostgreSQL listen/notify to channel
func (c *Conn) Listen(channel string) error {
_, err := c.Exec("listen " + channel)
_, err := c.Exec("listen " + quoteIdentifier(channel))
if err != nil {
return err
}
@ -669,7 +669,7 @@ func (c *Conn) Listen(channel string) error {
// Unlisten unsubscribes from a listen channel
func (c *Conn) Unlisten(channel string) error {
_, err := c.Exec("unlisten " + channel)
_, err := c.Exec("unlisten " + quoteIdentifier(channel))
if err != nil {
return err
}
@ -1205,3 +1205,7 @@ func (c *Conn) SetLogLevel(lvl int) (int, error) {
c.logLevel = lvl
return lvl, nil
}
func quoteIdentifier(s string) string {
return `"` + strings.Replace(s, `"`, `""`, -1) + `"`
}

View File

@ -1185,6 +1185,22 @@ func TestListenNotifySelfNotification(t *testing.T) {
}
}
func TestListenUnlistenSpecialCharacters(t *testing.T) {
t.Parallel()
conn := mustConnect(t, *defaultConnConfig)
defer closeConn(t, conn)
chanName := "special characters !@#{$%^&*()}"
if err := conn.Listen(chanName); err != nil {
t.Fatalf("Unable to start listening: %v", err)
}
if err := conn.Unlisten(chanName); err != nil {
t.Fatalf("Unable to stop listening: %v", err)
}
}
func TestFatalRxError(t *testing.T) {
t.Parallel()