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

189 lines
5.0 KiB

11 years ago
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
11 years ago
using System.Linq.Expressions;
using ServiceStack.OrmLite;
namespace NzbDrone.Core.Datastore
{
12 years ago
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
IEnumerable<TModel> All();
int Count();
bool Any(Expression<Func<TModel, bool>> predicate);
TModel Get(int id);
11 years ago
TModel Single(Expression<Func<TModel, bool>> predicate);
TModel SingleOrDefault();
11 years ago
TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate);
List<TModel> Where(Expression<Func<TModel, bool>> predicate);
12 years ago
TModel Insert(TModel model);
TModel Update(TModel model);
11 years ago
TModel Upsert(TModel model);
void Delete(int id);
void Delete(TModel model);
11 years ago
void InsertMany(IList<TModel> model);
void UpdateMany(IList<TModel> model);
void DeleteMany(List<TModel> model);
void Purge();
12 years ago
bool HasItems();
11 years ago
void DeleteMany(IEnumerable<int> ids);
void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
List<TModel> Where(SqlExpressionVisitor<TModel> expression);
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{
private readonly IDbConnection _database;
11 years ago
public BasicRepository(IDbConnection database)
{
_database = database;
}
12 years ago
public IEnumerable<TModel> All()
{
return _database.Select<TModel>();
}
public int Count()
{
return (int)_database.Count<TModel>();
}
public bool Any(Expression<Func<TModel, bool>> predicate)
{
return _database.Exists<TModel>(predicate);
}
public TModel Get(int id)
{
11 years ago
try
{
return _database.GetById<TModel>(id);
}
catch (ArgumentNullException e)
{
throw new InvalidOperationException(e.Message);
}
}
11 years ago
public TModel Single(Expression<Func<TModel, bool>> predicate)
{
return _database.Select(predicate).Single();
}
public TModel SingleOrDefault()
{
11 years ago
return All().Single();
}
public TModel Single()
{
throw new System.NotImplementedException();
}
public TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate)
{
return _database.Select(predicate).SingleOrDefault();
11 years ago
}
public List<TModel> Where(Expression<Func<TModel, bool>> predicate)
{
return _database.Select(predicate);
}
public List<TModel> Where(SqlExpressionVisitor<TModel> expression)
{
return _database.Select(expression);
}
12 years ago
public TModel Insert(TModel model)
{
11 years ago
if (model.Id != 0)
{
throw new InvalidOperationException("Can't insert model with existing ID");
}
_database.Insert(model);
model.Id = (int)_database.GetLastInsertId();
11 years ago
return model;
}
public TModel Update(TModel model)
{
11 years ago
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
_database.Update(model);
11 years ago
return model;
}
11 years ago
public void Delete(TModel model)
{
_database.Delete(model);
}
11 years ago
public void InsertMany(IList<TModel> models)
{
_database.InsertAll(models);
}
11 years ago
public void UpdateMany(IList<TModel> models)
{
_database.UpdateAll(models);
}
11 years ago
public void DeleteMany(List<TModel> models)
{
_database.DeleteAll(models);
}
11 years ago
public TModel Upsert(TModel model)
{
if (model.Id == 0)
{
_database.Insert(model);
model.Id = (int)_database.GetLastInsertId();
11 years ago
return model;
}
_database.Update(model);
11 years ago
return model;
}
public void Delete(int id)
{
_database.DeleteById<TModel>(id);
}
public void DeleteMany(IEnumerable<int> ids)
{
_database.DeleteByIds<TModel>(ids);
}
public void Purge()
{
_database.DeleteAll<TModel>();
}
12 years ago
public bool HasItems()
{
11 years ago
return Count() > 0;
}
public void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields)
11 years ago
{
if (model.Id == 0)
{
throw new InvalidOperationException("Attempted to updated model without ID");
}
_database.UpdateOnly(model, onlyFields, m => m.Id == model.Id);
12 years ago
}
}
}