2013-02-03 23:01:36 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2013-02-17 02:52:40 +01:00
|
|
|
|
using Sqo;
|
2013-02-03 23:01:36 +01:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
public interface IObjectDatabase : IDisposable
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
IEnumerable<T> AsQueryable<T>();
|
2013-02-18 04:18:25 +01:00
|
|
|
|
T Insert<T>(T obj) where T : ModelBase;
|
|
|
|
|
T Update<T>(T obj) where T : ModelBase;
|
|
|
|
|
IList<T> InsertMany<T>(IList<T> objects) where T : ModelBase;
|
|
|
|
|
IList<T> UpdateMany<T>(IList<T> objects) where T : ModelBase;
|
|
|
|
|
void Delete<T>(T obj) where T : ModelBase;
|
|
|
|
|
void DeleteMany<T>(IEnumerable<T> objects) where T : ModelBase;
|
2013-02-17 02:52:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SiaqodbProxy : IObjectDatabase
|
|
|
|
|
{
|
|
|
|
|
private readonly Siaqodb _db;
|
|
|
|
|
|
|
|
|
|
public SiaqodbProxy(Siaqodb db)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
}
|
2013-02-03 23:01:36 +01:00
|
|
|
|
|
2013-02-17 02:52:40 +01:00
|
|
|
|
public void Dispose()
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-04 01:10:15 +01:00
|
|
|
|
public IEnumerable<T> AsQueryable<T>()
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
return _db.Cast<T>();
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public T Insert<T>(T obj) where T : ModelBase
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
if (obj.OID != 0)
|
2013-02-16 05:03:54 +01:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Attempted to insert object with existing ID as new object");
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-17 05:33:56 +01:00
|
|
|
|
_db.StoreObject(obj);
|
2013-02-04 01:10:15 +01:00
|
|
|
|
return obj;
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public T Update<T>(T obj) where T : ModelBase
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
if (obj.OID == 0)
|
2013-02-16 05:03:54 +01:00
|
|
|
|
{
|
2013-02-17 02:52:40 +01:00
|
|
|
|
throw new InvalidOperationException("Attempted to update object without an ID");
|
2013-02-16 05:03:54 +01:00
|
|
|
|
}
|
2013-02-03 23:01:36 +01:00
|
|
|
|
|
2013-02-17 02:52:40 +01:00
|
|
|
|
_db.StoreObject(obj);
|
2013-02-04 01:10:15 +01:00
|
|
|
|
return obj;
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public IList<T> InsertMany<T>(IList<T> objects) where T : ModelBase
|
2013-02-16 05:03:54 +01:00
|
|
|
|
{
|
2013-02-17 05:33:56 +01:00
|
|
|
|
return DoMany(objects, Insert);
|
2013-02-16 05:03:54 +01:00
|
|
|
|
}
|
2013-02-17 02:52:40 +01:00
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public IList<T> UpdateMany<T>(IList<T> objects) where T : ModelBase
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-04 01:10:15 +01:00
|
|
|
|
return DoMany(objects, Update);
|
2013-02-17 02:52:40 +01:00
|
|
|
|
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public void Delete<T>(T obj) where T : ModelBase
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 05:33:56 +01:00
|
|
|
|
_db.Delete(obj);
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
public void DeleteMany<T>(IEnumerable<T> objects) where T : ModelBase
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-17 05:33:56 +01:00
|
|
|
|
foreach (var o in objects)
|
|
|
|
|
{
|
|
|
|
|
Delete(o);
|
|
|
|
|
}
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-18 04:18:25 +01:00
|
|
|
|
private IList<T> DoMany<T>(IEnumerable<T> objects, Func<T, T> function) where T : ModelBase
|
2013-02-03 23:01:36 +01:00
|
|
|
|
{
|
2013-02-04 01:10:15 +01:00
|
|
|
|
return objects.Select(function).ToList();
|
2013-02-03 23:01:36 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|