Class-Object now has to be passed

Will get the value of the object

Added DeleteByPrimaryKey
master
Alexander B 6 years ago
parent 248be3320f
commit 803b7b7b95

@ -1,5 +1,7 @@
using System;
using eu.railduction.netcore.dll.Database_Attribute_System.Attributes;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace eu.railduction.netcore.dll.Database_Attribute_System
@ -16,5 +18,69 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
{
return dt.ToString("yyyy-MM-dd HH:mm:ss");
}
public static object[] BuildKeyEqualQuery(Dictionary<string, object> keysets, string seperator)
{
object[] param = new object[keysets.Count * 2];
int c = 0;
foreach (KeyValuePair<string, object> keyset in keysets)
{
string sql_string = "";
if (c != 0) sql_string += seperator;
sql_string += $"{keyset.Key}=";
param[c] = sql_string;
param[c + 1] = keyset.Value;
c += 2;
}
return param;
}
public static string GetDbTableName(Type classType)
{
// Check if class has attribute 'DbObject' and get the database table-name
if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObjectAttribute)) throw new InvalidOperationException("Cannot generate SQL-Query of class. Missing Attribute 'DbObject'");
string tableName = dbObjectAttribute._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
return tableName;
}
public static void ReadDbClassFields<T>(T classObject, ref Dictionary<string, object> dbPrimaryKeys, ref Dictionary<string, object> dbAttributes, ref Dictionary<string, object> dbForeignKeys)
{
Type classType = T.GetType();
// Reset lists (just in case)
dbPrimaryKeys = new Dictionary<string, object> () { };
dbAttributes = new Dictionary<string, object>() { };
dbForeignKeys = new Dictionary<string, object>() { };
// Iterate thru all properties
foreach (System.Reflection.FieldInfo fi in classType.GetRuntimeFields())
{
// Check if current field is a db-field
if (fi.GetCustomAttribute(typeof(DbPrimaryKey), true) is DbPrimaryKey pkey) // PrimaryKey
{
string dbAttributeName = pkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbPrimaryKeys.Add(dbAttributeName, value);
}
else if (fi.GetCustomAttribute(typeof(DbAttribute), true) is DbAttribute att) // Attributes
{
string dbAttributeName = att._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbAttributes.Add(dbAttributeName, value);
}
else if (fi.GetCustomAttribute(typeof(DbForeignKey), true) is DbForeignKey fkey) // ForeignKeys
{
string dbAttributeName = fkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
object value = fi.GetValue(classObject);
dbForeignKeys.Add(dbAttributeName, value);
}
}
}
}
}

@ -7,59 +7,86 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
{
public class QueryBuilder
{
/// <summary>
/// Build an SELECT-SQL-query by a database-primary-key<pragma/>
/// Will build the query-string based on the <paramref name="comparisonValue"/> including serialising and escaping for SQL.<para/>
/// </summary>
/// <param name="handler">Method to send querys to the database</param>
/// <param name="primaryKey">Database comparison value</param>
/// <returns>Built SQL-Query</returns>
public static string SelectByPrimaryKey(Type classType, object comparisonValue) => SelectByPrimaryKeys(classType, comparisonValue);
/// <summary>
/// Build an SELECT-SQL-query by database-primary-keys<pragma/>
/// Will build the query-string based on the <paramref name="comparisonValues"/> including serialising and escaping for SQL.<para/>
/// </summary>
/// <param name="handler">Method to send querys to the database</param>
/// <param name="primaryKeys">Database comparison values (Must be the exact same amount as primary keys in class/table)</param>
/// <returns>Built SQL-Query</returns>
public static string SelectByPrimaryKeys(Type classType, params object[] comparisonValues)
public static string SelectByPrimaryKeys<T>(T classObject)
{
// Check if class has attribute 'DbObject' and get the database table-name
if (!(classType.GetCustomAttribute(typeof(DbObject), true) is DbObject dbObjectAttribute)) throw new InvalidOperationException("Cannot generate SQL-Query of class. Missing Attribute 'DbObject'");
string tableName = dbObjectAttribute._tableName ?? classType.Name; // If no alternative table-name is specified, use the class-name
Type classType = classObject.GetType();
// Get db-table-name from class
string tableName = Function.GetDbTableName(classType);
// Get class db-fields
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
// Build where statements with primaryKey/s
object[] param = Function.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
param[0] += $"SELECT * FROM {tableName} WHERE ";
// Build and return the query
return BuildQuery(param);
}
// Iterate thru all properties
List<string> dbPrimaryKeys = new List<string>() { };
foreach (System.Reflection.FieldInfo fi in classType.GetRuntimeFields())
public static string UpdateByPrimaryKeys<T>(T classObject)
{
// Get primaryKey attribute from current property
if (fi.GetCustomAttribute(typeof(DbPrimaryKey), true) is DbPrimaryKey pkey)
Type classType = classObject.GetType();
// Get db-table-name from class
string tableName = Function.GetDbTableName(classType);
// Get class db-fields
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
// Add foreign-keys to attributes
foreach(KeyValuePair<string, object> dbForeignKey in dbForeignKeys)
{
string dbAttributeName = pkey._attributeName ?? fi.Name; // If no alternative attribute-name is specified, use the property-name
dbPrimaryKeys.Add(dbAttributeName);
}
dbAttributes.Add(dbForeignKey.Key, dbForeignKey.Value);
}
if (comparisonValues.Length != dbPrimaryKeys.Count) throw new InvalidOperationException("Primary-key number of class/table and number of comparison values is not equal!");
// Build set-parameters
object[] paramSet = Function.BuildKeyEqualQuery(dbAttributes, ", ");
// Add SQL-command part
paramSet[0] += $"UPDATE {tableName} SET ";
// Build where-parameters
object[] paramWhere = Function.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
paramWhere[0] += " WHERE ";
// Build and return the query
return BuildQuery(paramSet, paramWhere);
}
object[] param = new object[comparisonValues.Length * 2];
for (int i = 0, c = 0; c < param.Length; i++, c += 2)
public static string DeleteByPrimaryKeys<T>(T classObject)
{
string sql_string = "";
Type classType = classObject.GetType();
if (i == 0) sql_string += $"SELECT * FROM {tableName} WHERE ";
else sql_string += " AND ";
sql_string += $"{dbPrimaryKeys[i]}=";
// Get db-table-name from class
string tableName = Function.GetDbTableName(classType);
param[c] = sql_string;
param[c + 1] = comparisonValues[i];
}
// Get class db-fields
Dictionary<string, object> dbPrimaryKeys = new Dictionary<string, object>() { };
Dictionary<string, object> dbAttributes = new Dictionary<string, object>() { };
Dictionary<string, object> dbForeignKeys = new Dictionary<string, object>() { };
Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
return BuildQuery(param);
// Build where-parameters
object[] paramWhere = Function.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
paramWhere[0] += $"DELETE FROM {tableName} WHERE ";
// Build and return the query
return BuildQuery(paramWhere);
}
/// <summary>
/// Build an SQL-query<para/>
/// Will build the query-string based on the <paramref name="param"/>s including serialising and escaping for SQL.<para/>

Loading…
Cancel
Save