Skip to content

Commit 1445062

Browse files
narqoarp242
authored andcommitted
pq: fix Connector example test
The connector's example had one rudimental if-err check and used confusing variable names, e.g. "name" instead of "dsn". This change makes the example to be in line with examples of Go's database/sql.
1 parent 0f5c64d commit 1445062

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

connector_example_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@ package pq_test
44

55
import (
66
"database/sql"
7-
"fmt"
7+
"log"
88

99
"github.com/lib/pq"
1010
)
1111

1212
func ExampleNewConnector() {
13-
name := ""
14-
connector, err := pq.NewConnector(name)
13+
c, err := pq.NewConnector("postgres://")
1514
if err != nil {
16-
fmt.Println(err)
17-
return
15+
log.Fatalf("could not create connector: %v", err)
1816
}
19-
db := sql.OpenDB(connector)
17+
18+
db := sql.OpenDB(c)
2019
defer db.Close()
2120

2221
// Use the DB
23-
txn, err := db.Begin()
22+
tx, err := db.Begin()
2423
if err != nil {
25-
fmt.Println(err)
26-
return
24+
log.Fatalf("could not start transaction: %v", err)
2725
}
28-
txn.Rollback()
26+
tx.Rollback()
2927
}

notice_example_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@ import (
1111
)
1212

1313
func ExampleConnectorWithNoticeHandler() {
14-
name := ""
1514
// Base connector to wrap
16-
base, err := pq.NewConnector(name)
15+
base, err := pq.NewConnector("postgres://")
1716
if err != nil {
1817
log.Fatal(err)
1918
}
19+
2020
// Wrap the connector to simply print out the message
2121
connector := pq.ConnectorWithNoticeHandler(base, func(notice *pq.Error) {
2222
fmt.Println("Notice sent: " + notice.Message)
2323
})
2424
db := sql.OpenDB(connector)
2525
defer db.Close()
26+
2627
// Raise a notice
2728
sql := "DO language plpgsql $$ BEGIN RAISE NOTICE 'test notice'; END $$"
2829
if _, err := db.Exec(sql); err != nil {
2930
log.Fatal(err)
3031
}
32+
3133
// Output:
3234
// Notice sent: test notice
3335
}

0 commit comments

Comments
 (0)