Improved entity deletion

This commit is contained in:
2025-07-06 08:52:29 +02:00
parent dc9c4a6279
commit 1fe70264fd
3 changed files with 16 additions and 8 deletions

View File

@@ -117,6 +117,19 @@ public static class DatabaseHelper
entityCache.RemoveAll(entity => entity.name == name);
}
public static int RemoveAllEntities(SQLHelper helper, string searchdomain)
{
Dictionary<string, dynamic> parameters = new()
{
{ "searchdomain", GetSearchdomainID(helper, searchdomain)}
};
helper.ExecuteSQLNonQuery("DELETE embedding.* FROM embedding JOIN datapoint dp ON id_datapoint = dp.id JOIN entity ON id_entity = entity.id WHERE entity.id_searchdomain = @searchdomain", parameters);
helper.ExecuteSQLNonQuery("DELETE datapoint.* FROM datapoint JOIN entity ON id_entity = entity.id WHERE entity.id_searchdomain = @searchdomain", parameters);
helper.ExecuteSQLNonQuery("DELETE attribute.* FROM attribute JOIN entity ON id_entity = entity.id WHERE entity.id_searchdomain = @searchdomain", parameters);
return helper.ExecuteSQLNonQuery("DELETE FROM entity WHERE entity.id_searchdomain = @searchdomain", parameters);
}
public static bool HasEntity(SQLHelper helper, string name, string searchdomain)
{
Dictionary<string, dynamic> parameters = new()

View File

@@ -40,7 +40,7 @@ public class SQLHelper:IDisposable
}
}
public void ExecuteSQLNonQuery(string query, Dictionary<string, dynamic> parameters)
public int ExecuteSQLNonQuery(string query, Dictionary<string, dynamic> parameters)
{
lock (connection)
{
@@ -52,7 +52,7 @@ public class SQLHelper:IDisposable
{
command.Parameters.AddWithValue($"@{parameter.Key}", parameter.Value);
}
command.ExecuteNonQuery();
return command.ExecuteNonQuery();
}
}

View File

@@ -100,12 +100,7 @@ public class SearchdomainManager
public int DeleteSearchdomain(string searchdomain)
{
Searchdomain searchdomain_ = GetSearchdomain(searchdomain);
int counter = 0;
while (searchdomain_.entityCache.Count > 0)
{
DatabaseHelper.RemoveEntity(searchdomain_.entityCache, helper, searchdomain_.entityCache.First().name, searchdomain);
counter += 1;
}
int counter = DatabaseHelper.RemoveAllEntities(helper, searchdomain);
_logger.LogDebug($"Number of entities deleted as part of deleting the searchdomain \"{searchdomain}\": {counter}");
helper.ExecuteSQLNonQuery("DELETE FROM searchdomain WHERE name = @name", new() {{"name", searchdomain}});
searchdomains.Remove(searchdomain);