From 6494dc9ca951015850808275275f9ada0fdc9b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=BAben=20Garcia?= Date: Mon, 16 Oct 2023 01:53:01 +0100 Subject: [PATCH] Rever async changes but keep net48 and config changes --- OpenMod.Economy/DataBase/MySqlDatabase.cs | 44 ++++++++++------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/OpenMod.Economy/DataBase/MySqlDatabase.cs b/OpenMod.Economy/DataBase/MySqlDatabase.cs index 0e62e49..0856844 100644 --- a/OpenMod.Economy/DataBase/MySqlDatabase.cs +++ b/OpenMod.Economy/DataBase/MySqlDatabase.cs @@ -27,10 +27,10 @@ public MySqlDatabase(IConfiguration configuration, IStringLocalizer stringLocali public Task CheckShemasAsync() { - return ExecuteMySqlAsync(command => + return ExecuteMySqlAsync(async command => { command.CommandText = $"SHOW TABLES LIKE '{TableName}';"; - if (command.ExecuteScalar() != null) + if (await command.ExecuteScalarAsync() != null) return; command.CommandText = $"CREATE TABLE `{TableName}` (" + @@ -39,50 +39,50 @@ public Task CheckShemasAsync() "`Balance` DECIMAL(10,2) NOT NULL, " + "PRIMARY KEY(`Id`, `Type`)) " + "COLLATE='utf8mb4_general_ci';"; - command.ExecuteNonQuery(); + await command.ExecuteNonQueryAsync(); }); } - private bool CreateAccountIntenal(DbCommand command) + private async Task CreateAccountIntenalAsync(DbCommand command) { command.CommandText = $"INSERT IGNORE INTO `{TableName}` " + "(`Id`, `Type`, `Balance`) VALUES " + "(@ownerid, @ownertype, @balance);"; - return command.ExecuteNonQuery() > 0; + return await command.ExecuteNonQueryAsync() > 0; } public override Task GetBalanceAsync(string ownerId, string ownerType) { - return ExecuteMySqlAsync(command => + return ExecuteMySqlAsync(async command => { command.Parameters.Add("@ownerid", MySqlDbType.VarChar).Value = ownerId; command.Parameters.Add("@ownertype", MySqlDbType.VarChar).Value = ownerType; command.Parameters.Add("@balance", MySqlDbType.Decimal).Value = DefaultBalance; - if (CreateAccountIntenal(command)) + if (await CreateAccountIntenalAsync(command)) return DefaultBalance; command.CommandText = "SELECT `Balance` " + $"FROM `{TableName}` " + "WHERE `Id`=@ownerid AND `Type`=@ownertype;"; - return command.ExecuteScalar() as decimal? ?? DefaultBalance; + return await command.ExecuteScalarAsync() as decimal? ?? DefaultBalance; }); } public override Task SetBalanceAsync(string ownerId, string ownerType, decimal balance) { - return ExecuteMySqlAsync(command => + return ExecuteMySqlAsync(async command => { command.Parameters.Add("@ownerid", MySqlDbType.VarChar).Value = ownerId; command.Parameters.Add("@ownertype", MySqlDbType.VarChar).Value = ownerType; command.Parameters.Add("@balance", MySqlDbType.Decimal).Value = balance; - if (CreateAccountIntenal(command)) + if (await CreateAccountIntenalAsync(command)) return; command.CommandText = $"UPDATE `{TableName}` " + "SET `Balance`=@balance " + "WHERE `Id`=@ownerid AND `Type`=@ownertype;"; - command.ExecuteNonQuery(); + await command.ExecuteNonQueryAsync(); }); } @@ -98,7 +98,7 @@ public override Task UpdateBalanceAsync(string ownerId, string ownerTyp var newDefault = DefaultBalance + amount; command.Parameters.Add("@balance", MySqlDbType.Decimal).Value = newDefault; - if (CreateAccountIntenal(command)) + if (await CreateAccountIntenalAsync(command)) return newDefault; command.CommandText = $"UPDATE `{TableName}` " + @@ -107,40 +107,32 @@ public override Task UpdateBalanceAsync(string ownerId, string ownerTyp "AND `Type`=@ownertype" + $"{(amount < 0 ? " AND `Balance`+@amount>='0'" : string.Empty)};"; - var success = command.ExecuteNonQuery() > 0; + var success = await command.ExecuteNonQueryAsync() > 0; var balance = await GetBalanceAsync(ownerId, ownerType); if (success) return balance; throw new NotEnoughBalanceException( m_StringLocalizer["economy:fail:not_enough_balance", - new { Amount = -amount, Balance = balance, EconomyProvider = (IEconomyProvider)this }], + new {Amount = -amount, Balance = balance, EconomyProvider = (IEconomyProvider) this}], balance); }); } - private async Task ExecuteMySqlAsync(Func task) - { - await using var connection = new MySqlConnection(ConnectionString); - await using var command = connection.CreateCommand(); - connection.Open(); - return task(command); - } - private async Task ExecuteMySqlAsync(Func> task) { await using var connection = new MySqlConnection(ConnectionString); await using var command = connection.CreateCommand(); - connection.Open(); + await connection.OpenAsync(); return await task(command); } - private async Task ExecuteMySqlAsync(Action task) + private async Task ExecuteMySqlAsync(Func task) { await using var connection = new MySqlConnection(ConnectionString); await using var command = connection.CreateCommand(); - connection.Open(); - task(command); + await connection.OpenAsync(); + await task(command); } } } \ No newline at end of file