add.go 4.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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
package dao_balance

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

func Add(name, serviceName, desc, appName, static, staticCluster, now string) (string, error) {

	//const sql = "INSERT INTO goku_balance (`balanceName`,`serviceName`,`appName`,`balanceDesc`,`static`,`staticCluster`,`createTime`,`updateTime`) VALUES (?,?,?,?,?,?,?,?,);"
	//
	//db := database.GetConnection()
	//stmt, err := db.Prepare(sql)
	//if err != nil {
	//	return "[ERROR]Illegal SQL statement!", err
	//}
	//defer stmt.Close()
	//_, err = stmt.Exec(name,serviceName,appName, desc, static, staticCluster, now, now)
	//if err != nil {
	//	return "[ERROR]Failed to add data!", err
	//}
	return "", nil
}
func AddStatic(name, serviceName, static, staticCluster, desc, now string) (string, error) {

	const sql = "INSERT INTO goku_balance (`balanceName`,`serviceName`,`static`,`staticCluster`,`balanceDesc`,`createTime`,`updateTime`,`appName`,`defaultConfig`,`clusterConfig`,`balanceConfig`) VALUES (?,?,?,?,?,?,?,'','','','');"

	db := database.GetConnection()
	stmt, err := db.Prepare(sql)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(name, serviceName, static, staticCluster, desc, now, now)
	if err != nil {
		return "[ERROR]Failed to add data!", err
	}
	return "", nil
}
func AddDiscovery(name, serviceName, appName, desc, now string) (string, error) {

	const sql = "INSERT INTO goku_balance (`balanceName`,`serviceName`,`appName`,`balanceDesc`,`createTime`,`updateTime`,`static`,`staticCluster`,`defaultConfig`,`clusterConfig`,`balanceConfig`) VALUES (?,?,?,?,?,?,'','','','','');"

	db := database.GetConnection()
	stmt, err := db.Prepare(sql)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(name, serviceName, appName, desc, now, now)
	if err != nil {
		return "[ERROR]Failed to add data!", err
	}
	return "", nil
}
func SaveStatic(name, serviceName, static, staticCluster, desc string, now string) (string, error) {
	const sql = "UPDATE `goku_balance` SET `serviceName`=? ,`static` = ?,`staticCluster`=?,`balanceDesc` =?,`updateTime`=? WHERE `balanceName`=?;"
	db := database.GetConnection()
	stmt, err := db.Prepare(sql)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(serviceName, static, staticCluster, desc, now, name)
	if err != nil {
		return "[ERROR]Failed to add data!", err
	}
	return "", nil
}
func SaveDiscover(name, serviceName, appName, desc string, now string) (string, error) {
	const sql = "UPDATE `goku_balance` SET `serviceName`=? ,`appName` = ?,`balanceDesc` =?,`updateTime`=? WHERE `balanceName`=?;"
	db := database.GetConnection()
	stmt, err := db.Prepare(sql)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	defer stmt.Close()
	_, err = stmt.Exec(serviceName, appName, desc, now, name)
	if err != nil {
		return "[ERROR]Failed to add data!", err
	}
	return "", nil
}
func Save(name, desc, static, staticCluster, now string) (string, error) {
	//const sql = "UPDATE `goku_balance` SET `balanceDesc` = ?,`static` =?,`staticCluster`=?,`updateTime`=? WHERE `balanceName` = ?;"
	//
	//db := database.GetConnection()
	//stmt, err := db.Prepare(sql)
	//if err != nil {
	//	return "[ERROR]Illegal SQL statement!", err
	//}
	//defer stmt.Close()
	//_, err = stmt.Exec(desc, defaultConfig, clusterConfig, now, name)
	//if err != nil {
	//	return "[ERROR]Failed to add data!", err
	//}
	return "", nil
}

func Delete(name string) (string, error) {
	const sql = "DELETE FROM `goku_balance` WHERE  `balanceName`= ?;"
	db := database.GetConnection()
	stmt, err := db.Prepare(sql)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	_, err = stmt.Exec(name)
	if err != nil {
		return "[ERROR]DELETE fail", err
	}
	return "", nil

}

// 批量删除负载
func BatchDelete(balanceNames []string) (string, error) {
	db := database.GetConnection()
	sql := "DELETE FROM `goku_balance` WHERE  `balanceName` = ?;"
	sql2 := "UPDATE goku_conn_strategy_api SET target = '' WHERE target = ?"
	stmt, err := db.Prepare(sql)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	stmt2, err := db.Prepare(sql2)
	if err != nil {
		return "[ERROR]Illegal SQL statement!", err
	}
	for _, balanceName := range balanceNames {
		stmt.Exec(balanceName)
		stmt2.Exec(balanceName)
	}
	return "", nil
}