mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-22 19:54:27 +00:00
feat: Add SQLite database support with MySQL compatibility
This commit is contained in:
68
MySQLConnection.cs
Normal file
68
MySQLConnection.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MySqlConnector;
|
||||
using System.Data;
|
||||
|
||||
namespace WeaponPaints
|
||||
{
|
||||
public class MySQLConnection : IDatabaseConnection
|
||||
{
|
||||
private readonly MySqlConnection _connection;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public MySQLConnection(string connectionString, ILogger logger)
|
||||
{
|
||||
_connection = new MySqlConnection(connectionString);
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task OpenAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await _connection.OpenAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Unable to connect to MySQL database: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CloseAsync()
|
||||
{
|
||||
await _connection.CloseAsync();
|
||||
}
|
||||
|
||||
public async Task<IDbTransaction> BeginTransactionAsync()
|
||||
{
|
||||
return await _connection.BeginTransactionAsync();
|
||||
}
|
||||
|
||||
public Task CommitTransactionAsync(IDbTransaction transaction)
|
||||
{
|
||||
transaction.Commit();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RollbackTransactionAsync(IDbTransaction transaction)
|
||||
{
|
||||
transaction.Rollback();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public IDbConnection GetConnection()
|
||||
{
|
||||
return _connection;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection?.Dispose();
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await _connection.DisposeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user