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.

130 lines
4.8 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.Collections.Generic;
  25. using System.Linq;
  26. using Externals.MySql.Data.MySqlClient;
  27. namespace Externals.MySql.Data.MySqlClient.Interceptors
  28. {
  29. /// <summary>
  30. /// BaseExceptionInterceptor is the base class that should be used for all userland
  31. /// exception interceptors.
  32. /// </summary>
  33. internal abstract class BaseExceptionInterceptor
  34. {
  35. /// <summary>
  36. /// Returns the received exception.
  37. /// </summary>
  38. /// <param name="exception">The exception to be returned.</param>
  39. /// <returns>The exception originally received.</returns>
  40. public abstract Exception InterceptException(Exception exception);
  41. /// <summary>
  42. /// Gets the active connection.
  43. /// </summary>
  44. protected MySqlConnection ActiveConnection { get; private set; }
  45. /// <summary>
  46. /// Initilizes this object by setting the active connection.
  47. /// </summary>
  48. /// <param name="connection">The connection to become active.</param>
  49. public virtual void Init(MySqlConnection connection)
  50. {
  51. ActiveConnection = connection;
  52. }
  53. }
  54. /// <summary>
  55. /// StandardExceptionInterceptor is the standard interceptor that simply returns the exception.
  56. /// It is the default action.
  57. /// </summary>
  58. internal sealed class StandardExceptionInterceptor : BaseExceptionInterceptor
  59. {
  60. /// <summary>
  61. /// Returns the received exception, which is the default action
  62. /// </summary>
  63. /// <param name="exception">The exception to be returned.</param>
  64. /// <returns>The exception originally received.</returns>
  65. public override Exception InterceptException(Exception exception)
  66. {
  67. return exception;
  68. }
  69. }
  70. /// <summary>
  71. /// ExceptionInterceptor is the "manager" class that keeps the list of registered interceptors
  72. /// for the given connection.
  73. /// </summary>
  74. internal sealed class ExceptionInterceptor : Interceptor
  75. {
  76. readonly List<BaseExceptionInterceptor> _interceptors = new List<BaseExceptionInterceptor>();
  77. public ExceptionInterceptor(MySqlConnection connection)
  78. {
  79. Connection = connection;
  80. LoadInterceptors(connection.Settings.ExceptionInterceptors);
  81. // we always have the standard interceptor
  82. _interceptors.Add(new StandardExceptionInterceptor());
  83. }
  84. protected override void AddInterceptor(object o)
  85. {
  86. if (o == null)
  87. throw new ArgumentException("Unable to instantiate ExceptionInterceptor");
  88. if (!(o is BaseExceptionInterceptor))
  89. throw new InvalidOperationException(String.Format(Resources.TypeIsNotExceptionInterceptor,
  90. o.GetType()));
  91. BaseExceptionInterceptor ie = o as BaseExceptionInterceptor;
  92. ie.Init(Connection);
  93. _interceptors.Insert(0, (BaseExceptionInterceptor)o);
  94. }
  95. public void Throw(Exception exception)
  96. {
  97. Exception e = _interceptors.Aggregate(exception, (current, ie) => ie.InterceptException(current));
  98. throw e;
  99. }
  100. #if !NETSTANDARD1_3
  101. protected override string ResolveType(string nameOrType)
  102. {
  103. if (MySqlConfiguration.Settings == null || MySqlConfiguration.Settings.ExceptionInterceptors == null)
  104. return base.ResolveType(nameOrType);
  105. foreach (InterceptorConfigurationElement e in MySqlConfiguration.Settings.ExceptionInterceptors)
  106. if (String.Compare(e.Name, nameOrType, true) == 0)
  107. return e.Type;
  108. return base.ResolveType(nameOrType);
  109. }
  110. #endif
  111. }
  112. }
  113. #endif