Generic insert method
This commit is contained in:
parent
dbefc460a1
commit
ca9fa659ba
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
92
ChaosBot/Database/Controller.cs
Normal file
92
ChaosBot/Database/Controller.cs
Normal 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}]>.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
ChaosBot/Database/Entity/Raffle.cs
Normal file
13
ChaosBot/Database/Entity/Raffle.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
30
ChaosBot/Database/Repository/RaffleRepository.cs
Normal file
30
ChaosBot/Database/Repository/RaffleRepository.cs
Normal 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()
|
||||||
|
// {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using ChaosBot.Controllers;
|
using ChaosBot.Controllers;
|
||||||
|
using ChaosBot.Database;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ namespace ChaosBot.Discord.Modules
|
|||||||
sb.AppendLine();
|
sb.AppendLine();
|
||||||
sb.AppendLine($"Bot Version: {Program.Cfg.GetValue<string>("Bot:Version")}");
|
sb.AppendLine($"Bot Version: {Program.Cfg.GetValue<string>("Bot:Version")}");
|
||||||
sb.AppendLine($"Bot Prefix: {Program.Cfg.GetValue<string>("Discord:Prefix")}");
|
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
|
* Add the string to the Embed
|
||||||
|
|||||||
@ -3,6 +3,9 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using ChaosBot.Database;
|
||||||
|
using ChaosBot.Database.Entity;
|
||||||
|
using ChaosBot.Database.Repository;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user