ConnectionHelper.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using DevExpress.Xpo;
  2. using DevExpress.Xpo.DB;
  3. using DevExpress.Xpo.Metadata;
  4. using Ips.Spd.SysSettings;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace Ips.Sps
  12. {
  13. public static class ConnectionHelper
  14. {
  15. static readonly Type[] PersistentTypes = new Type[]{
  16. typeof(Adcs.AdCard),
  17. typeof(Adcs.AdChannel),
  18. typeof(Ants.Ant),
  19. typeof(Emts.Emt),
  20. typeof(Emts.EmtSample),
  21. typeof(Ephs.Eph),
  22. typeof(Ephs.EphHigh),
  23. typeof(Sats.Sat),
  24. typeof(Sigs.Signal),
  25. typeof(TskResults.Pases.Pas),
  26. typeof(TskResults.Peses.Pes),
  27. typeof(TskResults.Peses.PesXgf),
  28. typeof(TskResults.Peses.PesRef),
  29. typeof(TskResults.Poses.Pos),
  30. typeof(TskResults.Poses.PosRel),
  31. typeof(TskResults.Stses.Tst),
  32. typeof(Tsks.Tsk),
  33. typeof(Tsks.TskAdCard),
  34. typeof(Tsks.TskAdChannel),
  35. typeof(Tsks.TskSignal),
  36. typeof(TskStrategys.TskStrategy),
  37. typeof(SysSetting)
  38. };
  39. public static string connectionString;
  40. public static void Connect(string conString, bool threadSafe = true)
  41. {
  42. XPBaseObject.AutoSaveOnEndEdit = false;
  43. //XpoDefault.NullableBehavior = NullableBehavior.ByUnderlyingType;
  44. connectionString = conString;
  45. XpoDefault.DataLayer = CreateDataLayer(threadSafe);
  46. }
  47. static IDataLayer CreateDataLayer(bool threadSafe)
  48. {
  49. ReflectionDictionary dictionary = new ReflectionDictionary();
  50. dictionary.GetDataStoreSchema(PersistentTypes);
  51. AutoCreateOption autoCreateOption = AutoCreateOption.DatabaseAndSchema;
  52. IDataStore provider = XpoDefault.GetConnectionProvider(connectionString, autoCreateOption);
  53. return threadSafe ? new ThreadSafeDataLayer(dictionary, provider) : new SimpleDataLayer(dictionary, provider);
  54. }
  55. public static string GetMigrationSql()
  56. {
  57. IDataStore provider = XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.DatabaseAndSchema);
  58. var migrationProvider = (IDataStoreSchemaMigrationProvider)provider;
  59. var migrationScriptFormatter = (IUpdateSchemaSqlFormatter)provider;
  60. var dictionary = new ReflectionDictionary();
  61. DBTable[] targetSchema = dictionary.GetDataStoreSchema(PersistentTypes);
  62. var migrationOptions = new SchemaMigrationOptions();
  63. var updateSchemaStatements = migrationProvider.CompareSchema(targetSchema, migrationOptions);
  64. string sql = migrationScriptFormatter.FormatUpdateSchemaScript(updateSchemaStatements);
  65. return sql;
  66. }
  67. }
  68. }