gitea源码

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package nuget
  4. import (
  5. "context"
  6. "strings"
  7. "code.gitea.io/gitea/models/db"
  8. packages_model "code.gitea.io/gitea/models/packages"
  9. "xorm.io/builder"
  10. )
  11. // SearchVersions gets all versions of packages matching the search options
  12. func SearchVersions(ctx context.Context, opts *packages_model.PackageSearchOptions) ([]*packages_model.PackageVersion, int64, error) {
  13. cond := toConds(opts)
  14. e := db.GetEngine(ctx)
  15. total, err := e.
  16. Where(cond).
  17. Count(&packages_model.Package{})
  18. if err != nil {
  19. return nil, 0, err
  20. }
  21. inner := builder.
  22. Dialect(db.BuilderDialect()). // builder needs the sql dialect to build the Limit() below
  23. Select("*").
  24. From("package").
  25. Where(cond).
  26. OrderBy("package.name ASC")
  27. if opts.Paginator != nil {
  28. skip, take := opts.Paginator.GetSkipTake()
  29. inner = inner.Limit(take, skip)
  30. }
  31. sess := e.
  32. Where(opts.ToConds()).
  33. Table("package_version").
  34. Join("INNER", inner, "package.id = package_version.package_id")
  35. pvs := make([]*packages_model.PackageVersion, 0, 10)
  36. return pvs, total, sess.Find(&pvs)
  37. }
  38. // CountPackages counts all packages matching the search options
  39. func CountPackages(ctx context.Context, opts *packages_model.PackageSearchOptions) (int64, error) {
  40. return db.GetEngine(ctx).
  41. Where(toConds(opts)).
  42. Count(&packages_model.Package{})
  43. }
  44. func toConds(opts *packages_model.PackageSearchOptions) builder.Cond {
  45. var cond builder.Cond = builder.Eq{
  46. "package.is_internal": opts.IsInternal.Value(),
  47. "package.owner_id": opts.OwnerID,
  48. "package.type": packages_model.TypeNuGet,
  49. }
  50. if opts.Name.Value != "" {
  51. if opts.Name.ExactMatch {
  52. cond = cond.And(builder.Eq{"package.lower_name": strings.ToLower(opts.Name.Value)})
  53. } else {
  54. cond = cond.And(builder.Like{"package.lower_name", strings.ToLower(opts.Name.Value)})
  55. }
  56. }
  57. return cond
  58. }