ReferenceHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.SQLite;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using NLog;
  13. namespace DTHelperStd
  14. {
  15. public static class ReferenceHelper
  16. {
  17. public static Dictionary<string, HashSet<string>> ReferenceDic { get; } = new Dictionary<string, HashSet<string>>();
  18. private static Logger Logger = LogManager.GetCurrentClassLogger();
  19. private const string DATABASE_PATH = @"Resources\lookups.db";
  20. private const string PUBLISH_PATH = @"resources\app\bin\Resources\lookups.db";
  21. private const string PUBLISH_MAC_PATH = @"Resources/lookups.db";
  22. private const string VS_PATH = @"..\..\bin\Resources\lookups.db";
  23. public static string GetDBPath()
  24. {
  25. /*
  26. * If publish to Window, use <electronAppPath + PUBLISH_PATH>
  27. * else if publish to MAC, use <electronAppPath + PUBLISH_MAC_PATH>
  28. * else if running electron in VS, use <VS_PATH>
  29. */
  30. var databasePath = string.Empty;
  31. #if ELECTRON
  32. databasePath = (Environment.CommandLine.IndexOf("electron") > -1) ? PUBLISH_PATH : DATABASE_PATH;
  33. Logger.Trace("ELECTRON is defined");
  34. #elif MACOSX
  35. databasePath = Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), PUBLISH_MAC_PATH);
  36. Logger.Trace("MacOSX is defined");
  37. #else
  38. databasePath = (Environment.CommandLine.IndexOf("electron") > -1) ? VS_PATH : DATABASE_PATH;
  39. Logger.Trace("RELEASE/DEBUG is defined");
  40. #endif
  41. return databasePath;
  42. }
  43. public static void InitializeReferenceDict()
  44. {
  45. var tables = GetTables();
  46. var connectString = new SQLiteConnectionStringBuilder
  47. {
  48. Version = 3,
  49. DataSource = GetDBPath()
  50. };
  51. using (var conn = new SQLiteConnection(connectString.ToString()))
  52. {
  53. conn.Open();
  54. foreach (var tableName in tables)
  55. {
  56. using (SQLiteCommand command = conn.CreateCommand())
  57. {
  58. var tempSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  59. command.CommandText = "SELECT * FROM " + tableName;
  60. var reader = command.ExecuteReader();
  61. while (reader.Read())
  62. {
  63. tempSet.Add(reader[0].ToString());
  64. }
  65. ReferenceDic.Add(tableName, tempSet);
  66. }
  67. }
  68. }
  69. }
  70. private static List<string> GetTables()
  71. {
  72. var tables = new List<string>();
  73. var connectString = new SQLiteConnectionStringBuilder
  74. {
  75. Version = 3,
  76. DataSource = GetDBPath()
  77. };
  78. try
  79. {
  80. using (var conn = new SQLiteConnection(connectString.ToString()))
  81. {
  82. conn.Open();
  83. using (SQLiteCommand command = conn.CreateCommand())
  84. {
  85. command.CommandText = $"SELECT name FROM sqlite_master WHERE type='table' ORDER BY 1";
  86. var reader = command.ExecuteReader();
  87. while (reader.Read())
  88. {
  89. tables.Add(reader[0].ToString());
  90. }
  91. }
  92. }
  93. }catch(Exception e)
  94. {
  95. Logger.Info($"connectString datasource path: {connectString.DataSource}");
  96. Logger.Info($"Current working dir: {Environment.CurrentDirectory} / App path: {System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}");
  97. Logger.Fatal(e,$"Cannot find database file");
  98. }
  99. return tables;
  100. }
  101. }
  102. }