func InitAllTables(dbm *gorp.DbMap) error { defineAllTables(dbm) if err := dbm.CreateTablesIfNotExists(); err != nil { return err } txn, err := dbm.Begin() if err != nil { return err } defer txn.Rollback() count, err := CountRows("loglevel", "id", txn) if err != nil { return err } if count == 0 { InitLoglevelTable(txn) } count, err = CountRows("utmstatus", "id", txn) if err != nil { return err } if count == 0 { InitUtmstatusTable(txn) } txn.Commit() return nil }
func getSequence(dbMap *gorp.DbMap) (uint64, error) { tx, err := dbMap.Begin() if err != nil { return 0, err } defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() st, err := tx.Prepare("UPDATE seq set id = LAST_INSERT_ID(id + 1)") if err != nil { return 0, err } defer st.Close() st.Exec() stmt, err := tx.Prepare("SELECT LAST_INSERT_ID()") defer stmt.Close() var id uint64 err = stmt.QueryRow().Scan(&id) if err != nil { return 0, err } return id, nil }
func TransactionFactory(conn *gorp.DbMap) repo.TransactionFactory { return func() (repo.Transaction, error) { return conn.Begin() } }
func (wfc *WebFilterCommand) LoadFile( fname string, dbm *gorp.DbMap) (int64, error) { r1, _ := regexp.Compile(FILENAME_WLOG_PATTERN1) r2, _ := regexp.Compile(FILENAME_WLOG_PATTERN2) var dt1, dt2 time.Time var isPresent bool if m := r1.FindStringSubmatch(fname); m != nil { dt1, _ = time.Parse(FILENAME_WLOG_DATE_PATTERN, m[1]) dt2, _ = time.Parse(FILENAME_WLOG_DATE_PATTERN, m[2]) isPresent = false } else if m = r2.FindStringSubmatch(fname); m != nil { dt1, _ = time.Parse(FILENAME_WLOG_DATE_PATTERN, m[1]) isPresent = true } else { fmt.Println("Skipped file:", fname) return 0, nil } txn, err := dbm.Begin() if err != nil { return 0, err } defer txn.Rollback() fileRow, err := dal.GetFileByDate(txn, dt1) if err != nil { return 0, err } else if fileRow == nil { fileRow = &models.File{ Begin: dt1, Count: 0, } if !isPresent { fileRow.End = dt2 } err = txn.Insert(fileRow) if err != nil { return 0, err } } else if fileRow.End.Year() > 1 { return 0, nil } var reader io.Reader file, err := os.Open(fname) if err != nil { return 0, err } defer file.Close() gz, err := gzip.NewReader(file) if err != nil { return 0, err } defer gz.Close() reader = gz countInitial := fileRow.Count err = ParseFile(reader, fileRow, dbm) if err != nil { return 0, err } if !isPresent { fileRow.End = dt2 } txn.Update(fileRow) txn.Commit() return fileRow.Count - countInitial, nil }