diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index 308c7bd..54de900 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -127,6 +127,34 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System return obj; } + // ---- + + /// + /// Gets an dbObject by primaryKey/s + /// + /// + /// Given object (marked with Db-attributes) + /// Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause) + /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] + public static List GetListWithWhere(Type classType, Func>> queryExecutor, params object[] whereClause) where T : new() + { + // Read dbObject - attribute + DbObject dbObject = ClassAction.Init(classType); + + string query = QueryBuilder.SelectWithWhere(dbObject._tableName, whereClause); // 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); // Fill it + objs.Add(obj); // Add to list + } + + return objs; // Return list + } + /// /// Resolves dbObject by primaryKey/s /// Object needs to have primaryKey/s set! diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs index d056b97..6bf5202 100644 --- a/Database-Attribute_System/QueryBuilder.cs +++ b/Database-Attribute_System/QueryBuilder.cs @@ -56,6 +56,24 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System return BuildQuery(param); } + /// + /// Builds an SELECT-Sql-query based on an object with custom where clause + /// Object needs to have at least 1 attribute! + /// + /// + /// The db-table-name + /// Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause) + /// SELECT-Sql-query + public static string SelectWithWhere(string tableName, params object[] whereClause) + { + string sqlCmd = $"SELECT * FROM {tableName}"; + // Add SQL-command part + whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}"; + + // Build and return the query + return BuildQuery(whereClause); + } + /// /// Builds an UPDATE-Sql-query based on an object /// Object needs to have at least 1 primary-key!