Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be7277fada | ||
|
|
b8218fbacd | ||
|
|
253f63dfac | ||
|
|
d6337ef591 | ||
|
|
1253a935b7 | ||
|
|
2d4a4d5f7e |
@@ -9,7 +9,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
|
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
|
||||||
public class DbForeignObject : Attribute
|
public class DbForeignObject : Attribute
|
||||||
{
|
{
|
||||||
public Type _foreignObjectType;
|
public Type foreignObjectType;
|
||||||
|
|
||||||
public string _foreignKeyName;
|
public string _foreignKeyName;
|
||||||
public DbForeignKey foreignKeyAttribute;
|
public DbForeignKey foreignKeyAttribute;
|
||||||
@@ -20,11 +20,9 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Marks variable as foreign-object of an dbObject
|
/// Marks variable as foreign-object of an dbObject
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="foreignObjectType">Type of foreignObject</param>
|
|
||||||
/// <param name="foreignKeyName">Fieldname of foreignKey associated with the foreignObject</param>
|
/// <param name="foreignKeyName">Fieldname of foreignKey associated with the foreignObject</param>
|
||||||
public DbForeignObject(Type foreignObjectType, string foreignKeyName = null)
|
public DbForeignObject(string foreignKeyName = null)
|
||||||
{
|
{
|
||||||
this._foreignObjectType = foreignObjectType;
|
|
||||||
this._foreignKeyName = foreignKeyName;
|
this._foreignKeyName = foreignKeyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,9 +30,10 @@ 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();
|
||||||
|
|
||||||
// Init foreign-object class
|
// Init foreign-object class
|
||||||
DbObject foreignClassAttribute = ClassAction.Init(this._foreignObjectType);
|
DbObject 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.");
|
||||||
|
|||||||
@@ -69,7 +69,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="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>
|
|
||||||
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>() { };
|
Dictionary<string, object> primaryKeyData = new Dictionary<string, object>() { };
|
||||||
@@ -115,13 +114,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}'");
|
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
|
ResolveByPrimaryKey<T>(obj, queryExecutor);
|
||||||
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
|
||||||
FillObject(obj, dataSet[0]); // Fill the object
|
|
||||||
|
|
||||||
return obj;
|
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>
|
/// <summary>
|
||||||
/// Gets a list of dbObjects by attribute/s
|
/// Gets a list of dbObjects by attribute/s
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -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</Version>
|
<Version>1.5.6</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -138,7 +138,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];
|
||||||
|
int c = 0;
|
||||||
|
for(int i=0; i<attributes.Count*2; i+=2)
|
||||||
|
{
|
||||||
|
attributesSeperatedByComma += attributes[i];
|
||||||
|
attributeData[c] = data[i+1];
|
||||||
|
|
||||||
|
if(c+1 != attributes.Count*2)
|
||||||
|
{
|
||||||
|
attributesSeperatedByComma += ", ";
|
||||||
|
attributeData[c+1] = ",";
|
||||||
|
}
|
||||||
|
c +=2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build and return the query
|
||||||
|
return BuildQuery($"INSERT INTO {tableName} ({attributesSeperatedByComma}) VALUES (", attributeData, ")");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user