MainViewModel.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Data.SQLite;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Threading.Tasks;
  9. using System.Windows.Threading;
  10. using CommunityToolkit.Mvvm.ComponentModel;
  11. using CommunityToolkit.Mvvm.Input;
  12. using Microsoft.Win32;
  13. namespace DbReload;
  14. public partial class MainViewModel : ObservableObject
  15. {
  16. [ObservableProperty] private string _sqLiteFile = "";
  17. [ObservableProperty] private string[] _logFiles = [];
  18. [ObservableProperty] private string _progress = "";
  19. [ObservableProperty] private List<string> _log = new();
  20. [RelayCommand]
  21. private void CreateSQLiteFile()
  22. {
  23. OpenFileDialog dlg = new OpenFileDialog();
  24. dlg.Filter = "SQLite Files (*.db,*.dbs)|*.db;*.dbs";
  25. dlg.CheckFileExists = false;
  26. if (dlg.ShowDialog() == true)
  27. SqLiteFile = dlg.FileName;
  28. }
  29. [RelayCommand]
  30. private void SelectLogFiles()
  31. {
  32. OpenFileDialog dlg = new OpenFileDialog();
  33. dlg.Filter = "SQL Log Files (*.sql)|*.sql";
  34. dlg.Multiselect = true;
  35. if (dlg.ShowDialog() == true)
  36. LogFiles = dlg.FileNames;
  37. }
  38. [RelayCommand]
  39. private async Task Go()
  40. {
  41. Progress = "Opening Database File";
  42. Log.Clear();
  43. var sb = new SQLiteConnectionStringBuilder();
  44. sb.DataSource = SqLiteFile;
  45. sb.Version = 3;
  46. sb.DateTimeFormat = SQLiteDateFormats.Ticks;
  47. sb.JournalMode = SQLiteJournalModeEnum.Wal;
  48. var conn = sb.ToString();
  49. using var connection = new SQLiteConnection(conn);
  50. connection.BusyTimeout = Convert.ToInt32(TimeSpan.FromMinutes(2).TotalMilliseconds);
  51. connection.Update += _connection_Update;
  52. connection.Trace += _connection_Trace;
  53. connection.Open();
  54. using var command = new SQLiteCommand(connection);
  55. var ordered = LogFiles.OrderBy(x => x);
  56. foreach (var logfile in ordered)
  57. {
  58. Progress = $"Opening {Path.GetFileName(logfile)}";
  59. var text = await File.ReadAllTextAsync(logfile).ConfigureAwait(false);
  60. var statements = SqlTokenizer.SplitStatements(text);
  61. int i = 0;
  62. foreach (var line in statements)
  63. {
  64. Progress = $"Processing {Path.GetFileName(logfile)} ({i}/{statements.Count})";
  65. command.CommandText = line;
  66. try
  67. {
  68. await command.ExecuteNonQueryAsync();
  69. }
  70. catch (Exception e)
  71. {
  72. Log.Add($"{Path.GetFileName(logfile)}: \"{e.Message}\" in \"{line}\"");
  73. Console.WriteLine(e);
  74. //throw;
  75. }
  76. i++;
  77. }
  78. }
  79. var logFile = Path.Combine(
  80. Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location) ?? "",
  81. "log.txt");
  82. await File.AppendAllLinesAsync(logFile, new string[] { "", $"Reload at {DateTime.Now:yyyy-MM-dd hh\\:mm\\:ss}" }.Concat(Log));
  83. Progress = "Done";
  84. }
  85. private void _connection_Update(object sender, UpdateEventArgs e)
  86. {
  87. }
  88. private void _connection_Trace(object sender, TraceEventArgs e)
  89. {
  90. Console.WriteLine(e.Statement);
  91. }
  92. }