DbProviderFactories.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // System.Data.Common.DbProviderFactories.cs
  3. //
  4. // Author:
  5. // Sureshkumar T ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2003
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. #if NET_2_0
  33. using System.Threading;
  34. using System.Reflection;
  35. using System.Collections;
  36. using System.Configuration;
  37. namespace System.Data.Common {
  38. public sealed class DbProviderFactories
  39. {
  40. private static object configEntries = null; // DataSet
  41. private const string CONFIG_SECTION_NAME = "system.data";
  42. private const string CONFIG_SEC_TABLE_NAME = "DbProviderFactories";
  43. #region Constructors
  44. private DbProviderFactories ()
  45. {
  46. }
  47. #endregion Constructors
  48. #region Methods
  49. public static DbProviderFactory GetFactory (DataRow providerRow)
  50. {
  51. string assemblyType = (string) providerRow ["AssemblyQualifiedName"];
  52. Type type = Type.GetType (assemblyType, false, true);
  53. if (type != null && type.IsSubclassOf (typeof (DbProviderFactory))) {
  54. // Provider factories are singletons with Instance field having
  55. // the sole instance
  56. FieldInfo field = type.GetField ("Instance", BindingFlags.Public |
  57. BindingFlags.Static);
  58. if (field != null) {
  59. return field.GetValue (null) as DbProviderFactory;
  60. }
  61. }
  62. throw new ConfigurationException("DataProvider is missing!");
  63. }
  64. public static DbProviderFactory GetFactory (string providerInvariantName)
  65. {
  66. DataTable table = GetFactoryClasses ();
  67. if (table != null) {
  68. DataRow row = table.Rows.Find (providerInvariantName);
  69. if (row != null)
  70. return GetFactory (row);
  71. }
  72. throw new ConfigurationException ("DataProvider is not found!");
  73. }
  74. public static DataTable GetFactoryClasses ()
  75. {
  76. DataSet ds = GetConfigEntries ();
  77. DataTable table = ds != null ? ds.Tables [CONFIG_SEC_TABLE_NAME] : null;
  78. if (table != null)
  79. table = table.Copy (); // avoid modifications by user
  80. return table;
  81. }
  82. #endregion // Methods
  83. #region Internal Methods
  84. internal static DataSet GetConfigEntries ()
  85. {
  86. if (configEntries != null)
  87. return configEntries as DataSet;
  88. DataSet ds = (DataSet) ConfigurationSettings.GetConfig (CONFIG_SECTION_NAME);
  89. Interlocked.CompareExchange (ref configEntries, ds, null);
  90. return configEntries as DataSet;
  91. }
  92. #endregion Internal Methods
  93. }
  94. }
  95. #endif // NET_2_0