Lidarr/src/NzbDrone.Core/Datastore/BasicRepository.cs

300 lines
8.3 KiB
C#
Raw Normal View History

2018-03-15 01:28:46 +00:00
using System;
2013-03-24 04:16:00 +00:00
using System.Collections.Generic;
using System.Data;
2013-02-05 04:07:07 +00:00
using System.Linq;
2013-03-24 04:16:00 +00:00
using System.Linq.Expressions;
2013-03-25 03:51:32 +00:00
using Marr.Data;
using Marr.Data.QGen;
using NzbDrone.Common.Extensions;
2013-05-05 21:24:33 +00:00
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.Datastore.Extensions;
using NzbDrone.Core.Messaging.Events;
2013-03-25 03:51:32 +00:00
2013-02-05 04:07:07 +00:00
namespace NzbDrone.Core.Datastore
{
2013-03-02 18:25:39 +00:00
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
2013-02-05 04:07:07 +00:00
{
2013-02-19 06:56:02 +00:00
IEnumerable<TModel> All();
2013-02-23 19:38:25 +00:00
int Count();
TModel Get(int id);
2013-04-18 00:33:38 +00:00
IEnumerable<TModel> Get(IEnumerable<int> ids);
TModel SingleOrDefault();
2013-02-19 06:56:02 +00:00
TModel Insert(TModel model);
TModel Update(TModel model);
2013-03-24 04:16:00 +00:00
TModel Upsert(TModel model);
void Delete(int id);
void Delete(TModel model);
2013-03-24 04:16:00 +00:00
void InsertMany(IList<TModel> model);
void UpdateMany(IList<TModel> model);
void DeleteMany(List<TModel> model);
void Purge(bool vacuum = false);
2013-03-02 18:25:39 +00:00
bool HasItems();
2013-03-24 04:16:00 +00:00
void DeleteMany(IEnumerable<int> ids);
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
2013-04-25 04:27:49 +00:00
TModel Single();
2013-06-05 00:49:53 +00:00
PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec);
2013-02-05 04:07:07 +00:00
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
2013-02-05 04:07:07 +00:00
{
2013-05-11 20:06:57 +00:00
private readonly IDatabase _database;
private readonly IEventAggregator _eventAggregator;
2013-04-25 04:27:49 +00:00
2016-12-09 06:54:15 +00:00
protected IDataMapper DataMapper => _database.GetDataMapper();
public BasicRepository(IDatabase database, IEventAggregator eventAggregator)
2013-02-05 04:07:07 +00:00
{
2013-05-11 20:06:57 +00:00
_database = database;
_eventAggregator = eventAggregator;
2013-02-05 04:07:07 +00:00
}
protected virtual QueryBuilder<TModel> Query => DataMapper.Query<TModel>();
2013-03-27 03:44:52 +00:00
protected void Delete(Expression<Func<TModel, bool>> filter)
{
2013-05-11 20:06:57 +00:00
DataMapper.Delete(filter);
2013-02-05 04:07:07 +00:00
}
2013-03-25 03:51:32 +00:00
public IEnumerable<TModel> All()
2013-02-23 19:38:25 +00:00
{
2013-05-11 20:06:57 +00:00
return DataMapper.Query<TModel>().ToList();
}
2013-03-25 03:51:32 +00:00
public int Count()
{
2013-05-11 20:06:57 +00:00
return DataMapper.Query<TModel>().GetRowCount();
2013-02-23 19:38:25 +00:00
}
public TModel Get(int id)
2013-02-05 04:07:07 +00:00
{
var model = Query.Where(c => c.Id == id).SingleOrDefault();
if (model == null)
{
throw new ModelNotFoundException(typeof(TModel), id);
}
return model;
2013-02-05 04:07:07 +00:00
}
2013-04-18 00:33:38 +00:00
public IEnumerable<TModel> Get(IEnumerable<int> ids)
{
var idList = ids.ToList();
var query = string.Format("[t0].[Id] IN ({0})", string.Join(",", idList));
var result = Query.Where(query).ToList();
if (result.Count != idList.Count())
{
2017-01-05 23:32:17 +00:00
throw new ApplicationException($"Expected query to return {idList.Count} rows but returned {result.Count}");
}
2013-04-18 00:33:38 +00:00
return result;
}
public TModel SingleOrDefault()
2013-04-25 04:27:49 +00:00
{
return All().SingleOrDefault();
}
public TModel Single()
{
2013-03-24 04:16:00 +00:00
return All().Single();
}
2013-02-19 06:56:02 +00:00
public TModel Insert(TModel model)
2013-02-05 04:07:07 +00:00
{
2013-03-24 19:56:51 +00:00
if (model.Id != 0)
{
2013-06-03 03:44:31 +00:00
throw new InvalidOperationException("Can't insert model with existing ID " + model.Id);
2013-03-24 19:56:51 +00:00
}
2013-05-11 20:06:57 +00:00
DataMapper.Insert(model);
2013-05-05 21:24:33 +00:00
2013-11-13 20:08:37 +00:00
ModelCreated(model);
2013-03-24 04:16:00 +00:00
return model;
2013-02-05 04:07:07 +00:00
}
public TModel Update(TModel model)
{
2013-03-24 19:56:51 +00:00
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
2013-05-11 20:06:57 +00:00
DataMapper.Update(model, c => c.Id == model.Id);
2013-11-13 20:08:37 +00:00
ModelUpdated(model);
2013-03-24 04:16:00 +00:00
return model;
}
public void Delete(TModel model)
{
2013-11-13 20:08:37 +00:00
Delete(model.Id);
}
2013-03-24 04:16:00 +00:00
public void InsertMany(IList<TModel> models)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
2013-03-25 03:51:32 +00:00
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
foreach (var model in models)
{
unitOfWork.DB.Insert(model);
}
unitOfWork.Commit();
2013-03-25 03:51:32 +00:00
}
}
2013-03-24 04:16:00 +00:00
public void UpdateMany(IList<TModel> models)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
2013-03-25 03:51:32 +00:00
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
foreach (var model in models)
{
var localModel = model;
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
unitOfWork.DB.Update(model, c => c.Id == localModel.Id);
}
unitOfWork.Commit();
2013-03-25 03:51:32 +00:00
}
}
2013-03-24 04:16:00 +00:00
public void DeleteMany(List<TModel> models)
{
DeleteMany(models.Select(m => m.Id));
}
2013-03-24 04:16:00 +00:00
public TModel Upsert(TModel model)
{
if (model.Id == 0)
2013-02-23 19:38:25 +00:00
{
2013-03-25 03:51:32 +00:00
Insert(model);
2013-03-24 04:16:00 +00:00
return model;
2013-02-23 19:38:25 +00:00
}
2013-03-25 03:51:32 +00:00
Update(model);
2013-03-24 04:16:00 +00:00
return model;
}
public void Delete(int id)
2013-02-05 04:07:07 +00:00
{
2013-05-11 20:06:57 +00:00
DataMapper.Delete<TModel>(c => c.Id == id);
2013-02-05 04:07:07 +00:00
}
2013-02-23 19:38:25 +00:00
public void DeleteMany(IEnumerable<int> ids)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
foreach (var id in ids)
{
var localId = id;
unitOfWork.DB.Delete<TModel>(c => c.Id == localId);
}
unitOfWork.Commit();
}
2013-02-23 19:38:25 +00:00
}
public void Purge(bool vacuum = false)
2013-02-23 19:38:25 +00:00
{
2013-05-11 20:06:57 +00:00
DataMapper.Delete<TModel>(c => c.Id > -1);
if (vacuum)
{
Vacuum();
}
}
protected void Vacuum()
{
_database.Vacuum();
2013-02-23 19:38:25 +00:00
}
2013-03-02 18:25:39 +00:00
public bool HasItems()
{
2013-03-24 04:16:00 +00:00
return Count() > 0;
}
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
2013-03-24 04:16:00 +00:00
{
if (model.Id == 0)
{
throw new InvalidOperationException("Attempted to updated model without ID");
}
2013-05-11 20:06:57 +00:00
DataMapper.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
2013-11-13 20:08:37 +00:00
ModelUpdated(model);
2013-03-02 18:25:39 +00:00
}
2013-05-10 23:53:50 +00:00
2013-06-05 00:49:53 +00:00
public virtual PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec)
{
pagingSpec.Records = GetPagedQuery(Query, pagingSpec).ToList();
pagingSpec.TotalRecords = GetPagedQuery(Query, pagingSpec).GetRowCount();
2013-06-05 00:49:53 +00:00
return pagingSpec;
}
protected virtual SortBuilder<TModel> GetPagedQuery(QueryBuilder<TModel> query, PagingSpec<TModel> pagingSpec)
{
2018-03-15 01:28:46 +00:00
var filterExpressions = pagingSpec.FilterExpressions;
var sortQuery = query.Where(filterExpressions.FirstOrDefault());
if (filterExpressions.Count > 1)
{
// Start at the second item for the AndWhere clauses
for (var i = 1; i < filterExpressions.Count; i++)
{
sortQuery.AndWhere(filterExpressions[i]);
}
}
return sortQuery.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
.Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize);
}
2013-09-11 06:33:47 +00:00
protected void ModelCreated(TModel model)
{
2013-09-11 06:33:47 +00:00
PublishModelEvent(model, ModelAction.Created);
}
2013-09-11 06:33:47 +00:00
protected void ModelUpdated(TModel model)
2013-05-30 23:32:50 +00:00
{
2013-09-11 06:33:47 +00:00
PublishModelEvent(model, ModelAction.Updated);
2013-05-30 23:32:50 +00:00
}
2013-09-11 06:33:47 +00:00
protected void ModelDeleted(TModel model)
2013-05-30 23:32:50 +00:00
{
2013-09-11 06:33:47 +00:00
PublishModelEvent(model, ModelAction.Deleted);
}
2013-05-30 23:32:50 +00:00
2013-09-11 06:33:47 +00:00
private void PublishModelEvent(TModel model, ModelAction action)
{
if (PublishModelEvents)
{
_eventAggregator.PublishEvent(new ModelEvent<TModel>(model, action));
2013-09-11 06:33:47 +00:00
}
2013-05-30 23:32:50 +00:00
}
2016-12-09 06:54:15 +00:00
protected virtual bool PublishModelEvents => false;
2013-02-05 04:07:07 +00:00
}
}