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.

117 lines
3.8 KiB

4 years ago
  1. #if MYSQL_6_9
  2. // Copyright © 2004, 2011, 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.Text;
  26. using Externals.MySql.Data.MySqlClient.Properties;
  27. using System.Reflection;
  28. namespace Externals.MySql.Data.MySqlClient
  29. {
  30. /// <summary>
  31. /// BaseExceptionInterceptor is the base class that should be used for all userland
  32. /// exception interceptors
  33. /// </summary>
  34. internal abstract class BaseExceptionInterceptor
  35. {
  36. public abstract Exception InterceptException(Exception exception);
  37. protected MySqlConnection ActiveConnection { get; private set; }
  38. public virtual void Init(MySqlConnection connection)
  39. {
  40. ActiveConnection = connection;
  41. }
  42. }
  43. /// <summary>
  44. /// StandardExceptionInterceptor is the standard interceptor that simply throws the exception.
  45. /// It is the default action.
  46. /// </summary>
  47. internal sealed class StandardExceptionInterceptor : BaseExceptionInterceptor
  48. {
  49. public override Exception InterceptException(Exception exception)
  50. {
  51. return exception;
  52. }
  53. }
  54. /// <summary>
  55. /// ExceptionInterceptor is the "manager" class that keeps the list of registered interceptors
  56. /// for the given connection.
  57. /// </summary>
  58. internal sealed class ExceptionInterceptor : Interceptor
  59. {
  60. List<BaseExceptionInterceptor> interceptors = new List<BaseExceptionInterceptor>();
  61. public ExceptionInterceptor(MySqlConnection connection)
  62. {
  63. this.connection = connection;
  64. LoadInterceptors(connection.Settings.ExceptionInterceptors);
  65. // we always have the standard interceptor
  66. interceptors.Add(new StandardExceptionInterceptor());
  67. }
  68. protected override void AddInterceptor(object o)
  69. {
  70. if (o == null)
  71. throw new ArgumentException(String.Format("Unable to instantiate ExceptionInterceptor"));
  72. if (!(o is BaseExceptionInterceptor))
  73. throw new InvalidOperationException(String.Format(Resources.TypeIsNotExceptionInterceptor,
  74. o.GetType()));
  75. BaseExceptionInterceptor ie = o as BaseExceptionInterceptor;
  76. ie.Init(connection);
  77. interceptors.Insert(0, (BaseExceptionInterceptor)o);
  78. }
  79. public void Throw(Exception exception)
  80. {
  81. Exception e = exception;
  82. foreach (BaseExceptionInterceptor ie in interceptors)
  83. {
  84. e = ie.InterceptException(e);
  85. }
  86. throw e;
  87. }
  88. protected override string ResolveType(string nameOrType)
  89. {
  90. if (MySqlConfiguration.Settings != null && MySqlConfiguration.Settings.ExceptionInterceptors != null)
  91. {
  92. foreach (InterceptorConfigurationElement e in MySqlConfiguration.Settings.ExceptionInterceptors)
  93. if (String.Compare(e.Name, nameOrType, true) == 0)
  94. return e.Type;
  95. }
  96. return base.ResolveType(nameOrType);
  97. }
  98. }
  99. }
  100. #endif