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.

156 lines
6.1 KiB

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