diff --git a/Database-Attribute_System/Attributes/DbAttribute.cs b/Database-Attribute_System/Attributes/DbAttribute.cs
index ec79b91..0c6e48c 100644
--- a/Database-Attribute_System/Attributes/DbAttribute.cs
+++ b/Database-Attribute_System/Attributes/DbAttribute.cs
@@ -14,7 +14,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
/// Marks variable as database-attribute
///
/// Name of database-attribute (case-sensitivity is determined from database-attribute-settings) ['null' if the same as field-name]
- public DbAttribute(string attributeName = null)
+ public DbAttribute(string attributeName = null)
{
this._attributeName = attributeName;
}
diff --git a/Database-Attribute_System/QueryBuilder.cs b/Database-Attribute_System/QueryBuilder.cs
index 1000b7d..f6d67c2 100644
--- a/Database-Attribute_System/QueryBuilder.cs
+++ b/Database-Attribute_System/QueryBuilder.cs
@@ -7,21 +7,29 @@ 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!
+ ///
+ ///
+ /// Given object (marked with Db-attributes)
+ /// SELECT-Sql-query
public static string SelectByPrimaryKeys(T classObject)
{
Type classType = classObject.GetType();
// Get db-table-name from class
- string tableName = Function.GetDbTableName(classType);
+ string tableName = DbFunction.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);
+ DbFunction.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 = Function.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
+ object[] param = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
param[0] = $"SELECT * FROM {tableName} WHERE "+ param[0];
@@ -29,33 +37,40 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return BuildQuery(param);
}
-
+ ///
+ /// Builds an UPDATE-Sql-query based on an object
+ /// Object needs to have at least 1 primary-key!
+ ///
+ ///
+ /// Given object (marked with Db-attributes)
+ /// UPDATE-Sql-query
public static string UpdateByPrimaryKeys(T classObject)
{
Type classType = classObject.GetType();
// Get db-table-name from class
- string tableName = Function.GetDbTableName(classType);
+ string tableName = DbFunction.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);
+ DbFunction.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!");
// Add foreign-keys to attributes
- foreach(KeyValuePair dbForeignKey in dbForeignKeys)
+ foreach (KeyValuePair dbForeignKey in dbForeignKeys)
{
dbAttributes.Add(dbForeignKey.Key, dbForeignKey.Value);
}
// Build set-parameters
- object[] paramSet = Function.BuildKeyEqualQuery(dbAttributes, ", ");
+ object[] paramSet = DbFunction.BuildKeyEqualQuery(dbAttributes, ", ");
// Add SQL-command part
paramSet[0] = $"UPDATE {tableName} SET "+ paramSet[0];
// Build where-parameters
- object[] paramWhere = Function.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
+ object[] paramWhere = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
paramWhere[0] = " WHERE "+ paramWhere[0];
@@ -63,21 +78,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return BuildQuery(paramSet, 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)
+ /// DELETE-Sql-query
public static string DeleteByPrimaryKeys(T classObject)
{
Type classType = classObject.GetType();
// Get db-table-name from class
- string tableName = Function.GetDbTableName(classType);
+ string tableName = DbFunction.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);
+ DbFunction.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 = Function.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
+ object[] paramWhere = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
paramWhere[0] = $"DELETE FROM {tableName} WHERE "+ paramWhere[0];
@@ -90,25 +113,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
///
/// Build an SQL-query
/// Will build the query-string based on the s including serialising and escaping for SQL.
+ /// (Supported-Types: null, string, byte, int, float, double, DateTime, Guid)
///
/// Params with alternating query-parts and objects beginning with a query-part
- /// Example: "SELECT * FROM table WHERE id=", id, " AND name=", name
+ /// Example: "SELECT * FROM table WHERE id=", id, " AND name=", name
+ /// Info: Any object[] will be opened recursively and added as accordingly!
+ ///
public static string BuildQuery(params object[] param)
{
- // Convert array to list and add object[] to it accordingly
+ // Convert array to list and add object[] to it recursively
List