所有数据库相关的操作支持事务

This commit is contained in:
刘祥超
2021-01-01 23:31:30 +08:00
parent 0cf398a436
commit f49c26cdab
146 changed files with 2845 additions and 2068 deletions

View File

@@ -40,8 +40,8 @@ func init() {
}
// 启用条目
func (this *DNSDomainDAO) EnableDNSDomain(id int64) error {
_, err := this.Query().
func (this *DNSDomainDAO) EnableDNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", DNSDomainStateEnabled).
Update()
@@ -49,8 +49,8 @@ func (this *DNSDomainDAO) EnableDNSDomain(id int64) error {
}
// 禁用条目
func (this *DNSDomainDAO) DisableDNSDomain(id int64) error {
_, err := this.Query().
func (this *DNSDomainDAO) DisableDNSDomain(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", DNSDomainStateDisabled).
Update()
@@ -58,8 +58,8 @@ func (this *DNSDomainDAO) DisableDNSDomain(id int64) error {
}
// 查找启用中的条目
func (this *DNSDomainDAO) FindEnabledDNSDomain(id int64) (*DNSDomain, error) {
result, err := this.Query().
func (this *DNSDomainDAO) FindEnabledDNSDomain(tx *dbs.Tx, id int64) (*DNSDomain, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", DNSDomainStateEnabled).
Find()
@@ -70,15 +70,15 @@ func (this *DNSDomainDAO) FindEnabledDNSDomain(id int64) (*DNSDomain, error) {
}
// 根据主键查找名称
func (this *DNSDomainDAO) FindDNSDomainName(id int64) (string, error) {
return this.Query().
func (this *DNSDomainDAO) FindDNSDomainName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// 创建域名
func (this *DNSDomainDAO) CreateDomain(adminId int64, userId int64, providerId int64, name string) (int64, error) {
func (this *DNSDomainDAO) CreateDomain(tx *dbs.Tx, adminId int64, userId int64, providerId int64, name string) (int64, error) {
op := NewDNSDomainOperator()
op.ProviderId = providerId
op.AdminId = adminId
@@ -86,7 +86,7 @@ func (this *DNSDomainDAO) CreateDomain(adminId int64, userId int64, providerId i
op.Name = name
op.State = DNSDomainStateEnabled
op.IsOn = true
err := this.Save(op)
err := this.Save(tx, op)
if err != nil {
return 0, err
}
@@ -94,7 +94,7 @@ func (this *DNSDomainDAO) CreateDomain(adminId int64, userId int64, providerId i
}
// 修改域名
func (this *DNSDomainDAO) UpdateDomain(domainId int64, name string, isOn bool) error {
func (this *DNSDomainDAO) UpdateDomain(tx *dbs.Tx, domainId int64, name string, isOn bool) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
@@ -102,7 +102,7 @@ func (this *DNSDomainDAO) UpdateDomain(domainId int64, name string, isOn bool) e
op.Id = domainId
op.Name = name
op.IsOn = isOn
err := this.Save(op)
err := this.Save(tx, op)
if err != nil {
return err
}
@@ -110,8 +110,8 @@ func (this *DNSDomainDAO) UpdateDomain(domainId int64, name string, isOn bool) e
}
// 查询一个服务商下面的所有域名
func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(providerId int64) (result []*DNSDomain, err error) {
_, err = this.Query().
func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (result []*DNSDomain, err error) {
_, err = this.Query(tx).
State(DNSDomainStateEnabled).
Attr("providerId", providerId).
AscPk().
@@ -121,27 +121,27 @@ func (this *DNSDomainDAO) FindAllEnabledDomainsWithProviderId(providerId int64)
}
// 计算某个服务商下的域名数量
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(providerId int64) (int64, error) {
return this.Query().
func (this *DNSDomainDAO) CountAllEnabledDomainsWithProviderId(tx *dbs.Tx, providerId int64) (int64, error) {
return this.Query(tx).
State(DNSDomainStateEnabled).
Attr("providerId", providerId).
Count()
}
// 更新域名数据
func (this *DNSDomainDAO) UpdateDomainData(domainId int64, data string) error {
func (this *DNSDomainDAO) UpdateDomainData(tx *dbs.Tx, domainId int64, data string) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
op := NewDNSDomainOperator()
op.Id = domainId
op.Data = data
err := this.Save(op)
err := this.Save(tx, op)
return err
}
// 更新域名解析记录
func (this *DNSDomainDAO) UpdateDomainRecords(domainId int64, recordsJSON []byte) error {
func (this *DNSDomainDAO) UpdateDomainRecords(tx *dbs.Tx, domainId int64, recordsJSON []byte) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
@@ -149,12 +149,12 @@ func (this *DNSDomainDAO) UpdateDomainRecords(domainId int64, recordsJSON []byte
op.Id = domainId
op.Records = recordsJSON
op.DataUpdatedAt = time.Now().Unix()
err := this.Save(op)
err := this.Save(tx, op)
return err
}
// 更新线路
func (this *DNSDomainDAO) UpdateDomainRoutes(domainId int64, routesJSON []byte) error {
func (this *DNSDomainDAO) UpdateDomainRoutes(tx *dbs.Tx, domainId int64, routesJSON []byte) error {
if domainId <= 0 {
return errors.New("invalid domainId")
}
@@ -162,13 +162,13 @@ func (this *DNSDomainDAO) UpdateDomainRoutes(domainId int64, routesJSON []byte)
op.Id = domainId
op.Routes = routesJSON
op.DataUpdatedAt = time.Now().Unix()
err := this.Save(op)
err := this.Save(tx, op)
return err
}
// 查找域名线路
func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]*dnsclients.Route, error) {
routes, err := this.Query().
func (this *DNSDomainDAO) FindDomainRoutes(tx *dbs.Tx, domainId int64) ([]*dnsclients.Route, error) {
routes, err := this.Query(tx).
Pk(domainId).
Result("routes").
FindStringCol("")
@@ -187,8 +187,8 @@ func (this *DNSDomainDAO) FindDomainRoutes(domainId int64) ([]*dnsclients.Route,
}
// 查找线路名称
func (this *DNSDomainDAO) FindDomainRouteName(domainId int64, routeCode string) (string, error) {
routes, err := this.FindDomainRoutes(domainId)
func (this *DNSDomainDAO) FindDomainRouteName(tx *dbs.Tx, domainId int64, routeCode string) (string, error) {
routes, err := this.FindDomainRoutes(tx, domainId)
if err != nil {
return "", err
}
@@ -201,15 +201,15 @@ func (this *DNSDomainDAO) FindDomainRouteName(domainId int64, routeCode string)
}
// 判断是否有域名可选
func (this *DNSDomainDAO) ExistAvailableDomains() (bool, error) {
subQuery, err := SharedDNSProviderDAO.Query().
func (this *DNSDomainDAO) ExistAvailableDomains(tx *dbs.Tx) (bool, error) {
subQuery, err := SharedDNSProviderDAO.Query(tx).
Where("state=1"). // 这里要使用非变量
ResultPk().
AsSQL()
if err != nil {
return false, err
}
return this.Query().
return this.Query(tx).
State(DNSDomainStateEnabled).
Attr("isOn", true).
Where("providerId IN (" + subQuery + ")").
@@ -217,7 +217,7 @@ func (this *DNSDomainDAO) ExistAvailableDomains() (bool, error) {
}
// 检查域名解析记录是否存在
func (this *DNSDomainDAO) ExistDomainRecord(domainId int64, recordName string, recordType string, recordRoute string, recordValue string) (bool, error) {
func (this *DNSDomainDAO) ExistDomainRecord(tx *dbs.Tx, domainId int64, recordName string, recordType string, recordRoute string, recordValue string) (bool, error) {
query := maps.Map{
"name": recordName,
"type": recordType,
@@ -230,7 +230,7 @@ func (this *DNSDomainDAO) ExistDomainRecord(domainId int64, recordName string, r
// CNAME兼容点.)符号
if recordType == "CNAME" && !strings.HasSuffix(recordValue, ".") {
b, err := this.ExistDomainRecord(domainId, recordName, recordType, recordRoute, recordValue+".")
b, err := this.ExistDomainRecord(tx, domainId, recordName, recordType, recordRoute, recordValue+".")
if err != nil {
return false, err
}
@@ -240,7 +240,7 @@ func (this *DNSDomainDAO) ExistDomainRecord(domainId int64, recordName string, r
}
}
recordType = strings.ToUpper(recordType)
return this.Query().
return this.Query(tx).
Pk(domainId).
Where("JSON_CONTAINS(records, :query)").
Param("query", query.AsJSON()).