Skip to content

Commit

Permalink
Adding LISTEN and RAISE support for pgx driver
Browse files Browse the repository at this point in the history
Brings pgx driver up to par with the postgres driver by adding
LISTEN/RAISE listeners to the database connection.

Implements #438.
  • Loading branch information
kenshaw committed Jan 29, 2024
1 parent 8c055e4 commit 88a86e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 83 deletions.
31 changes: 30 additions & 1 deletion drivers/pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,48 @@ import (
"io"
"strings"

"github.com/jackc/pgconn"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/stdlib" // DRIVER
"github.com/xo/dburl"
"github.com/xo/usql/drivers"
"github.com/xo/usql/drivers/metadata"
pgmeta "github.com/xo/usql/drivers/metadata/postgres"
"github.com/xo/usql/text"
)

func init() {
drivers.Register("pgx", drivers.Driver{
AllowDollar: true,
AllowMultilineComments: true,
LexerName: "postgres",
Open: func(ctx context.Context, u *dburl.URL, stdout, stderr func() io.Writer) (func(string, string) (*sql.DB, error), error) {
return func(_, dsn string) (*sql.DB, error) {
config, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, err
}
config.OnNotice = func(_ *pgconn.PgConn, notice *pgconn.Notice) {
out := stderr()
fmt.Fprintln(out, notice.Severity+": ", notice.Message)
if notice.Hint != "" {
fmt.Fprintln(out, "HINT: ", notice.Hint)
}
}
config.OnNotification = func(_ *pgconn.PgConn, notification *pgconn.Notification) {
var payload string
if notification.Payload != "" {
payload = fmt.Sprintf(text.NotificationPayload, notification.Payload)
}
fmt.Fprintln(stdout(), fmt.Sprintf(text.NotificationReceived, notification.Channel, payload, notification.PID))
}
// NOTE: as opposed to the github.com/lib/pq driver, this
// NOTE: driver has a "prefer" mode that is enabled by default.
// NOTE: as such there is no logic here to try to reconnect as
// NOTE: in the postgres driver.
return stdlib.OpenDB(*config), nil
}, nil
},
Version: func(ctx context.Context, db drivers.DB) (string, error) {
var ver string
err := db.QueryRowContext(ctx, `SHOW server_version`).Scan(&ver)
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/goexpect v0.0.0-20210430020637-ab937bf7fd6f
github.com/googleapis/go-sql-spanner v1.1.1
github.com/jackc/pgconn v1.14.1
github.com/jackc/pgx/v5 v5.5.2
github.com/jeandeaual/go-locale v0.0.0-20220711133428-7de61946b173
github.com/jmrobles/h2go v0.5.0
Expand Down Expand Up @@ -202,10 +201,7 @@ require (
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/icholy/digest v0.1.22 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
Expand Down
Loading

0 comments on commit 88a86e4

Please sign in to comment.