DatabaseManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 MECF.Framework.Common.ControlDataContext;
  14. using CyberX8_Core;
  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. private Dictionary<string,int> _sqlErrorDictionary= new Dictionary<string,int>();
  22. PostgresqlDB _db = new PostgresqlDB();
  23. DatabaseCleaner _cleaner = new DatabaseCleaner();
  24. public DataBaseStatus DataBaseStatus => _db.DBStatus;
  25. public void Initialize(string connectionString, string dbName, string sqlFile)
  26. {
  27. if (string.IsNullOrEmpty(connectionString))
  28. {
  29. throw new ApplicationException("数据库连接字段未设置");
  30. }
  31. PostgresqlHelper.ConnectionString = connectionString;
  32. if (!_db.Open(connectionString, dbName))
  33. {
  34. //LOG.Error("数据库连接失败");
  35. }
  36. else
  37. {
  38. PrepareDatabaseTable(sqlFile);
  39. }
  40. _cleaner.Initialize(dbName);
  41. _thread = new PeriodicJob(100, this.PeriodicRun, "DBJob", true);
  42. DB.Instance = this;
  43. DatabaseTable.UpgradeDataTable();
  44. if (UserDataRecorder.GetUserItems().Count == 0)
  45. {
  46. UserItem userItem = new UserItem() { Name = "admin", Role = Role.Manager.ToString(), Password = "admin", Notes = "System Default" };
  47. UserDataRecorder.InserUser(userItem);
  48. }
  49. }
  50. public void Terminate()
  51. {
  52. if (_thread != null)
  53. {
  54. _thread.Stop();
  55. _thread = null;
  56. }
  57. _cleaner.Terminate();
  58. _db.Close();
  59. }
  60. bool PeriodicRun()
  61. {
  62. if (!_db.ActiveConnection())
  63. return true;
  64. string sql;
  65. while (_sqlQueue.TryDequeue(out sql))
  66. {
  67. try
  68. {
  69. _db.ExecuteNonQuery(sql);
  70. }
  71. catch (Exception ex)
  72. {
  73. LOG.WriteDBExeption(string.Format("执行数据库操作错误, {0}", sql), ex);
  74. _db.Close();
  75. _db.Open();
  76. if(!_sqlErrorDictionary.ContainsKey(sql))
  77. {
  78. _sqlErrorDictionary[sql] = 0;
  79. }
  80. _sqlErrorDictionary[sql]++;
  81. if (_sqlErrorDictionary[sql] < 3)
  82. {
  83. _sqlQueue.Enqueue(sql);
  84. }
  85. else
  86. {
  87. _sqlErrorDictionary.Remove(sql);
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. public void Insert(string sql)
  94. {
  95. _sqlQueue.Enqueue(sql);
  96. }
  97. /// <summary>
  98. /// 同步插入
  99. /// </summary>
  100. /// <param name="sql"></param>
  101. /// <returns></returns>
  102. public int SyncInsert(string sql)
  103. {
  104. try
  105. {
  106. return _db.ExecuteNonQuery(sql);
  107. }
  108. catch(Exception ex)
  109. {
  110. LOG.WriteDBExeption(string.Format("执行数据库操作错误, {0}", sql), ex);
  111. //if (ex.Message.Contains("Connection"))
  112. _db.Close();
  113. _db.Open();
  114. return 0;
  115. }
  116. }
  117. /// <summary>
  118. /// 更新
  119. /// </summary>
  120. /// <param name="sql"></param>
  121. public void Update(string sql)
  122. {
  123. _sqlQueue.Enqueue(sql);
  124. }
  125. /// <summary>
  126. /// 更新
  127. /// </summary>
  128. /// <param name="sql"></param>
  129. /// <returns></returns>
  130. public int SyncUpdate(string sql)
  131. {
  132. try
  133. {
  134. return _db.ExecuteNonQuery(sql);
  135. }
  136. catch(Exception ex)
  137. {
  138. LOG.WriteDBExeption(string.Format("执行数据库操作错误, {0}", sql), ex);
  139. //if (ex.Message.Contains("Connection"))
  140. _db.Close();
  141. _db.Open();
  142. return 0;
  143. }
  144. }
  145. public void CreateTableIfNotExisted(string table, Dictionary<string, Type> columns, bool addPID, string primaryKey)
  146. {
  147. _db.CreateTableIfNotExisted(table, columns, addPID, primaryKey);
  148. }
  149. public void CreateTableIndexIfNotExisted(string table, string index, string sql)
  150. {
  151. _db.CreateTableIndexIfNotExisted(table, index, sql);
  152. }
  153. public void CreateTableColumn(string table, Dictionary<string, Type> columns)
  154. {
  155. _db.CreateTableColumn(table, columns);
  156. }
  157. public DataSet ExecuteDataset(string cmdText, params object[] p)
  158. {
  159. return _db.ExecuteDataset(cmdText, p);
  160. }
  161. void PrepareDatabaseTable(string sqlFile)
  162. {
  163. if (string.IsNullOrEmpty(sqlFile) || !File.Exists(sqlFile))
  164. {
  165. //LOG.Info("没有更新Sql数据库,文件:" + sqlFile);
  166. return;
  167. }
  168. try
  169. {
  170. using (StreamReader fs = new System.IO.StreamReader(sqlFile))
  171. {
  172. string sql = fs.ReadToEnd();
  173. _db.ExecuteNonQuery(sql);
  174. //LOG.Write("完成对数据库表格的更新操作");
  175. }
  176. }
  177. catch (Exception ex)
  178. {
  179. LOG.WriteExeption(ex);
  180. }
  181. }
  182. }
  183. }