77 lines
1.8 KiB
C#
77 lines
1.8 KiB
C#
using System.Collections.Concurrent;
|
|
using Pf2eModel;
|
|
using Pf2eModel.Model.Base;
|
|
using Pf2eModel.Model.Enum;
|
|
|
|
namespace Pf2eViewer.DatabaseViewer;
|
|
|
|
internal class DatabaseQueryPerformer
|
|
{
|
|
public required EntityType Type;
|
|
|
|
public required int Page;
|
|
|
|
public required int PerPage;
|
|
|
|
public required string Search = "";
|
|
|
|
private readonly Thread _queryThread;
|
|
|
|
public DatabaseQueryPerformer()
|
|
{
|
|
_queryThread = new(PerformQuery);
|
|
}
|
|
|
|
public void Query()
|
|
{
|
|
_queryThread.Start();
|
|
}
|
|
|
|
public void Abort()
|
|
{
|
|
_queryThread.Interrupt();
|
|
}
|
|
|
|
private void PerformQuery()
|
|
{
|
|
try
|
|
{
|
|
Pf2eDbContext dbContext = new();
|
|
|
|
IQueryable<Entity> query =
|
|
Search == ""
|
|
? dbContext.Entities.Where(e => e.Type == Type)
|
|
:
|
|
// ReSharper disable once EntityFramework.UnsupportedServerSideFunctionCall
|
|
dbContext.Entities.Where(e =>
|
|
e.Type == Type && Pf2eDbContext.LocaleSearch(e.Name, Search)
|
|
);
|
|
|
|
int count = query.Count();
|
|
|
|
List<Entity> entities = query.Skip((Page - 1) * PerPage).Take(PerPage).ToList();
|
|
|
|
DatabaseQueryResult result = new(count, entities);
|
|
|
|
OnQueryCompleted?.Invoke(this, result);
|
|
}
|
|
catch (ThreadInterruptedException)
|
|
{
|
|
// Ignore
|
|
}
|
|
}
|
|
|
|
public delegate void DatabaseQueryPerformerDelegate(
|
|
DatabaseQueryPerformer sender,
|
|
DatabaseQueryResult result
|
|
);
|
|
public event DatabaseQueryPerformerDelegate? OnQueryCompleted;
|
|
}
|
|
|
|
internal class DatabaseQueryResult(int count, ICollection<Entity> entities)
|
|
{
|
|
public int Count { get; } = count;
|
|
|
|
public ConcurrentBag<Entity> Entities { get; } = new(entities);
|
|
}
|