DatabaseManager.cs 3.6 KB

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