Fix multiple where conditions and nullable fields on models

This commit is contained in:
Daniel_I_Am 2020-06-08 21:56:58 +02:00
parent 81c32f1137
commit 3c682780a4
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
5 changed files with 21 additions and 6 deletions

View File

@ -36,7 +36,18 @@ namespace ChaosBot.Attribute
foreach (PropertyInfo prop in type.GetProperties()) foreach (PropertyInfo prop in type.GetProperties())
{ {
string columnName = prop.Name; string columnName = prop.Name;
string columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString(); string columnType = null;
if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Type[] typeArguments = prop.PropertyType.GetGenericArguments();
if (typeArguments.Length == 1)
columnType = DBEntity.DataTypes.GetValueOrDefault(typeArguments[0]).ToString();
}
else
{
columnType = DBEntity.DataTypes.GetValueOrDefault(prop.PropertyType).ToString();
}
StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}"); StringBuilder constraintNameBuilder = new StringBuilder($"{table}_{columnName}");
List<string> constraintsList = new List<string>(); List<string> constraintsList = new List<string>();

View File

@ -1,3 +1,4 @@
using System;
using System.Data; using System.Data;
using ChaosBot.Attribute; using ChaosBot.Attribute;
@ -10,7 +11,7 @@ namespace ChaosBot.Database.Entity
[DBAutoIncrement] [DBAutoIncrement]
[DBNotNull] [DBNotNull]
[DBUnique] [DBUnique]
public int id { get; private set; } public Nullable<int> id { get; private set; }
public int points { get; private set; } public int points { get; private set; }
public long userId { get; private set; } public long userId { get; private set; }
public long guildId { get; private set; } public long guildId { get; private set; }

View File

@ -1,3 +1,4 @@
using System;
using System.Data; using System.Data;
using ChaosBot.Attribute; using ChaosBot.Attribute;
@ -10,7 +11,7 @@ namespace ChaosBot.Database.Entity
[DBAutoIncrement] [DBAutoIncrement]
[DBNotNull] [DBNotNull]
[DBUnique] [DBUnique]
public int id { get; private set; } public Nullable<int> id { get; private set; }
public long userId { get; private set; } public long userId { get; private set; }
public long guildId { get; private set; } public long guildId { get; private set; }

View File

@ -117,13 +117,14 @@ namespace ChaosBot.Database
public QueryBuilderRaw Where(Dictionary<string, FilterValue> whereCondition) public QueryBuilderRaw Where(Dictionary<string, FilterValue> whereCondition)
{ {
Append("WHERE"); Append("WHERE");
List<string> whereConditionList = new List<string>();
foreach (string key in whereCondition.Keys) foreach (string key in whereCondition.Keys)
{ {
FilterValue value = whereCondition.GetValueOrDefault(key); FilterValue value = whereCondition.GetValueOrDefault(key);
string parameterName = AddParameterAndReturnKey(key, value.Value); string parameterName = AddParameterAndReturnKey(key, value.Value);
Append($"{key} {value.Comparison ?? "="} {parameterName}"); whereConditionList.Add($"{key} {value.Comparison ?? "="} {parameterName}");
} }
Append(string.Join(" AND ", whereConditionList));
return this; return this;
} }

View File

@ -157,7 +157,8 @@ namespace ChaosBot.Discord.Modules
private void PickRaffle(StringBuilder sb) private void PickRaffle(StringBuilder sb)
{ {
Raffle winner = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id)); Raffle winner = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id));
RaffleRepository.Delete(winner.id); if (winner.id != null)
RaffleRepository.Delete((int)winner.id);
sb.Append($"<@{winner.userId}> has won the raffle!"); sb.Append($"<@{winner.userId}> has won the raffle!");
} }