cluster.go 1.3 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package console_mysql

import (
	"github.com/eolinker/goku/common/database"
	"github.com/eolinker/goku/server/entity"
	"github.com/json-iterator/go"
)

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{
		return e
	}
	for _,c:=range cs{
		db,_:=jsoniter.MarshalToString(c.DB)
		redis,_:=jsoniter.MarshalToString(c.Redis)
		stmt.Exec(c.Name,c.Title,c.Note,db,redis)
	}

	return  nil
}
func LoadClusters()(map[string]*entity.ClusterInfo,error)  {
	db:=database.GetConnection()
	sql:="select `id`, `name`,`title`,`note`,`db`,`redis` from `goku_cluster`;"
	stmt, e := db.Prepare(sql)
	if e!=nil{
		return nil,e
	}

	rows, e := stmt.Query()
	if e!= nil{
		return nil,e
	}

	vs := make(map[string]*entity.ClusterInfo)
	for rows.Next(){
		v := new(entity.ClusterInfo)
		db:= ""
		redis:= ""
		err:=rows.Scan(&v.Id,&v.Name,&v.Title,&v.Note,&db,&redis)
		if err!=nil{
			return nil, nil
		}

		err = jsoniter.UnmarshalFromString(db,&v.DB)
		if err!=nil{
			return nil, nil
		}

		err = jsoniter.UnmarshalFromString(redis,&v.Redis)
		if err!=nil{
			return nil, nil
		}
		vs[v.Name] = v
	}
	return vs, nil
}