diff --git a/Database-Attribute_System/Attributes/DbObject.cs b/Database-Attribute_System/Attributes/DbObject.cs index 1cb1133..5cbb993 100644 --- a/Database-Attribute_System/Attributes/DbObject.cs +++ b/Database-Attribute_System/Attributes/DbObject.cs @@ -71,7 +71,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes } catch(InvalidOperationException ex) { - throw new InvalidOperationException($"Cannot init foreignObject-field '{fi.Name}' of '{classType.Name}'. {ex.Message}"); + throw new InvalidOperationException($"Cannot init foreignObject-field='{fi.Name}' of class='{classType.Name}'.", ex); } } } diff --git a/Database-Attribute_System/ClassAction.cs b/Database-Attribute_System/ClassAction.cs index 54de900..693659d 100644 --- a/Database-Attribute_System/ClassAction.cs +++ b/Database-Attribute_System/ClassAction.cs @@ -130,18 +130,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System // ---- /// - /// Gets an dbObject by primaryKey/s + /// Gets all dbObjects of class/table /// /// /// 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() + public static List GetList(Type classType, Func>> queryExecutor) where T : new() { // Read dbObject - attribute DbObject dbObject = ClassAction.Init(classType); - string query = QueryBuilder.SelectWithWhere(dbObject._tableName, whereClause); // Generate query + string query = QueryBuilder.SelectByAttribute(dbObject._tableName); // Generate query List> dataSet = queryExecutor(query); // Execute List objs = new List() { }; @@ -156,17 +155,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } /// - /// Resolves dbObject by primaryKey/s - /// Object needs to have primaryKey/s set! + /// 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 void ResolveByPrimaryKey(T classObject, Func>> queryExecutor) + public static List GetListWithWhere(Type classType, Func>> queryExecutor, params object[] whereClause) where T : new() { - string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query + // Read dbObject - attribute + DbObject dbObject = ClassAction.Init(classType); + + string query = QueryBuilder.SelectWithWhere(dbObject._tableName, whereClause); // Generate query List> dataSet = queryExecutor(query); // Execute - FillObject(classObject, dataSet[0]); // Fill the object + + 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 } /// @@ -199,7 +210,21 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System return objs; // Return list } + // ----- + /// + /// Resolves dbObject by primaryKey/s + /// Object needs to have primaryKey/s set! + /// + /// + /// Given object (marked with Db-attributes) + /// Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue] + public static void ResolveByPrimaryKey(T classObject, Func>> queryExecutor) + { + string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query + List> dataSet = queryExecutor(query); // Execute + FillObject(classObject, dataSet[0]); // Fill the object + } /// /// Resolves all foreignKeys with the database @@ -224,7 +249,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System // When its empty, get it if(foreignObject_value == null) { - foreignObject_value = GetByPrimaryKey(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); ; + foreignObject_value = GetByPrimaryKey(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); } // Recursive resolving diff --git a/Database-Attribute_System/Database-Attribute_System.csproj b/Database-Attribute_System/Database-Attribute_System.csproj index e19d1de..b4bab88 100644 --- a/Database-Attribute_System/Database-Attribute_System.csproj +++ b/Database-Attribute_System/Database-Attribute_System.csproj @@ -4,7 +4,7 @@ netcoreapp2.1 eu.railduction.netcore.dll.Database_Attribute_System false - 1.5.9 + 1.5.11 diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs index 6bf5202..ddf2ed9 100644 --- a/Database-Attribute_System/QueryBuilder.cs +++ b/Database-Attribute_System/QueryBuilder.cs @@ -68,6 +68,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System { string sqlCmd = $"SELECT * FROM {tableName}"; // Add SQL-command part + if (!(whereClause[0] is string)) throw new InvalidOperationException("Cannot generate SQL-query. WhereClause-params not starting with string!"); whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}"; // Build and return the query diff --git a/Database-Attribute_System/internal/Function.cs b/Database-Attribute_System/internal/Function.cs index f03c8a1..b85fd87 100644 --- a/Database-Attribute_System/internal/Function.cs +++ b/Database-Attribute_System/internal/Function.cs @@ -95,7 +95,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } } - if (!nameFound) throw new InvalidOperationException($"{attributeNameAndValue.Key} has no classField!"); + if (!nameFound) throw new InvalidOperationException($"'{attributeNameAndValue.Key}' has no classField!"); } } internal static void ConvertAttributeToDbAttributes(Type classType, List attributeNames) @@ -117,7 +117,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System } } - if (!nameFound) throw new InvalidOperationException($"{attributeNames[i]} has no classField!"); + if (!nameFound) throw new InvalidOperationException($"'{attributeNames[i]}' has no classField!"); } } @@ -141,7 +141,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System DateTime dateTime = (DateTime)obj; return "'" + SqlSerialise(dateTime) + "'"; // wrap in sql-brackets and convert to sql-datetime } - else if (obj.GetType() == typeof(Guid)) // Handle DateTime + else if (obj.GetType() == typeof(Guid)) // Handle Guid { string guid = ((Guid)obj).ToString(); // Get Guid as string return "'" + guid + "'"; // wrap in sql-brackets