DatabaseManager.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.Util;
  6. using Aitex.Core.Utilities;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using System.Data;
  10. using System.IO;
  11. using Aitex.Core.RT.OperationCenter;
  12. using MECF.Framework.Common.DBCore;
  13. namespace Aitex.Core.RT.DBCore
  14. {
  15. public class DatabaseManager : ICommonDB
  16. {
  17. PeriodicJob _thread;
  18. FixSizeQueue<string> _sqlQueue = new FixSizeQueue<string>(1500);
  19. PostgresqlDB _db = new PostgresqlDB();
  20. DatabaseCleaner _cleaner = new DatabaseCleaner();
  21. private string _dbName;
  22. public void Initialize(string connectionString, string dbName, string sqlFile)
  23. {
  24. if (string.IsNullOrEmpty(connectionString))
  25. {
  26. throw new ApplicationException("数据库连接字段未设置");
  27. }
  28. _dbName = dbName;
  29. PostgresqlHelper.ConnectionString = connectionString;
  30. if (!_db.Open(connectionString, dbName))
  31. {
  32. LOG.Error("数据库连接失败");
  33. }
  34. else
  35. {
  36. PrepareDatabaseTable(sqlFile);
  37. }
  38. _thread = new PeriodicJob(100, this.PeriodicRun, "DBJob", true);
  39. DB.Instance = this;
  40. DatabaseTable.UpgradeDataTable();
  41. StartDataCleaner();
  42. }
  43. public void StartDataCleaner()
  44. {
  45. _cleaner.Initialize(_dbName);
  46. }
  47. public void Terminate()
  48. {
  49. if (_thread != null)
  50. {
  51. _thread.Stop();
  52. _thread = null;
  53. }
  54. _cleaner.Terminate();
  55. _db.Close();
  56. }
  57. bool PeriodicRun()
  58. {
  59. if (!_db.ActiveConnection())
  60. return true;
  61. string sql;
  62. while (_sqlQueue.TryDequeue(out sql))
  63. {
  64. try
  65. {
  66. _db.ExecuteNonQuery(sql);
  67. }
  68. catch (Exception ex)
  69. {
  70. LOG.Error(string.Format("DB operation error, {0}, {1}", ex.Message, sql));
  71. }
  72. }
  73. return true;
  74. }
  75. public void Insert(string sql)
  76. {
  77. _sqlQueue.Enqueue(sql);
  78. }
  79. public void CreateTableIfNotExisted(string table, Dictionary<string, Type> columns, bool addPID, string primaryKey)
  80. {
  81. _db.CreateTableIfNotExisted(table, columns, addPID, primaryKey);
  82. }
  83. public void CreateTableIndexIfNotExisted(string table, string index, string sql)
  84. {
  85. _db.CreateTableIndexIfNotExisted(table, index, sql);
  86. }
  87. public void CreateTableColumn(string table, Dictionary<string, Type> columns)
  88. {
  89. _db.CreateTableColumn(table, columns);
  90. }
  91. public DataSet ExecuteDataset(string cmdText, params object[] p)
  92. {
  93. return _db.ExecuteDataset(cmdText, p);
  94. }
  95. public int GetCount(string cmdText, params object[] p)
  96. {
  97. return _db.GetCount(cmdText, p);
  98. }
  99. void PrepareDatabaseTable(string sqlFile)
  100. {
  101. if (string.IsNullOrEmpty(sqlFile) || !File.Exists(sqlFile))
  102. {
  103. LOG.Info("没有更新Sql数据库,文件:" + sqlFile);
  104. return;
  105. }
  106. try
  107. {
  108. using (StreamReader fs = new System.IO.StreamReader(sqlFile))
  109. {
  110. string sql = fs.ReadToEnd();
  111. _db.ExecuteNonQuery(sql);
  112. LOG.Write($"Database updated by sql file {sqlFile}");
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. LOG.Write(ex);
  118. }
  119. }
  120. }
  121. }