ConnectionManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. using System.Data.Common;
  34. namespace MonoTests.System.Data
  35. {
  36. public class ConnectionManager
  37. {
  38. private static ConnectionManager Instance;
  39. private DbConnection _connection;
  40. private string _connectionString;
  41. private EngineConfig _engine;
  42. static ConnectionManager ()
  43. {
  44. Instance = new ConnectionManager ();
  45. }
  46. private ConnectionManager ()
  47. {
  48. string connection_name = Environment.GetEnvironmentVariable ("PROVIDER_TESTS_CONNECTION");
  49. if (connection_name == null || connection_name.Length == 0)
  50. throw new ArgumentException ("PROVIDER_TESTS_CONNECTION environment variable is not set.");
  51. ConnectionConfig [] connections = (ConnectionConfig [])
  52. ConfigurationManager.GetSection ("providerTests");
  53. foreach (ConnectionConfig connConfig in connections) {
  54. if (connConfig.Name != connection_name)
  55. continue;
  56. _connectionString = connConfig.ConnectionString;
  57. DbProviderFactory factory = DbProviderFactories.GetFactory (
  58. connConfig.Factory);
  59. _connection = factory.CreateConnection ();
  60. _connection.ConnectionString = _connectionString;
  61. _connectionString = _connection.ConnectionString;
  62. _engine = connConfig.Engine;
  63. return;
  64. }
  65. throw new ArgumentException ("Connection '" + connection_name + "' not found.");
  66. }
  67. public static ConnectionManager Singleton {
  68. get {return Instance;}
  69. }
  70. public
  71. DbConnection
  72. Connection {
  73. get {return _connection;}
  74. }
  75. public string ConnectionString {
  76. get {return _connectionString;}
  77. }
  78. internal EngineConfig Engine {
  79. get { return _engine; }
  80. }
  81. public void OpenConnection ()
  82. {
  83. if (!(_connection.State == ConnectionState.Closed || _connection.State == ConnectionState.Broken))
  84. _connection.Close ();
  85. _connection.ConnectionString = _connectionString;
  86. _connection.Open ();
  87. }
  88. public void CloseConnection ()
  89. {
  90. if (_connection != null && _connection.State != ConnectionState.Closed)
  91. _connection.Close ();
  92. }
  93. }
  94. }