You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
6.0 KiB

4 years ago
  1. // Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
  2. //
  3. // MySQL Connector/NET is licensed under the terms of the GPLv2
  4. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  5. // MySQL Connectors. There are special exceptions to the terms and
  6. // conditions of the GPLv2 as it is applied to this software, see the
  7. // FLOSS License Exception
  8. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published
  12. // by the Free Software Foundation; version 2 of the License.
  13. //
  14. // This program is distributed in the hope that it will be useful, but
  15. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  17. // for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License along
  20. // with this program; if not, write to the Free Software Foundation, Inc.,
  21. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. #if MYSQL_6_9
  23. using System.Data.Common;
  24. using System;
  25. using System.Reflection;
  26. using System.Security.Permissions;
  27. using System.Security;
  28. namespace Externals.MySql.Data.MySqlClient
  29. {
  30. /// <summary>
  31. /// DBProviderFactory implementation for MysqlClient.
  32. /// </summary>
  33. [ReflectionPermission(SecurityAction.Assert, MemberAccess = true)]
  34. internal sealed class MySqlClientFactory : DbProviderFactory, IServiceProvider
  35. {
  36. /// <summary>
  37. /// Gets an instance of the <see cref="MySqlClientFactory"/>.
  38. /// This can be used to retrieve strongly typed data objects.
  39. /// </summary>
  40. public static MySqlClientFactory Instance = new MySqlClientFactory();
  41. private Type dbServicesType;
  42. private FieldInfo mySqlDbProviderServicesInstance;
  43. /// <summary>
  44. /// Returns a strongly typed <see cref="DbCommandBuilder"/> instance.
  45. /// </summary>
  46. /// <returns>A new strongly typed instance of <b>DbCommandBuilder</b>.</returns>
  47. public override DbCommandBuilder CreateCommandBuilder()
  48. {
  49. return new MySqlCommandBuilder();
  50. }
  51. /// <summary>
  52. /// Returns a strongly typed <see cref="DbCommand"/> instance.
  53. /// </summary>
  54. /// <returns>A new strongly typed instance of <b>DbCommand</b>.</returns>
  55. public override DbCommand CreateCommand()
  56. {
  57. return new MySqlCommand();
  58. }
  59. /// <summary>
  60. /// Returns a strongly typed <see cref="DbConnection"/> instance.
  61. /// </summary>
  62. /// <returns>A new strongly typed instance of <b>DbConnection</b>.</returns>
  63. public override DbConnection CreateConnection()
  64. {
  65. return new MySqlConnection();
  66. }
  67. /// <summary>
  68. /// Returns a strongly typed <see cref="DbDataAdapter"/> instance.
  69. /// </summary>
  70. /// <returns>A new strongly typed instance of <b>DbDataAdapter</b>. </returns>
  71. public override DbDataAdapter CreateDataAdapter()
  72. {
  73. return new MySqlDataAdapter();
  74. }
  75. /// <summary>
  76. /// Returns a strongly typed <see cref="DbParameter"/> instance.
  77. /// </summary>
  78. /// <returns>A new strongly typed instance of <b>DbParameter</b>.</returns>
  79. public override DbParameter CreateParameter()
  80. {
  81. return new MySqlParameter();
  82. }
  83. /// <summary>
  84. /// Returns a strongly typed <see cref="DbConnectionStringBuilder"/> instance.
  85. /// </summary>
  86. /// <returns>A new strongly typed instance of <b>DbConnectionStringBuilder</b>.</returns>
  87. public override DbConnectionStringBuilder CreateConnectionStringBuilder()
  88. {
  89. return new MySqlConnectionStringBuilder();
  90. }
  91. /// <summary>
  92. /// Returns true if a <b>MySqlDataSourceEnumerator</b> can be created;
  93. /// otherwise false.
  94. /// </summary>
  95. public override bool CanCreateDataSourceEnumerator
  96. {
  97. get { return false; }
  98. }
  99. #region IServiceProvider Members
  100. /// <summary>
  101. /// Provide a simple caching layer
  102. /// </summary>
  103. private Type DbServicesType
  104. {
  105. get
  106. {
  107. if (dbServicesType == null)
  108. {
  109. // Get the type this way so we don't have to reference System.Data.Entity
  110. // from our core provider
  111. dbServicesType = Type.GetType(
  112. @"System.Data.Common.DbProviderServices, System.Data.Entity,
  113. Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
  114. false);
  115. }
  116. return dbServicesType;
  117. }
  118. }
  119. private FieldInfo MySqlDbProviderServicesInstance
  120. {
  121. get
  122. {
  123. if (mySqlDbProviderServicesInstance == null)
  124. {
  125. string fullName = Assembly.GetExecutingAssembly().FullName;
  126. string assemblyName = fullName.Replace("Externals.MySql.Data", "Externals.MySql.Data.Entity");
  127. string assemblyEf5Name = fullName.Replace("Externals.MySql.Data", "Externals.MySql.Data.Entity.EF5");
  128. fullName = String.Format("Externals.MySql.Data.MySqlClient.MySqlProviderServices, {0}", assemblyEf5Name);
  129. Type providerServicesType = Type.GetType(fullName, false);
  130. if (providerServicesType == null)
  131. {
  132. fullName = String.Format("Externals.MySql.Data.MySqlClient.MySqlProviderServices, {0}", assemblyName);
  133. providerServicesType = Type.GetType(fullName, false);
  134. if (providerServicesType == null)
  135. throw new DllNotFoundException(fullName);
  136. }
  137. mySqlDbProviderServicesInstance = providerServicesType.GetField("Instance",
  138. BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
  139. }
  140. return mySqlDbProviderServicesInstance;
  141. }
  142. }
  143. object IServiceProvider.GetService(Type serviceType)
  144. {
  145. // DbProviderServices is the only service we offer up right now
  146. if (serviceType != DbServicesType) return null;
  147. if (MySqlDbProviderServicesInstance == null) return null;
  148. return MySqlDbProviderServicesInstance.GetValue(null);
  149. }
  150. #endregion
  151. }
  152. }
  153. #endif