Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ad7221680 | ||
|
|
36da32f870 | ||
|
|
d8c9867e50 | ||
|
|
166956f10f | ||
|
|
ddb7f6dc89 | ||
|
|
06aa7969bf |
@@ -57,9 +57,6 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
// Iterate through data
|
// Iterate through data
|
||||||
foreach (KeyValuePair<string, object> data_keySet in data)
|
foreach (KeyValuePair<string, object> data_keySet in data)
|
||||||
{
|
{
|
||||||
// If the data was set
|
|
||||||
bool dataIsSet = false;
|
|
||||||
|
|
||||||
// Interate through class-fields
|
// Interate through class-fields
|
||||||
foreach (KeyValuePair<string, FieldInfo> field_keySet in dbFields)
|
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())
|
if (field_keySet.Key.ToLower() == data_keySet.Key.ToLower())
|
||||||
{
|
{
|
||||||
field_keySet.Value.SetValue(classObject, data_keySet.Value);
|
field_keySet.Value.SetValue(classObject, data_keySet.Value);
|
||||||
dataIsSet = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,7 +73,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Resolves an object with the database<para/>
|
/// 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/>
|
/// - Generates an query<para/>
|
||||||
/// - Sends an query via Func<para/>
|
/// - Sends an query via Func<para/>
|
||||||
/// - Fills the object with data
|
/// - Fills the object with data
|
||||||
@@ -86,11 +82,41 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
/// <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="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>
|
/// <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);
|
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
|
||||||
Dictionary<string, object> data = queryExecutor(query);
|
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
|
||||||
FillObject(classObject, data, runDataLossChecks);
|
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="attributes">attributes 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> attributes, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true) where T : new()
|
||||||
|
{
|
||||||
|
string tableName = Function.GetDbTableName(classType); // Get database-tableName
|
||||||
|
return GetListByAttribute<T>(tableName, attributes, queryExecutor, runDataLossChecks);
|
||||||
|
}
|
||||||
|
public static List<T> GetListByAttribute<T>(string tableName, Dictionary<string, object> attributes, Func<string, List<Dictionary<string, object>>> queryExecutor, bool runDataLossChecks = true) where T: new()
|
||||||
|
{
|
||||||
|
string query = QueryBuilder.SelectByAttribute(tableName, attributes); // 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||||
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
|
<RootNamespace>eu.railduction.netcore.dll.Database_Attribute_System</RootNamespace>
|
||||||
<SignAssembly>false</SignAssembly>
|
<SignAssembly>false</SignAssembly>
|
||||||
|
<Version>1.3</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
public class QueryBuilder
|
public class QueryBuilder
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Builds an SELECT-Sql-query based on an object<para/>
|
/// Builds an SELECT-Sql-query based on an object
|
||||||
/// Object needs to have at least 1 primary-key!
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
||||||
|
/// <param name="tableName">The db-table-name</param>
|
||||||
/// <returns>SELECT-Sql-query</returns>
|
/// <returns>SELECT-Sql-query</returns>
|
||||||
public static string SelectByPrimaryKey<T>(T classObject)
|
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);
|
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!");
|
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
|
return SelectByAttribute(tableName, dbPrimaryKeys);
|
||||||
object[] param = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
|
}
|
||||||
|
|
||||||
|
/// <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
|
// 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
|
// Build and return the query
|
||||||
return BuildQuery(param);
|
return BuildQuery(param);
|
||||||
}
|
}
|
||||||
@@ -99,15 +121,32 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
|||||||
Function.ReadDbClassFields(classObject, ref dbPrimaryKeys, ref dbAttributes, ref dbForeignKeys);
|
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!");
|
if (dbPrimaryKeys.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No primary-key/s found!");
|
||||||
|
|
||||||
// Build where-parameters
|
// Build and return the query
|
||||||
object[] paramWhere = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
|
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
|
// 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
|
// Build and return the query
|
||||||
return BuildQuery(paramWhere);
|
return BuildQuery(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user