Mercurial > gemma
comparison pkg/imports/agm.go @ 4030:040a5dc95eb9
AGM import: Moved parsing of CVS header line to separate function.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 23 Jul 2019 10:36:56 +0200 |
parents | b17453420eff |
children | 2fcfae3daa7d |
comparison
equal
deleted
inserted
replaced
4027:b17453420eff | 4030:040a5dc95eb9 |
---|---|
211 ` | 211 ` |
212 ) | 212 ) |
213 | 213 |
214 var errContinue = errors.New("continue") | 214 var errContinue = errors.New("continue") |
215 | 215 |
216 func parseAGMHeaders(headers []string, fkGaugeIDIdx, measureDateIdx, valueIdx *int) error { | |
217 | |
218 headerFields := []struct { | |
219 idx *int | |
220 name string | |
221 }{ | |
222 {fkGaugeIDIdx, "fk_gauge_id"}, | |
223 {measureDateIdx, "measure_date"}, | |
224 {valueIdx, "value"}, // "water_level", | |
225 } | |
226 | |
227 nextHeader: | |
228 for i, f := range headers { | |
229 h := strings.Replace(strings.ToLower( | |
230 strings.TrimSpace(f)), " ", "_", -1) | |
231 | |
232 for j := range headerFields { | |
233 if headerFields[j].name == h { | |
234 if *headerFields[j].idx != -1 { | |
235 return fmt.Errorf( | |
236 "There is more than one column namend '%s'", h) | |
237 } | |
238 *headerFields[j].idx = i | |
239 continue nextHeader | |
240 } | |
241 } | |
242 } | |
243 | |
244 var missing []string | |
245 for i := range headerFields { | |
246 if headerFields[i].name != "unit" && *headerFields[i].idx == -1 { | |
247 missing = append(missing, headerFields[i].name) | |
248 } | |
249 } | |
250 if len(missing) > 0 { | |
251 return fmt.Errorf("Missing columns: %s", strings.Join(missing, ", ")) | |
252 } | |
253 | |
254 return nil | |
255 } | |
256 | |
216 // Do executes the actual approved gauge measurements import. | 257 // Do executes the actual approved gauge measurements import. |
217 func (agm *ApprovedGaugeMeasurements) Do( | 258 func (agm *ApprovedGaugeMeasurements) Do( |
218 ctx context.Context, | 259 ctx context.Context, |
219 importID int64, | 260 importID int64, |
220 conn *sql.Conn, | 261 conn *sql.Conn, |
242 fkGaugeIDIdx = -1 | 283 fkGaugeIDIdx = -1 |
243 measureDateIdx = -1 | 284 measureDateIdx = -1 |
244 valueIdx = -1 | 285 valueIdx = -1 |
245 ) | 286 ) |
246 | 287 |
247 headerFields := []struct { | 288 if err := parseAGMHeaders( |
248 idx *int | 289 headers, |
249 name string | 290 &fkGaugeIDIdx, &measureDateIdx, &valueIdx, |
250 }{ | 291 ); err != nil { |
251 {&fkGaugeIDIdx, "fk_gauge_id"}, | 292 return nil, err |
252 {&measureDateIdx, "measure_date"}, | |
253 {&valueIdx, "value"}, // "water_level", | |
254 } | |
255 | |
256 nextHeader: | |
257 for i, f := range headers { | |
258 h := strings.Replace(strings.ToLower( | |
259 strings.TrimSpace(f)), " ", "_", -1) | |
260 | |
261 for j := range headerFields { | |
262 if headerFields[j].name == h { | |
263 if *headerFields[j].idx != -1 { | |
264 return nil, fmt.Errorf( | |
265 "There is more than one column namend '%s'", h) | |
266 } | |
267 *headerFields[j].idx = i | |
268 continue nextHeader | |
269 } | |
270 } | |
271 } | |
272 | |
273 var missing []string | |
274 for i := range headerFields { | |
275 if headerFields[i].name != "unit" && *headerFields[i].idx == -1 { | |
276 missing = append(missing, headerFields[i].name) | |
277 } | |
278 } | |
279 if len(missing) > 0 { | |
280 return nil, fmt.Errorf("Missing columns: %s", strings.Join(missing, ", ")) | |
281 } | 293 } |
282 | 294 |
283 gaugeCheckStmt, err := conn.PrepareContext(ctx, agmGaugeCheckSQL) | 295 gaugeCheckStmt, err := conn.PrepareContext(ctx, agmGaugeCheckSQL) |
284 if err != nil { | 296 if err != nil { |
285 return nil, err | 297 return nil, err |