From b014c2f392ff9114f193fbd76edd7a106ea1bbfd Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 8 Apr 2019 20:32:44 +0200 Subject: [PATCH] Added Method to convert classField-names to according dbAttributes Added convert in GetListByAttribute --- Database-Attribute_System/ClassAction.cs | 13 +++-- .../internal/Function.cs | 50 +++++++++++++++++-- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index ded6c7f..74232c1 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -94,18 +94,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System /// /// /// Type of class - /// attributes for select + /// class-fields for select /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] /// This checks if any class-field and data-attribute does not exists in either (Slower) /// - public static List GetListByAttribute(Type classType, Dictionary attributes, Func>> queryExecutor, bool runDataLossChecks = true) where T : new() + public static List GetListByAttribute(Type classType, Dictionary fields, Func>> queryExecutor, bool runDataLossChecks = true) where T : new() { string tableName = Function.GetDbTableName(classType); // Get database-tableName - return GetListByAttribute(tableName, attributes, queryExecutor, runDataLossChecks); - } - public static List GetListByAttribute(string tableName, Dictionary attributes, Func>> queryExecutor, bool runDataLossChecks = true) where T: new() - { - string query = QueryBuilder.SelectByAttribute(tableName, attributes); // Generate query + + Function.ConvertAttributeToDbAttributes(classType, fields); + + string query = QueryBuilder.SelectByAttribute(tableName, fields); // Generate query List> dataSet = queryExecutor(query); // Execute List objs = new List() { }; diff --git a/Database-Attribute_System/internal/Function.cs b/Database-Attribute_System/internal/Function.cs index 4342ad7..6fee030 100644 --- a/Database-Attribute_System/internal/Function.cs +++ b/Database-Attribute_System/internal/Function.cs @@ -31,6 +31,52 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } } + internal static void ConvertAttributeToDbAttributes(Type classType, Dictionary attributeNameAndValues) + { + Dictionary classFields = ReadDbClassFields(classType); + + foreach (KeyValuePair attributeNameAndValue in attributeNameAndValues) + { + bool nameFound = false; + foreach (KeyValuePair classField in classFields) + { + if (attributeNameAndValue.Key.ToLower() == classField.Value.Name.ToLower()) + { + attributeNameAndValues.Remove(attributeNameAndValue.Key); + + attributeNameAndValues.Add(classField.Key, attributeNameAndValue.Value); + + nameFound = true; + break; + } + } + + if (!nameFound) throw new InvalidOperationException($"{attributeNameAndValue.Key} has no classField!"); + } + } + internal static void ConvertAttributeToDbAttributes(Type classType, List attributeNames) + { + Dictionary classFields = ReadDbClassFields(classType); + + List dbAttributes = new List() { }; + for(int i=0; i< attributeNames.Count; i++) + { + bool nameFound = false; + foreach (KeyValuePair classField in classFields) + { + if(attributeNames[i].ToLower() == classField.Value.Name.ToLower()) + { + attributeNames[i] = classField.Key; + + nameFound = true; + break; + } + } + + if (!nameFound) throw new InvalidOperationException($"{attributeNames[i]} has no classField!"); + } + } + internal static void ReadDbClassFields(T classObject, ref Dictionary dbPrimaryKeys, ref Dictionary dbAttributes, ref Dictionary dbForeignKeys) { Type classType = typeof(T); @@ -65,10 +111,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } } - internal static Dictionary ReadDbClassFields(T classObject) + internal static Dictionary ReadDbClassFields(Type classType) { - Type classType = typeof(T); - Dictionary dbFields = new Dictionary(); // Iterate thru all properties