Sonarr/NzbDrone.Core/Datastore/EloqueraDb.cs

92 lines
2.4 KiB
C#
Raw Normal View History

2013-02-03 22:01:36 +00:00
using System;
using System.Collections;
2013-02-03 22:01:36 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
2013-02-03 22:01:36 +00:00
using Eloquera.Client;
namespace NzbDrone.Core.Datastore
{
public class EloqueraDb : IDisposable
{
private readonly IdService _idService;
public DB Db { get; private set; }
2013-02-03 22:01:36 +00:00
public EloqueraDb(DB db, IdService idService)
2013-02-03 22:01:36 +00:00
{
_idService = idService;
Db = db;
2013-02-03 22:01:36 +00:00
}
2013-02-04 00:10:15 +00:00
public IEnumerable<T> AsQueryable<T>()
2013-02-03 22:01:36 +00:00
{
return Db.Query<T>();
2013-02-03 22:01:36 +00:00
}
2013-02-13 01:25:32 +00:00
public T Insert<T>(T obj) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
2013-02-16 04:03:54 +00:00
if (obj.Id != 0)
{
throw new InvalidOperationException("Attempted to insert object with existing ID as new object");
}
_idService.EnsureIds(obj, new HashSet<object>());
Db.Store(obj);
2013-02-04 00:10:15 +00:00
return obj;
2013-02-03 22:01:36 +00:00
}
2013-02-16 04:03:54 +00:00
public T Update<T>(T obj) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
2013-02-16 04:03:54 +00:00
if (obj.Id == 0)
{
throw new InvalidOperationException("Attempted to update object without ID");
}
2013-02-03 22:01:36 +00:00
2013-02-16 04:03:54 +00:00
_idService.EnsureIds(obj, new HashSet<object>());
Db.Store(obj);
2013-02-04 00:10:15 +00:00
return obj;
2013-02-03 22:01:36 +00:00
}
2013-02-16 04:03:54 +00:00
public IList<T> InsertMany<T>(IList<T> objects) where T : BaseRepositoryModel
{
_idService.EnsureIds(objects, new HashSet<object>());
return DoMany(objects, Insert);
}
public IList<T> UpdateMany<T>(IList<T> objects) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
_idService.EnsureIds(objects, new HashSet<object>());
2013-02-04 00:10:15 +00:00
return DoMany(objects, Update);
2013-02-03 22:01:36 +00:00
}
2013-02-16 04:03:54 +00:00
public void Delete<T>(T obj) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
2013-02-16 04:03:54 +00:00
if (obj.Id == 0)
{
throw new InvalidOperationException("Attempted to delete an object without an ID");
}
Db.Delete(obj);
2013-02-03 22:01:36 +00:00
}
2013-02-16 04:03:54 +00:00
public void DeleteMany<T>(IEnumerable<T> objects) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
2013-02-04 00:10:15 +00:00
foreach (var o in objects)
{
Delete(o);
}
2013-02-03 22:01:36 +00:00
}
2013-02-16 04:03:54 +00:00
private IList<T> DoMany<T>(IEnumerable<T> objects, Func<T, T> function) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
2013-02-04 00:10:15 +00:00
return objects.Select(function).ToList();
2013-02-03 22:01:36 +00:00
}
2013-02-03 22:01:36 +00:00
public void Dispose()
{
Db.Dispose();
2013-02-03 22:01:36 +00:00
}
}
}