delete.go 878 字节
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8
package dao_service

import (
	"fmt"
)

const sqlDelete = "DELETE FROM  `goku_service_config` WHERE  `name` = ? AND NOT EXISTS (SELECT * FROM `goku_balance` B WHERE B.`serviceName` =  `goku_service_config`.`name` ) "

Y
Your Name 已提交
9
//DeleteError delete error
E
eoLinker API Management 已提交
10 11
type DeleteError string

Y
Your Name 已提交
12 13
func (e DeleteError) Error() string {
	return fmt.Sprintf("can not delete :%s", string(e))
E
eoLinker API Management 已提交
14 15
}

Y
Your Name 已提交
16
//Delete 删除服务发现
Y
Your Name 已提交
17
func (d *ServiceDao) Delete(names []string) error {
E
eoLinker API Management 已提交
18

Y
Your Name 已提交
19
	tx, err := d.db.Begin()
Y
Your Name 已提交
20
	if err != nil {
E
eoLinker API Management 已提交
21 22 23 24
		return err
	}

	stmt, e := tx.Prepare(sqlDelete)
Y
Your Name 已提交
25
	if e != nil {
E
eoLinker API Management 已提交
26 27 28 29 30
		return e
	}

	defer stmt.Close()

Y
Your Name 已提交
31 32 33
	for _, name := range names {
		r, e := stmt.Exec(name)
		if e != nil {
E
eoLinker API Management 已提交
34 35 36
			tx.Rollback()
			return e
		}
Y
Your Name 已提交
37 38
		rowCount, err := r.RowsAffected()
		if err != nil {
E
eoLinker API Management 已提交
39 40 41
			tx.Rollback()
			return e
		}
Y
Your Name 已提交
42
		if rowCount == 0 {
E
eoLinker API Management 已提交
43 44 45 46 47 48 49
			tx.Rollback()
			return DeleteError(name)
		}
	}

	return tx.Commit()

Y
Your Name 已提交
50
}