update.go 1.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
package dao

import (
	"fmt"
	"strings"
	"time"

	"github.com/eolinker/goku/common/database"
)

func GetLastUpdateOfApi(tables ...string) (time.Time, error) {
	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
}

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
}