2013-03-24 07:16:00 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Data;
|
2013-02-05 07:07:07 +03:00
|
|
|
using System.Linq;
|
2013-03-24 07:16:00 +03:00
|
|
|
using System.Linq.Expressions;
|
2013-03-25 06:51:32 +03:00
|
|
|
using Marr.Data;
|
|
|
|
using Marr.Data.QGen;
|
2013-03-31 23:25:39 +03:00
|
|
|
using NzbDrone.Core.Tv;
|
2013-03-25 06:51:32 +03:00
|
|
|
|
2013-02-05 07:07:07 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
{
|
2013-03-02 21:25:39 +03:00
|
|
|
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-02-19 09:56:02 +03:00
|
|
|
IEnumerable<TModel> All();
|
2013-02-23 22:38:25 +03:00
|
|
|
int Count();
|
2013-02-17 08:35:52 +03:00
|
|
|
TModel Get(int id);
|
2013-03-07 00:20:33 +03:00
|
|
|
TModel SingleOrDefault();
|
2013-02-19 09:56:02 +03:00
|
|
|
TModel Insert(TModel model);
|
2013-02-18 10:59:43 +03:00
|
|
|
TModel Update(TModel model);
|
2013-03-24 07:16:00 +03:00
|
|
|
TModel Upsert(TModel model);
|
2013-02-17 08:35:52 +03:00
|
|
|
void Delete(int id);
|
2013-03-07 07:34:56 +03:00
|
|
|
void Delete(TModel model);
|
2013-03-24 07:16:00 +03:00
|
|
|
void InsertMany(IList<TModel> model);
|
|
|
|
void UpdateMany(IList<TModel> model);
|
2013-03-07 00:20:33 +03:00
|
|
|
void DeleteMany(List<TModel> model);
|
2013-02-23 22:38:25 +03:00
|
|
|
void Purge();
|
2013-03-02 21:25:39 +03:00
|
|
|
bool HasItems();
|
2013-03-24 07:16:00 +03:00
|
|
|
void DeleteMany(IEnumerable<int> ids);
|
2013-03-27 09:16:55 +03:00
|
|
|
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
2013-02-18 06:18:25 +03:00
|
|
|
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
private readonly IDataMapper _dataMapper;
|
2013-03-24 07:25:16 +03:00
|
|
|
|
2013-03-25 06:51:32 +03:00
|
|
|
public BasicRepository(IDatabase database)
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
_dataMapper = database.DataMapper;
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
2013-03-27 06:44:52 +03:00
|
|
|
protected QueryBuilder<TModel> Query
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-03-27 06:44:52 +03:00
|
|
|
get { return _dataMapper.Query<TModel>(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void Delete(Expression<Func<TModel, bool>> filter)
|
|
|
|
{
|
|
|
|
_dataMapper.Delete(filter);
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
2013-03-25 06:51:32 +03:00
|
|
|
public IEnumerable<TModel> All()
|
2013-02-23 22:38:25 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
return _dataMapper.Query<TModel>().ToList();
|
2013-03-24 07:25:16 +03:00
|
|
|
}
|
|
|
|
|
2013-03-25 06:51:32 +03:00
|
|
|
public int Count()
|
2013-03-24 07:25:16 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
return _dataMapper.Query<TModel>().Count();
|
2013-02-23 22:38:25 +03:00
|
|
|
}
|
|
|
|
|
2013-02-16 06:50:22 +03:00
|
|
|
public TModel Get(int id)
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-03-25 10:36:22 +03:00
|
|
|
return _dataMapper.Query<TModel>().Single(c => c.Id == id);
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
2013-03-07 00:20:33 +03:00
|
|
|
public TModel SingleOrDefault()
|
|
|
|
{
|
2013-03-24 07:16:00 +03:00
|
|
|
return All().Single();
|
|
|
|
}
|
|
|
|
|
2013-02-19 09:56:02 +03:00
|
|
|
public TModel Insert(TModel model)
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-03-24 22:56:51 +03:00
|
|
|
if (model.Id != 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Can't insert model with existing ID");
|
|
|
|
}
|
|
|
|
|
2013-03-25 06:51:32 +03:00
|
|
|
var id = _dataMapper.Insert(model);
|
2013-03-24 07:16:00 +03:00
|
|
|
return model;
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
|
|
|
|
2013-02-18 10:59:43 +03:00
|
|
|
public TModel Update(TModel model)
|
|
|
|
{
|
2013-03-24 22:56:51 +03:00
|
|
|
if (model.Id == 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Can't update model with ID 0");
|
|
|
|
}
|
|
|
|
|
2013-03-25 06:51:32 +03:00
|
|
|
_dataMapper.Update(model, c => c.Id == model.Id);
|
2013-03-24 07:16:00 +03:00
|
|
|
return model;
|
2013-02-18 10:59:43 +03:00
|
|
|
}
|
|
|
|
|
2013-03-24 07:16:00 +03:00
|
|
|
|
2013-03-07 07:34:56 +03:00
|
|
|
public void Delete(TModel model)
|
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
_dataMapper.Delete<TModel>(c => c.Id == model.Id);
|
2013-03-07 07:34:56 +03:00
|
|
|
}
|
|
|
|
|
2013-03-24 07:16:00 +03:00
|
|
|
public void InsertMany(IList<TModel> models)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
Insert(model);
|
|
|
|
}
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-24 07:16:00 +03:00
|
|
|
public void UpdateMany(IList<TModel> models)
|
2013-02-20 05:05:15 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
foreach (var model in models)
|
|
|
|
{
|
|
|
|
Update(model);
|
|
|
|
}
|
2013-02-20 05:05:15 +03:00
|
|
|
}
|
|
|
|
|
2013-03-24 07:16:00 +03:00
|
|
|
public void DeleteMany(List<TModel> models)
|
2013-03-05 22:49:34 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
models.ForEach(Delete);
|
2013-03-05 22:49:34 +03:00
|
|
|
}
|
|
|
|
|
2013-03-24 07:16:00 +03:00
|
|
|
public TModel Upsert(TModel model)
|
2013-02-18 10:59:43 +03:00
|
|
|
{
|
2013-02-26 06:58:57 +03:00
|
|
|
if (model.Id == 0)
|
2013-02-23 22:38:25 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
Insert(model);
|
2013-03-24 07:16:00 +03:00
|
|
|
return model;
|
2013-02-23 22:38:25 +03:00
|
|
|
}
|
2013-03-25 06:51:32 +03:00
|
|
|
Update(model);
|
2013-03-24 07:16:00 +03:00
|
|
|
return model;
|
2013-02-18 10:59:43 +03:00
|
|
|
}
|
|
|
|
|
2013-02-16 06:50:22 +03:00
|
|
|
public void Delete(int id)
|
2013-02-05 07:07:07 +03:00
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
_dataMapper.Delete<TModel>(c => c.Id == id);
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
2013-02-23 22:38:25 +03:00
|
|
|
|
|
|
|
public void DeleteMany(IEnumerable<int> ids)
|
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
ids.ToList().ForEach(Delete);
|
2013-02-23 22:38:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Purge()
|
|
|
|
{
|
2013-03-25 06:51:32 +03:00
|
|
|
_dataMapper.Delete<TModel>(c => c.Id > -1);
|
2013-02-23 22:38:25 +03:00
|
|
|
}
|
2013-03-02 21:25:39 +03:00
|
|
|
|
|
|
|
public bool HasItems()
|
|
|
|
{
|
2013-03-24 07:16:00 +03:00
|
|
|
return Count() > 0;
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:16:55 +03:00
|
|
|
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
|
2013-03-24 07:16:00 +03:00
|
|
|
{
|
2013-03-24 07:25:16 +03:00
|
|
|
if (model.Id == 0)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Attempted to updated model without ID");
|
|
|
|
}
|
|
|
|
|
2013-03-27 09:16:55 +03:00
|
|
|
_dataMapper.Update<TModel>()
|
|
|
|
.Where(c => c.Id == model.Id)
|
|
|
|
.ColumnsIncluding(properties)
|
|
|
|
.Entity(model)
|
|
|
|
.Execute();
|
2013-03-02 21:25:39 +03:00
|
|
|
}
|
2013-03-27 09:16:55 +03:00
|
|
|
|
2013-02-05 07:07:07 +03:00
|
|
|
}
|
|
|
|
}
|