DumpViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using DBBackupTool.Data;
  4. using DBBackupTool.Helpers;
  5. using Microsoft.Win32;
  6. using SqlSugar;
  7. using System.Collections.ObjectModel;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Windows;
  11. namespace DBBackupTool.ViewModels;
  12. public partial class DumpViewModel : ObservableObject
  13. {
  14. public DumpViewModel()
  15. {
  16. this.DbInfo = new()
  17. {
  18. ServerAddress = "localhost",
  19. Port = 5432,
  20. User = "postgres",
  21. PassWord = "123456"
  22. };
  23. this.LoadingVis = Visibility.Collapsed;
  24. }
  25. private SqlSugarHelper? _dbHelper;
  26. [ObservableProperty]
  27. private Visibility _LoadingVis;
  28. [RelayCommand]
  29. private void CheckDB()
  30. {
  31. this.Tables ??= [];
  32. this.Tables.Clear();
  33. this.TableCount = 0;
  34. if (!CheckDBInfo())
  35. {
  36. MessageBox.Show("数据库信息不完整", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  37. return;
  38. }
  39. _dbHelper = new();
  40. string dbString = $"Database={DbInfo.DataBase};Password={DbInfo.PassWord};Host={DbInfo.ServerAddress};Username={DbInfo.User};Persist Security Info=True";
  41. if (!_dbHelper.Open(dbString, SqlSugar.DbType.PostgreSQL, true) || _dbHelper.Client is null)
  42. {
  43. MessageBox.Show("数据库连接失败", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  44. return;
  45. }
  46. IEnumerable<DbTableInfo> tables;
  47. try
  48. {
  49. tables = _dbHelper.Client.DbMaintenance.GetTableInfoList();
  50. }
  51. catch
  52. {
  53. MessageBox.Show("数据库不存在", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  54. return;
  55. }
  56. this.Tables ??= [];
  57. this.Tables.Clear();
  58. foreach (DbTableInfo item in tables)
  59. this.Tables.Add(item);
  60. this.TableCount = this.Tables.Count;
  61. MessageBox.Show("数据库连接成功", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  62. }
  63. [ObservableProperty]
  64. private DBInfo _dbInfo;
  65. [ObservableProperty]
  66. private ObservableCollection<DbTableInfo> _Tables = [];
  67. [ObservableProperty]
  68. private int _TableCount;
  69. [ObservableProperty]
  70. private string? _SavePath;
  71. [RelayCommand]
  72. private void Dump()
  73. {
  74. if (!CheckDBInfo())
  75. {
  76. MessageBox.Show("数据库信息不完整", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  77. return;
  78. }
  79. if (string.IsNullOrEmpty(this.SavePath))
  80. {
  81. MessageBox.Show("选择保存路径", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  82. return;
  83. }
  84. PostgreSQLTool tool = new();
  85. if (!tool.Initialize())
  86. {
  87. MessageBox.Show("PostgresSQL 未安装", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  88. return;
  89. }
  90. this.LoadingVis = Visibility.Visible;
  91. Task.Factory.StartNew(() =>
  92. {
  93. Stopwatch sw = new();
  94. sw.Start();
  95. tool.BackUp(this.DbInfo.ServerAddress!, this.DbInfo.Port, this.DbInfo.User!, this.DbInfo.PassWord!, this.SavePath, this.DbInfo.DataBase!);
  96. if (!File.Exists(this.SavePath))
  97. {
  98. MessageBox.Show("备份失败", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  99. return;
  100. }
  101. FileInfo file = new(this.SavePath);
  102. if (file.Directory is null)
  103. return;
  104. sw.Stop();
  105. MessageBox.Show($"备份完成,耗时 {sw.Elapsed.Seconds}s", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  106. App.Current.Dispatcher.Invoke(() =>
  107. {
  108. this.LoadingVis = Visibility.Collapsed;
  109. });
  110. System.Diagnostics.Process.Start("Explorer.exe", file.Directory.FullName);
  111. });
  112. }
  113. [RelayCommand]
  114. private void SelectPath()
  115. {
  116. SaveFileDialog saveFileDialog = new()
  117. {
  118. Filter = "DumpFile|*.Custom;",
  119. InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
  120. FileName = $"{this.DbInfo.DataBase}_{DateTime.Now:yyMMdd_HHmmss}.Custom"
  121. };
  122. if (saveFileDialog.ShowDialog() != true)
  123. return;
  124. if (!saveFileDialog.FileName.EndsWith(".Custom"))
  125. saveFileDialog.FileName += ".Custom";
  126. SavePath = saveFileDialog.FileName; //获取选择的文件,或者自定义的文件名的全路径。
  127. }
  128. private bool CheckDBInfo()
  129. {
  130. if (this.DbInfo is null)
  131. return false;
  132. if (string.IsNullOrEmpty(DbInfo.ServerAddress))
  133. return false;
  134. if (string.IsNullOrEmpty(DbInfo.DataBase))
  135. return false;
  136. if (string.IsNullOrEmpty(DbInfo.User))
  137. return false;
  138. if (string.IsNullOrEmpty(DbInfo.PassWord))
  139. return false;
  140. return true;
  141. }
  142. }