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