DatabaseManager.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. using Venus_Core;
  14. using MECF.Framework.Common.ControlDataContext;
  15. namespace Aitex.Core.RT.DBCore
  16. {
  17. public class DatabaseManager : ICommonDB
  18. {
  19. PeriodicJob _thread;
  20. FixSizeQueue<string> _sqlQueue = new FixSizeQueue<string>(1000);
  21. PostgresqlDB _db = new PostgresqlDB();
  22. DatabaseCleaner _cleaner = new DatabaseCleaner();
  23. public DataBaseStatus DataBaseStatus => _db.DBStatus;
  24. public void Initialize(string connectionString, string dbName, string sqlFile)
  25. {
  26. if (string.IsNullOrEmpty(connectionString))
  27. {
  28. throw new ApplicationException("数据库连接字段未设置");
  29. }
  30. PostgresqlHelper.ConnectionString = connectionString;
  31. if (!_db.Open(connectionString, dbName))
  32. {
  33. //LOG.Error("数据库连接失败");
  34. }
  35. else
  36. {
  37. PrepareDatabaseTable(sqlFile);
  38. }
  39. _cleaner.Initialize(dbName);
  40. _thread = new PeriodicJob(100, this.PeriodicRun, "DBJob", true);
  41. DB.Instance = this;
  42. DatabaseTable.UpgradeDataTable();
  43. if (UserDataRecorder.GetUserItems().Count == 0)
  44. {
  45. UserItem userItem = new UserItem() { Name = "admin", Role = Role.Manager.ToString(), Password = "admin", Notes = "System Default" };
  46. UserDataRecorder.InserUser(userItem);
  47. }
  48. }
  49. public void Terminate()
  50. {
  51. if (_thread != null)
  52. {
  53. _thread.Stop();
  54. _thread = null;
  55. }
  56. _cleaner.Terminate();
  57. _db.Close();
  58. }
  59. bool PeriodicRun()
  60. {
  61. if (!_db.ActiveConnection())
  62. return true;
  63. string sql;
  64. while (_sqlQueue.TryDequeue(out sql))
  65. {
  66. try
  67. {
  68. _db.ExecuteNonQuery(sql);
  69. }
  70. catch (Exception ex)
  71. {
  72. LOG.WriteExeption(string.Format("执行数据库操作错误, {0}", sql), ex);
  73. }
  74. }
  75. return true;
  76. }
  77. public void Insert(string sql)
  78. {
  79. _sqlQueue.Enqueue(sql);
  80. }
  81. public void CreateTableIfNotExisted(string table, Dictionary<string, Type> columns, bool addPID, string primaryKey)
  82. {
  83. _db.CreateTableIfNotExisted(table, columns, addPID, primaryKey);
  84. }
  85. public void CreateTableIndexIfNotExisted(string table, string index, string sql)
  86. {
  87. _db.CreateTableIndexIfNotExisted(table, index, sql);
  88. }
  89. public void CreateTableColumn(string table, Dictionary<string, Type> columns)
  90. {
  91. _db.CreateTableColumn(table, columns);
  92. }
  93. public DataSet ExecuteDataset(string cmdText, params object[] p)
  94. {
  95. return _db.ExecuteDataset(cmdText, p);
  96. }
  97. public int GetAllCount(string cmdText, params object[] p)
  98. {
  99. return _db.GetAllCount(cmdText, p);
  100. }
  101. void PrepareDatabaseTable(string sqlFile)
  102. {
  103. if (string.IsNullOrEmpty(sqlFile) || !File.Exists(sqlFile))
  104. {
  105. //LOG.Info("没有更新Sql数据库,文件:" + sqlFile);
  106. return;
  107. }
  108. try
  109. {
  110. using (StreamReader fs = new System.IO.StreamReader(sqlFile))
  111. {
  112. string sql = fs.ReadToEnd();
  113. _db.ExecuteNonQuery(sql);
  114. //LOG.Write("完成对数据库表格的更新操作");
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. LOG.WriteExeption(ex);
  120. }
  121. }
  122. }
  123. }