(input <-chan baker.OutputRecord)
| 332 | } |
| 333 | |
| 334 | func (c *SQLite) doRun(input <-chan baker.OutputRecord) error { |
| 335 | // Install a deferred rollback; if something errors out, we execute |
| 336 | // tx.Rollback(). |
| 337 | // |
| 338 | // At the end we set commitDone to true so this logic won't actually |
| 339 | // run if commit is successful. |
| 340 | commitDone := false |
| 341 | defer func() { |
| 342 | if !commitDone { |
| 343 | c.tx.Rollback() |
| 344 | } |
| 345 | }() |
| 346 | |
| 347 | if err := c.setupTable(c.tx); err != nil { |
| 348 | return fmt.Errorf("setup table: %s", err) |
| 349 | } |
| 350 | insert, err := c.prepInsertStatement(c.tx) |
| 351 | if err != nil { |
| 352 | return fmt.Errorf("build insert statement: %s", err) |
| 353 | } |
| 354 | |
| 355 | ncols := len(c.fieldNames) |
| 356 | if c.isRaw { |
| 357 | ncols++ |
| 358 | } |
| 359 | |
| 360 | values := make([]interface{}, ncols) |
| 361 | |
| 362 | for lldata := range input { |
| 363 | for i, str := range lldata.Fields { |
| 364 | values[i] = str |
| 365 | } |
| 366 | if c.isRaw { |
| 367 | values[len(values)-1] = lldata.Record |
| 368 | } |
| 369 | _, err = insert.Exec(values...) |
| 370 | if err != nil { |
| 371 | insert.Close() |
| 372 | return fmt.Errorf("cannot insert to SQLite file: %s", err) |
| 373 | } |
| 374 | c.nEvents++ |
| 375 | } |
| 376 | insert.Close() |
| 377 | |
| 378 | // Run final post-commands, if any are configured. |
| 379 | if err = runSQLCommands(c.tx, c.cfg.PostRun); err != nil { |
| 380 | return fmt.Errorf("cannot run post commands: %s", err) |
| 381 | } |
| 382 | |
| 383 | // Commit all changes. |
| 384 | if err = c.tx.Commit(); err != nil { |
| 385 | return fmt.Errorf("cannot commit SQLite transaction: %s", err) |
| 386 | } |
| 387 | commitDone = true |
| 388 | |
| 389 | // Vacuum the SQLite file, if defined so in configuration |
| 390 | if c.cfg.Vacuum { |
| 391 | _, err = c.conn.Exec("VACUUM") |
no test coverage detected