mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-03-12 17:16:29 +00:00
Fix: Factory patten when opening connection mysql
This commit is contained in:
11
Database.cs
11
Database.cs
@@ -4,17 +4,18 @@ namespace WeaponPaints
|
|||||||
{
|
{
|
||||||
public class Database
|
public class Database
|
||||||
{
|
{
|
||||||
private readonly IDatabaseConnection _connection;
|
private readonly Func<IDatabaseConnection> _connectionFactory;
|
||||||
|
|
||||||
public Database(IDatabaseConnection connection)
|
public Database(Func<IDatabaseConnection> connectionFactory)
|
||||||
{
|
{
|
||||||
_connection = connection;
|
_connectionFactory = connectionFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IDatabaseConnection> GetConnectionAsync()
|
public async Task<IDatabaseConnection> GetConnectionAsync()
|
||||||
{
|
{
|
||||||
await _connection.OpenAsync();
|
var connection = _connectionFactory();
|
||||||
return _connection;
|
await connection.OpenAsync();
|
||||||
|
return connection;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,8 +113,8 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Criar conexão baseada no tipo de banco
|
// Criar factory de conexão baseada no tipo de banco
|
||||||
IDatabaseConnection connection;
|
Func<IDatabaseConnection> connectionFactory;
|
||||||
if (config.DatabaseType.ToLower() == "mysql")
|
if (config.DatabaseType.ToLower() == "mysql")
|
||||||
{
|
{
|
||||||
var builder = new MySqlConnectionStringBuilder
|
var builder = new MySqlConnectionStringBuilder
|
||||||
@@ -127,7 +127,7 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
|
|||||||
Pooling = true,
|
Pooling = true,
|
||||||
MaximumPoolSize = 640,
|
MaximumPoolSize = 640,
|
||||||
};
|
};
|
||||||
connection = new MySQLConnection(builder.ConnectionString, Logger);
|
connectionFactory = () => new MySQLConnection(builder.ConnectionString, Logger);
|
||||||
}
|
}
|
||||||
else // SQLite
|
else // SQLite
|
||||||
{
|
{
|
||||||
@@ -142,10 +142,10 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig
|
|||||||
var connectionString = $"Data Source={dbPath}";
|
var connectionString = $"Data Source={dbPath}";
|
||||||
Logger.LogInformation($"[WeaponPaints] SQLite database path: {dbPath}");
|
Logger.LogInformation($"[WeaponPaints] SQLite database path: {dbPath}");
|
||||||
|
|
||||||
connection = new SQLiteConnection(connectionString, Logger);
|
connectionFactory = () => new SQLiteConnection(connectionString, Logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
Database = new Database(connection);
|
Database = new Database(connectionFactory);
|
||||||
|
|
||||||
Utility.Config = config;
|
Utility.Config = config;
|
||||||
Task.Run(async () => await Utility.CheckDatabaseTables());
|
Task.Run(async () => await Utility.CheckDatabaseTables());
|
||||||
|
|||||||
Reference in New Issue
Block a user