mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
fix(cloudevent): add distinct filed for cloudevent
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/pkg/errors"
|
||||
)
|
||||
|
||||
type SDistinctFieldManager struct {
|
||||
SModelBaseManager
|
||||
}
|
||||
|
||||
var DistinctFieldManager *SDistinctFieldManager
|
||||
|
||||
func init() {
|
||||
DistinctFieldManager = &SDistinctFieldManager{
|
||||
SModelBaseManager: NewModelBaseManager(
|
||||
SDistinctField{},
|
||||
"distinct_fields_tbl",
|
||||
"distinct_field",
|
||||
"distinct_fields",
|
||||
),
|
||||
}
|
||||
DistinctFieldManager.SetVirtualObject(DistinctFieldManager)
|
||||
}
|
||||
|
||||
const (
|
||||
DISTINCT_FIELD_SEP = "::"
|
||||
)
|
||||
|
||||
type SDistinctField struct {
|
||||
SModelBase
|
||||
|
||||
// 资源类型
|
||||
// example: network
|
||||
ObjType string `width:"40" charset:"ascii" index:"true" list:"user" get:"user"`
|
||||
|
||||
// 资源组合ID
|
||||
// example: obj_type::key::value
|
||||
Id string `width:"128" charset:"utf8" primary:"true" list:"user" get:"user"`
|
||||
|
||||
// Distinct Field
|
||||
// exmaple: 部门
|
||||
Key string `width:"64" charset:"utf8" primary:"true" list:"user" get:"user"`
|
||||
|
||||
// Distinct Value
|
||||
// example: 技术部
|
||||
Value string `charset:"utf8" list:"user" get:"user"`
|
||||
}
|
||||
|
||||
func (manager *SDistinctFieldManager) GetObjectDistinctFields(objType string) ([]SDistinctField, error) {
|
||||
q := manager.Query().Equals("obj_type", objType)
|
||||
fields := []SDistinctField{}
|
||||
err := FetchModelObjects(manager, q, &fields)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "FetchModelObjects")
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
func (manager *SDistinctFieldManager) InsertOrUpdate(ctx context.Context, modelManager IModelManager, key, value string) error {
|
||||
if len(key) == 0 || len(value) == 0 {
|
||||
return fmt.Errorf("empty key or value")
|
||||
}
|
||||
distinct := &SDistinctField{
|
||||
ObjType: modelManager.Keyword(),
|
||||
Key: key,
|
||||
Value: value,
|
||||
Id: modelManager.Keyword() + DISTINCT_FIELD_SEP + key + DISTINCT_FIELD_SEP + value,
|
||||
}
|
||||
distinct.SetModelManager(manager, distinct)
|
||||
return manager.TableSpec().InsertOrUpdate(ctx, distinct)
|
||||
}
|
||||
@@ -515,3 +515,26 @@ func FetchQueryObjectsByIds(q *sqlchemy.SQuery, fieldName string, ids []string,
|
||||
func FetchStandaloneObjectsByIds(modelManager IModelManager, ids []string, targets interface{}) error {
|
||||
return FetchModelObjectsByIds(modelManager, "id", ids, targets)
|
||||
}
|
||||
|
||||
func FetchDistinctField(modelManager IModelManager, field string) ([]string, error) {
|
||||
q := modelManager.Query(field).Distinct()
|
||||
rows, err := q.Rows()
|
||||
if err != nil {
|
||||
if errors.Cause(err) == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrapf(err, "q.Rows")
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
values := []string{}
|
||||
for rows.Next() {
|
||||
var value string
|
||||
err := rows.Scan(&value)
|
||||
if err != nil {
|
||||
return values, errors.Wrap(err, "rows.Scan")
|
||||
}
|
||||
values = append(values, value)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user