-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
thisisaaronland
committed
Oct 13, 2021
1 parent
f197f59
commit 52221e6
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.Pagination` | ||
// instance for the results. | ||
func QueryPaginated(ctx context.Context, conn *sql.DB, pg_opts pagination.PaginationOptions, q string, args ...interface{}) (spr.StandardPlacesResults, pagination.Pagination, 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.Pagination() | ||
|
||
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 | ||
} |