Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
178c92383b | ||
|
|
2f243e6ccf | ||
|
|
1c2c8f6e76 | ||
|
|
5da5d455b9 | ||
|
|
3a0af04b1c | ||
|
|
4f7495ea68 | ||
|
|
111b3bf7ce | ||
|
|
aca99302dd | ||
|
|
7317503759 | ||
|
|
75f4709db1 | ||
|
|
0d07d82ff5 | ||
|
|
b6bc82a766 | ||
|
|
3e8427c4c0 | ||
|
|
8816110211 | ||
|
|
40936fafcd | ||
|
|
800941d59c | ||
|
|
64c8711754 | ||
|
|
ad3c1da0cd | ||
|
|
ba0646f27c | ||
|
|
76922a4039 | ||
|
|
e3869b26c3 | ||
|
|
be7277fada | ||
|
|
b8218fbacd | ||
|
|
253f63dfac |
@@ -30,7 +30,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
{
|
{
|
||||||
this.parentField = fi;
|
this.parentField = fi;
|
||||||
this.classAttribute = classAttribute;
|
this.classAttribute = classAttribute;
|
||||||
this.foreignObjectType = fi.GetType();
|
this.foreignObjectType = fi.FieldType;
|
||||||
|
|
||||||
// Init foreign-object class
|
// Init foreign-object class
|
||||||
DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
|
DbObject foreignClassAttribute = ClassAction.Init(this.foreignObjectType);
|
||||||
@@ -45,7 +45,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
if(this._foreignKeyName != null) // If i have a name
|
if(this._foreignKeyName != null) // If i have a name
|
||||||
{
|
{
|
||||||
// check if name matches
|
// check if name matches
|
||||||
if (foreignKey.parentField.Name.ToLower() == this._foreignKeyName)
|
if (foreignKey.parentField.Name.ToLower() == this._foreignKeyName.ToLower())
|
||||||
{
|
{
|
||||||
if(foreignKey.parentField.GetType() == primaryKeyType)
|
if(foreignKey.parentField.GetType() == primaryKeyType)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,80 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
|
||||||
|
public class DbIntermediateForeignObject : Attribute
|
||||||
|
{
|
||||||
|
public Type foreignObjectType;
|
||||||
|
|
||||||
|
public string _intermediateTableName;
|
||||||
|
|
||||||
|
public string _keyName;
|
||||||
|
public string _foreignKeyName;
|
||||||
|
|
||||||
|
|
||||||
|
public DbPrimaryKey foreignPrimaryKeyAttribute;
|
||||||
|
|
||||||
|
public FieldInfo parentField;
|
||||||
|
public DbObject classAttribute;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Marks variable as intermediate-object of an dbObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="intermediateTableName">Table-name of intermediate-table. Must contain primaryKey of this class & target class</param>
|
||||||
|
/// <param name="keyName">Fieldname of primaryKey associated with the IntermediateObject on this side [m]:n (null if same as primary key) [Only works with 1 primaryKey]</param>
|
||||||
|
/// <param name="foreignKeyName">Fieldname of primaryKey associated with the IntermediateObject on the other side m:[n] (null if same as primary key) [Only works with 1 primaryKey]</param>
|
||||||
|
public DbIntermediateForeignObject(string intermediateTableName, string keyName = null, string foreignKeyName = null)
|
||||||
|
{
|
||||||
|
this._intermediateTableName = intermediateTableName;
|
||||||
|
this._foreignKeyName = foreignKeyName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init(FieldInfo fi, DbObject classAttribute)
|
||||||
|
{
|
||||||
|
this.parentField = fi;
|
||||||
|
this.classAttribute = classAttribute;
|
||||||
|
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($"IntermediateObject does not support multiple primaryKeys.");
|
||||||
|
// Get primaryKey name if none is set
|
||||||
|
if (_keyName == null) _keyName = classAttribute.primaryKeyAttributes[0]._attributeName;
|
||||||
|
|
||||||
|
if (!(fi.FieldType is IList && fi.FieldType.IsGenericType)) // 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.");
|
||||||
|
|
||||||
|
// Check the generic list and get inner-type
|
||||||
|
Type foreignObjectType = null;
|
||||||
|
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!");
|
||||||
|
|
||||||
|
// Now get the primaryKey from my foreignObject
|
||||||
|
DbObject foreignDbObject = ClassAction.Init(foreignObjectType);
|
||||||
|
// Check the primaryKey/s
|
||||||
|
if (foreignDbObject.primaryKeyAttributes.Count < 1) throw new InvalidOperationException($"'{foreignDbObject.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}')");
|
||||||
|
// Save it
|
||||||
|
foreignPrimaryKeyAttribute = foreignDbObject.primaryKeyAttributes[0];
|
||||||
|
|
||||||
|
if (_foreignKeyName == null) _foreignKeyName = foreignPrimaryKeyAttribute._attributeName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
public List<DbAttribute> attributeAttributes = new List<DbAttribute>() { };
|
public List<DbAttribute> attributeAttributes = new List<DbAttribute>() { };
|
||||||
public List<DbForeignKey> foreignKeyAttributes = new List<DbForeignKey>() { };
|
public List<DbForeignKey> foreignKeyAttributes = new List<DbForeignKey>() { };
|
||||||
public List<DbForeignObject> foreignObjectAttributes = new List<DbForeignObject>() { };
|
public List<DbForeignObject> foreignObjectAttributes = new List<DbForeignObject>() { };
|
||||||
|
public List<DbReverseForeignObject> reverseForeignObjectAttributes = new List<DbReverseForeignObject>() { };
|
||||||
|
public List<DbIntermediateForeignObject> intermediateObjectAttributes = new List<DbIntermediateForeignObject>() { };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marks variable as database-table
|
/// Marks variable as database-table
|
||||||
@@ -68,10 +70,20 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
|
|
||||||
this.foreignObjectAttributes.Add(fobj);
|
this.foreignObjectAttributes.Add(fobj);
|
||||||
}
|
}
|
||||||
|
else if (fi.GetCustomAttribute(typeof(DbReverseForeignObject), true) is DbReverseForeignObject rfobj) // ReverseForeignObjects
|
||||||
|
{
|
||||||
|
rfobj.Init(fi, this);
|
||||||
|
this.reverseForeignObjectAttributes.Add(rfobj);
|
||||||
|
}
|
||||||
|
else if (fi.GetCustomAttribute(typeof(DbIntermediateForeignObject), true) is DbIntermediateForeignObject iobj) // ReverseForeignObjects
|
||||||
|
{
|
||||||
|
iobj.Init(fi, this);
|
||||||
|
this.intermediateObjectAttributes.Add(iobj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch(InvalidOperationException ex)
|
catch(InvalidOperationException ex)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"Cannot init foreignObject-field '{fi.Name}' of '{classType.Name}'. {ex.Message}", ex);
|
throw new InvalidOperationException($"Cannot init foreignObject-field='{fi.Name}' of class='{classType.Name}'.", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
||||||
|
{
|
||||||
|
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
|
||||||
|
public class DbReverseForeignObject : Attribute
|
||||||
|
{
|
||||||
|
public Type foreignObjectType;
|
||||||
|
|
||||||
|
public string _foreignKeyName;
|
||||||
|
public DbForeignKey foreignKeyAttribute;
|
||||||
|
|
||||||
|
public FieldInfo parentField;
|
||||||
|
public DbObject classAttribute;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Marks variable as reverse-foreign-object of an dbObject
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="foreignKeyName">Fieldname of primaryKey associated with the reverseForeignObject (null if same as primary key) [Only works with 1 primaryKey]</param>
|
||||||
|
public DbReverseForeignObject(string foreignKeyName = null)
|
||||||
|
{
|
||||||
|
this._foreignKeyName = foreignKeyName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Init(FieldInfo fi, DbObject classAttribute)
|
||||||
|
{
|
||||||
|
this.parentField = fi;
|
||||||
|
this.classAttribute = classAttribute;
|
||||||
|
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($"ReverseForeignObject does not support multiple primaryKeys.");
|
||||||
|
// Get primaryKey name if none is set
|
||||||
|
if (_foreignKeyName == null) _foreignKeyName = classAttribute.primaryKeyAttributes[0]._attributeName;
|
||||||
|
|
||||||
|
// Check if my primary-key is set in the foreign-class as foreignKey
|
||||||
|
DbPrimaryKey primaryKey = classAttribute.primaryKeyAttributes[0];
|
||||||
|
foreach (DbForeignKey foreignKey in foreignClassAttribute.foreignKeyAttributes)
|
||||||
|
{
|
||||||
|
if (primaryKey._attributeName.ToLower() == foreignKey._attributeName.ToLower()) // Name matches
|
||||||
|
if (primaryKey.parentField.GetType() == foreignKey.parentField.GetType()) // Type matches
|
||||||
|
{
|
||||||
|
foreignKeyAttribute = foreignKey;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
// Same name, but wrong type
|
||||||
|
throw new InvalidOperationException($"ForeignObject='{foreignClassAttribute.parentClassType.Name}' has invalid type foreignKey='{foreignKey.parentField.Name}' for object='{classAttribute.parentClassType.Name}' with primaryKey='{primaryKey.parentField.Name}'.");
|
||||||
|
}
|
||||||
|
// No match
|
||||||
|
if (foreignKeyAttribute == null) throw new InvalidOperationException($"ForeignObject='{foreignClassAttribute.parentClassType.Name}' is missing foreignKey for object='{classAttribute.parentClassType.Name}' with primaryKey='{primaryKey.parentField.Name}'.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
using eu.railduction.netcore.dll.Database_Attribute_System.Attributes;
|
using eu.railduction.netcore.dll.Database_Attribute_System.Attributes;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -8,7 +9,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
{
|
{
|
||||||
public class ClassAction
|
public class ClassAction
|
||||||
{
|
{
|
||||||
private static List<Type> initiatedClassTypes = new List<Type>() { };
|
private static Dictionary<Type, DbObject> initiatedClassTypes = new Dictionary<Type, DbObject>() { };
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initiates the attribute-system and preloads all necessary information<para/>
|
/// Initiates the attribute-system and preloads all necessary information<para/>
|
||||||
/// INFO: Will initiate necessary foreignObjects recursively!<para/>
|
/// INFO: Will initiate necessary foreignObjects recursively!<para/>
|
||||||
@@ -18,16 +19,21 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
/// <returns>DbObject-attribute corresponding to the class</returns>
|
/// <returns>DbObject-attribute corresponding to the class</returns>
|
||||||
public static DbObject Init(Type classType)
|
public static DbObject Init(Type classType)
|
||||||
{
|
{
|
||||||
// Check if given class is marked as dbObject
|
DbObject cachedDbObject;
|
||||||
if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObject)) throw new InvalidOperationException($"Cannot init '{classType.Name}'. Missing Attribute 'DbObject'");
|
initiatedClassTypes.TryGetValue(classType, out cachedDbObject);
|
||||||
|
|
||||||
if (!initiatedClassTypes.Contains(classType))
|
if (cachedDbObject == null)
|
||||||
{
|
{
|
||||||
|
// 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'");
|
||||||
|
|
||||||
dbObject.Init(classType); // Init dbObject
|
dbObject.Init(classType); // Init dbObject
|
||||||
initiatedClassTypes.Add(classType); // Set it to the list
|
initiatedClassTypes.Add(classType, dbObject); // Set it to the list
|
||||||
|
|
||||||
|
cachedDbObject = dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dbObject;
|
return cachedDbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -38,7 +44,6 @@ 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="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)
|
public static void FillObject<T>(T classObject, Dictionary<string, object> data)
|
||||||
{
|
{
|
||||||
Type classType = classObject.GetType();
|
Type classType = classObject.GetType();
|
||||||
@@ -52,10 +57,21 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
// Interate through class-fields
|
// Interate through class-fields
|
||||||
foreach (BaseAttribute baseAttribute in dbObject.baseAttributes)
|
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 its a match, set the value
|
||||||
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
|
if (baseAttribute._attributeName.ToLower() == data_keySet.Key.ToLower())
|
||||||
{
|
{
|
||||||
baseAttribute.parentField.SetValue(classObject, 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
|
||||||
|
|
||||||
|
baseAttribute.parentField.SetValue(classObject, value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,10 +87,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()
|
||||||
{
|
{
|
||||||
@@ -91,6 +110,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
// 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}'");
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
@@ -111,28 +132,101 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If no data was found matching this field
|
// If no data was found matching this field
|
||||||
if (!dataMatchFound) throw new InvalidOperationException($"Cannot create object with primaryKeyData. No data assigned to field '{primaryKeyAtt.parentField.Name}'");
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolves dbObject by primaryKey/s<pragma/>
|
/// Gets all dbObjects of class/table
|
||||||
/// Object needs to have primaryKey/s set!
|
|
||||||
/// </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 - 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> GetList<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
|
||||||
{
|
{
|
||||||
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
|
// 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<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 an dbObject by custom where-clause
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <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="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
||||||
|
public static List<T> GetListWithWhere<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] whereClause) where T : new()
|
||||||
|
{
|
||||||
|
// Read dbObject - attribute
|
||||||
|
DbObject dbObject = ClassAction.Init(classType);
|
||||||
|
|
||||||
|
string query = QueryBuilder.SelectWithWhere(dbObject._tableName, whereClause); // 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 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 List<T> GetListWithQuery<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] customQuery) where T : new()
|
||||||
|
{
|
||||||
|
// Read dbObject - attribute
|
||||||
|
DbObject dbObject = ClassAction.Init(classType);
|
||||||
|
|
||||||
|
string query = QueryBuilder.BuildQuery(customQuery);
|
||||||
|
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 a list of dbObjects by attribute/s
|
/// Gets a list of dbObjects by attribute/s
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -140,7 +234,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
/// <param name="classType">Type of class</param>
|
/// <param name="classType">Type of class</param>
|
||||||
/// <param name="fields">class-fields for select</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="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>
|
/// <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()
|
public static List<T> GetListByAttribute<T>(Type classType, Dictionary<string, object> fields, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
|
||||||
{
|
{
|
||||||
@@ -163,7 +256,26 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
return objs; // Return list
|
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, bool throwExceptions = true)
|
||||||
|
{
|
||||||
|
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
|
||||||
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
||||||
|
|
||||||
|
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>
|
/// <summary>
|
||||||
/// Resolves all foreignKeys with the database<pragma/>
|
/// Resolves all foreignKeys with the database<pragma/>
|
||||||
@@ -173,7 +285,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
/// <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>
|
||||||
/// <param name="max_depth">Determents how deep resolving will be executed</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()
|
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();
|
Type classType = classObject.GetType();
|
||||||
@@ -181,22 +292,254 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
// Read dbObject-attribute
|
// Read dbObject-attribute
|
||||||
DbObject dbObject = ClassAction.Init(classType);
|
DbObject dbObject = ClassAction.Init(classType);
|
||||||
|
|
||||||
|
// Resolve foreignObjects
|
||||||
foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes)
|
foreach (DbForeignObject foreignObjectAtt in dbObject.foreignObjectAttributes)
|
||||||
{
|
{
|
||||||
object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject);
|
object foreignObject_value = foreignObjectAtt.parentField.GetValue(classObject);
|
||||||
|
|
||||||
// When its empty, get it
|
// When its empty, get it & set it
|
||||||
if(foreignObject_value == null)
|
if(foreignObject_value == null)
|
||||||
{
|
{
|
||||||
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); ;
|
// Resolve it
|
||||||
|
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor);
|
||||||
|
foreignObjectAtt.parentField.SetValue(classObject, foreignObject_value); // Set the value
|
||||||
|
|
||||||
|
// Now scan the just resolved class to be able to set myself
|
||||||
|
DbObject foreignDbObject = Init(foreignObject_value.GetType());
|
||||||
|
foreach(DbReverseForeignObject dbReverseForeignObject in foreignDbObject.reverseForeignObjectAttributes)
|
||||||
|
{
|
||||||
|
// If the field-names match
|
||||||
|
if(dbReverseForeignObject._foreignKeyName.ToLower() == dbObject.primaryKeyAttributes[0]._attributeName.ToLower())
|
||||||
|
{
|
||||||
|
object myReference;
|
||||||
|
if (dbReverseForeignObject.parentField.FieldType is IList && dbReverseForeignObject.parentField.FieldType.IsGenericType) // 1:m
|
||||||
|
{
|
||||||
|
// If its a list, i create a list with just myself
|
||||||
|
myReference = new List<T>() { classObject };
|
||||||
|
}
|
||||||
|
else // 1:1
|
||||||
|
{
|
||||||
|
// Otherwise ist just myself
|
||||||
|
myReference = classObject;
|
||||||
|
}
|
||||||
|
dbReverseForeignObject.parentField.SetValue(foreignObject_value, myReference);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursive resolving
|
// Recursive resolving
|
||||||
if (max_depth - 1 > 0)
|
if (max_depth > 1)
|
||||||
{
|
{
|
||||||
|
// Go recursively into the next class
|
||||||
ResolveForeignKeys(foreignObject_value, queryExecutor, max_depth - 1);
|
ResolveForeignKeys(foreignObject_value, queryExecutor, max_depth - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Resolve intermediateForeignObjects
|
||||||
|
foreach (DbIntermediateForeignObject intermediateForeignObjectAtt in dbObject.intermediateObjectAttributes)
|
||||||
|
{
|
||||||
|
object intermediateForeignObject_value = intermediateForeignObjectAtt.parentField.GetValue(classObject);
|
||||||
|
|
||||||
|
// When its empty, get it & set it
|
||||||
|
if (intermediateForeignObject_value == null)
|
||||||
|
{
|
||||||
|
// Generate & set attribute-set
|
||||||
|
Dictionary<string, object> attributes = new Dictionary<string, object>();
|
||||||
|
attributes.Add(intermediateForeignObjectAtt._keyName, dbObject.primaryKeyAttributes[0].parentField.GetValue(classObject));
|
||||||
|
|
||||||
|
string query = QueryBuilder.SelectByAttribute(intermediateForeignObjectAtt._intermediateTableName, attributes); // Generate query
|
||||||
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
||||||
|
// Extract data
|
||||||
|
List<object> values = new List<object>();
|
||||||
|
for (int i=0; i<dataSet.Count; i++)
|
||||||
|
{
|
||||||
|
Dictionary<string, object> data = dataSet[i];
|
||||||
|
object primaryKeyValue = data[intermediateForeignObjectAtt.foreignPrimaryKeyAttribute._attributeName];
|
||||||
|
|
||||||
|
values.Add(GetByPrimaryKey<object>(intermediateForeignObjectAtt.foreignPrimaryKeyAttribute.classAttribute.parentClassType, primaryKeyValue, queryExecutor));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now scan the just resolved class to be able to set myself
|
||||||
|
DbObject foreignDbObject = Init(intermediateForeignObjectAtt.foreignPrimaryKeyAttribute.classAttribute.parentClassType);
|
||||||
|
foreach (DbForeignObject dbForeignObject in foreignDbObject.foreignObjectAttributes)
|
||||||
|
{
|
||||||
|
// If the field-names match
|
||||||
|
if (dbForeignObject._foreignKeyName.ToLower() == dbObject.primaryKeyAttributes[0]._attributeName.ToLower())
|
||||||
|
{
|
||||||
|
object myReference = classObject;
|
||||||
|
foreach (object value in values)
|
||||||
|
{
|
||||||
|
dbForeignObject.parentField.SetValue(value, myReference);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set value
|
||||||
|
intermediateForeignObject_value = values;
|
||||||
|
intermediateForeignObjectAtt.parentField.SetValue(classObject, intermediateForeignObject_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive resolving
|
||||||
|
if (max_depth > 1)
|
||||||
|
{
|
||||||
|
// If we have a list of objects, we need to recursively go into each one
|
||||||
|
foreach (object value in (IList)intermediateForeignObject_value)
|
||||||
|
{
|
||||||
|
ResolveForeignKeys(value, queryExecutor, max_depth - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Resolve reverseForeignObjects
|
||||||
|
foreach (DbReverseForeignObject reverseForeignObjectAtt in dbObject.reverseForeignObjectAttributes)
|
||||||
|
{
|
||||||
|
object reverseForeignObject_value = reverseForeignObjectAtt.parentField.GetValue(classObject);
|
||||||
|
Type reverseForeignObject_type = reverseForeignObjectAtt.parentField.GetType();
|
||||||
|
|
||||||
|
// When its empty, get it & set it
|
||||||
|
if (reverseForeignObject_value == null)
|
||||||
|
{
|
||||||
|
// Generate & set attribute-set
|
||||||
|
Dictionary<string, object> attributes = new Dictionary<string, object>();
|
||||||
|
attributes.Add(reverseForeignObjectAtt._foreignKeyName, dbObject.primaryKeyAttributes[0].parentField.GetValue(classObject));
|
||||||
|
|
||||||
|
List<object> values = GetListByAttribute<object>(reverseForeignObjectAtt.foreignKeyAttribute.classAttribute.parentClassType, attributes, queryExecutor);
|
||||||
|
|
||||||
|
if(values.Count == 0) throw new InvalidOperationException($"'{reverseForeignObjectAtt.parentField.Name}' could not been resolved. ReverseForeignObject returned '{values.Count}' values.");
|
||||||
|
|
||||||
|
// Now scan the just resolved class to be able to set myself
|
||||||
|
DbObject foreignDbObject = Init(reverseForeignObjectAtt.foreignKeyAttribute.classAttribute.parentClassType);
|
||||||
|
foreach (DbForeignObject dbForeignObject in foreignDbObject.foreignObjectAttributes)
|
||||||
|
{
|
||||||
|
// If the field-names match
|
||||||
|
if (dbForeignObject._foreignKeyName.ToLower() == dbObject.primaryKeyAttributes[0]._attributeName.ToLower())
|
||||||
|
{
|
||||||
|
object myReference = classObject;
|
||||||
|
foreach(object value in values)
|
||||||
|
{
|
||||||
|
dbForeignObject.parentField.SetValue(value, myReference);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for type to determen 1:1 or 1:m
|
||||||
|
if (reverseForeignObject_type is IList && reverseForeignObject_type.IsGenericType) // List, so 1:m
|
||||||
|
{
|
||||||
|
reverseForeignObject_value = values;
|
||||||
|
}
|
||||||
|
else // Not list, so 1:1
|
||||||
|
{
|
||||||
|
if (values.Count > 1) throw new InvalidOperationException($"'{reverseForeignObjectAtt.parentField.Name}' could not been resolved as ReverseForeignObject returned '{values.Count}' values. (Is it 1:m instead of 1:1?)");
|
||||||
|
reverseForeignObject_value = values[0];
|
||||||
|
}
|
||||||
|
reverseForeignObjectAtt.parentField.SetValue(classObject, reverseForeignObject_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursive resolving
|
||||||
|
if (max_depth > 1)
|
||||||
|
{
|
||||||
|
if (reverseForeignObject_value is IList && reverseForeignObject_type.IsGenericType) // 1:m
|
||||||
|
{
|
||||||
|
// If we have a list of objects, we need to recursively go into each one
|
||||||
|
foreach(object value in (IList)reverseForeignObject_value)
|
||||||
|
{
|
||||||
|
ResolveForeignKeys(value, queryExecutor, max_depth - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // 1:1
|
||||||
|
{
|
||||||
|
// Go recursively into the next class
|
||||||
|
ResolveForeignKeys(reverseForeignObject_value, queryExecutor, max_depth - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <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.5</Version>
|
<Version>1.5.21</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -56,6 +56,25 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
return BuildQuery(param);
|
return BuildQuery(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds an SELECT-Sql-query based on an object with custom where clause<para/>
|
||||||
|
/// Object needs to have at least 1 attribute!
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="tableName">The db-table-name</param>
|
||||||
|
/// <param name="whereClause">Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause)</param>
|
||||||
|
/// <returns>SELECT-Sql-query</returns>
|
||||||
|
public static string SelectWithWhere(string tableName, params object[] whereClause)
|
||||||
|
{
|
||||||
|
string sqlCmd = $"SELECT * FROM {tableName}";
|
||||||
|
// Add SQL-command part
|
||||||
|
if (!(whereClause[0] is string)) throw new InvalidOperationException("Cannot generate SQL-query. WhereClause-params not starting with string!");
|
||||||
|
whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}";
|
||||||
|
|
||||||
|
// Build and return the query
|
||||||
|
return BuildQuery(whereClause);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Builds an UPDATE-Sql-query based on an object<para/>
|
/// Builds an UPDATE-Sql-query based on an object<para/>
|
||||||
/// Object needs to have at least 1 primary-key!
|
/// Object needs to have at least 1 primary-key!
|
||||||
@@ -138,7 +157,67 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
return BuildQuery(param);
|
return BuildQuery(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Builds an INSERT-Sql-query based on an object<para/>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tableName"></param>
|
||||||
|
/// <param name="dbAttributes"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string InsertAttributesByObject<T>(T classObject)
|
||||||
|
{
|
||||||
|
Type classType = classObject.GetType();
|
||||||
|
|
||||||
|
// Read dbObject-attribute
|
||||||
|
DbObject dbObject = ClassAction.Init(classType);
|
||||||
|
|
||||||
|
List<string> attributes = new List<string>() { };
|
||||||
|
List<object> data = new List<object>() { };
|
||||||
|
foreach(BaseAttribute baseAttribute in dbObject.baseAttributes)
|
||||||
|
{
|
||||||
|
attributes.Add(baseAttribute._attributeName);
|
||||||
|
data.Add(baseAttribute.parentField.GetValue(classObject));
|
||||||
|
}
|
||||||
|
|
||||||
|
return InsertAttributes(dbObject._tableName, attributes, data);
|
||||||
|
}
|
||||||
|
public static string InsertAttributes(string tableName, Dictionary<string, object> dbAttributes)
|
||||||
|
{
|
||||||
|
if (dbAttributes.Count == 0) throw new InvalidOperationException("Cannot generate SQL-Query. No attributes to insert.");
|
||||||
|
|
||||||
|
List<string> attributes = new List<string>() { };
|
||||||
|
List<object> data = new List<object>() { };
|
||||||
|
|
||||||
|
foreach (KeyValuePair<string, object> attribute in dbAttributes)
|
||||||
|
{
|
||||||
|
attributes.Add(attribute.Key);
|
||||||
|
data.Add(attribute.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return InsertAttributes(tableName, attributes, data);
|
||||||
|
}
|
||||||
|
public static string InsertAttributes(string tableName, List<string> attributes, List<object> data)
|
||||||
|
{
|
||||||
|
if (attributes.Count != data.Count) throw new InvalidOperationException("Cannot generate SQL-Query. Attribute-count and data-count not equal.");
|
||||||
|
|
||||||
|
string attributesSeperatedByComma = "";
|
||||||
|
object[] attributeData = new object[attributes.Count*2 -1];
|
||||||
|
int c = 0;
|
||||||
|
for(int i=0; i< attributes.Count; i++)
|
||||||
|
{
|
||||||
|
attributesSeperatedByComma += attributes[i];
|
||||||
|
attributeData[c] = data[i];
|
||||||
|
|
||||||
|
if(c+1 != attributeData.Length)
|
||||||
|
{
|
||||||
|
attributesSeperatedByComma += ", ";
|
||||||
|
attributeData[c+1] = ",";
|
||||||
|
}
|
||||||
|
c +=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build and return the query
|
||||||
|
return BuildQuery($"INSERT INTO {tableName} ({attributesSeperatedByComma}) VALUES (", attributeData, ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
return str_cpy;
|
return str_cpy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string SqlSerialise(DateTime dt, string format = "yyyy-MM-dd HH:mm:ss")
|
|
||||||
{
|
|
||||||
return dt.ToString(format);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recursive object[] copying
|
// Recursive object[] copying
|
||||||
internal static void RecursiveParameterCopying(ref List<object> paramz, object[] objects)
|
internal static void RecursiveParameterCopying(ref List<object> paramz, object[] objects)
|
||||||
{
|
{
|
||||||
@@ -79,6 +74,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
{
|
{
|
||||||
// Read dbObject-attribute
|
// Read dbObject-attribute
|
||||||
DbObject dbObject = ClassAction.Init(classType);
|
DbObject dbObject = ClassAction.Init(classType);
|
||||||
|
Dictionary<string, object> convertedAttributeNameAndValues = new Dictionary<string, object>();
|
||||||
|
|
||||||
foreach (KeyValuePair<string, object> attributeNameAndValue in attributeNameAndValues)
|
foreach (KeyValuePair<string, object> attributeNameAndValue in attributeNameAndValues)
|
||||||
{
|
{
|
||||||
@@ -87,16 +83,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
{
|
{
|
||||||
if (attributeNameAndValue.Key.ToLower() == baseAttribute.parentField.Name.ToLower())
|
if (attributeNameAndValue.Key.ToLower() == baseAttribute.parentField.Name.ToLower())
|
||||||
{
|
{
|
||||||
attributeNameAndValues.Remove(attributeNameAndValue.Key);
|
convertedAttributeNameAndValues.Add(baseAttribute._attributeName, attributeNameAndValue.Value);
|
||||||
|
|
||||||
attributeNameAndValues.Add(baseAttribute._attributeName, attributeNameAndValue.Value);
|
|
||||||
|
|
||||||
nameFound = true;
|
nameFound = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nameFound) throw new InvalidOperationException($"{attributeNameAndValue.Key} has no classField!");
|
if (!nameFound) throw new InvalidOperationException($"'{attributeNameAndValue.Key}' has no classField!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal static void ConvertAttributeToDbAttributes(Type classType, List<string> attributeNames)
|
internal static void ConvertAttributeToDbAttributes(Type classType, List<string> attributeNames)
|
||||||
@@ -118,31 +112,31 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nameFound) throw new InvalidOperationException($"{attributeNames[i]} has no classField!");
|
if (!nameFound) throw new InvalidOperationException($"'{attributeNames[i]}' has no classField!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static string SqlSerialise(object obj)
|
public static string SqlSerialise(object obj)
|
||||||
{
|
{
|
||||||
if (obj == null) // Handle null
|
if (obj == null || obj is DBNull) // Handle null
|
||||||
{
|
{
|
||||||
return "null";
|
return "null";
|
||||||
}
|
}
|
||||||
else if (obj.GetType() == typeof(string)) // Handle strings
|
else if (obj is string) // Handle strings
|
||||||
{
|
{
|
||||||
return "'" + SqlEscape((string)obj) + "'"; // wrap in sql-brackets and escape sql, if any
|
return "'" + SqlEscape((string)obj) + "'"; // wrap in sql-brackets and escape sql, if any
|
||||||
}
|
}
|
||||||
else if (obj.GetType() == typeof(byte) || obj.GetType() == typeof(int) || obj.GetType() == typeof(float) || obj.GetType() == typeof(double)) // Handle int, float & double
|
else if (obj is byte || obj is int || obj is float || obj is double) // Handle int, float & double
|
||||||
{
|
{
|
||||||
return obj.ToString().Replace(",", "."); // just format to string and form comma to sql-comma
|
return obj.ToString().Replace(",", "."); // just format to string and form comma to sql-comma
|
||||||
}
|
}
|
||||||
else if (obj.GetType() == typeof(DateTime)) // Handle DateTime
|
else if (obj is DateTime) // Handle DateTime
|
||||||
{
|
{
|
||||||
DateTime dateTime = (DateTime)obj;
|
DateTime dateTime = (DateTime)obj;
|
||||||
return "'" + SqlSerialise(dateTime) + "'"; // wrap in sql-brackets and convert to sql-datetime
|
return "'" + dateTime.ToString("yyyy-MM-dd HH:mm:ss") + "'"; // wrap in sql-brackets and convert to sql-datetime
|
||||||
}
|
}
|
||||||
else if (obj.GetType() == typeof(Guid)) // Handle DateTime
|
else if (obj is Guid) // Handle Guid
|
||||||
{
|
{
|
||||||
string guid = ((Guid)obj).ToString(); // Get Guid as string
|
string guid = ((Guid)obj).ToString(); // Get Guid as string
|
||||||
return "'" + guid + "'"; // wrap in sql-brackets
|
return "'" + guid + "'"; // wrap in sql-brackets
|
||||||
|
|||||||
Reference in New Issue
Block a user