Skip to content

Commit

Permalink
allowing scanning of unknown fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dr4hcu5-jan committed May 29, 2024
1 parent dc36664 commit 087a2a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
25 changes: 11 additions & 14 deletions routes/locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"strings"

"github.com/georgysavva/scany/v2/dbscan"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/jackc/pgx/v5"
"github.com/lib/pq"
errorMiddleware "github.com/wisdom-oss/microservice-middlewares/v5/error"

Expand Down Expand Up @@ -39,9 +39,9 @@ func UsageLocations(w http.ResponseWriter, r *http.Request) {
enabledFilters = enabledFilters | realityFilter
}

// now build an array of the arguments and build the query
var arguments []interface{}
queryString, err := globals.SqlQueries.Raw("usage-locations")
// now build an array of the arguments and build the query
queryString, err := globals.SqlQueries.Raw("get-locations")
if err != nil {
errorHandler <- fmt.Errorf("unable to load base query: %w", err)
return
Expand Down Expand Up @@ -109,23 +109,20 @@ func UsageLocations(w http.ResponseWriter, r *http.Request) {
queryString = strings.ReplaceAll(queryString, ";", "")
queryString += ";"

var rows pgx.Rows
// now query the database with the correct number of arguments
if len(arguments) == 0 {
rows, err = globals.Db.Query(r.Context(), queryString)
} else {
rows, err = globals.Db.Query(r.Context(), queryString, arguments...)
}

api, err := pgxscan.NewDBScanAPI(dbscan.WithAllowUnknownColumns(true))
if err != nil {
errorHandler <- fmt.Errorf("error while querying the database: %w", err)
errorHandler <- fmt.Errorf("unable to prepare query parser: %w", err)
return
}
scanner, err := pgxscan.NewAPI(api)
if err != nil {
errorHandler <- fmt.Errorf("unable to create query parser: %w", err)
}

var locations []types.UsageLocation
err = pgxscan.ScanAll(&locations, rows)
err = scanner.Select(r.Context(), globals.Db, &locations, queryString, arguments...)
if err != nil {
errorHandler <- fmt.Errorf("unable to parse query result: %w", err)
errorHandler <- fmt.Errorf("unable to retrieve usage locations: %w", err)
return
}

Expand Down
17 changes: 14 additions & 3 deletions routes/single-water-right.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/textproto"
"strings"

"github.com/georgysavva/scany/v2/dbscan"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/go-chi/chi/v5"
errorMiddleware "github.com/wisdom-oss/microservice-middlewares/v5/error"
Expand Down Expand Up @@ -74,8 +75,18 @@ func SingleWaterRight(w http.ResponseWriter, r *http.Request) {
return
}

api, err := pgxscan.NewDBScanAPI(dbscan.WithAllowUnknownColumns(true))
if err != nil {
errorHandler <- fmt.Errorf("unable to prepare query parser: %w", err)
return
}
scanner, err := pgxscan.NewAPI(api)
if err != nil {
errorHandler <- fmt.Errorf("unable to create query parser: %w", err)
}

var usageLocations []types.UsageLocation
err = pgxscan.Select(r.Context(), globals.Db, &usageLocations, query, internalWaterRightID)
err = scanner.Select(r.Context(), globals.Db, &usageLocations, query, internalWaterRightID)
if err != nil {
errorHandler <- fmt.Errorf("unable to retrieve water right usage locations: %w", err)
return
Expand All @@ -87,7 +98,7 @@ func SingleWaterRight(w http.ResponseWriter, r *http.Request) {
return
}

encodedUsageLocations, err := json.Marshal(waterRight)
encodedUsageLocations, err := json.Marshal(usageLocations)
if err != nil {
errorHandler <- fmt.Errorf("unable to encode water right: %w", err)
return
Expand All @@ -114,7 +125,7 @@ func SingleWaterRight(w http.ResponseWriter, r *http.Request) {
usageLocationsPartHeader := make(textproto.MIMEHeader)
usageLocationsPartHeader.Set("Content-Disposition", `form-data; name="usage-locations"`)
usageLocationsPartHeader.Set("Content-Type", "application/json")
usageLocationsPart, err := multipartWriter.CreatePart(waterRightPartHeader)
usageLocationsPart, err := multipartWriter.CreatePart(usageLocationsPartHeader)
if err != nil {
errorHandler <- fmt.Errorf("unable to create multipart field for usage locations: %w", err)

Expand Down

0 comments on commit 087a2a7

Please sign in to comment.