From 087a2a7f5aede1a245cce1503d7dff0d966e8c95 Mon Sep 17 00:00:00 2001 From: Jan Eike Suchard Date: Wed, 29 May 2024 11:15:46 +0200 Subject: [PATCH] allowing scanning of unknown fields --- routes/locations.go | 25 +++++++++++-------------- routes/single-water-right.go | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/routes/locations.go b/routes/locations.go index 030d3d2..9b7744f 100644 --- a/routes/locations.go +++ b/routes/locations.go @@ -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" @@ -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 @@ -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 } diff --git a/routes/single-water-right.go b/routes/single-water-right.go index 44068d3..2864949 100644 --- a/routes/single-water-right.go +++ b/routes/single-water-right.go @@ -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" @@ -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 @@ -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 @@ -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)