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.

150 lines
5.0 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 System.Reflection;
  27. using Externals.MySql.Data.MySqlClient.Properties;
  28. using System.Data.Common;
  29. using System.Data;
  30. namespace Externals.MySql.Data.MySqlClient
  31. {
  32. /// <summary>
  33. /// BaseCommandInterceptor is the base class that should be used for all userland
  34. /// command interceptors
  35. /// </summary>
  36. internal abstract class BaseCommandInterceptor
  37. {
  38. protected MySqlConnection ActiveConnection { get; private set; }
  39. public virtual bool ExecuteScalar(string sql, ref object returnValue)
  40. {
  41. return false;
  42. }
  43. public virtual bool ExecuteNonQuery(string sql, ref int returnValue)
  44. {
  45. return false;
  46. }
  47. public virtual bool ExecuteReader(string sql, CommandBehavior behavior, ref MySqlDataReader returnValue)
  48. {
  49. return false;
  50. }
  51. public virtual void Init(MySqlConnection connection)
  52. {
  53. ActiveConnection = connection;
  54. }
  55. }
  56. /// <summary>
  57. /// CommandInterceptor is the "manager" class that keeps the list of registered interceptors
  58. /// for the given connection.
  59. /// </summary>
  60. internal sealed class CommandInterceptor : Interceptor
  61. {
  62. bool insideInterceptor = false;
  63. List<BaseCommandInterceptor> interceptors = new List<BaseCommandInterceptor>();
  64. public CommandInterceptor(MySqlConnection connection)
  65. {
  66. this.connection = connection;
  67. LoadInterceptors(connection.Settings.CommandInterceptors);
  68. }
  69. public bool ExecuteScalar(string sql, ref object returnValue)
  70. {
  71. if (insideInterceptor) return false;
  72. insideInterceptor = true;
  73. bool handled = false;
  74. foreach (BaseCommandInterceptor bci in interceptors)
  75. handled |= bci.ExecuteScalar(sql, ref returnValue);
  76. insideInterceptor = false;
  77. return handled;
  78. }
  79. public bool ExecuteNonQuery(string sql, ref int returnValue)
  80. {
  81. if (insideInterceptor) return false;
  82. insideInterceptor = true;
  83. bool handled = false;
  84. foreach (BaseCommandInterceptor bci in interceptors)
  85. handled |= bci.ExecuteNonQuery(sql, ref returnValue);
  86. insideInterceptor = false;
  87. return handled;
  88. }
  89. public bool ExecuteReader(string sql, CommandBehavior behavior, ref MySqlDataReader returnValue)
  90. {
  91. if (insideInterceptor) return false;
  92. insideInterceptor = true;
  93. bool handled = false;
  94. foreach (BaseCommandInterceptor bci in interceptors)
  95. handled |= bci.ExecuteReader(sql, behavior, ref returnValue);
  96. insideInterceptor = false;
  97. return handled;
  98. }
  99. protected override void AddInterceptor(object o)
  100. {
  101. if (o == null)
  102. throw new ArgumentException(String.Format("Unable to instantiate CommandInterceptor"));
  103. if (!(o is BaseCommandInterceptor))
  104. throw new InvalidOperationException(String.Format(Resources.TypeIsNotCommandInterceptor, o.GetType()));
  105. BaseCommandInterceptor ie = o as BaseCommandInterceptor;
  106. ie.Init(connection);
  107. interceptors.Insert(0, (BaseCommandInterceptor)o);
  108. }
  109. protected override string ResolveType(string nameOrType)
  110. {
  111. if (MySqlConfiguration.Settings != null && MySqlConfiguration.Settings.CommandInterceptors != null)
  112. {
  113. foreach (InterceptorConfigurationElement e in MySqlConfiguration.Settings.CommandInterceptors)
  114. if (String.Compare(e.Name, nameOrType, true) == 0)
  115. return e.Type;
  116. }
  117. return base.ResolveType(nameOrType);
  118. }
  119. }
  120. }
  121. #endif