gitea源码

search.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package debian
  4. import (
  5. "context"
  6. "strconv"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/packages"
  9. debian_module "code.gitea.io/gitea/modules/packages/debian"
  10. "xorm.io/builder"
  11. )
  12. type PackageSearchOptions struct {
  13. OwnerID int64
  14. Distribution string
  15. Component string
  16. Architecture string
  17. }
  18. func (opts *PackageSearchOptions) toCond() builder.Cond {
  19. var cond builder.Cond = builder.Eq{
  20. "package_file.is_lead": true,
  21. "package.type": packages.TypeDebian,
  22. "package.owner_id": opts.OwnerID,
  23. "package.is_internal": false,
  24. "package_version.is_internal": false,
  25. }
  26. props := make(map[string]string)
  27. if opts.Distribution != "" {
  28. props[debian_module.PropertyDistribution] = opts.Distribution
  29. }
  30. if opts.Component != "" {
  31. props[debian_module.PropertyComponent] = opts.Component
  32. }
  33. if opts.Architecture != "" {
  34. props[debian_module.PropertyArchitecture] = opts.Architecture
  35. }
  36. if len(props) > 0 {
  37. var propsCond builder.Cond = builder.Eq{
  38. "package_property.ref_type": packages.PropertyTypeFile,
  39. }
  40. propsCond = propsCond.And(builder.Expr("package_property.ref_id = package_file.id"))
  41. propsCondBlock := builder.NewCond()
  42. for name, value := range props {
  43. propsCondBlock = propsCondBlock.Or(builder.Eq{
  44. "package_property.name": name,
  45. "package_property.value": value,
  46. })
  47. }
  48. propsCond = propsCond.And(propsCondBlock)
  49. cond = cond.And(builder.Eq{
  50. strconv.Itoa(len(props)): builder.Select("COUNT(*)").Where(propsCond).From("package_property"),
  51. })
  52. }
  53. return cond
  54. }
  55. // ExistPackages tests if there are packages matching the search options
  56. func ExistPackages(ctx context.Context, opts *PackageSearchOptions) (bool, error) {
  57. return db.GetEngine(ctx).
  58. Table("package_file").
  59. Join("INNER", "package_version", "package_version.id = package_file.version_id").
  60. Join("INNER", "package", "package.id = package_version.package_id").
  61. Where(opts.toCond()).
  62. Exist(new(packages.PackageFile))
  63. }
  64. // SearchPackages gets the packages matching the search options
  65. func SearchPackages(ctx context.Context, opts *PackageSearchOptions) ([]*packages.PackageFileDescriptor, error) {
  66. var pkgFiles []*packages.PackageFile
  67. err := db.GetEngine(ctx).
  68. Table("package_file").
  69. Select("package_file.*").
  70. Join("INNER", "package_version", "package_version.id = package_file.version_id").
  71. Join("INNER", "package", "package.id = package_version.package_id").
  72. Where(opts.toCond()).
  73. Asc("package.lower_name", "package_version.created_unix").Find(&pkgFiles)
  74. if err != nil {
  75. return nil, err
  76. }
  77. pfds := make([]*packages.PackageFileDescriptor, 0, len(pkgFiles))
  78. for _, pf := range pkgFiles {
  79. pfd, err := packages.GetPackageFileDescriptor(ctx, pf)
  80. if err != nil {
  81. return nil, err
  82. }
  83. pfds = append(pfds, pfd)
  84. }
  85. return pfds, nil
  86. }
  87. // GetDistributions gets all available distributions
  88. func GetDistributions(ctx context.Context, ownerID int64) ([]string, error) {
  89. return packages.GetDistinctPropertyValues(
  90. ctx,
  91. packages.TypeDebian,
  92. ownerID,
  93. packages.PropertyTypeFile,
  94. debian_module.PropertyDistribution,
  95. nil,
  96. )
  97. }
  98. // GetComponents gets all available components for the given distribution
  99. func GetComponents(ctx context.Context, ownerID int64, distribution string) ([]string, error) {
  100. return packages.GetDistinctPropertyValues(
  101. ctx,
  102. packages.TypeDebian,
  103. ownerID,
  104. packages.PropertyTypeFile,
  105. debian_module.PropertyComponent,
  106. &packages.DistinctPropertyDependency{
  107. Name: debian_module.PropertyDistribution,
  108. Value: distribution,
  109. },
  110. )
  111. }
  112. // GetArchitectures gets all available architectures for the given distribution
  113. func GetArchitectures(ctx context.Context, ownerID int64, distribution string) ([]string, error) {
  114. return packages.GetDistinctPropertyValues(
  115. ctx,
  116. packages.TypeDebian,
  117. ownerID,
  118. packages.PropertyTypeFile,
  119. debian_module.PropertyArchitecture,
  120. &packages.DistinctPropertyDependency{
  121. Name: debian_module.PropertyDistribution,
  122. Value: distribution,
  123. },
  124. )
  125. }