Skip to content

Commit

Permalink
Add ExampleQuery using new testdata/poi.db
Browse files Browse the repository at this point in the history
Add testdata/poi.db (a pre-built SQLite DB for example) to reduce length
of examples.

Add ExampleQuery that uses poi.db.
  • Loading branch information
dolmen committed Nov 5, 2023
1 parent 9d949f5 commit caba869
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,36 @@ func ExampleExec_withTx() {
// names: [Château de Versailles Villeperdue]
// countPOI after rollback: 0
}

func ExampleQuery() {
check := func(msg string, err error) {
if err != nil {
panic(fmt.Errorf("%s: %v", msg, err))
}
}

ctx := context.Background()
db, err := sql.Open(sqliteDriver, "file:testdata/poi.db?mode=ro&immutable=1")
check("Open", err)
defer db.Close()

var queryNames func(ctx context.Context) (*sql.Rows, error)
closeQueryNames, err := sqlfunc.Query(
ctx, db,
`SELECT name FROM poi ORDER BY name`,
&queryNames,
)
check("Prepare queryNames", err)
defer closeQueryNames()

rows, err := queryNames(ctx)
check("queryNames", err)
err = sqlfunc.ForEach(rows, func(name string) {
fmt.Println("-", name)
})
check("read rows", err)

// Output:
// - Château de Versailles
// - Villeperdue
}
Binary file added testdata/poi.db
Binary file not shown.

0 comments on commit caba869

Please sign in to comment.