diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs
new file mode 100644
index 0000000..62c756a
--- /dev/null
+++ b/Database-Attribute_System/ClassAction.cs
@@ -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
+ {
+ ///
+ /// Fills an given dbObject with given data
+ /// Data-attribute-names and class-fieldNames have to match! (non case-sensitive)
+ ///
+ ///
+ /// Given object (marked with Db-attributes)
+ /// The data
+ /// This disables errors when class-field has no data-attribute
+ public static void FillObject(T classObject, Dictionary data, bool ignoreDataAttributeNotInClass = false)
+ {
+ Type classType = classObject.GetType();
+
+ string tableName = Function.GetDbTableName(classType);
+
+ // Get class-fields
+ Dictionary dbFields = Function.ReadDbClassFields(classObject);
+
+ // Iterate through data
+ foreach (KeyValuePair data_keySet in data)
+ {
+ // If the data was set
+ bool dataIsSet = false;
+
+ // Interate through class-fields
+ foreach (KeyValuePair 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 classObject, Func> queryExecutor, bool ignoreDataAttributeNotInClass = false)
+ {
+ string query = QueryBuilder.SelectByPrimaryKeys(classObject);
+ Dictionary data = queryExecutor(query);
+ FillObject(classObject, data, ignoreDataAttributeNotInClass);
+ }
+ }
+}
diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs
index f6d67c2..4fb3270 100644
--- a/Database-Attribute_System/QueryBuilder.cs
+++ b/Database-Attribute_System/QueryBuilder.cs
@@ -14,18 +14,18 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
///
/// Given object (marked with Db-attributes)
/// SELECT-Sql-query
- public static string SelectByPrimaryKeys(T classObject)
+ public static string SelectByPrimaryKey(T classObject)
{
Type classType = classObject.GetType();
// Get db-table-name from class
- string tableName = DbFunction.GetDbTableName(classType);
+ string tableName = Function.GetDbTableName(classType);
// Get class db-fields
Dictionary dbPrimaryKeys = new Dictionary() { };
Dictionary dbAttributes = new Dictionary() { };
Dictionary dbForeignKeys = new Dictionary() { };
- 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!");
// Build where statements with primaryKey/s
@@ -44,18 +44,18 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
///
/// Given object (marked with Db-attributes)
/// UPDATE-Sql-query
- public static string UpdateByPrimaryKeys(T classObject)
+ public static string UpdateByPrimaryKey(T classObject)
{
Type classType = classObject.GetType();
// Get db-table-name from class
- string tableName = DbFunction.GetDbTableName(classType);
+ string tableName = Function.GetDbTableName(classType);
// Get class db-fields
Dictionary dbPrimaryKeys = new Dictionary() { };
Dictionary dbAttributes = new Dictionary() { };
Dictionary dbForeignKeys = new Dictionary() { };
- 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!");
// Add foreign-keys to attributes
@@ -85,18 +85,18 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
///
/// Given object (marked with Db-attributes)
/// DELETE-Sql-query
- public static string DeleteByPrimaryKeys(T classObject)
+ public static string DeleteByPrimaryKey(T classObject)
{
Type classType = classObject.GetType();
// Get db-table-name from class
- string tableName = DbFunction.GetDbTableName(classType);
+ string tableName = Function.GetDbTableName(classType);
// Get class db-fields
Dictionary dbPrimaryKeys = new Dictionary() { };
Dictionary dbAttributes = new Dictionary() { };
Dictionary dbForeignKeys = new Dictionary() { };
- 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!");
// Build where-parameters
diff --git a/Database-Attribute_System/internal/DbFunction.cs b/Database-Attribute_System/internal/DbFunction.cs
index 14ff831..5de0c05 100644
--- a/Database-Attribute_System/internal/DbFunction.cs
+++ b/Database-Attribute_System/internal/DbFunction.cs
@@ -27,48 +27,5 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
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 classObject, ref Dictionary dbPrimaryKeys, ref Dictionary dbAttributes, ref Dictionary dbForeignKeys)
- {
- Type classType = typeof(T);
-
- // Reset lists (just in case)
- dbPrimaryKeys = new Dictionary() { };
- dbAttributes = new Dictionary() { };
- dbForeignKeys = new Dictionary() { };
-
- // 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);
- }
- }
- }
}
}
diff --git a/Database-Attribute_System/internal/Function.cs b/Database-Attribute_System/internal/Function.cs
index b68fa05..dde96ab 100644
--- a/Database-Attribute_System/internal/Function.cs
+++ b/Database-Attribute_System/internal/Function.cs
@@ -30,5 +30,78 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
RecursiveParameterCopying(ref paramz, (object[])obj);
}
}
+
+ internal static void ReadDbClassFields(T classObject, ref Dictionary dbPrimaryKeys, ref Dictionary dbAttributes, ref Dictionary dbForeignKeys)
+ {
+ Type classType = typeof(T);
+
+ // Reset lists (just in case)
+ dbPrimaryKeys = new Dictionary() { };
+ dbAttributes = new Dictionary() { };
+ dbForeignKeys = new Dictionary() { };
+
+ // 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 ReadDbClassFields(T classObject)
+ {
+ Type classType = typeof(T);
+
+ Dictionary dbFields = new Dictionary();
+
+ // 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;
+ }
}
}