9 Commits
v1.2 ... v1.3.1

Author SHA1 Message Date
Railz
ff6fb08a08 Fixed missing convertion 2019-04-08 20:35:12 +02:00
Railz
d2900f364d Set version 2019-04-08 20:33:05 +02:00
Railz
b014c2f392 Added Method to convert classField-names to according dbAttributes
Added convert in GetListByAttribute
2019-04-08 20:32:44 +02:00
Railz
0ad7221680 Set version 2019-04-08 19:45:47 +02:00
Railz
36da32f870 Added Select/DeleteByAttribute
Added GetListByAttribute
2019-04-08 19:45:04 +02:00
Railz
d8c9867e50 Set version 2019-04-08 13:54:24 +02:00
Railz
166956f10f Added SelectByAttribute and DeleteByAttribute to QueryBuilder 2019-04-08 13:49:11 +02:00
Alexander B
ddb7f6dc89 Fixed ResolveByPrimaryKey - queryExecutor to be able to return multiple rows (via List) 2019-04-02 13:31:26 +02:00
Alexander B
06aa7969bf Removed unesserary variable 2019-04-02 09:49:04 +02:00
4 changed files with 132 additions and 23 deletions

View File

@@ -22,7 +22,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
string tableName = Function.GetDbTableName(classType);
// Get class-fields
Dictionary<string, FieldInfo> dbFields = Function.ReadDbClassFields(classObject);
Dictionary<string, FieldInfo> dbFields = Function.ReadDbClassFields(classType);
if (runDataLossChecks)
{
@@ -57,9 +57,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// Iterate through data
foreach (KeyValuePair<string, object> data_keySet in data)
{
// If the data was set
bool dataIsSet = false;
// Interate through class-fields
foreach (KeyValuePair<string, FieldInfo> field_keySet in dbFields)
{
@@ -67,7 +64,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower())
{
field_keySet.Value.SetValue(classObject, data_keySet.Value);
dataIsSet = true;
break;
}
}
@@ -77,7 +73,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <summary>
/// Resolves an object with the database<para/>
/// Needs to have primaryKey/s set!<para/>
/// Needs to have primaryKey/s-value/s set!<para/>
/// - Generates an query<para/>
/// - Sends an query via Func<para/>
/// - Fills the object with data
@@ -86,11 +82,40 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
/// <param name="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, Dictionary<string, object>> queryExecutor, bool runDataLossChecks = true)
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true)
{
string query = QueryBuilder.SelectByPrimaryKey(classObject);
Dictionary<string, object> data = queryExecutor(query);
FillObject(classObject, data, runDataLossChecks);
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
FillObject(classObject, dataSet[0], runDataLossChecks); // Fill the object
}
/// <summary>
/// Gets a list of dbObjects by attribute/s
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classType">Type of class</param>
/// <param name="fields">class-fields for select</param>
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
/// <param name="runDataLossChecks">This checks if any class-field and data-attribute does not exists in either (Slower)</param>
/// <returns></returns>
public static List<T> GetListByAttribute<T>(Type classType, Dictionary<string, object> fields, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true) where T : new()
{
string tableName = Function.GetDbTableName(classType); // Get database-tableName
Function.ConvertAttributeToDbAttributes(classType, fields);
string query = QueryBuilder.SelectByAttribute(tableName, fields); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
List<T> objs = new List<T>() { };
foreach(Dictionary<string, object> data in dataSet)
{
T obj = new T(); // New object
FillObject(obj, data, runDataLossChecks); // Fill it
objs.Add(obj); // Add to list
}
return objs; // Return list
}
}
}

View File

@@ -4,6 +4,7 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
<SignAssembly>false</SignAssembly>
<Version>1.3.1</Version>
</PropertyGroup>
</Project>

View File

@@ -8,11 +8,11 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
public class QueryBuilder
{
/// <summary>
/// Builds an SELECT-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key!
/// Builds an SELECT-Sql-query based on an object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <param name="tableName">The db-table-name</param>
/// <returns>SELECT-Sql-query</returns>
public static string SelectByPrimaryKey<T>(T classObject)
{
@@ -28,11 +28,33 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
Function.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 = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
return SelectByAttribute(tableName, dbPrimaryKeys);
}
/// <summary>
/// Builds an SELECT-Sql-query based on an object<para/>
/// Object needs to have at least 1 attribute!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tableName">The db-table-name</param>
/// <param name="dbAttributes">The db-attributes with dbAttribute-name and value<para/>If null is given, it will generate a default 'SELECT * FROM tableName'</param>
/// <returns>SELECT-Sql-query</returns>
public static string SelectByAttribute(string tableName, Dictionary<string, object> dbAttributes = null)
{
object[] param = new object[1];
if (dbAttributes != null)
{
// Build where statements with primaryKey/s
param = DbFunction.BuildKeyEqualQuery(dbAttributes, " AND ");
}
string sqlCmd = $"SELECT * FROM {tableName}";
// Add SQL-command part
param[0] = $"SELECT * FROM {tableName} WHERE "+ param[0];
if (dbAttributes != null)
param[0] = $"{sqlCmd} WHERE {param[0]}";
else
param[0] = sqlCmd;
// Build and return the query
return BuildQuery(param);
}
@@ -99,15 +121,32 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
Function.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 = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Build and return the query
return DeleteByAttribute(tableName, dbPrimaryKeys);
}
public static string DeleteByAttribute(string tableName, Dictionary<string, object> dbAttributes = null)
{
object[] param = new object[1];
if (dbAttributes != null)
{
// Build where statements with primaryKey/s
param = DbFunction.BuildKeyEqualQuery(dbAttributes, " AND ");
}
string sqlCmd = $"DELETE FROM {tableName}";
// Add SQL-command part
paramWhere[0] = $"DELETE FROM {tableName} WHERE "+ paramWhere[0];
if (dbAttributes != null)
param[0] = $"{sqlCmd} WHERE {param[0]}";
else
param[0] = sqlCmd;
// Build and return the query
return BuildQuery(paramWhere);
return BuildQuery(param);
}
/// <summary>

View File

@@ -31,6 +31,52 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
}
}
internal static void ConvertAttributeToDbAttributes(Type classType, Dictionary<string, object> attributeNameAndValues)
{
Dictionary<string, FieldInfo> classFields = ReadDbClassFields(classType);
foreach (KeyValuePair<string, object> attributeNameAndValue in attributeNameAndValues)
{
bool nameFound = false;
foreach (KeyValuePair<string, FieldInfo> classField in classFields)
{
if (attributeNameAndValue.Key.ToLower() == classField.Value.Name.ToLower())
{
attributeNameAndValues.Remove(attributeNameAndValue.Key);
attributeNameAndValues.Add(classField.Key, attributeNameAndValue.Value);
nameFound = true;
break;
}
}
if (!nameFound) throw new InvalidOperationException($"{attributeNameAndValue.Key} has no classField!");
}
}
internal static void ConvertAttributeToDbAttributes(Type classType, List<string> attributeNames)
{
Dictionary<string, FieldInfo> classFields = ReadDbClassFields(classType);
List<string> dbAttributes = new List<string>() { };
for(int i=0; i< attributeNames.Count; i++)
{
bool nameFound = false;
foreach (KeyValuePair<string, FieldInfo> classField in classFields)
{
if(attributeNames[i].ToLower() == classField.Value.Name.ToLower())
{
attributeNames[i] = classField.Key;
nameFound = true;
break;
}
}
if (!nameFound) throw new InvalidOperationException($"{attributeNames[i]} has no classField!");
}
}
internal static void ReadDbClassFields<T>(T classObject, ref Dictionary<string, object> dbPrimaryKeys, ref Dictionary<string, object> dbAttributes, ref Dictionary<string, object> dbForeignKeys)
{
Type classType = typeof(T);
@@ -65,10 +111,8 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
}
}
internal static Dictionary<string, FieldInfo> ReadDbClassFields<T>(T classObject)
internal static Dictionary<string, FieldInfo> ReadDbClassFields(Type classType)
{
Type classType = typeof(T);
Dictionary<string, FieldInfo> dbFields = new Dictionary<string, FieldInfo>();
// Iterate thru all properties