From 36da32f8709441fedcf332018720ea0947955575 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 8 Apr 2019 19:45:04 +0200 Subject: [PATCH] Added Select/DeleteByAttribute Added GetListByAttribute --- Database-Attribute_System/ClassAction.cs | 38 ++++++- Database-Attribute_System/QueryBuilder.cs | 115 +++++----------------- 2 files changed, 58 insertions(+), 95 deletions(-) diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index e1f7b82..ded6c7f 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -73,7 +73,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System /// /// Resolves an object with the database - /// Needs to have primaryKey/s set! + /// Needs to have primaryKey/s-value/s set! /// - Generates an query /// - Sends an query via Func /// - Fills the object with data @@ -84,9 +84,39 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System /// This checks if any class-field and data-attribute does not exists in either (Slower) public static void ResolveByPrimaryKey(T classObject, Func>> queryExecutor, bool runDataLossChecks = true) { - string query = QueryBuilder.SelectByPrimaryKey(classObject); - List> dataSet = queryExecutor(query); - FillObject(classObject, dataSet[0], runDataLossChecks); + string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query + List> dataSet = queryExecutor(query); // Execute + FillObject(classObject, dataSet[0], runDataLossChecks); // Fill the object + } + + /// + /// Gets a list of dbObjects by attribute/s + /// + /// + /// Type of class + /// attributes for select + /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] + /// This checks if any class-field and data-attribute does not exists in either (Slower) + /// + public static List GetListByAttribute(Type classType, Dictionary attributes, Func>> queryExecutor, bool runDataLossChecks = true) where T : new() + { + string tableName = Function.GetDbTableName(classType); // Get database-tableName + return GetListByAttribute(tableName, attributes, queryExecutor, runDataLossChecks); + } + public static List GetListByAttribute(string tableName, Dictionary attributes, Func>> queryExecutor, bool runDataLossChecks = true) where T: new() + { + string query = QueryBuilder.SelectByAttribute(tableName, attributes); // Generate query + List> dataSet = queryExecutor(query); // Execute + + List objs = new List() { }; + foreach(Dictionary data in dataSet) + { + T obj = new T(); // New object + FillObject(obj, data, runDataLossChecks); // Fill it + objs.Add(obj); // Add to list + } + + return objs; // Return list } } } diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs index e15b5d3..58cb2ec 100644 --- a/Database-Attribute_System/QueryBuilder.cs +++ b/Database-Attribute_System/QueryBuilder.cs @@ -8,11 +8,11 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System public class QueryBuilder { /// - /// Builds an SELECT-Sql-query based on an object - /// Object needs to have at least 1 primary-key! + /// Builds an SELECT-Sql-query based on an object /// /// /// Given object (marked with Db-attributes) + /// The db-table-name /// SELECT-Sql-query public static string SelectByPrimaryKey(T classObject) { @@ -28,13 +28,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys); if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!"); - // Build where statements with primaryKey/s - object[] param = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND "); - // Add SQL-command part - param[0] = $"SELECT * FROM {tableName} WHERE "+ param[0]; - - // Build and return the query - return BuildQuery(param); + return SelectByAttribute(tableName, dbPrimaryKeys); } /// @@ -42,54 +36,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System /// Object needs to have at least 1 attribute! /// /// - /// Given object (marked with Db-attributes) - /// Attributes and/or foreignKeys to use in lookup + /// The db-table-name + /// The db-attributes with dbAttribute-name and valueIf null is given, it will generate a default 'SELECT * FROM tableName' /// SELECT-Sql-query - public static string SelectByAttribute(T classObject, params string[] attributeNames) + public static string SelectByAttribute(string tableName, Dictionary dbAttributes = null) { - Type classType = classObject.GetType(); - - // Get db-table-name from class - string tableName = Function.GetDbTableName(classType); - - // Get class db-fields - Dictionary dbPrimaryKeys = new Dictionary() { }; - Dictionary dbAttributes = new Dictionary() { }; - Dictionary dbForeignKeys = new Dictionary() { }; - 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 attributes = new Dictionary() { }; - // Iterate through given names - foreach (string attributeName in attributeNames) - { - // Iterate through attributes of class - foreach (KeyValuePair 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) + if (dbAttributes != null) { // Build where statements with primaryKey/s - param = DbFunction.BuildKeyEqualQuery(attributes, " AND "); + param = DbFunction.BuildKeyEqualQuery(dbAttributes, " AND "); } - + + string sqlCmd = $"SELECT * FROM {tableName}"; // Add SQL-command part - param[0] = $"SELECT * FROM {tableName} WHERE " + param[0]; + if (dbAttributes != null) + param[0] = $"{sqlCmd} WHERE {param[0]}"; + else + param[0] = sqlCmd; // Build and return the query return BuildQuery(param); } - /// /// Builds an UPDATE-Sql-query based on an object /// Object needs to have at least 1 primary-key! @@ -152,67 +121,31 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys); if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!"); - // Build where-parameters - object[] paramWhere = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND "); - // Add SQL-command part - paramWhere[0] = $"DELETE FROM {tableName} WHERE "+ paramWhere[0]; - // Build and return the query - return BuildQuery(paramWhere); + return DeleteByAttribute(tableName, dbPrimaryKeys); } - /// - /// Builds an DELETE-Sql-query based on an object - /// Object needs to have at least 1 primary-key! - /// - /// - /// Given object (marked with Db-attributes) - /// Attributes and/or foreignKeys to use in lookup - /// DELETE-Sql-query - public static string DeleteByAttribute(T classObject, params string[] attributeNames) + public static string DeleteByAttribute(string tableName, Dictionary dbAttributes = null) { - Type classType = classObject.GetType(); - - // Get db-table-name from class - string tableName = Function.GetDbTableName(classType); - - // Get class db-fields - Dictionary dbPrimaryKeys = new Dictionary() { }; - Dictionary dbAttributes = new Dictionary() { }; - Dictionary dbForeignKeys = new Dictionary() { }; - 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 attributes = new Dictionary() { }; - // Iterate through given names - foreach (string attributeName in attributeNames) - { - // Iterate through attributes of class - foreach (KeyValuePair 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) + if (dbAttributes != null) { // Build where statements with primaryKey/s - param = DbFunction.BuildKeyEqualQuery(attributes, " AND "); + param = DbFunction.BuildKeyEqualQuery(dbAttributes, " AND "); } + string sqlCmd = $"DELETE FROM {tableName}"; // Add SQL-command part - param[0] = $"DELETE FROM {tableName} WHERE " + param[0]; + if (dbAttributes != null) + param[0] = $"{sqlCmd} WHERE {param[0]}"; + else + param[0] = sqlCmd; // Build and return the query return BuildQuery(param); } +