Mercurial > gemma
comparison pkg/imports/bn.go @ 1580:c503d2fa9776
Bottleneck import: Use a prepared statement for import tracking, too.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 13 Dec 2018 14:06:48 +0100 |
parents | 056a86b24be2 |
children | e3abd3b3ebc6 |
comparison
equal
deleted
inserted
replaced
1576:b5d7770fe632 | 1580:c503d2fa9776 |
---|---|
161 if err != nil { | 161 if err != nil { |
162 return nil, err | 162 return nil, err |
163 } | 163 } |
164 defer tx.Rollback() | 164 defer tx.Rollback() |
165 | 165 |
166 hasStmt, err := tx.PrepareContext(ctx, hasBottleneckSQL) | 166 var hasStmt, insertStmt, trackStmt *sql.Stmt |
167 if err != nil { | 167 |
168 return nil, err | 168 for _, x := range []struct { |
169 } | 169 sql string |
170 defer hasStmt.Close() | 170 stmt **sql.Stmt |
171 insertStmt, err := tx.PrepareContext(ctx, insertSQL) | 171 }{ |
172 if err != nil { | 172 {hasBottleneckSQL, &hasStmt}, |
173 return nil, err | 173 {insertSQL, &insertStmt}, |
174 } | 174 {trackImportSQL, &trackStmt}, |
175 defer insertStmt.Close() | 175 } { |
176 var err error | |
177 if *x.stmt, err = tx.PrepareContext(ctx, x.sql); err != nil { | |
178 return nil, err | |
179 } | |
180 defer (*x.stmt).Close() | |
181 } | |
176 | 182 |
177 var nids []int64 | 183 var nids []int64 |
178 | 184 |
179 start := time.Now() | 185 start := time.Now() |
180 | 186 |
187 case err == sql.ErrNoRows: | 193 case err == sql.ErrNoRows: |
188 // This is good. | 194 // This is good. |
189 case err != nil: | 195 case err != nil: |
190 return nil, err | 196 return nil, err |
191 case found: | 197 case found: |
198 // TODO: Deep comparison database vs. SOAP. | |
192 continue nextBN | 199 continue nextBN |
193 } | 200 } |
194 | 201 |
195 rb, lb := splitRBLB(bn.Rb_lb) | 202 rb, lb := splitRBLB(bn.Rb_lb) |
196 | 203 |
223 ).Scan(&nid) | 230 ).Scan(&nid) |
224 if err != nil { | 231 if err != nil { |
225 return nil, err | 232 return nil, err |
226 } | 233 } |
227 nids = append(nids, nid) | 234 nids = append(nids, nid) |
235 if _, err := trackStmt.ExecContext( | |
236 ctx, | |
237 tx, importID, "waterway.bottlenecks", nid, | |
238 ); err != nil { | |
239 return nil, err | |
240 } | |
228 feedback.Info("Inserted '%s'into database", bn.OBJNAM) | 241 feedback.Info("Inserted '%s'into database", bn.OBJNAM) |
229 if err := track(ctx, tx, importID, "waterway.bottlenecks", nid); err != nil { | |
230 return nil, err | |
231 } | |
232 } | 242 } |
233 if len(nids) == 0 { | 243 if len(nids) == 0 { |
234 feedback.Error("No new bottlenecks found") | 244 feedback.Error("No new bottlenecks found") |
235 return nil, errors.New("No new bottlenecks found") | 245 return nil, errors.New("No new bottlenecks found") |
236 } | 246 } |