You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/NzbDrone.Core/Datastore/BasicRepository.cs

191 lines
4.8 KiB

12 years ago
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
12 years ago
using System.Linq.Expressions;
using Marr.Data;
using Marr.Data.QGen;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Datastore
{
12 years ago
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
IEnumerable<TModel> All();
int Count();
TModel Get(int id);
IEnumerable<TModel> Get(IEnumerable<int> ids);
TModel SingleOrDefault();
12 years ago
TModel Insert(TModel model);
TModel Update(TModel model);
12 years ago
TModel Upsert(TModel model);
void Delete(int id);
void Delete(TModel model);
12 years ago
void InsertMany(IList<TModel> model);
void UpdateMany(IList<TModel> model);
void DeleteMany(List<TModel> model);
void Purge();
12 years ago
bool HasItems();
12 years ago
void DeleteMany(IEnumerable<int> ids);
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
12 years ago
TModel Single();
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
//TODO: add assertion to make sure model properly mapped
private readonly IDataMapper _dataMapper;
public BasicRepository(IDatabase database)
{
_dataMapper = database.DataMapper;
}
protected QueryBuilder<TModel> Query
{
get { return _dataMapper.Query<TModel>(); }
}
protected void Delete(Expression<Func<TModel, bool>> filter)
{
_dataMapper.Delete(filter);
}
public IEnumerable<TModel> All()
{
return _dataMapper.Query<TModel>().ToList();
}
public int Count()
{
return _dataMapper.Query<TModel>().Count();
}
public TModel Get(int id)
{
12 years ago
return _dataMapper.Query<TModel>().Single(c => c.Id == id);
}
public IEnumerable<TModel> Get(IEnumerable<int> ids)
{
var idList = ids.ToList();
var result = Query.Where(String.Format("Id IN ({0})", String.Join(",", idList))).ToList();
var resultCount = result.Count;
if (resultCount != idList.Count || result.Select(r => r.Id).Distinct().Count() != resultCount)
throw new InvalidOperationException("Unexpected result from query");
return result;
}
public TModel SingleOrDefault()
12 years ago
{
return All().SingleOrDefault();
}
public TModel Single()
{
12 years ago
return All().Single();
}
12 years ago
public TModel Insert(TModel model)
{
12 years ago
if (model.Id != 0)
{
throw new InvalidOperationException("Can't insert model with existing ID");
}
var id = _dataMapper.Insert(model);
12 years ago
return model;
}
public TModel Update(TModel model)
{
12 years ago
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
_dataMapper.Update(model, c => c.Id == model.Id);
12 years ago
return model;
}
12 years ago
public void Delete(TModel model)
{
_dataMapper.Delete<TModel>(c => c.Id == model.Id);
}
12 years ago
public void InsertMany(IList<TModel> models)
{
foreach (var model in models)
{
Insert(model);
}
}
12 years ago
public void UpdateMany(IList<TModel> models)
{
foreach (var model in models)
{
Update(model);
}
}
12 years ago
public void DeleteMany(List<TModel> models)
{
models.ForEach(Delete);
}
12 years ago
public TModel Upsert(TModel model)
{
if (model.Id == 0)
{
Insert(model);
12 years ago
return model;
}
Update(model);
12 years ago
return model;
}
public void Delete(int id)
{
_dataMapper.Delete<TModel>(c => c.Id == id);
}
public void DeleteMany(IEnumerable<int> ids)
{
ids.ToList().ForEach(Delete);
}
public void Purge()
{
_dataMapper.Delete<TModel>(c => c.Id > -1);
}
12 years ago
public bool HasItems()
{
12 years ago
return Count() > 0;
}
public void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties)
12 years ago
{
if (model.Id == 0)
{
throw new InvalidOperationException("Attempted to updated model without ID");
}
_dataMapper.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
12 years ago
}
}
}