Mercurial > gemma
comparison pkg/imports/bn.go @ 1572:056a86b24be2
Made bottleneck primary key an int. Attention: This may break something!
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 12 Dec 2018 19:21:02 +0100 |
parents | cf0ca4a9812a |
children | c503d2fa9776 |
comparison
equal
deleted
inserted
replaced
1571:b3f2d43f43e4 | 1572:056a86b24be2 |
---|---|
30 Insecure bool `json:"insecure"` | 30 Insecure bool `json:"insecure"` |
31 } | 31 } |
32 | 32 |
33 const BNJobKind JobKind = "bn" | 33 const BNJobKind JobKind = "bn" |
34 | 34 |
35 const insertSQL = `INSERT INTO waterway.bottlenecks ( | 35 const ( |
36 hasBottleneckSQL = ` | |
37 SELECT true FROM waterway.bottlenecks WHERE bottleneck_id = $1` | |
38 | |
39 insertSQL = ` | |
40 INSERT INTO waterway.bottlenecks ( | |
36 bottleneck_id, | 41 bottleneck_id, |
37 fk_g_fid, | 42 fk_g_fid, |
38 objnam, | 43 objnam, |
39 nobjnm, | 44 nobjnm, |
40 stretch, | 45 stretch, |
58 $9, | 63 $9, |
59 $10, | 64 $10, |
60 $11, | 65 $11, |
61 $12, | 66 $12, |
62 $13 | 67 $13 |
63 ) ON CONFLICT (bottleneck_id) DO NOTHING` | 68 ) |
69 RETURNING id` | |
70 ) | |
64 | 71 |
65 type bnJobCreator struct{} | 72 type bnJobCreator struct{} |
66 | 73 |
67 func init() { | 74 func init() { |
68 RegisterJobCreator(BNJobKind, bnJobCreator{}) | 75 RegisterJobCreator(BNJobKind, bnJobCreator{}) |
154 if err != nil { | 161 if err != nil { |
155 return nil, err | 162 return nil, err |
156 } | 163 } |
157 defer tx.Rollback() | 164 defer tx.Rollback() |
158 | 165 |
166 hasStmt, err := tx.PrepareContext(ctx, hasBottleneckSQL) | |
167 if err != nil { | |
168 return nil, err | |
169 } | |
170 defer hasStmt.Close() | |
171 insertStmt, err := tx.PrepareContext(ctx, insertSQL) | |
172 if err != nil { | |
173 return nil, err | |
174 } | |
175 defer insertStmt.Close() | |
176 | |
177 var nids []int64 | |
178 | |
159 start := time.Now() | 179 start := time.Now() |
160 for i := range bns { | 180 |
161 bn := bns[i] | 181 nextBN: |
182 for _, bn := range bns { | |
183 | |
184 var found bool | |
185 err := hasStmt.QueryRowContext(ctx, bn.Bottleneck_id).Scan(&found) | |
186 switch { | |
187 case err == sql.ErrNoRows: | |
188 // This is good. | |
189 case err != nil: | |
190 return nil, err | |
191 case found: | |
192 continue nextBN | |
193 } | |
194 | |
162 rb, lb := splitRBLB(bn.Rb_lb) | 195 rb, lb := splitRBLB(bn.Rb_lb) |
163 | 196 |
164 var limiting, country string | 197 var limiting, country string |
165 | 198 |
166 if bn.Limiting_factor != nil { | 199 if bn.Limiting_factor != nil { |
169 | 202 |
170 if bn.Responsible_country != nil { | 203 if bn.Responsible_country != nil { |
171 country = string(*bn.Responsible_country) | 204 country = string(*bn.Responsible_country) |
172 } | 205 } |
173 | 206 |
174 tx.Exec(insertSQL, | 207 var nid int64 |
208 | |
209 err = insertStmt.QueryRowContext( | |
210 ctx, | |
175 bn.Bottleneck_id, | 211 bn.Bottleneck_id, |
176 bn.Fk_g_fid, | 212 bn.Fk_g_fid, |
177 bn.OBJNAM, | 213 bn.OBJNAM, |
178 bn.NOBJNM, | 214 bn.NOBJNM, |
179 bn.From_ISRS, bn.To_ISRS, | 215 bn.From_ISRS, bn.To_ISRS, |
182 country, | 218 country, |
183 revisitingTime(bn.Revisiting_time), | 219 revisitingTime(bn.Revisiting_time), |
184 limiting, | 220 limiting, |
185 bn.Date_Info, | 221 bn.Date_Info, |
186 bn.Source, | 222 bn.Source, |
187 ) | 223 ).Scan(&nid) |
188 feedback.Info("Insert \"%s\" into database", bn.OBJNAM) | 224 if err != nil { |
189 //TODO: Track for potential later removal? Bottlenecks have an string PK, track wants int64. | 225 return nil, err |
190 // if err = track(ctx, tx, importID, "waterway.bottlenecks", bn.Bottleneck_id); err != nil { | 226 } |
191 // return nil, err | 227 nids = append(nids, nid) |
192 // } | 228 feedback.Info("Inserted '%s'into database", bn.OBJNAM) |
193 } | 229 if err := track(ctx, tx, importID, "waterway.bottlenecks", nid); err != nil { |
194 | 230 return nil, err |
195 feedback.Info("Storing %d bottlenecks took %s", len(bns), time.Since(start)) | 231 } |
232 } | |
233 if len(nids) == 0 { | |
234 feedback.Error("No new bottlenecks found") | |
235 return nil, errors.New("No new bottlenecks found") | |
236 } | |
237 | |
238 feedback.Info("Storing %d bottlenecks took %s", len(nids), time.Since(start)) | |
196 if err = tx.Commit(); err == nil { | 239 if err = tx.Commit(); err == nil { |
197 feedback.Info("Import of bottlenecks was successful") | 240 feedback.Info("Import of bottlenecks was successful") |
198 } | 241 } |
199 | 242 |
200 // TODO: needs to be filled. | 243 // TODO: needs to be filled more useful. |
201 summary := struct { | 244 summary := struct { |
202 BottleneckCount int `json:"bottleneckCount"` | 245 Bottlenecks []int64 `json:"bottlenecks"` |
203 }{ | 246 }{ |
204 BottleneckCount: len(bns), | 247 Bottlenecks: nids, |
205 } | 248 } |
206 return &summary, err | 249 return &summary, err |
207 } | 250 } |