Fix up some of the examples

pull/483/head
Jack Christensen 2019-04-13 14:31:16 -05:00
parent 6c0de9ff37
commit dac6bfdc61
4 changed files with 28 additions and 44 deletions

View File

@ -8,12 +8,7 @@ between them.
## Connection configuration ## Connection configuration
The database connection is configured via the standard PostgreSQL environment variables. The database connection is configured via DATABASE_URL and standard PostgreSQL environment variables (PGHOST, PGUSER, etc.)
* PGHOST - defaults to localhost
* PGUSER - defaults to current OS user
* PGPASSWORD - defaults to empty string
* PGDATABASE - defaults to user name
You can either export them then run chat: You can either export them then run chat:

View File

@ -6,19 +6,14 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/jackc/pgx" pgxpool "github.com/jackc/pgx/pool"
) )
var pool *pgx.ConnPool var pool *pgxpool.Pool
func main() { func main() {
config, err := pgx.ParseEnvLibpq() var err error
if err != nil { pool, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
fmt.Fprintln(os.Stderr, "Unable to parse environment:", err)
os.Exit(1)
}
pool, err = pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: config})
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Unable to connect to database:", err) fmt.Fprintln(os.Stderr, "Unable to connect to database:", err)
os.Exit(1) os.Exit(1)
@ -40,7 +35,7 @@ Type "exit" to quit.`)
os.Exit(0) os.Exit(0)
} }
_, err = pool.Exec("select pg_notify('chat', $1)", msg) _, err = pool.Exec(context.Background(), "select pg_notify('chat', $1)", msg)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error sending notification:", err) fmt.Fprintln(os.Stderr, "Error sending notification:", err)
os.Exit(1) os.Exit(1)
@ -53,22 +48,24 @@ Type "exit" to quit.`)
} }
func listen() { func listen() {
conn, err := pool.Acquire() conn, err := pool.Acquire(context.Background())
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error acquiring connection:", err) fmt.Fprintln(os.Stderr, "Error acquiring connection:", err)
os.Exit(1) os.Exit(1)
} }
defer pool.Release(conn) defer conn.Release()
// TODO - determine how listen should be handled in pgx vs. pgconn
conn.Exec(context.Background(), "listen chat") conn.Exec(context.Background(), "listen chat")
for { // for {
notification, err := conn.WaitForNotification(context.Background()) // notification, err := conn.WaitForNotification(context.Background())
if err != nil { // if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for notification:", err) // fmt.Fprintln(os.Stderr, "Error waiting for notification:", err)
os.Exit(1) // os.Exit(1)
} // }
fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload) // fmt.Println("PID:", notification.PID, "Channel:", notification.Channel, "Payload:", notification.Payload)
} // }
} }

View File

@ -19,12 +19,7 @@ Build todo:
## Connection configuration ## Connection configuration
The database connection is configured via enviroment variables. The database connection is configured via DATABASE_URL and standard PostgreSQL environment variables (PGHOST, PGUSER, etc.)
* PGHOST - defaults to localhost
* PGUSER - defaults to current OS user
* PGPASSWORD - defaults to empty string
* PGDATABASE - defaults to user name
You can either export them then run todo: You can either export them then run todo:

View File

@ -1,22 +1,19 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/jackc/pgx"
"os" "os"
"strconv" "strconv"
"github.com/jackc/pgx"
) )
var conn *pgx.Conn var conn *pgx.Conn
func main() { func main() {
config, err := pgx.ParseEnvLibpq() var err error
if err != nil { conn, err = pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
fmt.Fprintln(os.Stderr, "Unable to parse environment:", err)
os.Exit(1)
}
conn, err = pgx.Connect(config)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err) fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err)
os.Exit(1) os.Exit(1)
@ -74,7 +71,7 @@ func main() {
} }
func listTasks() error { func listTasks() error {
rows, _ := conn.Query("select * from tasks") rows, _ := conn.Query(context.Background(), "select * from tasks")
for rows.Next() { for rows.Next() {
var id int32 var id int32
@ -90,17 +87,17 @@ func listTasks() error {
} }
func addTask(description string) error { func addTask(description string) error {
_, err := conn.Exec("insert into tasks(description) values($1)", description) _, err := conn.Exec(context.Background(), "insert into tasks(description) values($1)", description)
return err return err
} }
func updateTask(itemNum int32, description string) error { func updateTask(itemNum int32, description string) error {
_, err := conn.Exec("update tasks set description=$1 where id=$2", description, itemNum) _, err := conn.Exec(context.Background(), "update tasks set description=$1 where id=$2", description, itemNum)
return err return err
} }
func removeTask(itemNum int32) error { func removeTask(itemNum int32) error {
_, err := conn.Exec("delete from tasks where id=$1", itemNum) _, err := conn.Exec(context.Background(), "delete from tasks where id=$1", itemNum)
return err return err
} }