From 178c92383b26da334ba375642126e07a7229e7d9 Mon Sep 17 00:00:00 2001 From: Railz Date: Thu, 26 Sep 2019 15:00:59 +0200 Subject: [PATCH] Added Insert, Update and Insert ClassActions --- Database-Attribute_System/ClassAction.cs | 84 ++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index 29a784c..c3f4a61 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -457,5 +457,89 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } } } + + + /// + /// Updates class to database-object + /// Only works with primary-key/s! + /// + /// + /// Given object (marked with Db-attributes) + /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] + /// Determents how deep resolving will be executed (if the corresponding foreignKey-object is resolved) + public static void Update(T classObject, Func>> queryExecutor, int max_depth = 1) + { + // Read dbObject-attribute + DbObject dbObject = ClassAction.Init(classObject.GetType()); + + if(max_depth-1 > 0) + { + // Update all foreignObjects before handling myself + foreach (DbForeignObject foreignObject in dbObject.foreignObjectAttributes) + { + object foreignObjectRef = foreignObject.parentField.GetValue(classObject); + if (foreignObjectRef != null) + Update(foreignObjectRef, queryExecutor, max_depth - 1); + } + } + + string updateQuery = QueryBuilder.UpdateByPrimaryKey(classObject); + queryExecutor.Invoke(updateQuery); + } + + /// + /// Inserts class to database-object + /// + /// + /// Given object (marked with Db-attributes) + /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] + /// Determents how deep insertion will be executed (if the corresponding foreignKey-object is resolved) + public static void Insert(T classObject, Func>> queryExecutor, int max_depth = 1) + { + // Read dbObject-attribute + DbObject dbObject = ClassAction.Init(classObject.GetType()); + + if (max_depth - 1 > 0) + { + // Update all foreignObjects before handling myself + foreach (DbForeignObject foreignObject in dbObject.foreignObjectAttributes) + { + object foreignObjectRef = foreignObject.parentField.GetValue(classObject); + if (foreignObjectRef != null) + Insert(foreignObjectRef, queryExecutor, max_depth - 1); + } + } + + string insertQuery = QueryBuilder.InsertAttributesByObject(classObject); + queryExecutor.Invoke(insertQuery); + } + + /// + /// Deletes class from database-object + /// Only works with primary-key/s! + /// + /// + /// Given object (marked with Db-attributes) + /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] + /// Determents how deep deletion will be executed (if the corresponding foreignKey-object is resolved) + public static void Delete(T classObject, Func>> queryExecutor, int max_depth = 1) + { + // Read dbObject-attribute + DbObject dbObject = ClassAction.Init(classObject.GetType()); + + if (max_depth - 1 > 0) + { + // Update all foreignObjects before handling myself + foreach (DbForeignObject foreignObject in dbObject.foreignObjectAttributes) + { + object foreignObjectRef = foreignObject.parentField.GetValue(classObject); + if (foreignObjectRef != null) + Delete(foreignObjectRef, queryExecutor, max_depth - 1); + } + } + + string deleteQuery = QueryBuilder.DeleteByPrimaryKey(classObject); + queryExecutor.Invoke(deleteQuery); + } } }