Adding Clear and Transactional Method

This commit is contained in:
Sean "Solao Bajiuik" Stoves 2020-06-03 20:32:04 -04:00
parent fcd3c04ebb
commit 4fa9884f88
3 changed files with 62 additions and 4 deletions

View File

@ -91,6 +91,56 @@ namespace ChaosBot.Database
} }
} }
public static int TransactionQuery(List<SqliteCommand> cmds)
{
SqliteCommand command = _conn.CreateCommand();
SqliteTransaction transaction;
// Start a local transaction.
transaction = _conn.BeginTransaction();
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = _conn;
command.Transaction = transaction;
try
{
foreach (var cmd in cmds)
{
command.CommandText = cmd.ToString();
command.ExecuteNonQuery();
}
// Attempt to commit the transaction.
transaction.Commit();
_logger.Info($"{cmds.Count} record(s) are written to database.");
}
catch (Exception ex)
{
_logger.Warn("Commit Exception Type: {0}", ex.GetType());
_logger.Warn(" Message: {0}", ex.Message);
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
_logger.Warn("Rollback Exception Type: {0}", ex2.GetType());
_logger.Warn(" Message: {0}", ex2.Message);
}
return 0;
}
return cmds.Count;
}
public static DataTable SelectQuery(string table, string selectColumns = "*", Dictionary<string, object> filterColumns = null, string orderByKey = null) public static DataTable SelectQuery(string table, string selectColumns = "*", Dictionary<string, object> filterColumns = null, string orderByKey = null)
{ {
DataTable dt = new DataTable(); DataTable dt = new DataTable();

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using ChaosBot.Database.Entity; using ChaosBot.Database.Entity;
using Microsoft.Data.Sqlite;
namespace ChaosBot.Database.Repository namespace ChaosBot.Database.Repository
{ {
@ -93,6 +94,15 @@ namespace ChaosBot.Database.Repository
return new Raffle(idFetch, userIdFetch); return new Raffle(idFetch, userIdFetch);
} }
public static int clearRaffle()
{
List<SqliteCommand> cmds = new List<SqliteCommand>();
cmds.Add(new SqliteCommand("delete from RaffleTable;"));
cmds.Add(new SqliteCommand("UPDATE SQLITE_SEQUENCE SET seq = 0 WHERE name = 'RaffleTable';"));
return Controller.TransactionQuery(cmds);
}
// public static void delete() // public static void delete()
// { // {
// //

View File

@ -167,11 +167,9 @@ namespace ChaosBot.Discord.Modules
private void ClearRaffle(StringBuilder sb, bool noOutput = false) private void ClearRaffle(StringBuilder sb, bool noOutput = false)
{ {
_currentPot.Clear(); int Removed = RaffleRepository.clearRaffle();
if (noOutput) return; sb.AppendLine($"{Context.User.Mention} has cleared all {Removed} rafflepoints");
sb.AppendLine($"{Context.User.Mention} has cleared all rafflepoints");
} }
private void StatusRaffle(StringBuilder sb, string user) private void StatusRaffle(StringBuilder sb, string user)