Compare commits

...

16 Commits

Author SHA1 Message Date
Railz
c65cffd2e7 Set version to 1.5.3 2019-12-16 12:28:23 +01:00
Railz
3356a3fa89 Added documentation for overloads
Added overloading methods for easier access
2019-12-16 12:27:04 +01:00
Railz
8c82d535f1 Added check if FillObject has missing attributes in class 2019-10-04 13:51:52 +02:00
Railz
56d8cea635 Removed type-conversion 2019-10-01 21:07:50 +02:00
Railz
64418fb9e9 Added conversion fo specific types to match correct type before filling 2019-09-30 19:49:38 +02:00
Railz
c7b9f67004 Fixed List-detection 2019-09-30 18:36:18 +02:00
Railz
d3d0ed7378 Fixed stackOverFlow when 2 classes depend on each other 2019-09-30 14:55:04 +02:00
Railz
7365ffbee6 Fixed init of DbReverseForeignObject generic list detection and extraction 2019-09-30 14:54:43 +02:00
Railz
3784108a4e Fixed init of DbIntermediateForeignObject 2019-09-30 14:54:15 +02:00
Railz
7e56187733 FillObject: Check if keys are Dbnull, so not set
ResolveForeignKeys: Check if key is set
2019-09-30 13:00:39 +02:00
Railz
ef73a51ab5 Fixed Init stackOverFlowException for recursive objects 2019-09-30 12:49:21 +02:00
Railz
d86e231bda Fixed ResolveForeignKeys:
Changed generic creation to reflective generation of new object
Fixed loading himself
2019-09-30 11:40:34 +02:00
Railz
178c92383b Added Insert, Update and Insert ClassActions 2019-09-26 15:00:59 +02:00
Railz
2f243e6ccf Catching known errors that may occur whilst resolving unknown object 2019-07-11 12:42:13 +02:00
Railz
1c2c8f6e76 Set verion to 1.5.21 2019-07-11 12:31:39 +02:00
Railz
5da5d455b9 Fixed GetByPrimaryKey with only primaryKeyValue throwing error because of null-key 2019-07-11 12:31:16 +02:00
6 changed files with 234 additions and 71 deletions

View File

@ -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.");

View File

@ -41,15 +41,12 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
this.classAttribute = classAttribute; this.classAttribute = classAttribute;
this.foreignObjectType = fi.FieldType; this.foreignObjectType = fi.FieldType;
// Init foreign-object class
DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
if (classAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey."); if (classAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey.");
if (classAttribute.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"IntermediateObject does not support multiple primaryKeys."); if (classAttribute.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"IntermediateObject does not support multiple primaryKeys.");
// Get primaryKey name if none is set // Get primaryKey name if none is set
if (_keyName == null) _keyName = classAttribute.primaryKeyAttributes[0]._attributeName; if (_keyName == null) _keyName = classAttribute.primaryKeyAttributes[0]._attributeName;
if (!(fi.FieldType is IList && fi.FieldType.IsGenericType)) // 1:m if (!(fi.FieldType.IsGenericType && (fi.FieldType.GetGenericTypeDefinition() == typeof(List<>)))) // 1:m
throw new InvalidOperationException($"IntermediateObject has to be typeof(List<T>). Maybe you meant to use DbForeignObject or DbReverseForeignObject for 1:m or 1:1 relations."); throw new InvalidOperationException($"IntermediateObject has to be typeof(List<T>). Maybe you meant to use DbForeignObject or DbReverseForeignObject for 1:m or 1:1 relations.");
// Check the generic list and get inner-type // Check the generic list and get inner-type
@ -66,13 +63,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
} }
if (foreignObjectType == null) throw new InvalidOperationException("Could not read innter-type of generic-list!"); if (foreignObjectType == null) throw new InvalidOperationException("Could not read innter-type of generic-list!");
// Now get the primaryKey from my foreignObject // Init inner-type class
DbObject foreignDbObject = ClassAction.Init(foreignObjectType); DbObject foreignClassAttribute = ClassAction.Init(foreignObjectType);
// Check the primaryKey/s // Check the primaryKey/s
if (foreignDbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignDbObject.parentClassType.Name}' does not have a primaryKey."); if (foreignClassAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignClassAttribute.parentClassType.Name}' does not have a primaryKey.");
if (foreignDbObject.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"IntermediateObject does not support multiple primaryKeys. (Found '{foreignDbObject.primaryKeyAttributes.Count}' in '{foreignDbObject.parentClassType.Name}')"); if (foreignClassAttribute.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"IntermediateObject does not support multiple primaryKeys. (Found '{foreignClassAttribute.primaryKeyAttributes.Count}' in '{foreignClassAttribute.parentClassType.Name}')");
// Save it // Save it
foreignPrimaryKeyAttribute = foreignDbObject.primaryKeyAttributes[0]; foreignPrimaryKeyAttribute = foreignClassAttribute.primaryKeyAttributes[0];
if (_foreignKeyName == null) _foreignKeyName = foreignPrimaryKeyAttribute._attributeName; if (_foreignKeyName == null) _foreignKeyName = foreignPrimaryKeyAttribute._attributeName;
} }

View File

@ -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);
} }

View File

@ -32,14 +32,31 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
this.classAttribute = classAttribute; this.classAttribute = classAttribute;
this.foreignObjectType = fi.FieldType; this.foreignObjectType = fi.FieldType;
// Init foreign-object class
DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
if (classAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey."); if (classAttribute.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{classAttribute.parentClassType.Name}' does not have a primaryKey.");
if (classAttribute.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"ReverseForeignObject does not support multiple primaryKeys."); if (classAttribute.primaryKeyAttributes.Count > 1) throw new InvalidOperationException($"ReverseForeignObject does not support multiple primaryKeys.");
// Get primaryKey name if none is set // Get primaryKey name if none is set
if (_foreignKeyName == null) _foreignKeyName = classAttribute.primaryKeyAttributes[0]._attributeName; if (_foreignKeyName == null) _foreignKeyName = classAttribute.primaryKeyAttributes[0]._attributeName;
// If its a List, get inner type
if (fi.FieldType.IsGenericType && (fi.FieldType.GetGenericTypeDefinition() == typeof(List<>)))
{
// Check the generic list and get inner-type
foreach (Type interfaceType in fi.FieldType.GetInterfaces())
{
if (interfaceType.IsGenericType &&
interfaceType.GetGenericTypeDefinition()
== typeof(IList<>))
{
foreignObjectType = fi.FieldType.GetGenericArguments()[0];
break;
}
}
if (foreignObjectType == null) throw new InvalidOperationException("Could not read innter-type of generic-list!");
}
// Init inner-type class
DbObject foreignClassAttribute = ClassAction.Init(foreignObjectType);
// Check if my primary-key is set in the foreign-class as foreignKey // Check if my primary-key is set in the foreign-class as foreignKey
DbPrimaryKey primaryKey = classAttribute.primaryKeyAttributes[0]; DbPrimaryKey primaryKey = classAttribute.primaryKeyAttributes[0];
foreach (DbForeignKey foreignKey in foreignClassAttribute.foreignKeyAttributes) foreach (DbForeignKey foreignKey in foreignClassAttribute.foreignKeyAttributes)

View File

@ -27,8 +27,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// Check if given class is marked as dbObject // Check if given class is marked as dbObject
if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObject)) throw new InvalidOperationException($"Cannot init '{classType.Name}'. Missing Attribute 'DbObject'"); if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObject)) throw new InvalidOperationException($"Cannot init '{classType.Name}'. Missing Attribute 'DbObject'");
dbObject.Init(classType); // Init dbObject
initiatedClassTypes.Add(classType, dbObject); // Set it to the list initiatedClassTypes.Add(classType, dbObject); // Set it to the list
dbObject.Init(classType); // Init dbObject
cachedDbObject = dbObject; cachedDbObject = dbObject;
} }
@ -36,7 +36,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return cachedDbObject; return cachedDbObject;
} }
/// <summary> /// <summary>
/// Fills an given dbObject with given data<para/> /// Fills an given dbObject with given data<para/>
/// Data-attribute-names and class-fieldNames have to match! (non case-sensitive) /// Data-attribute-names and class-fieldNames have to match! (non case-sensitive)
@ -44,6 +43,21 @@ 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="data">The data</param> /// <param name="data">The data</param>
/// <param name="key">Key-name of data</param>
public static void FillObject<T>(T classObject, string key, object data)
{
Dictionary<string, object> dicData = new Dictionary<string, object>();
dicData.Add(key, data);
FillObject<T>(classObject, dicData);
}
/// <summary>
/// Fills an given dbObject with given data<para/>
/// Data-attribute-names and class-fieldNames have to match! (non case-sensitive)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <param name="data">The data</param>
/// <param name="key">Key-name of data</param>
public static void FillObject<T>(T classObject, Dictionary<string, object> data) public static void FillObject<T>(T classObject, Dictionary<string, object> data)
{ {
Type classType = classObject.GetType(); Type classType = classObject.GetType();
@ -54,6 +68,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// Iterate through data // Iterate through data
foreach (KeyValuePair<string, object> data_keySet in data) foreach (KeyValuePair<string, object> data_keySet in data)
{ {
bool dataMatchFound = false;
// Interate through class-fields // Interate through class-fields
foreach (BaseAttribute baseAttribute in dbObject.baseAttributes) foreach (BaseAttribute baseAttribute in dbObject.baseAttributes)
{ {
@ -69,12 +85,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 (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
{
baseAttribute.parentField.SetValue(classObject, value);
}
baseAttribute.parentField.SetValue(classObject, value); dataMatchFound = true;
break; break;
} }
} }
if (!dataMatchFound) throw new InvalidOperationException($"Attribute '{data_keySet.Key}' has no match in object '{classObject.GetType().Name}'");
} }
} }
@ -83,15 +104,30 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// Gets an dbObject by primaryKey/s /// Gets an dbObject by primaryKey/s
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param> /// <param name="classType">The classType (marked with Db-attributes)</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param> /// <param name="primaryKeyName">Name of the primaryKey</param>
/// <param name="primaryKeyValue">Value of the primaryKey</param>
/// <param name="primaryKeyData">KeyData of multiple primaryKeys</param>
/// <param name="queryExecutor">Function to handle query-calls</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);
} }
/// <summary>
/// Gets an dbObject by primaryKey/s
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classType">The classType (marked with Db-attributes)</param>
/// <param name="primaryKeyName">Name of the primaryKey</param>
/// <param name="primaryKeyValue">Value of the primaryKey</param>
/// <param name="primaryKeyData">KeyData of multiple primaryKeys</param>
/// <param name="queryExecutor">Function to handle query-calls</param>
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()
{ {
Dictionary<string, object> primaryKeyData = new Dictionary<string, object>() { }; Dictionary<string, object> primaryKeyData = new Dictionary<string, object>() { };
@ -99,14 +135,25 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return GetByPrimaryKey<T>(classType, primaryKeyData, queryExecutor); return GetByPrimaryKey<T>(classType, primaryKeyData, queryExecutor);
} }
/// <summary>
/// Gets an dbObject by primaryKey/s
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classType">The classType (marked with Db-attributes)</param>
/// <param name="primaryKeyName">Name of the primaryKey</param>
/// <param name="primaryKeyValue">Value of the primaryKey</param>
/// <param name="primaryKeyData">KeyData of multiple primaryKeys</param>
/// <param name="queryExecutor">Function to handle query-calls</param>
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,7 +177,14 @@ 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;
} }
@ -142,7 +196,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// </summary> /// </summary>
/// <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</param>
public static List<T> GetList<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new() public static List<T> GetList<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
{ {
// Read dbObject - attribute // Read dbObject - attribute
@ -162,13 +216,42 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return objs; // Return list return objs; // Return list
} }
/// <summary>
/// Gets a list of dbObjects by attribute/s
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classType">Type of class</param>
/// <param name="fields">class-fields for select</param>
/// <param name="queryExecutor">Function to handle query-calls</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()
{
// Read dbObject-attribute
DbObject dbObject = ClassAction.Init(classType);
Function.ConvertAttributeToDbAttributes(classType, fields);
string query = QueryBuilder.SelectByAttribute(dbObject._tableName, fields); // 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> /// <summary>
/// Gets an dbObject by custom where-clause /// Gets an dbObject by custom where-clause
/// </summary> /// </summary>
/// <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="whereClause">Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause)</param> /// <param name="whereClause">Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause)</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param> /// <param name="queryExecutor">Function to handle query-calls</param>
public static List<T> GetListWithWhere<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] whereClause) where T : new() public static List<T> GetListWithWhere<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] whereClause) where T : new()
{ {
// Read dbObject - attribute // Read dbObject - attribute
@ -194,7 +277,7 @@ 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="customQuery">Custom sql-query</param> /// <param name="customQuery">Custom sql-query</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param> /// <param name="queryExecutor">Function to handle query-calls</param>
public static List<T> GetListWithQuery<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] customQuery) where T : new() public static List<T> GetListWithQuery<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] customQuery) where T : new()
{ {
// Read dbObject - attribute // Read dbObject - attribute
@ -214,36 +297,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return objs; // Return list return objs; // Return list
} }
/// <summary>
/// Gets a list of dbObjects by attribute/s
/// </summary>
/// <typeparam name="T"></typeparam>
/// <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>
/// <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()
{
// Read dbObject-attribute
DbObject dbObject = ClassAction.Init(classType);
Function.ConvertAttributeToDbAttributes(classType, fields);
string query = QueryBuilder.SelectByAttribute(dbObject._tableName, fields); // 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> /// <summary>
@ -252,14 +305,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// </summary> /// </summary>
/// <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</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>
@ -268,7 +324,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// </summary> /// </summary>
/// <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</param>
/// <param name="max_depth">Determents how deep resolving will be executed</param> /// <param name="max_depth">Determents how deep resolving will be executed</param>
public static void ResolveForeignKeys<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, int max_depth = 1) where T: new() public static void ResolveForeignKeys<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, int max_depth = 1) where T: new()
{ {
@ -280,13 +336,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 +499,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</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</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</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);
}
} }
} }

View File

@ -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.3</Version>
</PropertyGroup> </PropertyGroup>
</Project> </Project>