Skip to content

Commit

Permalink
fix: 避免sql erorr
Browse files Browse the repository at this point in the history
  • Loading branch information
Qu Xuan committed Jun 11, 2020
1 parent f441306 commit aa0d05c
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pkg/compute/models/dbinstance_skus.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,20 @@ func (manager *SDBInstanceSkuManager) ListItemFilter(
q = q.In("engine_version", query.EngineVersion)
}

zones := ZoneManager.Query().SubQuery()
for k, v := range map[string][]string{"zone1": query.Zone1, "zone2": query.Zone2, "zone3": query.Zone3} {
if len(v) > 0 {
q = q.Join(zones, sqlchemy.Equals(zones.Field("id"), q.Field(k))).Filter(
sqlchemy.OR(
sqlchemy.In(zones.Field("name"), v),
sqlchemy.In(zones.Field("id"), v),
),
)
for k, zoneIds := range map[string][]string{"zone1": query.Zone1, "zone2": query.Zone2, "zone3": query.Zone3} {
ids := []string{}
for _, zoneId := range zoneIds {
zone, err := ZoneManager.FetchByIdOrName(userCred, zoneId)
if err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, httperrors.NewResourceNotFoundError2("zone", zoneId)
}
return nil, httperrors.NewGeneralError(err)
}
ids = append(ids, zone.GetId())
}
if len(ids) > 0 {
q = q.In(k, ids)
}
}

Expand Down

0 comments on commit aa0d05c

Please sign in to comment.