Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
be7277fada | ||
|
|
b8218fbacd | ||
|
|
253f63dfac |
@@ -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.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