|
@@ -2380,7 +2380,7 @@ namespace InABox.Database.SQLite
|
|
|
|
|
|
#region CRUD Operations
|
|
|
|
|
|
- public object[] GetValues(IDataReader reader, int count)
|
|
|
+ public static object[] GetValues(IDataReader reader, int count)
|
|
|
{
|
|
|
var result = new object[count];
|
|
|
reader.GetValues(result);
|
|
@@ -2794,51 +2794,52 @@ namespace InABox.Database.SQLite
|
|
|
throw;
|
|
|
}
|
|
|
}
|
|
|
- private void OnSave<T>(IEnumerable<T> entities) where T : Entity
|
|
|
- => OnSaveNonGeneric(typeof(T), entities);
|
|
|
|
|
|
- public void Save(Type type, IEnumerable<Entity> entities)
|
|
|
- => OnSaveNonGeneric(type, entities);
|
|
|
+ private void OnSave<T>(T entity) where T : Entity
|
|
|
+ => OnSaveNonGeneric(typeof(T), entity);
|
|
|
|
|
|
- public static bool CanSave<T>()
|
|
|
+ public static bool CanSave(Type T)
|
|
|
{
|
|
|
if (DbFactory.IsReadOnly)
|
|
|
{
|
|
|
- if (typeof(T).IsAssignableTo(typeof(License)) || typeof(T).IsAssignableTo(typeof(UserTracking)))
|
|
|
+ if (T.IsAssignableTo(typeof(License)) || T.IsAssignableTo(typeof(UserTracking)))
|
|
|
return true;
|
|
|
DbFactory.LogReadOnly();
|
|
|
- return false;
|
|
|
+ throw new DbLockedException();
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+ public static bool CanSave<T>() => CanSave(typeof(T));
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- public void Save<T>(IEnumerable<T> entities) where T : Entity
|
|
|
+ public void Save(Type type, Entity entity)
|
|
|
{
|
|
|
- if (!CanSave<T>())
|
|
|
+ if (!CanSave(type))
|
|
|
+ {
|
|
|
return;
|
|
|
- OnSave(entities);
|
|
|
+ }
|
|
|
+ OnSaveNonGeneric(type, entity);
|
|
|
}
|
|
|
-
|
|
|
- private void OnSave<T>(T entity) where T : Entity
|
|
|
- => OnSaveNonGeneric(typeof(T), entity);
|
|
|
-
|
|
|
- public void Save<T>(T entity) where T : Entity
|
|
|
+ public void Save(Type type, IEnumerable<Entity> entities)
|
|
|
{
|
|
|
- if (!CanSave<T>())
|
|
|
+ if (!CanSave(type))
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
- OnSave(entity);
|
|
|
+ OnSaveNonGeneric(type, entities);
|
|
|
}
|
|
|
|
|
|
+ public void Save<T>(IEnumerable<T> entities) where T : Entity => Save(typeof(T), entities);
|
|
|
+
|
|
|
+ public void Save<T>(T entity) where T : Entity => Save(typeof(T), entity);
|
|
|
+
|
|
|
#endregion
|
|
|
|
|
|
#region Delete
|
|
|
|
|
|
public void Purge<T>(T entity) where T : Entity
|
|
|
{
|
|
|
+ if (!CanSave<T>()) return;
|
|
|
+
|
|
|
using var access = GetWriteAccess();
|
|
|
using var command = access.CreateCommand();
|
|
|
PrepareDelete(command, entity);
|
|
@@ -2847,6 +2848,8 @@ namespace InABox.Database.SQLite
|
|
|
|
|
|
public void Purge<T>(IEnumerable<T> entities) where T : Entity
|
|
|
{
|
|
|
+ if (!CanSave<T>()) return;
|
|
|
+
|
|
|
// Casting to IList so that we can use it multiple times.
|
|
|
entities = entities.AsIList();
|
|
|
|
|
@@ -2934,7 +2937,7 @@ namespace InABox.Database.SQLite
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private MethodInfo _deleteEntitiesMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
|
|
|
+ private readonly MethodInfo _deleteEntitiesMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
|
|
|
.Single(x => x.Name == nameof(DeleteEntity) && x.IsGenericMethod);
|
|
|
private void DeleteEntity<T>(Guid[] parentIDs, string parentField, DeletionData deletionData) where T : Entity, new()
|
|
|
{
|
|
@@ -2960,7 +2963,7 @@ namespace InABox.Database.SQLite
|
|
|
_deleteEntitiesMethod.MakeGenericMethod(T).Invoke(this, new object?[] { parentIDs, parentField, deletionData });
|
|
|
}
|
|
|
|
|
|
- private MethodInfo _setNullEntityMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
|
|
|
+ private readonly MethodInfo _setNullEntityMethod = typeof(SQLiteProvider).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
|
|
|
.Single(x => x.Name == nameof(SetNullEntity) && x.IsGenericMethod);
|
|
|
private void SetNullEntity<T>(List<string> properties, Guid[] parentIDs, DeletionData deletionData) where T : Entity, new()
|
|
|
{
|
|
@@ -3132,6 +3135,11 @@ namespace InABox.Database.SQLite
|
|
|
|
|
|
public void Purge(Deletion deletion)
|
|
|
{
|
|
|
+ if (!CanSave<Deletion>())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
var data = Serialization.Deserialize<DeletionData>(deletion.Data);
|
|
|
if(data is not null)
|
|
|
{
|
|
@@ -3155,6 +3163,11 @@ namespace InABox.Database.SQLite
|
|
|
|
|
|
public void Recover(Deletion deletion)
|
|
|
{
|
|
|
+ if (!CanSave<Deletion>())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
if (deletion.ID == Guid.Empty)
|
|
|
{
|
|
|
Logger.Send(LogType.Error, "", "Empty Deletion ID; Recovery cancelled");
|