2013-03-24 05:16:00 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
2013-02-05 05:07:07 +01:00
|
|
|
|
using System.Linq;
|
2013-03-24 05:16:00 +01:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using ServiceStack.OrmLite;
|
2013-02-05 05:07:07 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
2013-03-02 19:25:39 +01:00
|
|
|
|
public interface IBasicRepository<TModel> where TModel : ModelBase, new()
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-02-19 07:56:02 +01:00
|
|
|
|
IEnumerable<TModel> All();
|
2013-02-23 20:38:25 +01:00
|
|
|
|
int Count();
|
2013-03-24 05:25:16 +01:00
|
|
|
|
bool Any(Expression<Func<TModel, bool>> predicate);
|
2013-02-17 06:35:52 +01:00
|
|
|
|
TModel Get(int id);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
TModel Single(Expression<Func<TModel, bool>> predicate);
|
2013-03-06 22:20:33 +01:00
|
|
|
|
TModel SingleOrDefault();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate);
|
|
|
|
|
List<TModel> Where(Expression<Func<TModel, bool>> predicate);
|
2013-02-19 07:56:02 +01:00
|
|
|
|
TModel Insert(TModel model);
|
2013-02-18 08:59:43 +01:00
|
|
|
|
TModel Update(TModel model);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
TModel Upsert(TModel model);
|
2013-02-17 06:35:52 +01:00
|
|
|
|
void Delete(int id);
|
2013-03-07 05:34:56 +01:00
|
|
|
|
void Delete(TModel model);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
void InsertMany(IList<TModel> model);
|
|
|
|
|
void UpdateMany(IList<TModel> model);
|
2013-03-06 22:20:33 +01:00
|
|
|
|
void DeleteMany(List<TModel> model);
|
2013-02-23 20:38:25 +01:00
|
|
|
|
void Purge();
|
2013-03-02 19:25:39 +01:00
|
|
|
|
bool HasItems();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
void DeleteMany(IEnumerable<int> ids);
|
2013-03-24 05:25:16 +01:00
|
|
|
|
void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields);
|
2013-03-25 02:38:11 +01:00
|
|
|
|
List<TModel> Where(SqlExpressionVisitor<TModel> expression);
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public class BasicRepository<TModel> : IBasicRepository<TModel> where TModel : ModelBase, new()
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
private readonly IDbConnection _database;
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public BasicRepository(IDbConnection database)
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database = database;
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 07:56:02 +01:00
|
|
|
|
public IEnumerable<TModel> All()
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
return _database.Select<TModel>();
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 20:38:25 +01:00
|
|
|
|
public int Count()
|
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
return (int)_database.Count<TModel>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Any(Expression<Func<TModel, bool>> predicate)
|
|
|
|
|
{
|
|
|
|
|
return _database.Exists<TModel>(predicate);
|
2013-02-23 20:38:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 04:50:22 +01:00
|
|
|
|
public TModel Get(int id)
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-03-24 20:56:51 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _database.GetById<TModel>(id);
|
|
|
|
|
}
|
|
|
|
|
catch (ArgumentNullException e)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(e.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public TModel Single(Expression<Func<TModel, bool>> predicate)
|
2013-03-06 22:20:33 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
return _database.Select(predicate).Single();
|
2013-03-06 22:20:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TModel SingleOrDefault()
|
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
|
return All().Single();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TModel Single()
|
|
|
|
|
{
|
|
|
|
|
throw new System.NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TModel SingleOrDefault(Expression<Func<TModel, bool>> predicate)
|
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
return _database.Select(predicate).SingleOrDefault();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<TModel> Where(Expression<Func<TModel, bool>> predicate)
|
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
return _database.Select(predicate);
|
2013-03-06 22:20:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-25 02:38:11 +01:00
|
|
|
|
public List<TModel> Where(SqlExpressionVisitor<TModel> expression)
|
|
|
|
|
{
|
|
|
|
|
return _database.Select(expression);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-19 07:56:02 +01:00
|
|
|
|
public TModel Insert(TModel model)
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-03-24 20:56:51 +01:00
|
|
|
|
if (model.Id != 0)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Can't insert model with existing ID");
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.Insert(model);
|
|
|
|
|
model.Id = (int)_database.GetLastInsertId();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
return model;
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 08:59:43 +01:00
|
|
|
|
public TModel Update(TModel model)
|
|
|
|
|
{
|
2013-03-24 20:56:51 +01:00
|
|
|
|
if (model.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Can't update model with ID 0");
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.Update(model);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
return model;
|
2013-02-18 08:59:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
|
2013-03-07 05:34:56 +01:00
|
|
|
|
public void Delete(TModel model)
|
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.Delete(model);
|
2013-03-07 05:34:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public void InsertMany(IList<TModel> models)
|
2013-02-20 03:05:15 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.InsertAll(models);
|
2013-02-20 03:05:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public void UpdateMany(IList<TModel> models)
|
2013-02-20 03:05:15 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.UpdateAll(models);
|
2013-02-20 03:05:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public void DeleteMany(List<TModel> models)
|
2013-03-05 20:49:34 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.DeleteAll(models);
|
2013-03-05 20:49:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:16:00 +01:00
|
|
|
|
public TModel Upsert(TModel model)
|
2013-02-18 08:59:43 +01:00
|
|
|
|
{
|
2013-02-26 04:58:57 +01:00
|
|
|
|
if (model.Id == 0)
|
2013-02-23 20:38:25 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.Insert(model);
|
|
|
|
|
model.Id = (int)_database.GetLastInsertId();
|
2013-03-24 05:16:00 +01:00
|
|
|
|
return model;
|
2013-02-23 20:38:25 +01:00
|
|
|
|
}
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.Update(model);
|
2013-03-24 05:16:00 +01:00
|
|
|
|
return model;
|
2013-02-18 08:59:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-16 04:50:22 +01:00
|
|
|
|
public void Delete(int id)
|
2013-02-05 05:07:07 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.DeleteById<TModel>(id);
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
2013-02-23 20:38:25 +01:00
|
|
|
|
|
|
|
|
|
public void DeleteMany(IEnumerable<int> ids)
|
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.DeleteByIds<TModel>(ids);
|
2013-02-23 20:38:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Purge()
|
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
_database.DeleteAll<TModel>();
|
2013-02-23 20:38:25 +01:00
|
|
|
|
}
|
2013-03-02 19:25:39 +01:00
|
|
|
|
|
|
|
|
|
public bool HasItems()
|
|
|
|
|
{
|
2013-03-24 05:16:00 +01:00
|
|
|
|
return Count() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 05:25:16 +01:00
|
|
|
|
public void UpdateFields<TKey>(TModel model, Expression<Func<TModel, TKey>> onlyFields)
|
2013-03-24 05:16:00 +01:00
|
|
|
|
{
|
2013-03-24 05:25:16 +01:00
|
|
|
|
if (model.Id == 0)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Attempted to updated model without ID");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_database.UpdateOnly(model, onlyFields, m => m.Id == model.Id);
|
2013-03-02 19:25:39 +01:00
|
|
|
|
}
|
2013-02-05 05:07:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|