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.

167 lines
7.0 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.ComponentModel;
  25. using System.Data;
  26. using System.Data.Common;
  27. using System.Security;
  28. using System.Drawing;
  29. using System.Security.Permissions;
  30. using System.Transactions;
  31. namespace Externals.MySql.Data.MySqlClient
  32. {
  33. [DesignerCategory("Code")]
  34. [ToolboxItem(true)]
  35. internal sealed partial class MySqlConnection : DbConnection, ICloneable
  36. {
  37. /// <summary>
  38. /// Returns schema information for the data source of this <see cref="DbConnection"/>.
  39. /// </summary>
  40. /// <returns>A <see cref="DataTable"/> that contains schema information. </returns>
  41. public override DataTable GetSchema()
  42. {
  43. return GetSchema(null);
  44. }
  45. /// <summary>
  46. /// Returns schema information for the data source of this
  47. /// <see cref="DbConnection"/> using the specified string for the schema name.
  48. /// </summary>
  49. /// <param name="collectionName">Specifies the name of the schema to return. </param>
  50. /// <returns>A <see cref="DataTable"/> that contains schema information. </returns>
  51. public override DataTable GetSchema(string collectionName)
  52. {
  53. if (collectionName == null)
  54. collectionName = SchemaProvider.MetaCollection;
  55. return GetSchema(collectionName, null);
  56. }
  57. /// <summary>
  58. /// Returns schema information for the data source of this <see cref="DbConnection"/>
  59. /// using the specified string for the schema name and the specified string array
  60. /// for the restriction values.
  61. /// </summary>
  62. /// <param name="collectionName">Specifies the name of the schema to return.</param>
  63. /// <param name="restrictionValues">Specifies a set of restriction values for the requested schema.</param>
  64. /// <returns>A <see cref="DataTable"/> that contains schema information.</returns>
  65. public override DataTable GetSchema(string collectionName, string[] restrictionValues)
  66. {
  67. if (collectionName == null)
  68. collectionName = SchemaProvider.MetaCollection;
  69. string[] restrictions = _schemaProvider.CleanRestrictions(restrictionValues);
  70. MySqlSchemaCollection c = _schemaProvider.GetSchema(collectionName, restrictions);
  71. return c.AsDataTable();
  72. }
  73. /// <summary>
  74. /// Enlists in the specified transaction.
  75. /// </summary>
  76. /// <param name="transaction">
  77. /// A reference to an existing <see cref="System.Transactions.Transaction"/> in which to enlist.
  78. /// </param>
  79. public override void EnlistTransaction(Transaction transaction)
  80. {
  81. // enlisting in the null transaction is a noop
  82. if (transaction == null)
  83. return;
  84. // guard against trying to enlist in more than one transaction
  85. if (driver.currentTransaction != null)
  86. {
  87. if (driver.currentTransaction.BaseTransaction == transaction)
  88. return;
  89. Throw(new MySqlException("Already enlisted"));
  90. }
  91. // now see if we need to swap out drivers. We would need to do this since
  92. // we have to make sure all ops for a given transaction are done on the
  93. // same physical connection.
  94. Driver existingDriver = DriverTransactionManager.GetDriverInTransaction(transaction);
  95. if (existingDriver != null)
  96. {
  97. // we can't allow more than one driver to contribute to the same connection
  98. if (existingDriver.IsInActiveUse)
  99. Throw(new NotSupportedException(Resources.MultipleConnectionsInTransactionNotSupported));
  100. // there is an existing driver and it's not being currently used.
  101. // now we need to see if it is using the same connection string
  102. string text1 = existingDriver.Settings.ConnectionString;
  103. string text2 = Settings.ConnectionString;
  104. if (String.Compare(text1, text2, true) != 0)
  105. Throw(new NotSupportedException(Resources.MultipleConnectionsInTransactionNotSupported));
  106. // close existing driver
  107. // set this new driver as our existing driver
  108. CloseFully();
  109. driver = existingDriver;
  110. }
  111. if (driver.currentTransaction == null)
  112. {
  113. MySqlPromotableTransaction t = new MySqlPromotableTransaction(this, transaction);
  114. if (!transaction.EnlistPromotableSinglePhase(t))
  115. Throw(new NotSupportedException(Resources.DistributedTxnNotSupported));
  116. driver.currentTransaction = t;
  117. DriverTransactionManager.SetDriverInTransaction(driver);
  118. driver.IsInActiveUse = true;
  119. }
  120. }
  121. void AssertPermissions()
  122. {
  123. // Security Asserts can only be done when the assemblies
  124. // are put in the GAC as documented in
  125. // http://msdn.microsoft.com/en-us/library/ff648665.aspx
  126. if (this.Settings.IncludeSecurityAsserts)
  127. {
  128. PermissionSet set = new PermissionSet(PermissionState.None);
  129. set.AddPermission(new MySqlClientPermission(ConnectionString));
  130. set.Demand();
  131. MySqlSecurityPermission.CreatePermissionSet(true).Assert();
  132. }
  133. }
  134. /// <summary>
  135. /// Creates a new MySqlConnection object with the exact same ConnectionString value
  136. /// </summary>
  137. /// <returns>A cloned MySqlConnection object</returns>
  138. public object Clone()
  139. {
  140. MySqlConnection clone = new MySqlConnection();
  141. string connectionString = Settings.ConnectionString;
  142. if (connectionString != null)
  143. clone.ConnectionString = connectionString;
  144. return clone;
  145. }
  146. }
  147. }
  148. #endif