|
|
|
|
@@ -43,7 +43,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
|
|
|
|
/// <param name="data">The data</param>
|
|
|
|
|
/// <param name="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
|
|
|
|
|
public static void FillObject<T>(T classObject, Dictionary<string, object> data)
|
|
|
|
|
{
|
|
|
|
|
Type classType = classObject.GetType();
|
|
|
|
|
@@ -57,11 +56,19 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
// Interate through class-fields
|
|
|
|
|
foreach (BaseAttribute baseAttribute in dbObject.baseAttributes)
|
|
|
|
|
{
|
|
|
|
|
// Remove any leading dots and table-specifiers
|
|
|
|
|
string dbAttName = data_keySet.Key;
|
|
|
|
|
if (dbAttName.Contains("."))
|
|
|
|
|
{
|
|
|
|
|
string[] dbAttNameSplit = dbAttName.Split('.'); // Split at the '.'
|
|
|
|
|
dbAttName = dbAttNameSplit[dbAttNameSplit.Length - 1]; // Copy the ending text
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If its a match, set the value
|
|
|
|
|
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
|
|
|
|
|
{
|
|
|
|
|
object value = data_keySet.Value;
|
|
|
|
|
if (baseAttribute.parentField.FieldType == typeof(Guid)) value = new Guid((string)value); // If its a guid, i need to convert
|
|
|
|
|
//if (baseAttribute.parentField.FieldType == typeof(Guid)) value = new Guid((string)value); // If its a guid, i need to convert
|
|
|
|
|
|
|
|
|
|
baseAttribute.parentField.SetValue(classObject, value);
|
|
|
|
|
break;
|
|
|
|
|
@@ -130,7 +137,32 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
// ----
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an dbObject by primaryKey/s
|
|
|
|
|
/// Gets all dbObjects of class/table
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
|
|
|
|
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
|
|
|
|
public static List<T> GetList<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
|
|
|
|
|
{
|
|
|
|
|
// Read dbObject - attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classType);
|
|
|
|
|
|
|
|
|
|
string query = QueryBuilder.SelectByAttribute(dbObject._tableName); // Generate query
|
|
|
|
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
|
|
|
|
|
|
|
|
|
List<T> objs = new List<T>() { };
|
|
|
|
|
foreach (Dictionary<string, object> data in dataSet)
|
|
|
|
|
{
|
|
|
|
|
T obj = new T(); // New object
|
|
|
|
|
FillObject(obj, data); // Fill it
|
|
|
|
|
objs.Add(obj); // Add to list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return objs; // Return list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an dbObject by custom where-clause
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
|
|
|
|
@@ -156,19 +188,32 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves dbObject by primaryKey/s<pragma/>
|
|
|
|
|
/// Object needs to have primaryKey/s set!
|
|
|
|
|
/// Gets an dbObject by full query
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
|
|
|
|
/// <param name="customQuery">Custom sql-query</param>
|
|
|
|
|
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
|
|
|
|
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor)
|
|
|
|
|
public static List<T> GetListWithQuery<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] customQuery) where T : new()
|
|
|
|
|
{
|
|
|
|
|
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
|
|
|
|
|
// Read dbObject - attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classType);
|
|
|
|
|
|
|
|
|
|
string query = QueryBuilder.BuildQuery(customQuery);
|
|
|
|
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
|
|
|
|
FillObject(classObject, dataSet[0]); // Fill the object
|
|
|
|
|
|
|
|
|
|
List<T> objs = new List<T>() { };
|
|
|
|
|
foreach (Dictionary<string, object> data in dataSet)
|
|
|
|
|
{
|
|
|
|
|
T obj = new T(); // New object
|
|
|
|
|
FillObject(obj, data); // Fill it
|
|
|
|
|
objs.Add(obj); // Add to list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return objs; // Return list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a list of dbObjects by attribute/s
|
|
|
|
|
/// </summary>
|
|
|
|
|
@@ -176,7 +221,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
/// <param name="classType">Type of class</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>List of dbObjects</returns>
|
|
|
|
|
public static List<T> GetListByAttribute<T>(Type classType, Dictionary<string, object> fields, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
|
|
|
|
|
{
|
|
|
|
|
@@ -199,7 +243,23 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
return objs; // Return list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves dbObject by primaryKey/s<pragma/>
|
|
|
|
|
/// Object needs to have primaryKey/s set!
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
|
|
|
|
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
|
|
|
|
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor)
|
|
|
|
|
{
|
|
|
|
|
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
|
|
|
|
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
|
|
|
|
|
|
|
|
|
if (dataSet.Count == 0) throw new InvalidOperationException($"Cannot fetch '{typeof(T).Name}' by primary key/s. No results!");
|
|
|
|
|
FillObject(classObject, dataSet[0]); // Fill the object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves all foreignKeys with the database<pragma/>
|
|
|
|
|
@@ -209,7 +269,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
|
|
|
|
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
|
|
|
|
/// <param name="max_depth">Determents how deep resolving will be executed</param>
|
|
|
|
|
/// <param name="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
|
|
|
|
|
public static void ResolveForeignKeys<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, int max_depth = 1) where T: new()
|
|
|
|
|
{
|
|
|
|
|
Type classType = classObject.GetType();
|
|
|
|
|
@@ -224,7 +283,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
// When its empty, get it
|
|
|
|
|
if(foreignObject_value == null)
|
|
|
|
|
{
|
|
|
|
|
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); ;
|
|
|
|
|
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Recursive resolving
|
|
|
|
|
|