Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ff6fb08a08 | ||
|
|
d2900f364d | ||
|
|
b014c2f392 |
@@ -22,7 +22,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
string tableName = Function.GetDbTableName(classType);
|
string tableName = Function.GetDbTableName(classType);
|
||||||
|
|
||||||
// Get class-fields
|
// Get class-fields
|
||||||
Dictionary<string, FieldInfo> dbFields = Function.ReadDbClassFields(classObject);
|
Dictionary<string, FieldInfo> dbFields = Function.ReadDbClassFields(classType);
|
||||||
|
|
||||||
if (runDataLossChecks)
|
if (runDataLossChecks)
|
||||||
{
|
{
|
||||||
@@ -94,18 +94,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="classType">Type of class</param>
|
/// <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="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>
|
/// <param name="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
|
||||||
/// <returns></returns>
|
/// <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
|
string tableName = Function.GetDbTableName(classType); // Get database-tableName
|
||||||
return GetListByAttribute<T>(tableName, attributes, queryExecutor, runDataLossChecks);
|
|
||||||
}
|
Function.ConvertAttributeToDbAttributes(classType, fields);
|
||||||
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, fields); // Generate query
|
||||||
string query = QueryBuilder.SelectByAttribute(tableName, attributes); // Generate query
|
|
||||||
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
||||||
|
|
||||||
List<T> objs = new List<T>() { };
|
List<T> objs = new List<T>() { };
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
|
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
|
||||||
<SignAssembly>false</SignAssembly>
|
<SignAssembly>false</SignAssembly>
|
||||||
<Version>1.3</Version>
|
<Version>1.3.1</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -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)
|
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);
|
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>();
|
Dictionary<string, FieldInfo> dbFields = new Dictionary<string, FieldInfo>();
|
||||||
|
|
||||||
// Iterate thru all properties
|
// Iterate thru all properties
|
||||||
|
|||||||
Reference in New Issue
Block a user