You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

package_property.go 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package packages
  5. import (
  6. "context"
  7. "code.gitea.io/gitea/models/db"
  8. )
  9. func init() {
  10. db.RegisterModel(new(PackageProperty))
  11. }
  12. type PropertyType int64
  13. const (
  14. // PropertyTypeVersion means the reference is a package version
  15. PropertyTypeVersion PropertyType = iota // 0
  16. // PropertyTypeFile means the reference is a package file
  17. PropertyTypeFile // 1
  18. )
  19. // PackageProperty represents a property of a package version or file
  20. type PackageProperty struct {
  21. ID int64 `xorm:"pk autoincr"`
  22. RefType PropertyType `xorm:"INDEX NOT NULL"`
  23. RefID int64 `xorm:"INDEX NOT NULL"`
  24. Name string `xorm:"INDEX NOT NULL"`
  25. Value string `xorm:"TEXT NOT NULL"`
  26. }
  27. // InsertProperty creates a property
  28. func InsertProperty(ctx context.Context, refType PropertyType, refID int64, name, value string) (*PackageProperty, error) {
  29. pp := &PackageProperty{
  30. RefType: refType,
  31. RefID: refID,
  32. Name: name,
  33. Value: value,
  34. }
  35. _, err := db.GetEngine(ctx).Insert(pp)
  36. return pp, err
  37. }
  38. // GetProperties gets all properties
  39. func GetProperties(ctx context.Context, refType PropertyType, refID int64) ([]*PackageProperty, error) {
  40. pps := make([]*PackageProperty, 0, 10)
  41. return pps, db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ?", refType, refID).Find(&pps)
  42. }
  43. // GetPropertiesByName gets all properties with a specific name
  44. func GetPropertiesByName(ctx context.Context, refType PropertyType, refID int64, name string) ([]*PackageProperty, error) {
  45. pps := make([]*PackageProperty, 0, 10)
  46. return pps, db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ? AND name = ?", refType, refID, name).Find(&pps)
  47. }
  48. // DeleteAllProperties deletes all properties of a ref
  49. func DeleteAllProperties(ctx context.Context, refType PropertyType, refID int64) error {
  50. _, err := db.GetEngine(ctx).Where("ref_type = ? AND ref_id = ?", refType, refID).Delete(&PackageProperty{})
  51. return err
  52. }
  53. // DeletePropertyByID deletes a property
  54. func DeletePropertyByID(ctx context.Context, propertyID int64) error {
  55. _, err := db.GetEngine(ctx).ID(propertyID).Delete(&PackageProperty{})
  56. return err
  57. }