DbProviderFactories.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 static 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 Methods
  44. public static DbProviderFactory GetFactory (DataRow providerRow)
  45. {
  46. string assemblyType = (string) providerRow ["AssemblyQualifiedName"];
  47. #if TARGET_JVM // case insensitive GetType is not supported
  48. Type type = Type.GetType (assemblyType, false);
  49. #else
  50. Type type = Type.GetType (assemblyType, false, true);
  51. #endif
  52. if (type != null && type.IsSubclassOf (typeof (DbProviderFactory))) {
  53. // Provider factories are singletons with Instance field having
  54. // the sole instance
  55. FieldInfo field = type.GetField ("Instance", BindingFlags.Public |
  56. BindingFlags.Static);
  57. if (field != null) {
  58. return field.GetValue (null) as DbProviderFactory;
  59. }
  60. }
  61. throw new ConfigurationException("DataProvider is missing!");
  62. }
  63. public static DbProviderFactory GetFactory (string providerInvariantName)
  64. {
  65. DataTable table = GetFactoryClasses ();
  66. if (table != null) {
  67. DataRow row = table.Rows.Find (providerInvariantName);
  68. if (row != null)
  69. return GetFactory (row);
  70. }
  71. throw new ConfigurationException ("DataProvider is not found!");
  72. }
  73. public static DataTable GetFactoryClasses ()
  74. {
  75. DataSet ds = GetConfigEntries ();
  76. DataTable table = ds != null ? ds.Tables [CONFIG_SEC_TABLE_NAME] : null;
  77. if (table != null)
  78. table = table.Copy (); // avoid modifications by user
  79. return table;
  80. }
  81. #endregion // Methods
  82. #region Internal Methods
  83. internal static DataSet GetConfigEntries ()
  84. {
  85. if (configEntries != null)
  86. return configEntries as DataSet;
  87. DataSet ds = (DataSet) ConfigurationSettings.GetConfig (CONFIG_SECTION_NAME);
  88. Interlocked.CompareExchange (ref configEntries, ds, null);
  89. return configEntries as DataSet;
  90. }
  91. #endregion Internal Methods
  92. }
  93. }
  94. #endif // NET_2_0