MySqlDumpSegmenter.cs
Divise un dump MySQL en fichiers distincts basés sur les commentaires pour en simplifier l’analyse.
using System.Text.RegularExpressions;
const string path = "Dumps.sql";
var title = "";
using var sr = new StreamReader(path);
StreamWriter sw = null;
try
{
while (sr.ReadLine() is { } line)
{
if (string.IsNullOrWhiteSpace(line) || line == "--")
continue;
if (line.StartsWith("--"))
{
// Nettoyage du titre
title = Regex.Replace(line.Trim('-', ' '), "[^a-zA-Z0-9]", "_") ;
if (string.IsNullOrEmpty(title))
continue;
title += ".sql";
while (File.Exists(title))
{
File.Delete(title);
}
sw?.Dispose(); // Fermer le StreamWriter précédent, si il existe
sw = new StreamWriter(title);
}
if (!string.IsNullOrEmpty(title))
{
sw.WriteLine(line);
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
sw?.Dispose();
}