Skip to content

Commit

Permalink
Add ExampleQuery_withArgs, ExampleQueryRow_withArgs
Browse files Browse the repository at this point in the history
More examples for better code coverage.
  • Loading branch information
dolmen committed Nov 5, 2023
1 parent caba869 commit 791ddb7
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,64 @@ func ExampleQuery() {
// - Château de Versailles
// - Villeperdue
}

func ExampleQuery_withArgs() {
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 queryByName func(ctx context.Context, name string) (*sql.Rows, error)
closeQueryByName, err := sqlfunc.Query(
ctx, db,
`SELECT lat, lon FROM poi WHERE name = ?`,
&queryByName,
)
check("Prepare queryByName", err)
defer closeQueryByName()

rows, err := queryByName(ctx, "Château de Versailles")
check("queryByName", err)
err = sqlfunc.ForEach(rows, func(lat, lon float64) {
fmt.Printf("(%.4f %.4f)\n", lat, lon)
})
check("read rows", err)

// Output:
// (48.8016 2.1204)
}

func ExampleQueryRow_withArgs() {
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 queryByName func(ctx context.Context, name string) (lat, lon float64, err error)
closeQueryByName, err := sqlfunc.QueryRow(
ctx, db,
`SELECT lat, lon FROM poi WHERE name = ?`,
&queryByName,
)
check("Prepare queryByName", err)
defer closeQueryByName()

lat, lon, err := queryByName(ctx, "Château de Versailles")
check("queryByName", err)
fmt.Printf("(%.4f %.4f)\n", lat, lon)

// Output:
// (48.8016 2.1204)
}

0 comments on commit 791ddb7

Please sign in to comment.