using System; using System.Collections.Generic; using System.Linq; using Sqo; namespace NzbDrone.Core.Datastore { public interface IObjectDatabase : IDisposable { IEnumerable AsQueryable(); T Insert(T obj) where T : BaseRepositoryModel; T Update(T obj) where T : BaseRepositoryModel; IList InsertMany(IList objects) where T : BaseRepositoryModel; IList UpdateMany(IList objects) where T : BaseRepositoryModel; void Delete(T obj) where T : BaseRepositoryModel; void DeleteMany(IEnumerable objects) where T : BaseRepositoryModel; } public class SiaqodbProxy : IObjectDatabase { private readonly Siaqodb _db; public SiaqodbProxy(Siaqodb db) { _db = db; } public void Dispose() { } public IEnumerable AsQueryable() { return _db.Cast(); } public T Insert(T obj) where T : BaseRepositoryModel { if (obj.OID != 0) { throw new InvalidOperationException("Attempted to insert object with existing ID as new object"); } _db.StoreObject(obj); return obj; } public T Update(T obj) where T : BaseRepositoryModel { if (obj.OID == 0) { throw new InvalidOperationException("Attempted to update object without an ID"); } _db.StoreObject(obj); return obj; } public IList InsertMany(IList objects) where T : BaseRepositoryModel { return DoMany(objects,Insert); } public IList UpdateMany(IList objects) where T : BaseRepositoryModel { return DoMany(objects, Update); } public void Delete(T obj) where T : BaseRepositoryModel { throw new NotImplementedException(); } public void DeleteMany(IEnumerable objects) where T : BaseRepositoryModel { throw new NotImplementedException(); } private IList DoMany(IEnumerable objects, Func function) where T : BaseRepositoryModel { return objects.Select(function).ToList(); } } }