|
|
|
|
@@ -69,11 +69,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
|
|
|
|
|
{
|
|
|
|
|
object value = data_keySet.Value;
|
|
|
|
|
if (!(value is DBNull)) // Check if value is empty
|
|
|
|
|
{
|
|
|
|
|
//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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -87,10 +93,13 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
|
|
|
|
public static T GetByPrimaryKey<T>(Type classType, object primaryKeyValue, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
|
|
|
|
|
{
|
|
|
|
|
Dictionary<string, object> primaryKeyData = new Dictionary<string, object>() { };
|
|
|
|
|
primaryKeyData.Add(null, primaryKeyValue);
|
|
|
|
|
// Read dbObject-attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classType);
|
|
|
|
|
|
|
|
|
|
return GetByPrimaryKey<T>(classType, primaryKeyData, queryExecutor);
|
|
|
|
|
if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'");
|
|
|
|
|
if (dbObject.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"This 'GetByPrimaryKey' method only supports 1 primaryKey ('{dbObject.primaryKeyAttributes.Count}' found in '{classType.Name}')");
|
|
|
|
|
|
|
|
|
|
return GetByPrimaryKey<T>(classType, dbObject.primaryKeyAttributes[0]._attributeName, primaryKeyValue, queryExecutor);
|
|
|
|
|
}
|
|
|
|
|
public static T GetByPrimaryKey<T>(Type classType, string primaryKeyName, object primaryKeyValue, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
|
|
|
|
|
{
|
|
|
|
|
@@ -101,12 +110,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
}
|
|
|
|
|
public static T GetByPrimaryKey<T>(Type classType, Dictionary<string, object> primaryKeyData, Func<string, List<Dictionary<string, object>>> queryExecutor) where T: new()
|
|
|
|
|
{
|
|
|
|
|
// Create new empty object
|
|
|
|
|
T obj = new T();
|
|
|
|
|
|
|
|
|
|
// Read dbObject-attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classType);
|
|
|
|
|
|
|
|
|
|
if (dbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"No primaryKey found in '{classType.Name}'");
|
|
|
|
|
|
|
|
|
|
// Create new empty object
|
|
|
|
|
T obj = (T)dbObject.parentCInfo.Invoke(null);
|
|
|
|
|
|
|
|
|
|
// iterate thru them to check and fill object
|
|
|
|
|
foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes)
|
|
|
|
|
{
|
|
|
|
|
@@ -130,7 +141,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
if (!dataMatchFound) throw new InvalidOperationException($"PrimaryKey='{primaryKeyAtt.parentField.Name}' is missing.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
ResolveByPrimaryKey<T>(obj, queryExecutor);
|
|
|
|
|
}catch(InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
// If there is no result, return null
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
@@ -253,13 +271,16 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
/// <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)
|
|
|
|
|
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, bool throwExceptions = true)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
if (dataSet.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
if (throwExceptions) throw new InvalidOperationException($"Cannot fetch '{typeof(T).Name}' by primary key/s. No results!");
|
|
|
|
|
}
|
|
|
|
|
else FillObject(classObject, dataSet[0]); // Fill the object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
@@ -280,13 +301,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
// Resolve foreignObjects
|
|
|
|
|
foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes)
|
|
|
|
|
{
|
|
|
|
|
object foreignKey_value = foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject);
|
|
|
|
|
object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject);
|
|
|
|
|
|
|
|
|
|
// When its empty, get it & set it
|
|
|
|
|
if(foreignObject_value == null)
|
|
|
|
|
// When key is set and object is empty, get it & set it
|
|
|
|
|
if(foreignKey_value != null && foreignObject_value == null)
|
|
|
|
|
{
|
|
|
|
|
// Resolve it
|
|
|
|
|
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor);
|
|
|
|
|
foreignObject_value = GetByPrimaryKey<T>(foreignObjectAtt.foreignObjectType, foreignKey_value, queryExecutor);
|
|
|
|
|
foreignObjectAtt.parentField.SetValue(classObject, foreignObject_value); // Set the value
|
|
|
|
|
|
|
|
|
|
// Now scan the just resolved class to be able to set myself
|
|
|
|
|
@@ -442,5 +464,89 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Updates class to database-object</pragma>
|
|
|
|
|
/// Only works with primary-key/s!
|
|
|
|
|
/// </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>
|
|
|
|
|
/// <param name="max_depth">Determents how deep resolving will be executed (if the corresponding foreignKey-object is resolved)</param>
|
|
|
|
|
public static void Update<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, int max_depth = 1)
|
|
|
|
|
{
|
|
|
|
|
// Read dbObject-attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classObject.GetType());
|
|
|
|
|
|
|
|
|
|
if(max_depth-1 > 0)
|
|
|
|
|
{
|
|
|
|
|
// Update all foreignObjects before handling myself
|
|
|
|
|
foreach (DbForeignObject foreignObject in dbObject.foreignObjectAttributes)
|
|
|
|
|
{
|
|
|
|
|
object foreignObjectRef = foreignObject.parentField.GetValue(classObject);
|
|
|
|
|
if (foreignObjectRef != null)
|
|
|
|
|
Update(foreignObjectRef, queryExecutor, max_depth - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string updateQuery = QueryBuilder.UpdateByPrimaryKey(classObject);
|
|
|
|
|
queryExecutor.Invoke(updateQuery);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Inserts class to database-object
|
|
|
|
|
/// </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>
|
|
|
|
|
/// <param name="max_depth">Determents how deep insertion will be executed (if the corresponding foreignKey-object is resolved)</param>
|
|
|
|
|
public static void Insert<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, int max_depth = 1)
|
|
|
|
|
{
|
|
|
|
|
// Read dbObject-attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classObject.GetType());
|
|
|
|
|
|
|
|
|
|
if (max_depth - 1 > 0)
|
|
|
|
|
{
|
|
|
|
|
// Update all foreignObjects before handling myself
|
|
|
|
|
foreach (DbForeignObject foreignObject in dbObject.foreignObjectAttributes)
|
|
|
|
|
{
|
|
|
|
|
object foreignObjectRef = foreignObject.parentField.GetValue(classObject);
|
|
|
|
|
if (foreignObjectRef != null)
|
|
|
|
|
Insert(foreignObjectRef, queryExecutor, max_depth - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string insertQuery = QueryBuilder.InsertAttributesByObject(classObject);
|
|
|
|
|
queryExecutor.Invoke(insertQuery);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deletes class from database-object</pragma>
|
|
|
|
|
/// Only works with primary-key/s!
|
|
|
|
|
/// </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>
|
|
|
|
|
/// <param name="max_depth">Determents how deep deletion will be executed (if the corresponding foreignKey-object is resolved)</param>
|
|
|
|
|
public static void Delete<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, int max_depth = 1)
|
|
|
|
|
{
|
|
|
|
|
// Read dbObject-attribute
|
|
|
|
|
DbObject dbObject = ClassAction.Init(classObject.GetType());
|
|
|
|
|
|
|
|
|
|
if (max_depth - 1 > 0)
|
|
|
|
|
{
|
|
|
|
|
// Update all foreignObjects before handling myself
|
|
|
|
|
foreach (DbForeignObject foreignObject in dbObject.foreignObjectAttributes)
|
|
|
|
|
{
|
|
|
|
|
object foreignObjectRef = foreignObject.parentField.GetValue(classObject);
|
|
|
|
|
if (foreignObjectRef != null)
|
|
|
|
|
Delete(foreignObjectRef, queryExecutor, max_depth - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string deleteQuery = QueryBuilder.DeleteByPrimaryKey(classObject);
|
|
|
|
|
queryExecutor.Invoke(deleteQuery);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|