Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
40936fafcd | ||
|
|
800941d59c | ||
|
|
64c8711754 | ||
|
|
ad3c1da0cd | ||
|
|
ba0646f27c | ||
|
|
76922a4039 | ||
|
|
e3869b26c3 |
@@ -30,7 +30,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
||||
{
|
||||
this.parentField = fi;
|
||||
this.classAttribute = classAttribute;
|
||||
this.foreignObjectType = fi.GetType();
|
||||
this.foreignObjectType = fi.FieldType;
|
||||
|
||||
// Init foreign-object class
|
||||
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
|
||||
{
|
||||
// check if name matches
|
||||
if (foreignKey.parentField.Name.ToLower() == this._foreignKeyName)
|
||||
if (foreignKey.parentField.Name.ToLower() == this._foreignKeyName.ToLower())
|
||||
{
|
||||
if(foreignKey.parentField.GetType() == primaryKeyType)
|
||||
{
|
||||
|
||||
@@ -71,7 +71,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
||||
}
|
||||
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 '{classType.Name}'. {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
{
|
||||
public class ClassAction
|
||||
{
|
||||
private static List<Type> initiatedClassTypes = new List<Type>() { };
|
||||
private static Dictionary<Type, DbObject> initiatedClassTypes = new Dictionary<Type, DbObject>() { };
|
||||
/// <summary>
|
||||
/// Initiates the attribute-system and preloads all necessary information<para/>
|
||||
/// INFO: Will initiate necessary foreignObjects recursively!<para/>
|
||||
@@ -17,17 +17,22 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
/// <param name="classType">The classType to preload</param>
|
||||
/// <returns>DbObject-attribute corresponding to the class</returns>
|
||||
public static DbObject Init(Type classType)
|
||||
{
|
||||
DbObject cachedDbObject;
|
||||
initiatedClassTypes.TryGetValue(classType, out cachedDbObject);
|
||||
|
||||
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'");
|
||||
|
||||
if (!initiatedClassTypes.Contains(classType))
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +60,10 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
// If its a match, set the value
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -119,6 +127,34 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
return obj;
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
/// <summary>
|
||||
/// Gets an dbObject by primaryKey/s
|
||||
/// </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>
|
||||
/// Resolves dbObject by primaryKey/s<pragma/>
|
||||
/// Object needs to have primaryKey/s set!
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
|
||||
<SignAssembly>false</SignAssembly>
|
||||
<Version>1.5.6</Version>
|
||||
<Version>1.5.9</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -56,6 +56,24 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
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
|
||||
whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}";
|
||||
|
||||
// Build and return the query
|
||||
return BuildQuery(whereClause);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an UPDATE-Sql-query based on an object<para/>
|
||||
/// Object needs to have at least 1 primary-key!
|
||||
@@ -181,14 +199,14 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
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];
|
||||
object[] attributeData = new object[attributes.Count*2 -1];
|
||||
int c = 0;
|
||||
for(int i=0; i<attributes.Count*2; i+=2)
|
||||
for(int i=0; i< attributes.Count; i++)
|
||||
{
|
||||
attributesSeperatedByComma += attributes[i];
|
||||
attributeData[c] = data[i+1];
|
||||
attributeData[c] = data[i];
|
||||
|
||||
if(c+1 != attributes.Count*2)
|
||||
if(c+1 != attributeData.Length)
|
||||
{
|
||||
attributesSeperatedByComma += ", ";
|
||||
attributeData[c+1] = ",";
|
||||
|
||||
@@ -79,6 +79,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
{
|
||||
// Read dbObject-attribute
|
||||
DbObject dbObject = ClassAction.Init(classType);
|
||||
Dictionary<string, object> convertedAttributeNameAndValues = new Dictionary<string, object>();
|
||||
|
||||
foreach (KeyValuePair<string, object> attributeNameAndValue in attributeNameAndValues)
|
||||
{
|
||||
@@ -87,9 +88,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
{
|
||||
if (attributeNameAndValue.Key.ToLower() == baseAttribute.parentField.Name.ToLower())
|
||||
{
|
||||
attributeNameAndValues.Remove(attributeNameAndValue.Key);
|
||||
|
||||
attributeNameAndValues.Add(baseAttribute._attributeName, attributeNameAndValue.Value);
|
||||
convertedAttributeNameAndValues.Add(baseAttribute._attributeName, attributeNameAndValue.Value);
|
||||
|
||||
nameFound = true;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user