Improved Exception messages (variables)

CLassAction: Added GetList
Set Version to 1.5.11
master v1.5.11
Railz 6 years ago
parent 40936fafcd
commit 8816110211

@ -71,7 +71,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System.Attributes
} }
catch(InvalidOperationException ex) catch(InvalidOperationException ex)
{ {
throw new InvalidOperationException($"Cannot init foreignObject-field '{fi.Name}' of '{classType.Name}'. {ex.Message}"); throw new InvalidOperationException($"Cannot init foreignObject-field='{fi.Name}' of class='{classType.Name}'.", ex);
} }
} }
} }

@ -130,18 +130,17 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// ---- // ----
/// <summary> /// <summary>
/// Gets an dbObject by primaryKey/s /// Gets all dbObjects of class/table
/// </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="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> /// <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() public static List<T> GetList<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor) where T : new()
{ {
// Read dbObject - attribute // Read dbObject - attribute
DbObject dbObject = ClassAction.Init(classType); DbObject dbObject = ClassAction.Init(classType);
string query = QueryBuilder.SelectWithWhere(dbObject._tableName, whereClause); // Generate query string query = QueryBuilder.SelectByAttribute(dbObject._tableName); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
List<T> objs = new List<T>() { }; List<T> objs = new List<T>() { };
@ -156,17 +155,29 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
} }
/// <summary> /// <summary>
/// Resolves dbObject by primaryKey/s<pragma/> /// Gets an dbObject by primaryKey/s
/// Object needs to have primaryKey/s set!
/// </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="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> /// <param name="queryExecutor">Function to handle query-calls - Has to return Dictionary[attributeName, attributeValue]</param>
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor) public static List<T> GetListWithWhere<T>(Type classType, Func<string, List<Dictionary<string, object>>> queryExecutor, params object[] whereClause) where T : new()
{ {
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query // 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<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
FillObject(classObject, dataSet[0]); // Fill the object
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> /// <summary>
@ -199,7 +210,21 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
return objs; // Return list return objs; // Return list
} }
// -----
/// <summary>
/// Resolves dbObject by primaryKey/s<pragma/>
/// Object needs to have primaryKey/s set!
/// </summary>
/// <typeparam name="T"></typeparam>
/// <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>
public static void ResolveByPrimaryKey<T>(T classObject, Func<string, List<Dictionary<string, object>>> queryExecutor)
{
string query = QueryBuilder.SelectByPrimaryKey(classObject); // Generate query
List<Dictionary<string, object>> dataSet = queryExecutor(query); // Execute
FillObject(classObject, dataSet[0]); // Fill the object
}
/// <summary> /// <summary>
/// Resolves all foreignKeys with the database<pragma/> /// Resolves all foreignKeys with the database<pragma/>
@ -224,7 +249,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
// When its empty, get it // When its empty, get it
if(foreignObject_value == null) if(foreignObject_value == null)
{ {
foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor); ; foreignObject_value = GetByPrimaryKey<T>(classType, foreignObjectAtt.foreignKeyAttribute.parentField.GetValue(classObject), queryExecutor);
} }
// Recursive resolving // Recursive resolving

@ -4,7 +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.5.9</Version> <Version>1.5.11</Version>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

@ -68,6 +68,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
{ {
string sqlCmd = $"SELECT * FROM {tableName}"; string sqlCmd = $"SELECT * FROM {tableName}";
// Add SQL-command part // Add SQL-command part
if (!(whereClause[0] is string)) throw new InvalidOperationException("Cannot generate SQL-query. WhereClause-params not starting with string!");
whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}"; whereClause[0] = $"{sqlCmd} WHERE {whereClause[0]}";
// Build and return the query // Build and return the query

@ -95,7 +95,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
} }
} }
if (!nameFound) throw new InvalidOperationException($"{attributeNameAndValue.Key} has no classField!"); if (!nameFound) throw new InvalidOperationException($"'{attributeNameAndValue.Key}' has no classField!");
} }
} }
internal static void ConvertAttributeToDbAttributes(Type classType, List<string> attributeNames) internal static void ConvertAttributeToDbAttributes(Type classType, List<string> attributeNames)
@ -117,7 +117,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
} }
} }
if (!nameFound) throw new InvalidOperationException($"{attributeNames[i]} has no classField!"); if (!nameFound) throw new InvalidOperationException($"'{attributeNames[i]}' has no classField!");
} }
} }
@ -141,7 +141,7 @@ namespace eu.railduction.netcore.dll.Database_Attribute_System
DateTime dateTime = (DateTime)obj; DateTime dateTime = (DateTime)obj;
return "'" + SqlSerialise(dateTime) + "'"; // wrap in sql-brackets and convert to sql-datetime return "'" + SqlSerialise(dateTime) + "'"; // wrap in sql-brackets and convert to sql-datetime
} }
else if (obj.GetType() == typeof(Guid)) // Handle DateTime else if (obj.GetType() == typeof(Guid)) // Handle Guid
{ {
string guid = ((Guid)obj).ToString(); // Get Guid as string string guid = ((Guid)obj).ToString(); // Get Guid as string
return "'" + guid + "'"; // wrap in sql-brackets return "'" + guid + "'"; // wrap in sql-brackets

Loading…
Cancel
Save