You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Prowlarr/NzbDrone.Core/Datastore/SiaqodbProxy.cs

126 lines
3.3 KiB

using System;
using System.Collections.Generic;
12 years ago
using System.Diagnostics;
using System.Linq;
12 years ago
using Sqo;
namespace NzbDrone.Core.Datastore
{
12 years ago
public interface IObjectDatabase : IDisposable
{
12 years ago
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;
12 years ago
}
12 years ago
public static class SiaqodbLogger
{
public static void ListenTo(Siaqodb db)
{
db.DeletedObject += OnDeletedObject;
db.LoadingObject +=OnLoadingObject;
db.LoadedObject +=OnLoadedObject;
}
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");
}
}
12 years ago
public class SiaqodbProxy : IObjectDatabase
{
private readonly Siaqodb _db;
public SiaqodbProxy(Siaqodb db)
{
_db = db;
12 years ago
//SiaqodbConfigurator.SetRaiseLoadEvents(true);
//SiaqodbLogger.ListenTo(_db);
12 years ago
}
12 years ago
12 years ago
public void Dispose()
{
12 years ago
}
public IEnumerable<T> AsQueryable<T>()
{
12 years ago
return _db.LoadAllLazy<T>();
}
public T Insert<T>(T obj) where T : ModelBase
{
if (obj.Id != 0)
{
throw new InvalidOperationException("Attempted to insert object with existing ID as new object");
}
_db.StoreObject(obj);
return obj;
}
public T Update<T>(T obj) where T : ModelBase
{
if (obj.Id == 0)
{
12 years ago
throw new InvalidOperationException("Attempted to update object without an ID");
}
12 years ago
_db.StoreObject(obj);
return obj;
}
public IList<T> InsertMany<T>(IList<T> objects) where T : ModelBase
{
return DoMany(objects, Insert);
}
12 years ago
public IList<T> UpdateMany<T>(IList<T> objects) where T : ModelBase
{
return DoMany(objects, Update);
12 years ago
}
public void Delete<T>(T obj) where T : ModelBase
{
_db.Delete(obj);
}
public void DeleteMany<T>(IEnumerable<T> objects) where T : ModelBase
{
foreach (var o in objects)
{
Delete(o);
}
}
private IList<T> DoMany<T>(IEnumerable<T> objects, Func<T, T> function) where T : ModelBase
{
return objects.Select(function).ToList();
}
}
}