sonarr-repo-only/NzbDrone.Core/Datastore/EloqueraDb.cs

76 lines
1.8 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
{
_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
}
public IList<T> InsertMany<T>(IList<T> objects) where T : BaseRepositoryModel
2013-02-03 22:01:36 +00:00
{
_idService.EnsureIds(objects, new HashSet<object>());
2013-02-04 01:27:34 +00:00
return DoMany(objects, Insert);
2013-02-03 22:01:36 +00:00
}
2013-02-04 00:10:15 +00:00
public T Update<T>(T obj)
2013-02-03 22:01:36 +00:00
{
Db.Store(obj);
2013-02-04 00:10:15 +00:00
return obj;
2013-02-03 22:01:36 +00:00
}
public IList<T> UpdateMany<T>(IList<T> objects)
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-04 04:18:59 +00:00
public void Delete<T>(T obj) where T : new()
2013-02-03 22:01:36 +00:00
{
Db.Delete(obj);
2013-02-03 22:01:36 +00:00
}
2013-02-13 01:25:32 +00:00
public void DeleteMany<T>(IEnumerable<T> objects) where T : new()
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-04 00:10:15 +00:00
private IList<T> DoMany<T>(IEnumerable<T> objects, Func<T, T> function)
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
}
}
}