// ExecuteSQL is execute SQL to aws rds func (c *Command) ExecuteSQL(args *ExecuteSQLArgs) ([]time.Duration, error) { driver, dsn := c.getDbOpenValues(args) if driver == "" { log.Errorf("%s", ErrDriverNotFound.Error()) return nil, ErrDriverNotFound } db, err := sql.Open(driver, dsn) if err != nil { log.Errorf("%s", err.Error()) return nil, err } defer db.Close() times := make([]time.Duration, 0, len(args.Queries)) for _, value := range args.Queries { log.Debugf("query value : %s", value) sTime := time.Now() log.Infof("query start time: %s", sTime) result, err := db.Query(value.SQL) if err != nil { log.Errorf("%s", err.Error()) return times, err } eTime := time.Now() log.Infof("query end time: %s", eTime) times = append(times, eTime.Sub(sTime)) // output csv file cols, _ := result.Columns() if c.OutConfig.File && len(cols) > 0 { fileName := value.Name + "-" + utils.GetFormatedTime() + ".csv" outPath := utils.GetHomeDir() if c.OutConfig.Root != "" { outPath = c.OutConfig.Root } outState := writeCSVFile( &writeCSVFileArgs{ Rows: result, FileName: fileName, Path: outPath, Bom: c.OutConfig.Bom, }) log.Debugf("out_state:%+v", outState) } result.Close() } return times, nil }
// use the tag for identification func getSpecifyTags() []*rds.Tag { var tagList []*rds.Tag // append name keyName := rtNameText valueName := utils.GetFormatedAppName() tagName := &rds.Tag{ Key: &keyName, Value: &valueName, } tagList = append(tagList, tagName) // append time keyTime := rtTimeText valueTime := utils.GetFormatedTime() tagTime := &rds.Tag{ Key: &keyTime, Value: &valueTime, } tagList = append(tagList, tagTime) return tagList }