2013-03-24 04:16:00 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
using System.Linq;
|
2013-03-24 04:16:00 +00:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
using ServiceStack.OrmLite;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
2013-03-02 18:25:39 +00:00
|
|
|
|
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-02-19 06:56:02 +00:00
|
|
|
|
IEnumerable<TModel> All();
|
2013-02-23 19:38:25 +00:00
|
|
|
|
int Count();
|
2013-02-17 05:35:52 +00:00
|
|
|
|
TModel Get(int id);
|
2013-03-24 04:16:00 +00:00
|
|
|
|
TModel Single(Expression<Func<TModel, bool>> predicate);
|
2013-03-06 21:20:33 +00:00
|
|
|
|
TModel SingleOrDefault();
|
2013-03-24 04:16:00 +00:00
|
|
|
|
TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate);
|
|
|
|
|
List<TModel> Where(Expression<Func<TModel, bool>> predicate);
|
2013-02-19 06:56:02 +00:00
|
|
|
|
TModel Insert(TModel model);
|
2013-02-18 07:59:43 +00:00
|
|
|
|
TModel Update(TModel model);
|
2013-03-24 04:16:00 +00:00
|
|
|
|
TModel Upsert(TModel model);
|
2013-02-17 05:35:52 +00:00
|
|
|
|
void Delete(int id);
|
2013-03-07 04:34:56 +00:00
|
|
|
|
void Delete(TModel model);
|
2013-03-24 04:16:00 +00:00
|
|
|
|
void InsertMany(IList<TModel> model);
|
|
|
|
|
void UpdateMany(IList<TModel> model);
|
2013-03-06 21:20:33 +00:00
|
|
|
|
void DeleteMany(List<TModel> model);
|
2013-02-23 19:38:25 +00:00
|
|
|
|
void Purge();
|
2013-03-02 18:25:39 +00:00
|
|
|
|
bool HasItems();
|
2013-03-24 04:16:00 +00:00
|
|
|
|
void DeleteMany(IEnumerable<int> ids);
|
|
|
|
|
void UpdateOnly<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 03:18:25 +00:00
|
|
|
|
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public BasicRepository(IDbConnection database)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database = database;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public IDbConnection Database { get; private set; }
|
2013-02-17 05:09:35 +00:00
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
public IEnumerable<TModel> All()
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
return Database.Select<TModel>();
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 19:38:25 +00:00
|
|
|
|
public int Count()
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
return (int)Database.Count<TModel>();
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
|
public TModel Get(int id)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
return Database.GetById<TModel>(id);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public TModel Single(Expression<Func<TModel, bool>> predicate)
|
2013-03-06 21:20:33 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
return Database.Select(predicate).Single();
|
2013-03-06 21:20:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TModel SingleOrDefault()
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
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);
|
2013-03-06 21:20:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 06:56:02 +00:00
|
|
|
|
public TModel Insert(TModel model)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.Insert(model);
|
|
|
|
|
model.Id = (int)Database.GetLastInsertId();
|
|
|
|
|
return model;
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 07:59:43 +00:00
|
|
|
|
public TModel Update(TModel model)
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.Update(model);
|
|
|
|
|
return model;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
|
2013-03-07 04:34:56 +00:00
|
|
|
|
public void Delete(TModel model)
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.Delete(model);
|
2013-03-07 04:34:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public void InsertMany(IList<TModel> models)
|
2013-02-20 02:05:15 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.InsertAll(models);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public void UpdateMany(IList<TModel> models)
|
2013-02-20 02:05:15 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.UpdateAll(models);
|
2013-02-20 02:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public void DeleteMany(List<TModel> models)
|
2013-03-05 19:49:34 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.DeleteAll(models);
|
2013-03-05 19:49:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 04:16:00 +00:00
|
|
|
|
public TModel Upsert(TModel model)
|
2013-02-18 07:59:43 +00:00
|
|
|
|
{
|
2013-02-26 03:58:57 +00:00
|
|
|
|
if (model.Id == 0)
|
2013-02-23 19:38:25 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.Insert(model);
|
|
|
|
|
model.Id = (int)Database.GetLastInsertId();
|
|
|
|
|
return model;
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.Update(model);
|
|
|
|
|
return model;
|
2013-02-18 07:59:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 03:50:22 +00:00
|
|
|
|
public void Delete(int id)
|
2013-02-05 04:07:07 +00:00
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.DeleteById<TModel>(id);
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
2013-02-23 19:38:25 +00:00
|
|
|
|
|
|
|
|
|
public void DeleteMany(IEnumerable<int> ids)
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.DeleteByIds<TModel>(ids);
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Purge()
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
Database.DeleteAll<TModel>();
|
2013-02-23 19:38:25 +00:00
|
|
|
|
}
|
2013-03-02 18:25:39 +00:00
|
|
|
|
|
|
|
|
|
public bool HasItems()
|
|
|
|
|
{
|
2013-03-24 04:16:00 +00:00
|
|
|
|
return Count() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateOnly<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields)
|
|
|
|
|
{
|
|
|
|
|
Database.UpdateOnly(model, onlyFields);
|
2013-03-02 18:25:39 +00:00
|
|
|
|
}
|
2013-02-05 04:07:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|