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.
Readarr/NzbDrone.Core/Datastore/BasicRepository.cs

155 lines
3.9 KiB

12 years ago
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
12 years ago
using System.Linq.Expressions;
using NzbDrone.Core.Tv;
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();
TModel Get(int id);
12 years ago
TModel Single(Expression<Func<TModel, bool>> predicate);
TModel SingleOrDefault();
12 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);
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 UpdateOnly<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
}
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
{
12 years ago
public BasicRepository(IDbConnection database)
{
12 years ago
Database = database;
}
12 years ago
public IDbConnection Database { get; private set; }
12 years ago
public IEnumerable<TModel> All()
{
12 years ago
return Database.Select<TModel>();
}
public int Count()
{
12 years ago
return (int)Database.Count<TModel>();
}
public TModel Get(int id)
{
12 years ago
return Database.GetById<TModel>(id);
}
12 years ago
public TModel Single(Expression<Func<TModel, bool>> predicate)
{
12 years ago
return Database.Select(predicate).Single();
}
public TModel SingleOrDefault()
{
12 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();
}
public List<TModel> Where(Expression<Func<TModel, bool>> predicate)
{
return Database.Select(predicate);
}
12 years ago
public TModel Insert(TModel model)
{
12 years ago
Database.Insert(model);
model.Id = (int)Database.GetLastInsertId();
return model;
}
public TModel Update(TModel model)
{
12 years ago
Database.Update(model);
return model;
}
12 years ago
public void Delete(TModel model)
{
12 years ago
Database.Delete(model);
}
12 years ago
public void InsertMany(IList<TModel> models)
{
12 years ago
Database.InsertAll(models);
}
12 years ago
public void UpdateMany(IList<TModel> models)
{
12 years ago
Database.UpdateAll(models);
}
12 years ago
public void DeleteMany(List<TModel> models)
{
12 years ago
Database.DeleteAll(models);
}
12 years ago
public TModel Upsert(TModel model)
{
if (model.Id == 0)
{
12 years ago
Database.Insert(model);
model.Id = (int)Database.GetLastInsertId();
return model;
}
12 years ago
Database.Update(model);
return model;
}
public void Delete(int id)
{
12 years ago
Database.DeleteById<TModel>(id);
}
public void DeleteMany(IEnumerable<int> ids)
{
12 years ago
Database.DeleteByIds<TModel>(ids);
}
public void Purge()
{
12 years ago
Database.DeleteAll<TModel>();
}
12 years ago
public bool HasItems()
{
12 years ago
return Count() > 0;
}
public void UpdateOnly<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields)
{
Database.UpdateOnly(model, onlyFields);
12 years ago
}
}
}