code.gitea.io/gitea@v1.22.3/models/packages/debian/search.go (about)

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