using System.Collections.Generic; using System.Data; using System.Linq; using ServiceStack.OrmLite; namespace NzbDrone.Core.Datastore { public interface IBasicDb where TModel : ModelBase, new() { IEnumerable All(); int Count(); TModel Get(int id); //TModel Single(); //TModel SingleOrDefault(); TModel Insert(TModel model); TModel Update(TModel model); TModel Upsert(TModel model); void Delete(int id); void Delete(TModel model); void InsertMany(IList models); void UpdateMany(IList models); void DeleteMany(List models); void Purge(); bool HasItems(); } public class BasicDb : IBasicDb where TModel : ModelBase, new() { public BasicDb(IDbConnection database) { Database = database; } public IDbConnection Database { get; private set; } public IEnumerable All() { return Database.Select(); } public int Count() { return (int)Database.Count(); } public TModel Get(int id) { return Database.GetById(id); } //public TModel Single() //{ // return Queryable.Single(); //} //public TModel SingleOrDefault() //{ // return Queryable.SingleOrDefault(); //} public TModel Insert(TModel model) { Database.Insert(model); model.Id = (int)Database.GetLastInsertId(); return model; } public TModel Update(TModel model) { 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; } } }