Generic insert method

This commit is contained in:
Daniel_I_Am 2020-06-04 01:13:49 +02:00
parent dbefc460a1
commit ca9fa659ba
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
6 changed files with 140 additions and 48 deletions

View File

@ -1,47 +0,0 @@
using System;
using System.Data;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using NLog;
namespace ChaosBot.Controllers
{
public class DBWork
{
private static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{Program.Cfg.GetValue<string>("Bot:Database")}");
private static Logger _logger = Program._logger;
public static DataTable RawQuery(string query)
{
DataTable dt = new DataTable();
try
{
using (_conn)
{
_conn.Open();
SqliteCommand cmd = _conn.CreateCommand();
cmd.CommandText = query;
SqliteDataReader executeReader = cmd.ExecuteReader(CommandBehavior.SingleResult);
dt.Load(executeReader);
_conn.Close();
}
}
catch (Exception ex)
{
_logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
return dt;
}
}
public class Raffle
{
private int _id;
private ulong userId;
}
}

View File

@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using NLog;
namespace ChaosBot.Database
{
public static class Controller
{
static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{Program.Cfg.GetValue<string>("Bot:Database")}");
private static Logger _logger = Program._logger;
public static DataTable RawQuery(string query)
{
DataTable dt = new DataTable();
try
{
using (_conn)
{
_conn.Open();
SqliteCommand cmd = _conn.CreateCommand();
cmd.CommandText = query;
SqliteDataReader executeReader = cmd.ExecuteReader(CommandBehavior.SingleResult);
dt.Load(executeReader);
_conn.Close();
}
}
catch (Exception ex)
{
_logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
return dt;
}
public static void InsertQuery(string table, Dictionary<string, object> parameters)
{
try
{
using (_conn)
{
_conn.Open();
SqliteCommand cmd = _conn.CreateCommand();
StringBuilder commandText = new StringBuilder();
commandText.Append("INSERT INTO ");
commandText.Append(table);
commandText.Append(" (");
foreach (string key in parameters.Keys)
{
commandText.Append($"{key}, ");
}
commandText.Remove(commandText.Length - 2, 2);
commandText.Append(") VALUES (");
foreach (string key in parameters.Keys)
{
commandText.Append($"@{key},");
}
commandText.Remove(commandText.Length - 1, 1);
commandText.Append(")");
cmd.CommandText = commandText.ToString();
foreach (string key in parameters.Keys)
{
cmd.Parameters.AddWithValue($"@{key}", parameters.GetValueOrDefault(key));
}
cmd.Prepare();
cmd.ExecuteNonQuery();
_conn.Close();
}
}
catch (Exception ex)
{
_logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
}
}

View File

@ -0,0 +1,13 @@
namespace ChaosBot.Database.Entity
{
public class Raffle
{
public int id { get; }
public string userId { get; private set; }
public Raffle(string userId)
{
this.userId = userId;
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using ChaosBot.Database.Entity;
namespace ChaosBot.Database.Repository
{
public static class RaffleRepository
{
private static readonly string Table = "RaffleTable";
// public static Raffle[] all()
// {
//
// }
public static void insert(Raffle raffle)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
dict.Add("userId", raffle.userId);
Controller.InsertQuery(Table, dict);
}
// public static void delete()
// {
//
// }
}
}

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using System.Collections.Generic;
using System.Data;
using ChaosBot.Controllers;
using ChaosBot.Database;
using Microsoft.Extensions.Configuration;
using NLog;
@ -31,7 +32,7 @@ namespace ChaosBot.Discord.Modules
sb.AppendLine();
sb.AppendLine($"Bot Version: {Program.Cfg.GetValue<string>("Bot:Version")}");
sb.AppendLine($"Bot Prefix: {Program.Cfg.GetValue<string>("Discord:Prefix")}");
sb.AppendLine($"{DBWork.RawQuery("select * from RaffleTable where Id = (abs(random()) % (select (select max(Id) from RaffleTable)+1)) or rowid = (select min(Id) from RaffleTable) order by Id DESC limit 1;").Rows[0]["userId"]}");
sb.AppendLine($"{Controller.RawQuery("select * from RaffleTable where Id = (abs(random()) % (select (select max(Id) from RaffleTable)+1)) or rowid = (select min(Id) from RaffleTable) order by Id DESC limit 1;").Rows[0]["userId"]}");
/*
* Add the string to the Embed

View File

@ -3,6 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ChaosBot.Database;
using ChaosBot.Database.Entity;
using ChaosBot.Database.Repository;
using Discord;
using Discord.Commands;
using Discord.WebSocket;