DataBaseSwitch.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. namespace MinicsConsole.Helper;
  2. public class DataBaseSwitch(OrmCollections orms, HardWareMonitor hardWareMonitor, BasicInfo basicInfo)
  3. {
  4. public bool SwitchDataBaseLibrary(out string? newDBName)
  5. {
  6. newDBName = null;
  7. if (string.IsNullOrEmpty(basicInfo.DBConnectionString))
  8. return false;
  9. if (orms.MainORM is null)
  10. return false;
  11. List<UserInfo> oldusers = [];
  12. orms.MainORM.Query<UserInfo>("UserAuthority", oldusers.AddRange).Wait();
  13. int start = basicInfo.DBConnectionString.IndexOf('=') + 1;
  14. int end = basicInfo.DBConnectionString.IndexOf(';');
  15. string part1 = basicInfo.DBConnectionString[..start];
  16. string part2 = basicInfo.DBConnectionString[end..];
  17. newDBName = $"{DateTime.Now:yyyy_MM_dd_HH_mm_ss}";
  18. string newDBString = $"{part1}{newDBName}{part2}";
  19. IORM orm_new = new SqlSugarCustom();
  20. if (!orm_new.Initialize() || !orm_new.Open(newDBString, DbType.PostgreSQL))
  21. return false;
  22. basicInfo.DBConnectionString = newDBString;
  23. BaseConfigFileLoader.Save(basicInfo);
  24. orm_new.CreateTable<UserInfo>("UserAuthority");
  25. oldusers.ForEach(userInfo => orm_new.Insert("UserAuthority", userInfo));
  26. IORM orm_old = orms.MainORM;
  27. orms.MainORM = orm_new;
  28. orm_old.Dispose();
  29. hardWareMonitor.FullyReset();
  30. return true;
  31. }
  32. }