DbConnection.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Collections.Concurrent;
  2. using System.Data;
  3. using System.Threading.Tasks;
  4. namespace appMpower.Data
  5. {
  6. public class DbConnection : IDbConnection
  7. {
  8. private string _connectionString;
  9. internal InternalConnection _internalConnection;
  10. public DbConnection()
  11. {
  12. _connectionString = DbProviderFactory.ConnectionString;
  13. }
  14. public DbConnection(string connectionString)
  15. {
  16. _connectionString = connectionString;
  17. }
  18. internal ConcurrentDictionary<string, DbCommand> DbCommands
  19. {
  20. get
  21. {
  22. return _internalConnection.DbCommands;
  23. }
  24. set
  25. {
  26. _internalConnection.DbCommands = value;
  27. }
  28. }
  29. public short Number
  30. {
  31. get
  32. {
  33. return _internalConnection.Number;
  34. }
  35. set
  36. {
  37. _internalConnection.Number = value;
  38. }
  39. }
  40. public IDbConnection Connection
  41. {
  42. get
  43. {
  44. return _internalConnection.DbConnection;
  45. }
  46. set
  47. {
  48. _internalConnection.DbConnection = value;
  49. }
  50. }
  51. public string ConnectionString
  52. {
  53. get
  54. {
  55. return _internalConnection.DbConnection.ConnectionString;
  56. }
  57. set
  58. {
  59. _internalConnection.DbConnection.ConnectionString = value;
  60. }
  61. }
  62. public int ConnectionTimeout
  63. {
  64. get
  65. {
  66. return _internalConnection.DbConnection.ConnectionTimeout;
  67. }
  68. }
  69. public string Database
  70. {
  71. get
  72. {
  73. return _internalConnection.DbConnection.Database;
  74. }
  75. }
  76. public ConnectionState State
  77. {
  78. get
  79. {
  80. if (_internalConnection is null) return ConnectionState.Closed;
  81. return _internalConnection.DbConnection.State;
  82. }
  83. }
  84. public IDbTransaction BeginTransaction()
  85. {
  86. return _internalConnection.DbConnection.BeginTransaction();
  87. }
  88. public IDbTransaction BeginTransaction(IsolationLevel il)
  89. {
  90. return _internalConnection.DbConnection.BeginTransaction(il);
  91. }
  92. public void ChangeDatabase(string databaseName)
  93. {
  94. _internalConnection.DbConnection.ChangeDatabase(databaseName);
  95. }
  96. public void Close()
  97. {
  98. _internalConnection.DbConnection.Close();
  99. }
  100. public async Task CloseAsync()
  101. {
  102. await (_internalConnection.DbConnection as System.Data.Common.DbConnection).CloseAsync();
  103. }
  104. public IDbCommand CreateCommand()
  105. {
  106. return _internalConnection.DbConnection.CreateCommand();
  107. }
  108. public void Open()
  109. {
  110. if (_internalConnection.DbConnection.State == ConnectionState.Closed)
  111. {
  112. _internalConnection.DbConnection.Open();
  113. }
  114. }
  115. public void Dispose()
  116. {
  117. #if ADO
  118. _internalConnection.DbConnection.Dispose();
  119. _internalConnection.Dispose();
  120. #else
  121. DbConnections.Release(_internalConnection);
  122. #endif
  123. }
  124. public async Task OpenAsync()
  125. {
  126. #if ADO && POSTGRESQL
  127. _internalConnection = new();
  128. _internalConnection.DbConnection = new Npgsql.NpgsqlConnection(_connectionString);
  129. #else
  130. if (_internalConnection is null)
  131. {
  132. _internalConnection = await DbConnections.GetConnection(_connectionString);
  133. }
  134. #endif
  135. if (_internalConnection.DbConnection.State == ConnectionState.Closed)
  136. {
  137. await (_internalConnection.DbConnection as System.Data.Common.DbConnection).OpenAsync();
  138. }
  139. }
  140. internal DbCommand GetCommand(string commandText, CommandType commandType, DbCommand dbCommand)
  141. {
  142. #if ADO
  143. dbCommand.Command = _internalConnection.DbConnection.CreateCommand();
  144. dbCommand.Command.CommandText = commandText;
  145. dbCommand.Command.CommandType = commandType;
  146. dbCommand.DbConnection = this;
  147. #else
  148. DbCommand internalCommand;
  149. if (_internalConnection.DbCommands.TryRemove(commandText, out internalCommand))
  150. {
  151. dbCommand.Command = internalCommand.Command;
  152. dbCommand.DbConnection = internalCommand.DbConnection;
  153. }
  154. else
  155. {
  156. dbCommand.Command = _internalConnection.DbConnection.CreateCommand();
  157. dbCommand.Command.CommandText = commandText;
  158. dbCommand.Command.CommandType = commandType;
  159. dbCommand.DbConnection = this;
  160. //For non odbc drivers like Npgsql which do not support Prepare
  161. dbCommand.Command.Prepare();
  162. //Console.WriteLine("prepare pool connection: " + this._internalConnection.Number + " for command " + _internalConnection.DbCommands.Count);
  163. }
  164. #endif
  165. return dbCommand;
  166. }
  167. public void ReleaseCommand(DbCommand dbCommand)
  168. {
  169. #if !ADO
  170. _internalConnection.DbCommands.TryAdd(dbCommand.CommandText, dbCommand);
  171. #endif
  172. }
  173. }
  174. }