#region Apache Notice /***************************************************************************** * $Header: $ * $Revision: $ * $Date: $ * * iBATIS.NET Data Mapper * Copyright (C) 2004 - Gilles Bayon * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion #region Using using System; using System.Collections; using System.Data; using System.Text; using Castle.DynamicProxy; using IBatisNet.Common.Logging; #endregion namespace IBatisNet.DataMapper.Commands { /// /// Summary description for IPreparedCommandProxy. /// [CLSCompliant(false)] public class IPreparedCommandProxy : IInterceptor { #region Fields private IPreparedCommand _preparedCommand = null; private static ArrayList _passthroughMethods = new ArrayList(); private static readonly ILog _logger = LogManager.GetLogger( "IBatisNet.DataMapper.Commands.IPreparedCommand" ); #endregion #region Constructors static IPreparedCommandProxy() { _passthroughMethods.Add("GetType"); _passthroughMethods.Add("ToString"); } /// /// Constructor for a connection proxy /// /// The connection which been proxified. internal IPreparedCommandProxy(IPreparedCommand preparedCommand) { _preparedCommand = preparedCommand; } #endregion #region Methods /// /// Static constructor /// /// The connection which been proxified. /// A proxy internal static IPreparedCommand NewInstance(IPreparedCommand preparedCommand) { object proxyIPreparedCommand = null; IInterceptor handler = new IPreparedCommandProxy(preparedCommand); ProxyGenerator proxyGenerator = new ProxyGenerator(); proxyIPreparedCommand = proxyGenerator.CreateProxy(typeof(IPreparedCommand), handler, preparedCommand); return (IPreparedCommand) proxyIPreparedCommand; } #endregion #region IInterceptor Members /// /// /// /// /// /// public object Intercept(IInvocation invocation, params object[] arguments) { object returnValue = null; returnValue = invocation.Method.Invoke( _preparedCommand, arguments); if (invocation.Method.Name=="Create") { if (_logger.IsDebugEnabled) { IDbCommand command = (IDbCommand)returnValue; _logger.Debug("PreparedStatement : [" + command.CommandText + "]"); if (command.Parameters.Count>0) { StringBuilder valueList = new StringBuilder(); // Log info StringBuilder typeList = new StringBuilder(); // Log info for ( int i = 0; i < command.Parameters.Count; ++i ) { IDataParameter sqlParameter = (IDataParameter)command.Parameters[i]; if (sqlParameter.Value == DBNull.Value) { valueList.Append("null,"); typeList.Append("null,"); } else { valueList.Append( sqlParameter.Value.ToString() ); valueList.Append( "," ); typeList.Append( sqlParameter.Value.GetType().ToString() ); typeList.Append( "," ); } } _logger.Debug("Parameters: [" + valueList.ToString().Remove(valueList.ToString().Length-1,1) + "]"); _logger.Debug("Types: [" + typeList.ToString().Remove(typeList.ToString().Length-1,1) + "]"); } } } return returnValue; } #endregion } }