Added Method to convert classField-names to according dbAttributes

Added convert in GetListByAttribute
master
Railz 6 years ago
parent 0ad7221680
commit b014c2f392

@ -94,18 +94,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classType">Type of class</param>
/// <param name="attributes">attributes for select</param>
/// <param name="fields">class-fields for select</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
/// <param name="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
/// <returns></returns>
public static List<T> GetListByAttribute<T>(Type classType, Dictionary<string, object> attributes, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true) where T : new()
public static List<T> GetListByAttribute<T>(Type classType, Dictionary<string, object> fields, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true) where T : new()
{
string tableName = Function.GetDbTableName(classType); // Get database-tableName
return GetListByAttribute<T>(tableName, attributes, queryExecutor, runDataLossChecks);
}
public static List<T> GetListByAttribute<T>(string tableName, Dictionary<string, object> attributes, Func<string, List<Dictionary<string, object>>> 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<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
List<T> objs = new List<T>() { };

@ -31,6 +31,52 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
}
}
internal static void ConvertAttributeToDbAttributes(Type classType, Dictionary<string, object> attributeNameAndValues)
{
Dictionary<string, FieldInfo> classFields = ReadDbClassFields(classType);
foreach (KeyValuePair<string, object> attributeNameAndValue in attributeNameAndValues)
{
bool nameFound = false;
foreach (KeyValuePair<string, FieldInfo> 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<string> attributeNames)
{
Dictionary<string, FieldInfo> classFields = ReadDbClassFields(classType);
List<string> dbAttributes = new List<string>() { };
for(int i=0; i< attributeNames.Count; i++)
{
bool nameFound = false;
foreach (KeyValuePair<string, FieldInfo> 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>(T classObject, ref Dictionary<string, object> dbPrimaryKeys, ref Dictionary<string, object> dbAttributes, ref Dictionary<string, object> dbForeignKeys)
{
Type classType = typeof(T);
@ -65,10 +111,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
}
}
internal static Dictionary<string, FieldInfo> ReadDbClassFields<T>(T classObject)
internal static Dictionary<string, FieldInfo> ReadDbClassFields(Type classType)
{
Type classType = typeof(T);
Dictionary<string, FieldInfo> dbFields = new Dictionary<string, FieldInfo>();
// Iterate thru all properties

Loading…
Cancel
Save