From 166956f10f7297fd1335a2e21ae89dc4c2431e3a Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 8 Apr 2019 13:49:11 +0200 Subject: [PATCH] Added SelectByAttribute and DeleteByAttribute to QueryBuilder --- Database-Attribute_System/QueryBuilder.cs | 106 ++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs index 35c6dad..e15b5d3 100644 --- a/Database-Attribute_System/QueryBuilder.cs +++ b/Database-Attribute_System/QueryBuilder.cs @@ -37,6 +37,59 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System return BuildQuery(param); } + /// + /// Builds an SELECT-Sql-query based on an object + /// Object needs to have at least 1 attribute! + /// + /// + /// Given object (marked with Db-attributes) + /// Attributes and/or foreignKeys to use in lookup + /// SELECT-Sql-query + public static string SelectByAttribute(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 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) + { + // 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); + } + + /// /// Builds an UPDATE-Sql-query based on an object /// Object needs to have at least 1 primary-key! @@ -108,6 +161,59 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System return BuildQuery(paramWhere); } + /// + /// 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) + { + 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) + { + // 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); + } + + ///