New Function-class

Changed some references to new Function-class
Added ClassAction.FillObject
Added ClassAction.FillObject
Renamed unecessary plural names to singular
master
Railz 6 years ago
parent 97b7eaeffe
commit 46e8ec1180

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace eu.railduction.netcore.dll.Database_Attribute_System
{
class ClassAction
{
/// <summary>
/// Fills an given dbObject with given data<para/>
/// Data-attribute-names and class-fieldNames have to match! (non case-sensitive)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <param name="data">The data</param>
/// <param name="ignoreDataAttributeNotInClass">This disables errors when class-field has no data-attribute</param>
public static void FillObject<T>(T classObject, Dictionary<string, object> data, bool ignoreDataAttributeNotInClass = false)
{
Type classType = classObject.GetType();
string tableName = Function.GetDbTableName(classType);
// Get class-fields
Dictionary<string, FieldInfo> dbFields = Function.ReadDbClassFields(classObject);
// Iterate through data
foreach (KeyValuePair<string, object> data_keySet in data)
{
// If the data was set
bool dataIsSet = false;
// Interate through class-fields
foreach (KeyValuePair<string, FieldInfo> field_keySet in dbFields)
{
// If its a match, set the value
if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower())
{
field_keySet.Value.SetValue(classObject, data_keySet.Value);
dataIsSet = true;
break;
}
}
// If the field was not filled, throw an error if it will not be ignored
if (!ignoreDataAttributeNotInClass && !dataIsSet) throw new InvalidOperationException($"Could not fill object. Data-Attribute '{data_keySet.Key}' was not found class!");
}
}
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, Dictionary<string, object>> queryExecutor, bool ignoreDataAttributeNotInClass = false)
{
string query = QueryBuilder.SelectByPrimaryKeys(classObject);
Dictionary<string, object> data = queryExecutor(query);
FillObject(classObject, data, ignoreDataAttributeNotInClass);
}
}
}

@ -14,18 +14,18 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param> /// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <returns>SELECT-Sql-query</returns> /// <returns>SELECT-Sql-query</returns>
public static string SelectByPrimaryKeys<T>(T classObject) public static string SelectByPrimaryKey<T>(T classObject)
{ {
Type classType = classObject.GetType(); Type classType = classObject.GetType();
// Get db-table-name from class // Get db-table-name from class
string tableName = DbFunction.GetDbTableName(classType); string tableName = Function.GetDbTableName(classType);
// Get class db-fields // Get class db-fields
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { }; Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { }; Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { }; Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
DbFunction.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys); Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!"); if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!");
// Build where statements with primaryKey/s // Build where statements with primaryKey/s
@ -44,18 +44,18 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param> /// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <returns>UPDATE-Sql-query</returns> /// <returns>UPDATE-Sql-query</returns>
public static string UpdateByPrimaryKeys<T>(T classObject) public static string UpdateByPrimaryKey<T>(T classObject)
{ {
Type classType = classObject.GetType(); Type classType = classObject.GetType();
// Get db-table-name from class // Get db-table-name from class
string tableName = DbFunction.GetDbTableName(classType); string tableName = Function.GetDbTableName(classType);
// Get class db-fields // Get class db-fields
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { }; Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { }; Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { }; Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
DbFunction.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys); Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!"); if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!");
// Add foreign-keys to attributes // Add foreign-keys to attributes
@ -85,18 +85,18 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param> /// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <returns>DELETE-Sql-query</returns> /// <returns>DELETE-Sql-query</returns>
public static string DeleteByPrimaryKeys<T>(T classObject) public static string DeleteByPrimaryKey<T>(T classObject)
{ {
Type classType = classObject.GetType(); Type classType = classObject.GetType();
// Get db-table-name from class // Get db-table-name from class
string tableName = DbFunction.GetDbTableName(classType); string tableName = Function.GetDbTableName(classType);
// Get class db-fields // Get class db-fields
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { }; Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { }; Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { }; Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
DbFunction.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys); Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!"); if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!");
// Build where-parameters // Build where-parameters

@ -27,48 +27,5 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return param; return param;
} }
public static string GetDbTableName(Type classType)
{
// Check if class has attribute 'DbObject' and get the database table-name
if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObjectAttribute)) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. Missing Attribute 'DbObject'");
string tableName = dbObjectAttribute._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
return tableName;
}
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);
// Reset lists (just in case)
dbPrimaryKeys = new Dictionary<string, object>() { };
dbAttributes = new Dictionary<string, object>() { };
dbForeignKeys = new Dictionary<string, object>() { };
// Iterate thru all properties
foreach (System.Reflection.FieldInfo fi in classType.GetRuntimeFields())
{
// Check if current field is a db-field
if (fi.GetCustomAttribute(typeof(DbPrimaryKey), true) is DbPrimaryKey pkey) // PrimaryKey
{
string dbAttributeName = pkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbPrimaryKeys.Add(dbAttributeName, value);
}
else if (fi.GetCustomAttribute(typeof(DbAttribute), true) is DbAttribute att) // Attributes
{
string dbAttributeName = att._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbAttributes.Add(dbAttributeName, value);
}
else if (fi.GetCustomAttribute(typeof(DbForeignKey), true) is DbForeignKey fkey) // ForeignKeys
{
string dbAttributeName = fkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbForeignKeys.Add(dbAttributeName, value);
}
}
}
} }
} }

@ -30,5 +30,78 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
RecursiveParameterCopying(ref paramz, (object[])obj); RecursiveParameterCopying(ref paramz, (object[])obj);
} }
} }
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);
// Reset lists (just in case)
dbPrimaryKeys = new Dictionary<string, object>() { };
dbAttributes = new Dictionary<string, object>() { };
dbForeignKeys = new Dictionary<string, object>() { };
// Iterate thru all properties
foreach (System.Reflection.FieldInfo fi in classType.GetRuntimeFields())
{
// Check if current field is a db-field
if (fi.GetCustomAttribute(typeof(DbPrimaryKey), true) is DbPrimaryKey pkey) // PrimaryKey
{
string dbAttributeName = pkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbPrimaryKeys.Add(dbAttributeName, value);
}
else if (fi.GetCustomAttribute(typeof(DbAttribute), true) is DbAttribute att) // Attributes
{
string dbAttributeName = att._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbAttributes.Add(dbAttributeName, value);
}
else if (fi.GetCustomAttribute(typeof(DbForeignKey), true) is DbForeignKey fkey) // ForeignKeys
{
string dbAttributeName = fkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbForeignKeys.Add(dbAttributeName, value);
}
}
}
internal static Dictionary<string, FieldInfo> ReadDbClassFields<T>(T classObject)
{
Type classType = typeof(T);
Dictionary<string, FieldInfo> dbFields = new Dictionary<string, FieldInfo>();
// Iterate thru all properties
foreach (System.Reflection.FieldInfo fi in classType.GetRuntimeFields())
{
// Check if current field is a db-field
if (fi.GetCustomAttribute(typeof(DbPrimaryKey), true) is DbPrimaryKey pkey) // PrimaryKey
{
string dbAttributeName = pkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
dbFields.Add(dbAttributeName, fi);
}
else if (fi.GetCustomAttribute(typeof(DbAttribute), true) is DbAttribute att) // Attributes
{
string dbAttributeName = att._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
dbFields.Add(dbAttributeName, fi);
}
else if (fi.GetCustomAttribute(typeof(DbForeignKey), true) is DbForeignKey fkey) // ForeignKeys
{
string dbAttributeName = fkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
dbFields.Add(dbAttributeName, fi);
}
}
return dbFields;
}
public static string GetDbTableName(Type classType)
{
// Check if class has attribute 'DbObject' and get the database table-name
if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObjectAttribute)) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. Missing Attribute 'DbObject'");
string tableName = dbObjectAttribute._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
return tableName;
}
} }
} }

Loading…
Cancel
Save