4 Commits
v1.2 ... v1.2.2

Author SHA1 Message Date
Railz
d8c9867e50 Set version 2019-04-08 13:54:24 +02:00
Railz
166956f10f Added SelectByAttribute and DeleteByAttribute to QueryBuilder 2019-04-08 13:49:11 +02:00
Alexander B
ddb7f6dc89 Fixed ResolveByPrimaryKey - queryExecutor to be able to return multiple rows (via List) 2019-04-02 13:31:26 +02:00
Alexander B
06aa7969bf Removed unesserary variable 2019-04-02 09:49:04 +02:00
3 changed files with 110 additions and 7 deletions

View File

@@ -57,9 +57,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// Iterate through data // Iterate through data
foreach (KeyValuePair<string, object> data_keySet in data) foreach (KeyValuePair<string, object> data_keySet in data)
{ {
// If the data was set
bool dataIsSet = false;
// Interate through class-fields // Interate through class-fields
foreach (KeyValuePair<string, FieldInfo> field_keySet in dbFields) 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()) if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower())
{ {
field_keySet.Value.SetValue(classObject, data_keySet.Value); field_keySet.Value.SetValue(classObject, data_keySet.Value);
dataIsSet = true;
break; 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="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> /// <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); string query = QueryBuilder.SelectByPrimaryKey(classObject);
Dictionary<string, object> data = queryExecutor(query); List<Dictionary<string, object>> dataSet = queryExecutor(query);
FillObject(classObject, data, runDataLossChecks); FillObject(classObject, dataSet[0], runDataLossChecks);
} }
} }
} }

View File

@@ -4,6 +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.2.3</Version>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -37,6 +37,59 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return BuildQuery(param); 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> /// <summary>
/// Builds an UPDATE-Sql-query based on an object<para/> /// Builds an UPDATE-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key! /// Object needs to have at least 1 primary-key!
@@ -108,6 +161,59 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return BuildQuery(paramWhere); 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> /// <summary>