10 Commits

Author SHA1 Message Date
Railz
64c8711754 Set version 2019-05-09 08:02:49 +02:00
Railz
ad3c1da0cd Fixes:
- Fields not matching
- FieldType being lookedUp wrong
- dictionary-access-violation
- guid saved as string not casting
- InsertQuery built wrong
2019-05-08 23:22:20 +02:00
Railz
ba0646f27c Set version to 1.5.7 2019-05-08 21:56:55 +02:00
Railz
76922a4039 Changed cache-check to dictionary
> Fixed bug, not restoring cache
2019-05-08 21:56:34 +02:00
Railz
e3869b26c3 removed inner Exception 2019-04-20 10:29:52 +02:00
Alexander B
be7277fada Set version 1.5.6 2019-04-12 13:36:02 +02:00
Alexander B
b8218fbacd Added InsertAttributesByObject 2019-04-12 13:35:18 +02:00
Alexander B
253f63dfac Added InsertAttributes to generate insert-statements 2019-04-12 12:54:37 +02:00
Alexander B
d6337ef591 Ser version 2019-04-12 11:39:25 +02:00
Alexander B
1253a935b7 Added ResolveByPrimaryKey to resolve an object with set primaryKey/s 2019-04-12 11:33:30 +02:00
6 changed files with 97 additions and 19 deletions

View File

@@ -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)
{

View File

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

View File

@@ -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;
}
}
@@ -69,7 +77,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <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="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
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>() { };
@@ -115,13 +122,25 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
if (!dataMatchFound) throw new InvalidOperationException($"Cannot create object with primaryKeyData. No data assigned to field '{primaryKeyAtt.parentField.Name}'");
}
string query = QueryBuilder.SelectByPrimaryKey(obj); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
FillObject(obj, dataSet[0]); // Fill the object
ResolveByPrimaryKey<T>(obj, queryExecutor);
return obj;
}
/// <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)
{
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
FillObject(classObject, dataSet[0]); // Fill the object
}
/// <summary>
/// Gets a list of dbObjects by attribute/s
/// </summary>

View File

@@ -4,7 +4,7 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
<SignAssembly>false</SignAssembly>
<Version>1.5.1</Version>
<Version>1.5.8</Version>
</PropertyGroup>
</Project>

View File

@@ -138,7 +138,67 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
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, ")");
}

View File

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