2013-03-24 04:16:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
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;
|
2013-05-05 21:24:33 +00:00
|
|
|
|
using NzbDrone.Core.Datastore.Events;
|
2013-06-02 06:41:30 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-09-11 06:33:47 +00:00
|
|
|
|
using NzbDrone.Core.Messaging;
|
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-02-23 19:38:25 +00:00
|
|
|
|
void Purge();
|
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-05-30 23:32:50 +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-05-05 21:24:33 +00:00
|
|
|
|
private readonly IMessageAggregator _messageAggregator;
|
2013-04-25 04:27:49 +00:00
|
|
|
|
|
2013-05-11 20:06:57 +00:00
|
|
|
|
private IDataMapper DataMapper
|
|
|
|
|
{
|
2013-07-16 00:45:49 +00:00
|
|
|
|
get { return _database.GetDataMapper(); }
|
2013-05-11 20:06:57 +00:00
|
|
|
|
}
|
2013-03-24 04:25:16 +00:00
|
|
|
|
|
2013-05-05 21:24:33 +00:00
|
|
|
|
public BasicRepository(IDatabase database, IMessageAggregator messageAggregator)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
_database = database;
|
2013-05-05 21:24:33 +00:00
|
|
|
|
_messageAggregator = messageAggregator;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-27 03:44:52 +00:00
|
|
|
|
protected QueryBuilder<TModel> Query
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
get { return 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
|
|
|
|
{
|
2013-07-09 22:06:30 +00:00
|
|
|
|
var model = DataMapper.Query<TModel>().SingleOrDefault(c => c.Id == id);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
|
{
|
2013-08-03 03:01:16 +00:00
|
|
|
|
throw new ApplicationException("Expected query to return {0} rows but returned {1}".Inject(idList.Count(), 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-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-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-05-11 20:06:57 +00:00
|
|
|
|
DataMapper.Delete<TModel>(c => c.Id == 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
|
|
|
|
{
|
2013-03-25 03:51:32 +00:00
|
|
|
|
foreach (var model in models)
|
|
|
|
|
{
|
|
|
|
|
Insert(model);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2013-03-25 03:51:32 +00:00
|
|
|
|
foreach (var model in models)
|
|
|
|
|
{
|
|
|
|
|
Update(model);
|
|
|
|
|
}
|
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
|
|
|
|
{
|
2013-03-25 03:51:32 +00:00
|
|
|
|
models.ForEach(Delete);
|
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)
|
|
|
|
|
{
|
2013-03-25 03:51:32 +00:00
|
|
|
|
ids.ToList().ForEach(Delete);
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Purge()
|
|
|
|
|
{
|
2013-05-11 20:06:57 +00:00
|
|
|
|
DataMapper.Delete<TModel>(c => c.Id > -1);
|
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-03-02 18:25:39 +00:00
|
|
|
|
}
|
2013-05-10 23:53:50 +00:00
|
|
|
|
|
2013-05-21 04:10:04 +00:00
|
|
|
|
|
2013-06-05 00:49:53 +00:00
|
|
|
|
public virtual PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec)
|
|
|
|
|
{
|
|
|
|
|
var pagingQuery = Query.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
|
|
|
|
|
.Skip(pagingSpec.PagingOffset())
|
|
|
|
|
.Take(pagingSpec.PageSize);
|
|
|
|
|
|
|
|
|
|
pagingSpec.Records = pagingQuery.ToList();
|
|
|
|
|
|
|
|
|
|
//TODO: Use the same query for count and records
|
|
|
|
|
pagingSpec.TotalRecords = Count();
|
|
|
|
|
|
|
|
|
|
return pagingSpec;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 03:18:26 +00:00
|
|
|
|
public void DeleteAll()
|
|
|
|
|
{
|
|
|
|
|
DataMapper.Delete<TModel>(c => c.Id > 0);
|
|
|
|
|
}
|
2013-06-05 00:49:53 +00:00
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
_messageAggregator.PublishEvent(new ModelEvent<TModel>(model, action));
|
|
|
|
|
}
|
2013-05-30 23:32:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-22 05:31:29 +00:00
|
|
|
|
protected virtual bool PublishModelEvents
|
|
|
|
|
{
|
|
|
|
|
get { return false; }
|
|
|
|
|
}
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|