-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
49 lines (35 loc) · 1.23 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package spr
import (
"context"
"database/sql"
"fmt"
"github.com/aaronland/go-pagination"
sql_pagination "github.com/aaronland/go-pagination-sql"
"github.com/whosonfirst/go-whosonfirst-spr/v2"
)
// QueryPaginated will iterate over all the rows for 'q' in batches determined by 'pg_opts' and return a `spr.StandardPlacesResults` and `pagination.Results`
// instance for the results.
func QueryPaginated(ctx context.Context, conn *sql.DB, pg_opts pagination.Options, q string, args ...interface{}) (spr.StandardPlacesResults, pagination.Results, error) {
rsp, err := sql_pagination.QueryPaginated(conn, pg_opts, q, args...)
if err != nil {
return nil, nil, fmt.Errorf("Failed to query database, %w", err)
}
rows := rsp.Rows()
pg := rsp.Results()
spr_results := make([]spr.StandardPlacesResult, 0)
for rows.Next() {
result_spr, err := RetrieveSPRWithRows(ctx, rows)
if err != nil {
return nil, nil, fmt.Errorf("Failed to retrieve SPR from row, %w", err)
}
spr_results = append(spr_results, result_spr)
}
err = rows.Err()
if err != nil {
return nil, nil, fmt.Errorf("There was a problem retrieving rows from the database, %w", err)
}
results := &SQLiteResults{
Places: spr_results,
}
return results, pg, nil
}