Skip to content

Commit

Permalink
db delete fix
Browse files Browse the repository at this point in the history
  • Loading branch information
panthernet committed Jan 31, 2019
1 parent 5b8e520 commit 85e3718
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
5 changes: 3 additions & 2 deletions ThunderED/Classes/DiscordCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,6 @@ public async Task SysCommand([Remainder] string x)
switch (values[0])
{
case "cleartable":
{
if (values.Length < 2)
{
await APIHelper.DiscordAPI.ReplyMessageAsync(Context, LM.Get("helpSysClear"), true);
Expand All @@ -679,7 +678,9 @@ public async Task SysCommand([Remainder] string x)
if(!await SQLHelper.ClearTable(values[1]))
await APIHelper.DiscordAPI.ReplyMessageAsync(Context, LM.Get("sysClearTableNotFound"), true);
else await APIHelper.DiscordAPI.ReplyMessageAsync(Context, LM.Get("sysOperationComplete"), true);
}
return;
default:
await APIHelper.DiscordAPI.ReplyMessageAsync(Context, LM.Get("helpSys"), true);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion ThunderED/Classes/IDatabasePovider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IDatabasePovider
{
Task<T> Query<T>(string table, string field, Dictionary<string, object> where);
Task Update(string table, string setField, object setData, Dictionary<string, object> where);
Task<bool> Delete(string table, Dictionary<string, object> where);
Task<bool> Delete(string table, Dictionary<string, object> where = null);
Task DeleteWhereIn(string table, string field, List<long> list, bool not);
Task InsertOrUpdate(string table, Dictionary<string, object> values);
Task<List<object[]>> SelectData(string query);
Expand Down
2 changes: 2 additions & 0 deletions ThunderED/Helpers/SQLHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private static async Task Insert(string table, Dictionary<string, object> values
#region Delete
private static async Task<bool> Delete(string table, string whereField = null, object whereData = null)
{
if (whereField == null && whereData == null)
return await Provider?.Delete(table, null);
return await Provider?.Delete(table, new Dictionary<string, object> {{whereField, whereData}});
}

Expand Down
17 changes: 10 additions & 7 deletions ThunderED/Providers/SqliteDatabaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,22 +328,25 @@ await SessionWrapper(query, async command =>
#endregion

#region Delete
public async Task<bool> Delete(string table, Dictionary<string, object> where)
public async Task<bool> Delete(string table, Dictionary<string, object> where = null)
{
var whereText = string.Empty;
int count = 1;
var last = where.Keys.Last();
foreach (var pair in where)
if (where != null)
{
whereText += $"{pair.Key}=@var{count++}{(pair.Key == last? null : " and ")}";
var last = where.Keys.Last();
foreach (var pair in where)
whereText += $"{pair.Key}=@var{count++}{(pair.Key == last ? null : " and ")}";
whereText = $" WHERE {whereText}";
}

var query = $"DELETE FROM {table} WHERE {whereText}";
var query = $"DELETE FROM {table}{whereText}";
return await SessionWrapper(query, async command =>
{
count = 1;
foreach (var pair in where)
command.Parameters.Add(CreateParam<SqliteParameter>($"@var{count++}", pair.Value));
if(where != null)
foreach (var pair in where)
command.Parameters.Add(CreateParam<SqliteParameter>($"@var{count++}", pair.Value));
try
{
command.ExecuteNonQuery();
Expand Down

0 comments on commit 85e3718

Please sign in to comment.