distinct_fields.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package db
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/sqlchemy"
  20. )
  21. type SDistinctFieldManager struct {
  22. SModelBaseManager
  23. }
  24. var DistinctFieldManager *SDistinctFieldManager
  25. func init() {
  26. DistinctFieldManager = &SDistinctFieldManager{
  27. SModelBaseManager: NewModelBaseManager(
  28. SDistinctField{},
  29. "distinct_fields_tbl",
  30. "distinct_field",
  31. "distinct_fields",
  32. ),
  33. }
  34. DistinctFieldManager.SetVirtualObject(DistinctFieldManager)
  35. }
  36. const (
  37. DISTINCT_FIELD_SEP = "::"
  38. )
  39. type SDistinctField struct {
  40. SModelBase
  41. // 资源类型
  42. // example: network
  43. ObjType string `width:"40" charset:"ascii" index:"true" list:"user" get:"user"`
  44. // 资源组合ID
  45. // example: obj_type::key::value
  46. Id string `width:"128" charset:"utf8" primary:"true" list:"user" get:"user"`
  47. // Distinct Field
  48. // exmaple: 部门
  49. Key string `width:"64" charset:"utf8" primary:"true" list:"user" get:"user"`
  50. // Distinct Value
  51. // example: 技术部
  52. Value string `charset:"utf8" list:"user" get:"user"`
  53. }
  54. func (manager *SDistinctFieldManager) GetObjectDistinctFields(objType string) ([]SDistinctField, error) {
  55. q := manager.Query().Equals("obj_type", objType)
  56. fields := []SDistinctField{}
  57. err := FetchModelObjects(manager, q, &fields)
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "FetchModelObjects")
  60. }
  61. return fields, nil
  62. }
  63. func (manager *SDistinctFieldManager) InsertOrUpdate(ctx context.Context, modelManager IModelManager, key, value string) error {
  64. if len(key) == 0 || len(value) == 0 {
  65. return fmt.Errorf("empty key or value")
  66. }
  67. distinct := &SDistinctField{
  68. ObjType: modelManager.Keyword(),
  69. Key: key,
  70. Value: value,
  71. Id: modelManager.Keyword() + DISTINCT_FIELD_SEP + key + DISTINCT_FIELD_SEP + value,
  72. }
  73. distinct.SetModelManager(manager, distinct)
  74. err := manager.TableSpec().InsertOrUpdate(ctx, distinct)
  75. if err != nil {
  76. if errors.Cause(err) == sqlchemy.ErrUnexpectRowCount {
  77. return nil
  78. }
  79. return err
  80. }
  81. return nil
  82. }