ConnectionManager.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // ConnectionManager.cs - Singleton ConnectionManager class to manage
  2. // database connections for test cases.
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright Novell Inc., and the individuals listed on the
  8. // ChangeLog entries.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person
  12. // obtaining a copy of this software and associated documentation
  13. // files (the "Software"), to deal in the Software without
  14. // restriction, including without limitation the rights to use, copy,
  15. // modify, merge, publish, distribute, sublicense, and/or sell copies
  16. // of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. // SOFTWARE.
  30. using System;
  31. using System.Configuration;
  32. using System.Data;
  33. #if NET_2_0
  34. using System.Data.Common;
  35. #endif
  36. #if ONLY_1_1
  37. using Mono.Data;
  38. #endif
  39. namespace MonoTests.System.Data
  40. {
  41. public class ConnectionManager
  42. {
  43. private static ConnectionManager Instance;
  44. #if NET_2_0
  45. private DbConnection _connection;
  46. #else
  47. private IDbConnection _connection;
  48. #endif
  49. private string _connectionString;
  50. private EngineConfig _engine;
  51. static ConnectionManager ()
  52. {
  53. Instance = new ConnectionManager ();
  54. }
  55. private ConnectionManager ()
  56. {
  57. string connection_name = Environment.GetEnvironmentVariable ("PROVIDER_TESTS_CONNECTION");
  58. if (connection_name == null || connection_name.Length == 0)
  59. throw new ArgumentException ("PROVIDER_TESTS_CONNECTION environment variable is not set.");
  60. ConnectionConfig [] connections = (ConnectionConfig [])
  61. #if NET_2_0
  62. ConfigurationManager.GetSection ("providerTests");
  63. #else
  64. ConfigurationSettings.GetConfig ("providerTests");
  65. #endif
  66. foreach (ConnectionConfig connConfig in connections) {
  67. if (connConfig.Name != connection_name)
  68. continue;
  69. _connectionString = connConfig.ConnectionString;
  70. #if NET_2_0
  71. DbProviderFactory factory = DbProviderFactories.GetFactory (
  72. connConfig.Factory);
  73. _connection = factory.CreateConnection ();
  74. _connection.ConnectionString = _connectionString;
  75. #else
  76. _connection = ProviderFactory.CreateConnection (
  77. string.Concat ("factory=", connConfig.Factory,
  78. ";", _connectionString));
  79. #endif
  80. _connectionString = _connection.ConnectionString;
  81. _engine = connConfig.Engine;
  82. return;
  83. }
  84. throw new ArgumentException ("Connection '" + connection_name + "' not found.");
  85. }
  86. public static ConnectionManager Singleton {
  87. get {return Instance;}
  88. }
  89. public
  90. #if NET_2_0
  91. DbConnection
  92. #else
  93. IDbConnection
  94. #endif
  95. Connection {
  96. get {return _connection;}
  97. }
  98. public string ConnectionString {
  99. get {return _connectionString;}
  100. }
  101. internal EngineConfig Engine {
  102. get { return _engine; }
  103. }
  104. public void OpenConnection ()
  105. {
  106. if (!(_connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken))
  107. _connection.Close ();
  108. _connection.ConnectionString = _connectionString;
  109. _connection.Open ();
  110. }
  111. public void CloseConnection ()
  112. {
  113. if (_connection != null && _connection.State != ConnectionState.Closed)
  114. _connection.Close ();
  115. }
  116. }
  117. }