diff --git a/database/mysql.go b/database/mysql.go index f3a48c06ce3a1af8f7a33d5a4b0ae16d03a1a7af..ef3ab9cb6dde80672656290986879b81eae2a99a 100644 --- a/database/mysql.go +++ b/database/mysql.go @@ -23,6 +23,7 @@ import ( "regexp" "strconv" "strings" + "time" "github.com/XiaoMi/soar/common" @@ -287,3 +288,17 @@ func (db *Connector) dangerousQuery(query string) bool { return false } + +// Sandard MySQL datetime format +const TimeFormat = "2006-01-02 15:04:05.000000000" + +// TimeString returns t as string in MySQL format Converts time.Time zero to MySQL zero. +func TimeString(t time.Time) string { + if t.IsZero() { + return "0000-00-00 00:00:00" + } + if t.Nanosecond() == 0 { + return t.Format(TimeFormat[:19]) + } + return t.Format(TimeFormat) +} diff --git a/database/sampling.go b/database/sampling.go index 51a98cb25fb7b5e1ecdedbe0cf98d90d846e8a68..0d63958116e00e8279aa52545eca4f75dabc897e 100644 --- a/database/sampling.go +++ b/database/sampling.go @@ -23,7 +23,6 @@ import ( "time" "github.com/XiaoMi/soar/common" - "github.com/ziutek/mymysql/mysql" ) /*-------------------- @@ -135,7 +134,7 @@ func (db *Connector) startSampling(onlineConn *sql.DB, database, table string, w case "TIMESTAMP", "DATETIME": t, err := time.Parse(time.RFC3339, string(val)) common.LogIfWarn(err, "") - values = append(values, fmt.Sprintf(`"%s"`, mysql.TimeString(t))) + values = append(values, fmt.Sprintf(`"%s"`, TimeString(t))) default: values = append(values, fmt.Sprintf(`unhex("%s")`, fmt.Sprintf("%x", val))) }