Lidarr/NzbDrone.Core/Datastore/SiaqodbProxy.cs

133 lines
3.5 KiB
C#
Raw Normal View History

2013-02-03 22:01:36 +00:00
using System;
using System.Collections.Generic;
2013-03-01 00:50:50 +00:00
using System.Diagnostics;
2013-02-03 22:01:36 +00:00
using System.Linq;
2013-02-17 01:52:40 +00:00
using Sqo;
2013-02-03 22:01:36 +00:00
namespace NzbDrone.Core.Datastore
{
2013-02-17 01:52:40 +00:00
public interface IObjectDatabase : IDisposable
2013-02-03 22:01:36 +00:00
{
2013-02-17 01:52:40 +00:00
IEnumerable<T> AsQueryable<T>();
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-03-01 07:03:41 +00:00
void UpdateField<T>(T model, string fieldName) where T : ModelBase;
2013-02-17 01:52:40 +00:00
}
2013-03-01 00:50:50 +00:00
public static class SiaqodbLogger
{
public static void ListenTo(Siaqodb db)
{
db.DeletedObject += OnDeletedObject;
2013-03-01 07:03:41 +00:00
db.LoadingObject += OnLoadingObject;
db.LoadedObject += OnLoadedObject;
2013-03-01 00:50:50 +00:00
}
private static void OnLoadedObject(object sender, LoadedObjectEventArgs e)
{
Write("Loaded", e.Object.GetType(), e.OID);
}
private static void OnLoadingObject(object sender, LoadingObjectEventArgs e)
{
Write("Loading", e.ObjectType, e.OID);
}
static void OnDeletedObject(object sender, DeletedEventsArgs e)
{
Write("Deleted", e.ObjectType, e.OID);
}
private static void Write(string operation, Type modelType, int id)
{
var message = string.Format("{0} {1}[{2}]", operation, modelType.Name, id);
Trace.WriteLine(message, "Siaqodb");
}
}
2013-02-17 01:52:40 +00:00
public class SiaqodbProxy : IObjectDatabase
{
private readonly Siaqodb _db;
public SiaqodbProxy(Siaqodb db)
{
_db = db;
2013-03-01 00:50:50 +00:00
//SiaqodbConfigurator.SetRaiseLoadEvents(true);
//SiaqodbLogger.ListenTo(_db);
2013-02-17 01:52:40 +00:00
}
2013-02-03 22:01:36 +00:00
2013-03-01 00:50:50 +00:00
2013-02-17 01:52:40 +00:00
public void Dispose()
2013-02-03 22:01:36 +00:00
{
2013-02-17 01:52:40 +00:00
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
{
2013-03-01 00:50:50 +00:00
return _db.LoadAllLazy<T>();
2013-02-03 22:01:36 +00:00
}
public T Insert<T>(T obj) where T : ModelBase
2013-02-03 22:01:36 +00:00
{
if (obj.Id != 0)
2013-02-16 04:03:54 +00:00
{
throw new InvalidOperationException("Attempted to insert object with existing ID as new object");
}
_db.StoreObject(obj);
2013-02-04 00:10:15 +00:00
return obj;
2013-02-03 22:01:36 +00:00
}
public T Update<T>(T obj) where T : ModelBase
2013-02-03 22:01:36 +00:00
{
if (obj.Id == 0)
2013-02-16 04:03:54 +00:00
{
2013-02-17 01:52:40 +00:00
throw new InvalidOperationException("Attempted to update object without an ID");
2013-02-16 04:03:54 +00:00
}
2013-02-03 22:01:36 +00:00
2013-02-17 01:52:40 +00:00
_db.StoreObject(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 : ModelBase
2013-02-16 04:03:54 +00:00
{
return DoMany(objects, Insert);
2013-02-16 04:03:54 +00:00
}
2013-02-17 01:52:40 +00:00
public IList<T> UpdateMany<T>(IList<T> objects) where T : ModelBase
2013-02-03 22:01:36 +00:00
{
2013-02-04 00:10:15 +00:00
return DoMany(objects, Update);
2013-02-17 01:52:40 +00:00
2013-02-03 22:01:36 +00:00
}
public void Delete<T>(T obj) where T : ModelBase
2013-02-03 22:01:36 +00:00
{
_db.Delete(obj);
2013-02-03 22:01:36 +00:00
}
public void DeleteMany<T>(IEnumerable<T> objects) where T : ModelBase
2013-02-03 22:01:36 +00:00
{
foreach (var o in objects)
{
Delete(o);
}
2013-02-03 22:01:36 +00:00
}
2013-03-01 07:03:41 +00:00
public void UpdateField<T>(T model, string fieldName) where T : ModelBase
{
2013-03-05 01:47:51 +00:00
_db.StoreObjectPartially(model, fieldName);
2013-03-01 07:03:41 +00:00
}
private IList<T> DoMany<T>(IEnumerable<T> objects, Func<T, T> function) where T : ModelBase
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
}
}
}