Implemented:
QueryBuilder.SelectWithWhere() ClassAction.GetListWithWhere()
This commit is contained in:
parent
64c8711754
commit
800941d59c
@ -127,6 +127,34 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
return obj;
|
||||
}
|
||||
|
||||
// ----
|
||||
|
||||
/// <summary>
|
||||
/// Gets an dbObject by primaryKey/s
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="classObject">Given object (marked with Db-attributes)</param>
|
||||
/// <param name="whereClause">Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause)</param>
|
||||
/// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
|
||||
public static List<T> GetListWithWhere<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] whereClause) where T : new()
|
||||
{
|
||||
// Read dbObject - attribute
|
||||
DbObject dbObject = ClassAction.Init(classType);
|
||||
|
||||
string query = QueryBuilder.SelectWithWhere(dbObject._tableName, whereClause); // 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); // Fill it
|
||||
objs.Add(obj); // Add to list
|
||||
}
|
||||
|
||||
return objs; // Return list
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves dbObject by primaryKey/s<pragma/>
|
||||
/// Object needs to have primaryKey/s set!
|
||||
|
@ -56,6 +56,24 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
|
||||
return BuildQuery(param);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an SELECT-Sql-query based on an object with custom where clause<para/>
|
||||
/// Object needs to have at least 1 attribute!
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="tableName">The db-table-name</param>
|
||||
/// <param name="whereClause">Custom where-clause params attached to query (SELECT * FROM tableName WHERE whereClause)</param>
|
||||
/// <returns>SELECT-Sql-query</returns>
|
||||
public static string SelectWithWhere(string tableName, params object[] whereClause)
|
||||
{
|
||||
string sqlCmd = $"SELECT * FROM {tableName}";
|
||||
// Add SQL-command part
|
||||
whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}";
|
||||
|
||||
// Build and return the query
|
||||
return BuildQuery(whereClause);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds an UPDATE-Sql-query based on an object<para/>
|
||||
/// Object needs to have at least 1 primary-key!
|
||||
|
Loading…
x
Reference in New Issue
Block a user