Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e56187733 | ||
|
|
ef73a51ab5 | ||
|
|
d86e231bda | ||
|
|
178c92383b | ||
|
|
2f243e6ccf | ||
|
|
1c2c8f6e76 | ||
|
|
5da5d455b9 |
@@ -26,14 +26,15 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
this._foreignKeyName = foreignKeyName;
|
this._foreignKeyName = foreignKeyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(FieldInfo fi, DbObject classAttribute)
|
public void Init(FieldInfo fi, DbObject classAttribute, DbObject foreignClassAttribute = null)
|
||||||
{
|
{
|
||||||
this.parentField = fi;
|
this.parentField = fi;
|
||||||
this.classAttribute = classAttribute;
|
this.classAttribute = classAttribute;
|
||||||
this.foreignObjectType = fi.FieldType;
|
this.foreignObjectType = fi.FieldType;
|
||||||
|
|
||||||
// Init foreign-object class
|
// Init foreign-object class
|
||||||
DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
|
if(foreignClassAttribute == null)
|
||||||
|
foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
|
||||||
|
|
||||||
// Check if something is weird
|
// Check if something is weird
|
||||||
if (foreignClassAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey.");
|
if (foreignClassAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey.");
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
public string _tableName;
|
public string _tableName;
|
||||||
|
|
||||||
public Type parentClassType;
|
public Type parentClassType;
|
||||||
|
public ConstructorInfo parentCInfo;
|
||||||
|
|
||||||
// All childrenAttributes
|
// All childrenAttributes
|
||||||
public List<BaseAttribute> baseAttributes = new List<BaseAttribute>() { };
|
public List<BaseAttribute> baseAttributes = new List<BaseAttribute>() { };
|
||||||
@@ -35,6 +36,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
public void Init(Type classType)
|
public void Init(Type classType)
|
||||||
{
|
{
|
||||||
this.parentClassType = classType;
|
this.parentClassType = classType;
|
||||||
|
parentCInfo = classType.GetConstructor(Type.EmptyTypes);
|
||||||
|
|
||||||
this._tableName = this._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
|
this._tableName = this._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
|
||||||
|
|
||||||
// Iterate thru all fields
|
// Iterate thru all fields
|
||||||
@@ -66,7 +69,10 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
}
|
}
|
||||||
else if (fi.GetCustomAttribute(typeof(DbForeignObject), true) is DbForeignObject fobj) // ForeignObjects
|
else if (fi.GetCustomAttribute(typeof(DbForeignObject), true) is DbForeignObject fobj) // ForeignObjects
|
||||||
{
|
{
|
||||||
fobj.Init(fi, this);
|
if(fi.FieldType == this.parentClassType) // Check if we are referencing ourselfes
|
||||||
|
fobj.Init(fi, this, this);
|
||||||
|
else
|
||||||
|
fobj.Init(fi, this);
|
||||||
|
|
||||||
this.foreignObjectAttributes.Add(fobj);
|
this.foreignObjectAttributes.Add(fobj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,10 +69,16 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
|
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
|
||||||
{
|
{
|
||||||
object value = data_keySet.Value;
|
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 (!(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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>
|
/// <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()
|
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>() { };
|
// Read dbObject-attribute
|
||||||
primaryKeyData.Add(null, primaryKeyValue);
|
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()
|
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()
|
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
|
// Read dbObject-attribute
|
||||||
DbObject dbObject = ClassAction.Init(classType);
|
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
|
// iterate thru them to check and fill object
|
||||||
foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes)
|
foreach (DbPrimaryKey primaryKeyAtt in dbObject.primaryKeyAttributes)
|
||||||
{
|
{
|
||||||
@@ -130,8 +141,15 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
if (!dataMatchFound) throw new InvalidOperationException($"PrimaryKey='{primaryKeyAtt.parentField.Name}' is missing.");
|
if (!dataMatchFound) throw new InvalidOperationException($"PrimaryKey='{primaryKeyAtt.parentField.Name}' is missing.");
|
||||||
}
|
}
|
||||||
|
|
||||||
ResolveByPrimaryKey<T>(obj, queryExecutor);
|
try
|
||||||
|
{
|
||||||
|
ResolveByPrimaryKey<T>(obj, queryExecutor);
|
||||||
|
}catch(InvalidOperationException)
|
||||||
|
{
|
||||||
|
// If there is no result, return null
|
||||||
|
return default(T);
|
||||||
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,13 +271,16 @@ 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>
|
||||||
/// <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>
|
||||||
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
|
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
|
||||||
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
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!");
|
if (dataSet.Count == 0)
|
||||||
FillObject(classObject, dataSet[0]); // Fill the object
|
{
|
||||||
|
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>
|
/// <summary>
|
||||||
@@ -280,13 +301,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
// Resolve foreignObjects
|
// Resolve foreignObjects
|
||||||
foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes)
|
foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes)
|
||||||
{
|
{
|
||||||
|
object foreignKey_value = foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject);
|
||||||
object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject);
|
object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject);
|
||||||
|
|
||||||
// When its empty, get it & set it
|
// When key is set and object is empty, get it & set it
|
||||||
if(foreignObject_value == null)
|
if(foreignKey_value != null && foreignObject_value == null)
|
||||||
{
|
{
|
||||||
// Resolve it
|
// 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
|
foreignObjectAtt.parentField.SetValue(classObject, foreignObject_value); // Set the value
|
||||||
|
|
||||||
// Now scan the just resolved class to be able to set myself
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.5.20</Version>
|
<Version>1.5.21</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user