Skip to content
This repository
tree: 6874fcb4c1
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 49 lines (43 sloc) 1.576 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
using System.Collections.Generic;
using System.Data.Entity;

namespace NuGetGallery
{
    public interface IPackageIdsQuery
    {
        IEnumerable<string> Execute(
            string partialId,
            bool? includePrerelease = false);
    }

    public class PackageIdsQuery : IPackageIdsQuery
    {
        const string _partialIdSqlFormat = @"SELECT TOP 30 pr.ID
FROM Packages p
JOIN PackageRegistrations pr on pr.[Key] = p.PackageRegistrationKey
WHERE pr.ID LIKE {{0}}
{0}
GROUP BY pr.ID
ORDER BY pr.ID";
        private const string _noPartialIdSql = @"SELECT TOP 30 pr.ID
FROM Packages p
JOIN PackageRegistrations pr on pr.[Key] = p.PackageRegistrationKey
GROUP BY pr.ID
ORDER BY MAX(pr.DownloadCount) DESC";
        private readonly IEntitiesContext _entities;

        public PackageIdsQuery(IEntitiesContext entities)
        {
            _entities = entities;
        }

        public IEnumerable<string> Execute(
            string partialId,
            bool? includePrerelease = false)
        {
            var dbContext = (DbContext)_entities;

            if (string.IsNullOrWhiteSpace(partialId))
                return dbContext.Database.SqlQuery<string>(_noPartialIdSql);
            
            var prereleaseFilter = string.Empty;
            if (!includePrerelease.HasValue || !includePrerelease.Value)
                prereleaseFilter = "AND p.IsPrerelease = {1}";
            return dbContext.Database.SqlQuery<string>(string.Format(_partialIdSqlFormat, prereleaseFilter), partialId + "%", includePrerelease ?? false);
        }
    }
}
Something went wrong with that request. Please try again.