using System; using System.Collections.Generic; using System.Linq; using NPoco; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Persistence.Repositories; using Umbraco.Cms.Core.Scoping; using Umbraco.Cms.Infrastructure.Persistence.Dtos; using Umbraco.Cms.Infrastructure.Persistence.Factories; using Umbraco.Extensions; namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement { /// /// Represents the NPoco implementation of . /// internal class CacheInstructionRepository : ICacheInstructionRepository { /// public int CountAll(IScope scope) { Sql sql = scope.SqlContext.Sql().Select("COUNT(*)") .From(); return scope.Database.ExecuteScalar(sql); } /// public int CountPendingInstructions(IScope scope, int lastId) => scope.Database.ExecuteScalar("SELECT SUM(instructionCount) FROM umbracoCacheInstruction WHERE id > @lastId", new { lastId }); /// public int GetMaxId(IScope scope) => scope.Database.ExecuteScalar("SELECT MAX(id) FROM umbracoCacheInstruction"); /// public bool Exists(IScope scope, int id) => scope.Database.Exists(id); /// public void Add(IScope scope, CacheInstruction cacheInstruction) { CacheInstructionDto dto = CacheInstructionFactory.BuildDto(cacheInstruction); scope.Database.Insert(dto); } /// public IEnumerable GetPendingInstructions(IScope scope, int lastId, int maxNumberToRetrieve) { Sql sql = scope.SqlContext.Sql().SelectAll() .From() .Where(dto => dto.Id > lastId) .OrderBy(dto => dto.Id); Sql topSql = sql.SelectTop(maxNumberToRetrieve); return scope.Database.Fetch(topSql).Select(CacheInstructionFactory.BuildEntity); } /// public void DeleteInstructionsOlderThan(IScope scope, DateTime pruneDate) { // Using 2 queries is faster than convoluted joins. var maxId = scope.Database.ExecuteScalar("SELECT MAX(id) FROM umbracoCacheInstruction;"); Sql deleteSql = new Sql().Append(@"DELETE FROM umbracoCacheInstruction WHERE utcStamp < @pruneDate AND id < @maxId", new { pruneDate, maxId }); scope.Database.Execute(deleteSql); } } }