Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8c9867e50 | ||
|
|
166956f10f | ||
|
|
ddb7f6dc89 | ||
|
|
06aa7969bf |
@@ -57,9 +57,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
// Iterate through data
|
||||
foreach (KeyValuePair<string, object> data_keySet in data)
|
||||
{
|
||||
// If the data was set
|
||||
bool dataIsSet = false;
|
||||
|
||||
// Interate through class-fields
|
||||
foreach (KeyValuePair<string, FieldInfo> field_keySet in dbFields)
|
||||
{
|
||||
@@ -67,7 +64,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower())
|
||||
{
|
||||
field_keySet.Value.SetValue(classObject, data_keySet.Value);
|
||||
dataIsSet = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -86,11 +82,11 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
/// <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 void ResolveByPrimaryKey<T>(T classObject, Func<string, Dictionary<string, object>> queryExecutor, bool runDataLossChecks = true)
|
||||
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true)
|
||||
{
|
||||
string query = QueryBuilder.SelectByPrimaryKey(classObject);
|
||||
Dictionary<string, object> data = queryExecutor(query);
|
||||
FillObject(classObject, data, runDataLossChecks);
|
||||
List<Dictionary<string, object>> dataSet = queryExecutor(query);
|
||||
FillObject(classObject, dataSet[0], runDataLossChecks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
|
||||
<SignAssembly>false</SignAssembly>
|
||||
<Version>1.2.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -37,6 +37,59 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
return BuildQuery(param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an SELECT-Sql-query based on an object<para/>
|
||||
/// Object needs to have at least 1 attribute!
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
||||
/// <param name="attributeNames">Attributes and/or foreignKeys to use in lookup</param>
|
||||
/// <returns>SELECT-Sql-query</returns>
|
||||
public static string SelectByAttribute<T>(T classObject, params string[] attributeNames)
|
||||
{
|
||||
Type classType = classObject.GetType();
|
||||
|
||||
// Get db-table-name from class
|
||||
string tableName = Function.GetDbTableName(classType);
|
||||
|
||||
// Get class db-fields
|
||||
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
|
||||
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
|
||||
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
|
||||
Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
|
||||
if (dbAttributes.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No attribute found!");
|
||||
|
||||
|
||||
Dictionary<string, object> attributes = new Dictionary<string, object>() { };
|
||||
// Iterate through given names
|
||||
foreach (string attributeName in attributeNames)
|
||||
{
|
||||
// Iterate through attributes of class
|
||||
foreach (KeyValuePair<string, object> dbAttribute in dbAttributes)
|
||||
{
|
||||
// If its a match, copy it to list
|
||||
if (dbAttribute.Key.ToLower() == attributeName.ToLower())
|
||||
{
|
||||
attributes.Add(dbAttribute.Key, dbAttribute.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object[] param = new object[1];
|
||||
if (attributeNames != null)
|
||||
{
|
||||
// Build where statements with primaryKey/s
|
||||
param = DbFunction.BuildKeyEqualQuery(attributes, " AND ");
|
||||
}
|
||||
|
||||
// Add SQL-command part
|
||||
param[0] = $"SELECT * FROM {tableName} WHERE " + param[0];
|
||||
|
||||
// Build and return the query
|
||||
return BuildQuery(param);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Builds an UPDATE-Sql-query based on an object<para/>
|
||||
/// Object needs to have at least 1 primary-key!
|
||||
@@ -108,6 +161,59 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
return BuildQuery(paramWhere);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an DELETE-Sql-query based on an object<para/>
|
||||
/// Object needs to have at least 1 primary-key!
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
||||
/// <param name="attributeNames">Attributes and/or foreignKeys to use in lookup</param>
|
||||
/// <returns>DELETE-Sql-query</returns>
|
||||
public static string DeleteByAttribute<T>(T classObject, params string[] attributeNames)
|
||||
{
|
||||
Type classType = classObject.GetType();
|
||||
|
||||
// Get db-table-name from class
|
||||
string tableName = Function.GetDbTableName(classType);
|
||||
|
||||
// Get class db-fields
|
||||
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
|
||||
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
|
||||
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
|
||||
Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
|
||||
if (dbAttributes.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No attribute found!");
|
||||
|
||||
|
||||
Dictionary<string, object> attributes = new Dictionary<string, object>() { };
|
||||
// Iterate through given names
|
||||
foreach (string attributeName in attributeNames)
|
||||
{
|
||||
// Iterate through attributes of class
|
||||
foreach (KeyValuePair<string, object> dbAttribute in dbAttributes)
|
||||
{
|
||||
// If its a match, copy it to list
|
||||
if (dbAttribute.Key.ToLower() == attributeName.ToLower())
|
||||
{
|
||||
attributes.Add(dbAttribute.Key, dbAttribute.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object[] param = new object[1];
|
||||
if (attributeNames != null)
|
||||
{
|
||||
// Build where statements with primaryKey/s
|
||||
param = DbFunction.BuildKeyEqualQuery(attributes, " AND ");
|
||||
}
|
||||
|
||||
// Add SQL-command part
|
||||
param[0] = $"DELETE FROM {tableName} WHERE " + param[0];
|
||||
|
||||
// Build and return the query
|
||||
return BuildQuery(param);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user