Files
CS2-SimpleAdmin/CS2-SimpleAdmin/Database/Database.cs
Dawid Bepierszcz 5701455de0 Refactor database layer and add module/plugin improvements
Reworked the database layer to support both MySQL and SQLite via new provider classes and migration scripts for each backend. Updated the build workflow to support building and packaging additional modules, including StealthModule and BanSoundModule, and improved artifact handling. Refactored command registration to allow dynamic registration/unregistration and improved API event handling. Updated dependencies, project structure, and configuration checks for better reliability and extensibility. Added new language files, updated versioning, and removed obsolete files.

**⚠️ Warning: SQLite support is currently experimental.
Using this version requires reconfiguration of your database settings!
Plugin now uses UTC time. Please adjust your configurations accordingly!
**
2025-10-03 01:37:03 +02:00

69 lines
1.8 KiB
C#

using Microsoft.Extensions.Logging;
using MySqlConnector;
namespace CS2_SimpleAdmin.Database;
public class Database(string dbConnectionString)
{
public MySqlConnection GetConnection()
{
try
{
var connection = new MySqlConnection(dbConnectionString);
connection.Open();
// using var cmd = connection.CreateCommand();
// cmd.CommandText = "SET NAMES 'utf8mb4' COLLATE 'utf8mb4_general_ci';";
// cmd.ExecuteNonQueryAsync();
return connection;
}
catch (Exception ex)
{
CS2_SimpleAdmin._logger?.LogCritical($"Unable to connect to database: {ex.Message}");
throw;
}
}
public async Task<MySqlConnection> GetConnectionAsync()
{
try
{
var connection = new MySqlConnection(dbConnectionString);
await connection.OpenAsync();
// await using var cmd = connection.CreateCommand();
// cmd.CommandText = "SET NAMES 'utf8mb4' COLLATE 'utf8mb4_general_ci';";
// await cmd.ExecuteNonQueryAsync();
return connection;
}
catch (Exception ex)
{
CS2_SimpleAdmin._logger?.LogCritical($"Unable to connect to database: {ex.Message}");
throw;
}
}
// public async Task DatabaseMigration()
// {
// Migration migrator = new(this);
// await migrator.ExecuteMigrationsAsync();
// }
public bool CheckDatabaseConnection(out string? exception)
{
using var connection = GetConnection();
exception = null;
try
{
return connection.Ping();
}
catch (Exception ex)
{
exception = ex.Message;
return false;
}
}
}