func main() { flag.Parse() if flag.NArg() == 0 { fmt.Println("no args") return } config := flag.Arg(0) var err error cfg, err = goconfig.LoadConfigFile(config) if err != nil { fmt.Printf("%s\n", err) panic(err) } retention.TraceFlag = cfg.MustBool(goconfig.DEFAULT_SECTION, "traceFlag") dsn, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "dsn") sqlstr, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "sql") retention.Trace(sqlstr) rows, err := mysqldb.GetRows(dsn, sqlstr) if err != nil { panic(err.Error()) // proper error handling instead of panic in your app } retention.Trace(rows) retention.AnalysisRows(rows) retention.Output() dsn2, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "dsn2") sqlstr2, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "sql2") mysqldb.InsertRows(dsn2, sqlstr2, retention.Results()) }
func GetRows(dsn string, sqlstr string) (*sql.Rows, error) { db, err := sql.Open("mysql", dsn) if err != nil { fmt.Println(err) } defer db.Close() err = db.Ping() retention.Trace(err) retention.Trace(sqlstr) rows, err := db.Query(sqlstr) if err != nil { panic(err.Error()) // proper error handling instead of panic in your app } return rows, err }
func InsertRows(dsn string, sqlstr string, results []*retention.Result) { db, err := sql.Open("mysql", dsn) if err != nil { fmt.Println(err) } defer db.Close() err = db.Ping() retention.Trace(err) for _, r := range results { ddate := r.Date.Format("2006-01-02 15:04:05") ddate = strings.Split(ddate, " ")[0] sqlone := strings.Replace(sqlstr, "@0", "'"+ddate+"'", 1) for i := 1; i <= 10; i++ { sqlone = strings.Replace(sqlone, "@"+strconv.Itoa(i), strconv.Itoa(r.Daycnts[i-1].Cnt), 1) } if retention.TraceFlag == true { fmt.Println(sqlone) } _, err := db.Exec(sqlone) if err != nil { panic(err.Error()) // proper error handling instead of panic in your app } } }