comparison 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
comparison
equal deleted inserted replaced
4930:8b83b18a1d49 4931:e41d42be0e13
14 package imports 14 package imports
15 15
16 import ( 16 import (
17 "context" 17 "context"
18 "database/sql" 18 "database/sql"
19 "time"
20 19
21 "gemma.intevation.de/gemma/pkg/pgxutils" 20 "gemma.intevation.de/gemma/pkg/pgxutils"
22 ) 21 )
23 22
24 // Boycar is an import job to import 23 // Boycar is an import job to import
129 importID int64, 128 importID int64,
130 conn *sql.Conn, 129 conn *sql.Conn,
131 feedback Feedback, 130 feedback Feedback,
132 ) (interface{}, error) { 131 ) (interface{}, error) {
133 132
134 start := time.Now() 133 err := getFMFeatures(
135 134 ctx,
136 feedback.Info("Import fairway marks of type BOYCAR") 135 conn,
137
138 fms, epsg, err := getFMFeatures(
139 feedback, 136 feedback,
140 fm.FairwayMarks, 137 fm.FairwayMarks,
141 func() interface{} { return new(boycarProperties) }, 138 func() interface{} { return new(boycarProperties) },
142 func(p pointSlice, props interface{}) interface{} { 139 func(p pointSlice, props interface{}) interface{} {
143 return &boycarFeaturetype{p, props.(*boycarProperties)} 140 return &boycarFeaturetype{p, props.(*boycarProperties)}
144 }, 141 },
145 ) 142 func(
146 if err != nil { 143 tx *sql.Tx, epsg int, fms []interface{},
147 return nil, err 144 ) (outsideOrDup int, features int, err error) {
148 } 145
149 146 feedback.Info("Store fairway marks of type BOYCAR")
150 tx, err := conn.BeginTx(ctx, nil) 147
151 if err != nil { 148 insertStmt, err := tx.PrepareContext(ctx, insertBOYCARSQL)
152 return nil, err 149 if err != nil {
153 } 150 return
154 defer tx.Rollback() 151 }
155 152 defer insertStmt.Close()
156 insertStmt, err := tx.PrepareContext(ctx, insertBOYCARSQL) 153
157 if err != nil { 154 savepoint := Savepoint(ctx, tx, "feature")
158 return nil, err 155
159 } 156 for _, fm := range fms {
160 defer insertStmt.Close() 157
161 158 f := fm.(*boycarFeaturetype)
162 savepoint := Savepoint(ctx, tx, "feature") 159
163 160 var fmid int64
164 var ( 161 err := savepoint(func() error {
165 outsideOrDup int 162 err := insertStmt.QueryRowContext(
166 features int 163 ctx,
167 ) 164 f.geom.asWKB(),
168 for _, fm := range fms { 165 epsg,
169 166 f.props.Datsta,
170 f := fm.(*boycarFeaturetype) 167 f.props.Datend,
171 168 f.props.Persta,
172 var fmid int64 169 f.props.Perend,
173 err := savepoint(func() error { 170 f.props.Objnam,
174 err := insertStmt.QueryRowContext( 171 f.props.Nobjnm,
175 ctx, 172 f.props.Inform,
176 f.geom.asWKB(), 173 f.props.Ninfom,
177 epsg, 174 f.props.Scamin,
178 f.props.Datsta, 175 f.props.Picrep,
179 f.props.Datend, 176 f.props.Txtdsc,
180 f.props.Persta, 177 f.props.Sordat,
181 f.props.Perend, 178 f.props.Sorind,
182 f.props.Objnam, 179 f.props.Colour,
183 f.props.Nobjnm, 180 f.props.Colpat,
184 f.props.Inform, 181 f.props.Conrad,
185 f.props.Ninfom, 182 f.props.Marsys,
186 f.props.Scamin, 183 f.props.Boyshp,
187 f.props.Picrep, 184 f.props.Catcam,
188 f.props.Txtdsc, 185 ).Scan(&fmid)
189 f.props.Sordat, 186 return err
190 f.props.Sorind, 187 })
191 f.props.Colour, 188 switch {
192 f.props.Colpat, 189 case err == sql.ErrNoRows:
193 f.props.Conrad, 190 outsideOrDup++
194 f.props.Marsys, 191 // ignore -> filtered by responsibility_areas
195 f.props.Boyshp, 192 case err != nil:
196 f.props.Catcam, 193 feedback.Error(pgxutils.ReadableError{Err: err}.Error())
197 ).Scan(&fmid) 194 default:
198 return err 195 features++
196 }
197 }
198 return
199 }) 199 })
200 switch {
201 case err == sql.ErrNoRows:
202 outsideOrDup++
203 // ignore -> filtered by responsibility_areas
204 case err != nil:
205 feedback.Error(pgxutils.ReadableError{Err: err}.Error())
206 default:
207 features++
208 }
209 }
210
211 if outsideOrDup > 0 {
212 feedback.Info(
213 "Features outside responsibility area and duplicates: %d",
214 outsideOrDup)
215 }
216
217 if features == 0 {
218 err := UnchangedError("no valid new features found")
219 return nil, err
220 }
221
222 if err = tx.Commit(); err == nil {
223 feedback.Info("Storing %d features took %s",
224 features, time.Since(start))
225 }
226 200
227 return nil, err 201 return nil, err
228 } 202 }