Added Select/DeleteByAttribute

Added GetListByAttribute
master
Railz 6 years ago
parent d8c9867e50
commit 36da32f870

@ -73,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
@ -84,9 +84,39 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <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, List<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
List<Dictionary<string, object>> dataSet = queryExecutor(query); List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
FillObject(classObject, dataSet[0], 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
} }
} }
} }

@ -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,13 +28,7 @@ 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 ");
// Add SQL-command part
param[0] = $"SELECT * FROM {tableName} WHERE "+ param[0];
// Build and return the query
return BuildQuery(param);
} }
/// <summary> /// <summary>
@ -42,54 +36,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// Object needs to have at least 1 attribute! /// Object needs to have at least 1 attribute!
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam> /// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param> /// <param name="tableName">The db-table-name</param>
/// <param name="attributeNames">Attributes and/or foreignKeys to use in lookup</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> /// <returns>SELECT-Sql-query</returns>
public static string SelectByAttribute<T>(T classObject, params string[] attributeNames) public static string SelectByAttribute(string tableName, Dictionary<string, object> dbAttributes = null)
{ {
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);
if (dbAttributes.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No attribute found!");
Dictionary<string, object> attributes = new Dictionary<string, object>() { };
// Iterate through given names
foreach (string attributeName in attributeNames)
{
// Iterate through attributes of class
foreach (KeyValuePair<string, object> dbAttribute in dbAttributes)
{
// If its a match, copy it to list
if (dbAttribute.Key.ToLower() == attributeName.ToLower())
{
attributes.Add(dbAttribute.Key, dbAttribute.Value);
}
}
}
object[] param = new object[1]; object[] param = new object[1];
if (attributeNames != null) if (dbAttributes != null)
{ {
// Build where statements with primaryKey/s // Build where statements with primaryKey/s
param = DbFunction.BuildKeyEqualQuery(attributes, " AND "); 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);
} }
/// <summary> /// <summary>
/// Builds an UPDATE-Sql-query based on an object<para/> /// Builds an UPDATE-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key! /// Object needs to have at least 1 primary-key!
@ -152,62 +121,25 @@ 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
object[] paramWhere = DbFunction.BuildKeyEqualQuery(dbPrimaryKeys, " AND ");
// Add SQL-command part
paramWhere[0] = $"DELETE FROM {tableName} WHERE "+ paramWhere[0];
// Build and return the query // Build and return the query
return BuildQuery(paramWhere); return DeleteByAttribute(tableName, dbPrimaryKeys);
} }
/// <summary> public static string DeleteByAttribute(string tableName, Dictionary<string, object> dbAttributes = null)
/// Builds an DELETE-Sql-query based on an object<para/>
/// Object needs to have at least 1 primary-key!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="classObject">Given object (marked with Db-attributes)</param>
/// <param name="attributeNames">Attributes and/or foreignKeys to use in lookup</param>
/// <returns>DELETE-Sql-query</returns>
public static string DeleteByAttribute<T>(T classObject, params string[] attributeNames)
{ {
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);
if (dbAttributes.Count == 0) throw new InvalidOperationException($"Cannot generate SQL-Query of '{classType.Name}'. No attribute found!");
Dictionary<string, object> attributes = new Dictionary<string, object>() { };
// Iterate through given names
foreach (string attributeName in attributeNames)
{
// Iterate through attributes of class
foreach (KeyValuePair<string, object> dbAttribute in dbAttributes)
{
// If its a match, copy it to list
if (dbAttribute.Key.ToLower() == attributeName.ToLower())
{
attributes.Add(dbAttribute.Key, dbAttribute.Value);
}
}
}
object[] param = new object[1]; object[] param = new object[1];
if (attributeNames != null) if (dbAttributes != null)
{ {
// Build where statements with primaryKey/s // Build where statements with primaryKey/s
param = DbFunction.BuildKeyEqualQuery(attributes, " AND "); param = DbFunction.BuildKeyEqualQuery(dbAttributes, " AND ");
} }
string sqlCmd = $"DELETE FROM {tableName}";
// Add SQL-command part // Add SQL-command part
param[0] = $"DELETE 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);
@ -216,6 +148,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
/// <summary> /// <summary>
/// Build an SQL-query<para/> /// Build an SQL-query<para/>
/// Will build the query-string based on the <paramref name="param"/>s including serialising and escaping for SQL.<para/> /// Will build the query-string based on the <paramref name="param"/>s including serialising and escaping for SQL.<para/>

Loading…
Cancel
Save