diff pkg/imports/fm_boycar.go @ 4931:e41d42be0e13 fairway-marks-import

One more callback to avoid code duplication more consequently
author Tom Gottfried <tom@intevation.de>
date Fri, 14 Feb 2020 19:34:08 +0100
parents b86ce7fc4da3
children 9f9d72a1d398
line wrap: on
line diff
--- a/pkg/imports/fm_boycar.go	Fri Feb 14 17:31:17 2020 +0100
+++ b/pkg/imports/fm_boycar.go	Fri Feb 14 19:34:08 2020 +0100
@@ -16,7 +16,6 @@
 import (
 	"context"
 	"database/sql"
-	"time"
 
 	"gemma.intevation.de/gemma/pkg/pgxutils"
 )
@@ -131,98 +130,73 @@
 	feedback Feedback,
 ) (interface{}, error) {
 
-	start := time.Now()
-
-	feedback.Info("Import fairway marks of type BOYCAR")
-
-	fms, epsg, err := getFMFeatures(
+	err := getFMFeatures(
+		ctx,
+		conn,
 		feedback,
 		fm.FairwayMarks,
 		func() interface{} { return new(boycarProperties) },
 		func(p pointSlice, props interface{}) interface{} {
 			return &boycarFeaturetype{p, props.(*boycarProperties)}
 		},
-	)
-	if err != nil {
-		return nil, err
-	}
+		func(
+			tx *sql.Tx, epsg int, fms []interface{},
+		) (outsideOrDup int, features int, err error) {
 
-	tx, err := conn.BeginTx(ctx, nil)
-	if err != nil {
-		return nil, err
-	}
-	defer tx.Rollback()
+			feedback.Info("Store fairway marks of type BOYCAR")
 
-	insertStmt, err := tx.PrepareContext(ctx, insertBOYCARSQL)
-	if err != nil {
-		return nil, err
-	}
-	defer insertStmt.Close()
-
-	savepoint := Savepoint(ctx, tx, "feature")
+			insertStmt, err := tx.PrepareContext(ctx, insertBOYCARSQL)
+			if err != nil {
+				return
+			}
+			defer insertStmt.Close()
 
-	var (
-		outsideOrDup int
-		features     int
-	)
-	for _, fm := range fms {
+			savepoint := Savepoint(ctx, tx, "feature")
 
-		f := fm.(*boycarFeaturetype)
+			for _, fm := range fms {
+
+				f := fm.(*boycarFeaturetype)
 
-		var fmid int64
-		err := savepoint(func() error {
-			err := insertStmt.QueryRowContext(
-				ctx,
-				f.geom.asWKB(),
-				epsg,
-				f.props.Datsta,
-				f.props.Datend,
-				f.props.Persta,
-				f.props.Perend,
-				f.props.Objnam,
-				f.props.Nobjnm,
-				f.props.Inform,
-				f.props.Ninfom,
-				f.props.Scamin,
-				f.props.Picrep,
-				f.props.Txtdsc,
-				f.props.Sordat,
-				f.props.Sorind,
-				f.props.Colour,
-				f.props.Colpat,
-				f.props.Conrad,
-				f.props.Marsys,
-				f.props.Boyshp,
-				f.props.Catcam,
-			).Scan(&fmid)
-			return err
+				var fmid int64
+				err := savepoint(func() error {
+					err := insertStmt.QueryRowContext(
+						ctx,
+						f.geom.asWKB(),
+						epsg,
+						f.props.Datsta,
+						f.props.Datend,
+						f.props.Persta,
+						f.props.Perend,
+						f.props.Objnam,
+						f.props.Nobjnm,
+						f.props.Inform,
+						f.props.Ninfom,
+						f.props.Scamin,
+						f.props.Picrep,
+						f.props.Txtdsc,
+						f.props.Sordat,
+						f.props.Sorind,
+						f.props.Colour,
+						f.props.Colpat,
+						f.props.Conrad,
+						f.props.Marsys,
+						f.props.Boyshp,
+						f.props.Catcam,
+					).Scan(&fmid)
+					return err
+				})
+				switch {
+				case err == sql.ErrNoRows:
+					outsideOrDup++
+					// ignore -> filtered by responsibility_areas
+				case err != nil:
+					feedback.Error(pgxutils.ReadableError{Err: err}.Error())
+				default:
+					features++
+				}
+			}
+			return
 		})
-		switch {
-		case err == sql.ErrNoRows:
-			outsideOrDup++
-			// ignore -> filtered by responsibility_areas
-		case err != nil:
-			feedback.Error(pgxutils.ReadableError{Err: err}.Error())
-		default:
-			features++
-		}
-	}
-
-	if outsideOrDup > 0 {
-		feedback.Info(
-			"Features outside responsibility area and duplicates: %d",
-			outsideOrDup)
-	}
-
-	if features == 0 {
-		err := UnchangedError("no valid new features found")
-		return nil, err
-	}
-
-	if err = tx.Commit(); err == nil {
-		feedback.Info("Storing %d features took %s",
-			features, time.Since(start))
-	}
 
 	return nil, err
 }