update.go 1.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package dao

import (
	"fmt"
	"strings"
	"time"

黄孟柱 已提交
8
	"github.com/eolinker/goku-api-gateway/common/database"
E
eoLinker API Management 已提交
9 10
)

Y
Your Name 已提交
11 12
//GetLastUpdateOfAPI 获取最后更新的接口记录
func GetLastUpdateOfAPI(tables ...string) (time.Time, error) {
E
eoLinker API Management 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	t := time.Time{}
	var updateTime string
	tb := make([]string, len(tables))
	tv := make([]interface{}, len(tables))
	for i, table := range tables {
		tb[i] = "?"
		tv[i] = table
	}
	tablesStr := strings.Join(tb, ",")
	db := database.GetConnection()

	sql := fmt.Sprintf("SELECT updateTime FROM goku_table_update_record WHERE name IN (%s) ORDER BY updateTime desc LIMIT 1;", tablesStr)

	err := db.QueryRow(sql, tv...).Scan(&updateTime)
	if err != nil {
		return t, err
	}
	t, _ = time.ParseInLocation("2006-01-02 15:04:05", updateTime, time.Local)
	return t, nil
}

Y
Your Name 已提交
34
//UpdateTable 更新goku_table_update_record的updateTime字段
E
eoLinker API Management 已提交
35 36 37 38 39 40 41 42 43 44 45
func UpdateTable(name string) error {
	db := database.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	sql := "INSERT INTO `goku_table_update_record` (`name`,`updateTime`) VALUES (?,?) ON DUPLICATE KEY UPDATE `updateTime` = VALUES(`updateTime`)"
	_, err := db.Exec(sql, name, now)
	if err != nil {
		panic(err)
		return err
	}
	return nil
}