2013-03-24 04:16:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2014-11-24 20:00:19 +00:00
|
|
|
|
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;
|
2014-12-02 06:26:25 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
2013-05-05 21:24:33 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2014-04-10 22:19:40 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Extensions;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
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();
|
2013-02-17 05:35:52 +00:00
|
|
|
|
TModel Get(int id);
|
2013-04-18 00:33:38 +00:00
|
|
|
|
IEnumerable<TModel> Get(IEnumerable<int> ids);
|
2013-03-06 21:20:33 +00:00
|
|
|
|
TModel SingleOrDefault();
|
2013-02-19 06:56:02 +00:00
|
|
|
|
TModel Insert(TModel model);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
TModel Update(TModel model);
|
2013-03-24 04:16:00 +00:00
|
|
|
|
TModel Upsert(TModel model);
|
2013-02-17 05:35:52 +00:00
|
|
|
|
void Delete(int id);
|
2013-03-07 04:34:56 +00:00
|
|
|
|
void Delete(TModel model);
|
2013-03-24 04:16:00 +00:00
|
|
|
|
void InsertMany(IList<TModel> model);
|
|
|
|
|
void UpdateMany(IList<TModel> model);
|
2013-03-06 21:20:33 +00:00
|
|
|
|
void DeleteMany(List<TModel> model);
|
2013-11-28 07:11:05 +00:00
|
|
|
|
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);
|
2013-03-27 06:16:55 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 03:18:25 +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;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
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();
|
2013-03-24 04:25:16 +00:00
|
|
|
|
|
2013-09-14 06:36:07 +00:00
|
|
|
|
public BasicRepository(IDatabase database, IEventAggregator eventAggregator)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_database = database;
|
2013-09-14 06:36:07 +00:00
|
|
|
|
_eventAggregator = eventAggregator;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 06:54:15 +00:00
|
|
|
|
protected 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-24 04:25:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 03:51:32 +00:00
|
|
|
|
public int Count()
|
2013-03-24 04:25:16 +00:00
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
return DataMapper.Query<TModel>().GetRowCount();
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
|
public TModel Get(int id)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2014-02-22 08:53:29 +00:00
|
|
|
|
var model = Query.Where(c => c.Id == id).SingleOrDefault();
|
2013-07-09 22:06:30 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-08-03 03:01:16 +00:00
|
|
|
|
var idList = ids.ToList();
|
2015-10-03 17:45:26 +00:00
|
|
|
|
var query = string.Format("Id IN ({0})", string.Join(",", idList));
|
2013-06-02 06:41:30 +00:00
|
|
|
|
var result = Query.Where(query).ToList();
|
|
|
|
|
|
2013-08-03 03:01:16 +00:00
|
|
|
|
if (result.Count != idList.Count())
|
2013-06-02 06:41:30 +00:00
|
|
|
|
{
|
2017-01-05 23:32:17 +00:00
|
|
|
|
throw new ApplicationException($"Expected query to return {idList.Count} rows but returned {result.Count}");
|
2013-06-02 06:41:30 +00:00
|
|
|
|
}
|
2013-04-18 00:33:38 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-06 21:20:33 +00:00
|
|
|
|
public TModel SingleOrDefault()
|
2013-04-25 04:27:49 +00:00
|
|
|
|
{
|
|
|
|
|
return All().SingleOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TModel Single()
|
2013-03-06 21:20:33 +00:00
|
|
|
|
{
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 07:59:43 +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;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-07 04:34:56 +00:00
|
|
|
|
public void Delete(TModel model)
|
|
|
|
|
{
|
2013-11-13 20:08:37 +00:00
|
|
|
|
Delete(model.Id);
|
2013-03-07 04:34:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public void InsertMany(IList<TModel> models)
|
2013-02-20 02:05:15 +00:00
|
|
|
|
{
|
2014-02-21 07:03:36 +00:00
|
|
|
|
using (var unitOfWork = new UnitOfWork(() => DataMapper))
|
2013-03-25 03:51:32 +00:00
|
|
|
|
{
|
2014-11-24 20:00:19 +00:00
|
|
|
|
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
|
2014-02-21 07:03:36 +00:00
|
|
|
|
|
|
|
|
|
foreach (var model in models)
|
|
|
|
|
{
|
|
|
|
|
unitOfWork.DB.Insert(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unitOfWork.Commit();
|
2013-03-25 03:51:32 +00:00
|
|
|
|
}
|
2013-02-20 02:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public void UpdateMany(IList<TModel> models)
|
2013-02-20 02:05:15 +00:00
|
|
|
|
{
|
2014-02-21 07:03:36 +00:00
|
|
|
|
using (var unitOfWork = new UnitOfWork(() => DataMapper))
|
2013-03-25 03:51:32 +00:00
|
|
|
|
{
|
2014-11-24 20:00:19 +00:00
|
|
|
|
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
|
2014-02-21 07:03:36 +00:00
|
|
|
|
|
|
|
|
|
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-02-20 02:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public void DeleteMany(List<TModel> models)
|
2013-03-05 19:49:34 +00:00
|
|
|
|
{
|
2014-02-21 07:03:36 +00:00
|
|
|
|
DeleteMany(models.Select(m => m.Id));
|
2013-03-05 19:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public TModel Upsert(TModel model)
|
2013-02-18 07:59:43 +00:00
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
|
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;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
2014-02-21 07:03:36 +00:00
|
|
|
|
using (var unitOfWork = new UnitOfWork(() => DataMapper))
|
|
|
|
|
{
|
2014-11-24 20:00:19 +00:00
|
|
|
|
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
|
2014-02-21 07:03:36 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-11-28 07:11:05 +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);
|
2013-11-28 07:11:05 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-27 06:16:55 +00:00
|
|
|
|
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
|
2013-03-24 04:16:00 +00:00
|
|
|
|
{
|
2013-03-24 04:25:16 +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>()
|
2013-03-27 06:16:55 +00:00
|
|
|
|
.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)
|
|
|
|
|
{
|
2014-02-01 22:09:22 +00:00
|
|
|
|
pagingSpec.Records = GetPagedQuery(Query, pagingSpec).ToList();
|
|
|
|
|
pagingSpec.TotalRecords = GetPagedQuery(Query, pagingSpec).GetRowCount();
|
2013-06-05 00:49:53 +00:00
|
|
|
|
|
|
|
|
|
return pagingSpec;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-01 22:09:22 +00:00
|
|
|
|
protected virtual SortBuilder<TModel> GetPagedQuery(QueryBuilder<TModel> query, PagingSpec<TModel> pagingSpec)
|
|
|
|
|
{
|
|
|
|
|
return query.Where(pagingSpec.FilterExpression)
|
|
|
|
|
.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-05-21 04:10:04 +00:00
|
|
|
|
{
|
2013-09-11 06:33:47 +00:00
|
|
|
|
PublishModelEvent(model, ModelAction.Created);
|
2013-05-21 04:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-09-14 06:36:07 +00:00
|
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
}
|