mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-11 13:53:35 +08:00
所有数据库相关的操作支持事务
This commit is contained in:
@@ -40,19 +40,19 @@ func init() {
|
||||
func (this *HTTPFirewallPolicyDAO) Init() {
|
||||
this.DAOObject.Init()
|
||||
this.DAOObject.OnUpdate(func() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
return SharedSysEventDAO.CreateEvent(nil, NewServerChangeEvent())
|
||||
})
|
||||
this.DAOObject.OnInsert(func() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
return SharedSysEventDAO.CreateEvent(nil, NewServerChangeEvent())
|
||||
})
|
||||
this.DAOObject.OnDelete(func() error {
|
||||
return SharedSysEventDAO.CreateEvent(NewServerChangeEvent())
|
||||
return SharedSysEventDAO.CreateEvent(nil, NewServerChangeEvent())
|
||||
})
|
||||
}
|
||||
|
||||
// 启用条目
|
||||
func (this *HTTPFirewallPolicyDAO) EnableHTTPFirewallPolicy(id int64) error {
|
||||
_, err := this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) EnableHTTPFirewallPolicy(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", HTTPFirewallPolicyStateEnabled).
|
||||
Update()
|
||||
@@ -60,8 +60,8 @@ func (this *HTTPFirewallPolicyDAO) EnableHTTPFirewallPolicy(id int64) error {
|
||||
}
|
||||
|
||||
// 禁用条目
|
||||
func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(id int64) error {
|
||||
_, err := this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", HTTPFirewallPolicyStateDisabled).
|
||||
Update()
|
||||
@@ -69,8 +69,8 @@ func (this *HTTPFirewallPolicyDAO) DisableHTTPFirewallPolicy(id int64) error {
|
||||
}
|
||||
|
||||
// 查找启用中的条目
|
||||
func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicy(id int64) (*HTTPFirewallPolicy, error) {
|
||||
result, err := this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicy(tx *dbs.Tx, id int64) (*HTTPFirewallPolicy, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Attr("state", HTTPFirewallPolicyStateEnabled).
|
||||
Find()
|
||||
@@ -81,16 +81,16 @@ func (this *HTTPFirewallPolicyDAO) FindEnabledHTTPFirewallPolicy(id int64) (*HTT
|
||||
}
|
||||
|
||||
// 根据主键查找名称
|
||||
func (this *HTTPFirewallPolicyDAO) FindHTTPFirewallPolicyName(id int64) (string, error) {
|
||||
return this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) FindHTTPFirewallPolicyName(tx *dbs.Tx, id int64) (string, error) {
|
||||
return this.Query(tx).
|
||||
Pk(id).
|
||||
Result("name").
|
||||
FindStringCol("")
|
||||
}
|
||||
|
||||
// 查找所有可用策略
|
||||
func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies() (result []*HTTPFirewallPolicy, err error) {
|
||||
_, err = this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies(tx *dbs.Tx) (result []*HTTPFirewallPolicy, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(HTTPFirewallPolicyStateEnabled).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
@@ -99,7 +99,7 @@ func (this *HTTPFirewallPolicyDAO) FindAllEnabledFirewallPolicies() (result []*H
|
||||
}
|
||||
|
||||
// 创建策略
|
||||
func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) {
|
||||
func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(tx *dbs.Tx, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte) (int64, error) {
|
||||
op := NewHTTPFirewallPolicyOperator()
|
||||
op.State = HTTPFirewallPolicyStateEnabled
|
||||
op.IsOn = isOn
|
||||
@@ -111,12 +111,12 @@ func (this *HTTPFirewallPolicyDAO) CreateFirewallPolicy(isOn bool, name string,
|
||||
if len(outboundJSON) > 0 {
|
||||
op.Outbound = outboundJSON
|
||||
}
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return types.Int64(op.Id), err
|
||||
}
|
||||
|
||||
// 修改策略的Inbound和Outbound
|
||||
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(policyId int64, inboundJSON []byte, outboundJSON []byte) error {
|
||||
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(tx *dbs.Tx, policyId int64, inboundJSON []byte, outboundJSON []byte) error {
|
||||
if policyId <= 0 {
|
||||
return errors.New("invalid policyId")
|
||||
}
|
||||
@@ -132,12 +132,12 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInboundAndOutbound(policy
|
||||
} else {
|
||||
op.Outbound = "null"
|
||||
}
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改策略的Inbound
|
||||
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(policyId int64, inboundJSON []byte) error {
|
||||
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(tx *dbs.Tx, policyId int64, inboundJSON []byte) error {
|
||||
if policyId <= 0 {
|
||||
return errors.New("invalid policyId")
|
||||
}
|
||||
@@ -148,12 +148,12 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicyInbound(policyId int64, i
|
||||
} else {
|
||||
op.Inbound = "null"
|
||||
}
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改策略
|
||||
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(policyId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte, blockOptionsJSON []byte) error {
|
||||
func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(tx *dbs.Tx, policyId int64, isOn bool, name string, description string, inboundJSON []byte, outboundJSON []byte, blockOptionsJSON []byte) error {
|
||||
if policyId <= 0 {
|
||||
return errors.New("invalid policyId")
|
||||
}
|
||||
@@ -175,20 +175,20 @@ func (this *HTTPFirewallPolicyDAO) UpdateFirewallPolicy(policyId int64, isOn boo
|
||||
if len(blockOptionsJSON) > 0 {
|
||||
op.BlockOptions = blockOptionsJSON
|
||||
}
|
||||
err := this.Save(op)
|
||||
err := this.Save(tx, op)
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算所有可用的策略数量
|
||||
func (this *HTTPFirewallPolicyDAO) CountAllEnabledFirewallPolicies() (int64, error) {
|
||||
return this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) CountAllEnabledFirewallPolicies(tx *dbs.Tx) (int64, error) {
|
||||
return this.Query(tx).
|
||||
State(HTTPFirewallPolicyStateEnabled).
|
||||
Count()
|
||||
}
|
||||
|
||||
// 列出单页的策略
|
||||
func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(offset int64, size int64) (result []*HTTPFirewallPolicy, err error) {
|
||||
_, err = this.Query().
|
||||
func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(tx *dbs.Tx, offset int64, size int64) (result []*HTTPFirewallPolicy, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(HTTPFirewallPolicyStateEnabled).
|
||||
Offset(offset).
|
||||
Limit(size).
|
||||
@@ -199,8 +199,8 @@ func (this *HTTPFirewallPolicyDAO) ListEnabledFirewallPolicies(offset int64, siz
|
||||
}
|
||||
|
||||
// 组合策略配置
|
||||
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(policyId int64) (*firewallconfigs.HTTPFirewallPolicy, error) {
|
||||
policy, err := this.FindEnabledHTTPFirewallPolicy(policyId)
|
||||
func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(tx *dbs.Tx, policyId int64) (*firewallconfigs.HTTPFirewallPolicy, error) {
|
||||
policy, err := this.FindEnabledHTTPFirewallPolicy(tx, policyId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(policyId int64) (*firew
|
||||
resultGroups := []*firewallconfigs.HTTPFirewallRuleGroup{}
|
||||
|
||||
for _, groupRef := range inbound.GroupRefs {
|
||||
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(groupRef.GroupId)
|
||||
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, groupRef.GroupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -254,7 +254,7 @@ func (this *HTTPFirewallPolicyDAO) ComposeFirewallPolicy(policyId int64) (*firew
|
||||
resultGroups := []*firewallconfigs.HTTPFirewallRuleGroup{}
|
||||
|
||||
for _, groupRef := range outbound.GroupRefs {
|
||||
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(groupRef.GroupId)
|
||||
groupConfig, err := SharedHTTPFirewallRuleGroupDAO.ComposeFirewallRuleGroup(tx, groupRef.GroupId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user