Merge-Commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using BeWoDatabaseUpdater.Extensions;
|
||||
using MySql.Data.MySqlClient;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BeWoDatabaseUpdater
|
||||
{
|
||||
public static class MySqlBeWoHelper
|
||||
{
|
||||
private static readonly object _Lock = new();
|
||||
|
||||
public static async Task<List<string>> LoadBeWoDatabaseNames(string connectionString)
|
||||
{
|
||||
var connection = new MySqlConnection(connectionString);
|
||||
|
||||
var result = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
|
||||
var command = new MySqlCommand("SELECT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES tables WHERE tables.TABLE_NAME = 'bewofile';") { Connection = connection };
|
||||
|
||||
await using var dataReader = command.ExecuteReader();
|
||||
|
||||
while(await dataReader.ReadAsync())
|
||||
{
|
||||
var schemaName = dataReader["TABLE_SCHEMA"]?.ToString();
|
||||
|
||||
if(schemaName is not null)
|
||||
{
|
||||
result.AddIfNotIn(schemaName);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(connection.State is ConnectionState.Open or ConnectionState.Executing)
|
||||
{
|
||||
await connection.CloseAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Dictionary<int, string> DistinctErrors = [];
|
||||
|
||||
public static void WriteSqlErrorsToFile()
|
||||
{
|
||||
lock(_Lock)
|
||||
{
|
||||
File.WriteAllLines(Program.FailedSqlCommandsFilePath, DistinctErrors.Values);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ExecuteUpdateOrCreateAsync(string sql, string filePath, string connectionString)
|
||||
{
|
||||
var connection = new MySqlConnection(connectionString);
|
||||
|
||||
try
|
||||
{
|
||||
connection.Open();
|
||||
new MySqlCommand(sql, connection).ExecuteNonQuery();
|
||||
}
|
||||
catch(MySqlException exception)
|
||||
{
|
||||
var errorMessage = $"{exception.Number} {new FileInfo(filePath).Name}: {exception.Message}";
|
||||
|
||||
DistinctErrors.AddAndIgnoreDuplicates(exception.Number, errorMessage);
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BeWoDatabaseUpdater.Utils
|
||||
{
|
||||
public class FilePath2MySqlCommands(string filePath, List<string> commands)
|
||||
{
|
||||
public string FilePath { get; set; } = filePath;
|
||||
public List<string> Commands { get; set; } = commands ?? [];
|
||||
}
|
||||
}
|
||||
21
Tools/BeWoDatabaseUpdater/BeWoDatabaseUpdater/Utils/Utils.cs
Normal file
21
Tools/BeWoDatabaseUpdater/BeWoDatabaseUpdater/Utils/Utils.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace BeWoDatabaseUpdater.Utils
|
||||
{
|
||||
public static class Utils
|
||||
{
|
||||
public static void ConsoleWriteException(Exception exception)
|
||||
{
|
||||
ConsoleWriteError(exception.ToString());
|
||||
}
|
||||
|
||||
public static void ConsoleWriteError(string errorMessage)
|
||||
{
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
Console.ForegroundColor = ConsoleColor.DarkRed;
|
||||
Console.WriteLine(errorMessage);
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user