Skip to content

Commit

Permalink
Bug fix: Actually add query.go
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisaaronland committed Oct 13, 2021
1 parent f197f59 commit 52221e6
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions query.go
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
}

0 comments on commit 52221e6

Please sign in to comment.