cluster.go 1.4 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5
package console_mysql

import (
	"github.com/eolinker/goku/common/database"
	"github.com/eolinker/goku/server/entity"
Y
Your Name 已提交
6
	jsoniter "github.com/json-iterator/go"
E
eoLinker API Management 已提交
7 8
)

Y
Your Name 已提交
9 10 11 12
func InsertClusters(cs []*entity.ClusterInfo) error {
	db := database.GetConnection()
	stmt, e := db.Prepare("INSERT INTO `goku_cluster`(`name`,`title`,`note`,`db`,`redis`) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE `title`=VALUES(`title`),`note`=VALUES(`note`),`db`=VALUES(`db`),`redis`=VALUES(`redis`)")
	if e != nil {
E
eoLinker API Management 已提交
13 14
		return e
	}
Y
Your Name 已提交
15 16 17 18
	for _, c := range cs {
		db, _ := jsoniter.MarshalToString(c.DB)
		redis, _ := jsoniter.MarshalToString(c.Redis)
		stmt.Exec(c.Name, c.Title, c.Note, db, redis)
E
eoLinker API Management 已提交
19 20
	}

Y
Your Name 已提交
21
	return nil
E
eoLinker API Management 已提交
22
}
Y
Your Name 已提交
23 24 25
func LoadClusters() (map[string]*entity.ClusterInfo, error) {
	db := database.GetConnection()
	sql := "select `id`, `name`,`title`,`note`,`db`,`redis` from `goku_cluster`;"
E
eoLinker API Management 已提交
26
	stmt, e := db.Prepare(sql)
Y
Your Name 已提交
27 28
	if e != nil {
		return nil, e
E
eoLinker API Management 已提交
29 30 31
	}

	rows, e := stmt.Query()
Y
Your Name 已提交
32 33
	if e != nil {
		return nil, e
E
eoLinker API Management 已提交
34 35 36
	}

	vs := make(map[string]*entity.ClusterInfo)
Y
Your Name 已提交
37
	for rows.Next() {
E
eoLinker API Management 已提交
38
		v := new(entity.ClusterInfo)
Y
Your Name 已提交
39 40 41 42
		db := ""
		redis := ""
		err := rows.Scan(&v.Id, &v.Name, &v.Title, &v.Note, &db, &redis)
		if err != nil {
E
eoLinker API Management 已提交
43 44 45
			return nil, nil
		}

Y
Your Name 已提交
46 47
		err = jsoniter.UnmarshalFromString(db, &v.DB)
		if err != nil {
E
eoLinker API Management 已提交
48 49 50
			return nil, nil
		}

Y
Your Name 已提交
51 52
		err = jsoniter.UnmarshalFromString(redis, &v.Redis)
		if err != nil {
E
eoLinker API Management 已提交
53 54 55 56 57 58
			return nil, nil
		}
		vs[v.Name] = v
	}
	return vs, nil
}