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