Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e56187733 | ||
|
|
ef73a51ab5 | ||
|
|
d86e231bda |
@@ -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,6 +69,9 @@ 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
|
||||||
{
|
{
|
||||||
|
if(fi.FieldType == this.parentClassType) // Check if we are referencing ourselfes
|
||||||
|
fobj.Init(fi, this, this);
|
||||||
|
else
|
||||||
fobj.Init(fi, this);
|
fobj.Init(fi, this);
|
||||||
|
|
||||||
this.foreignObjectAttributes.Add(fobj);
|
this.foreignObjectAttributes.Add(fobj);
|
||||||
|
|||||||
@@ -69,11 +69,17 @@ 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 (!(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
|
//if (baseAttribute.parentField.FieldType == typeof(Guid)) value = new Guid((string)value); // If its a guid, i need to convert
|
||||||
|
|
||||||
baseAttribute.parentField.SetValue(classObject, value);
|
baseAttribute.parentField.SetValue(classObject, value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,14 +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}'");
|
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)
|
||||||
{
|
{
|
||||||
@@ -295,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
|
||||||
|
|||||||
Reference in New Issue
Block a user