You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by ro...@apache.org on 2005/03/29 16:28:56 UTC

svn commit: r159375 - in incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands: ./ DefaultPreparedCommand.cs EmbedParamsPreparedCommand.cs IPreparedCommand.cs PreparedCommandFactory.cs

Author: roberto
Date: Tue Mar 29 06:28:55 2005
New Revision: 159375

URL: http://svn.apache.org/viewcvs?view=rev&rev=159375
Log:
+ Adding first version of IPreparedCommand files (changes are not yet integrated into core in this commit!)

Added:
    incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/
    incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/DefaultPreparedCommand.cs
    incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/EmbedParamsPreparedCommand.cs
    incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/IPreparedCommand.cs
    incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/PreparedCommandFactory.cs

Added: incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/DefaultPreparedCommand.cs
URL: http://svn.apache.org/viewcvs/incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/DefaultPreparedCommand.cs?view=auto&rev=159375
==============================================================================
--- incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/DefaultPreparedCommand.cs (added)
+++ incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/DefaultPreparedCommand.cs Tue Mar 29 06:28:55 2005
@@ -0,0 +1,172 @@
+
+#region Apache Notice
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2005 - 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 Imports
+using System.Data;
+using System.Collections;
+
+using IBatisNet.Common;
+using IBatisNet.Common.Utilities.Objects;
+
+using IBatisNet.DataMapper.Configuration.Statements;
+using IBatisNet.DataMapper.Configuration.ParameterMapping;
+using IBatisNet.DataMapper.Exceptions;
+using IBatisNet.DataMapper.Scope;
+#endregion
+
+namespace IBatisNet.DataMapper.Commands
+{
+	/// <summary>
+	/// Summary description for DefaultPreparedCommand.
+	/// </summary>
+	public class DefaultPreparedCommand : IPreparedCommand
+	{
+		#region IPreparedCommand Members
+
+		/// <summary>
+		/// Create an IDbCommand for the IDalSession and the current SQL Statement
+		/// and fill IDbCommand IDataParameter's with the parameterObject.
+		/// </summary>
+		/// <param name="request"></param>
+		/// <param name="session">The IDalSession</param>
+		/// <param name="statement">The IStatement</param>
+		/// <param name="parameterObject">
+		/// The parameter object that will fill the sql parameter
+		/// </param>
+		/// <returns>An IDbCommand with all the IDataParameter filled.</returns>
+		public IDbCommand Create(RequestScope request, IDalSession session, IStatement statement, object parameterObject )
+		{
+			// the IDbConnection & the IDbTransaction are assign in the CreateCommand 
+			IDbCommand command = session.CreateCommand(statement.CommandType);
+				
+			command.CommandText = request.PreparedStatement.PreparedSql;
+
+			ApplyParameterMap( session, command, request, statement, parameterObject );
+
+			return command;
+		}
+
+		/// <summary>
+		/// 
+		/// </summary>
+		/// <param name="session"></param>
+		/// <param name="command"></param>
+		/// <param name="request"></param>
+		/// <param name="statement"></param>
+		/// <param name="parameterObject"></param>
+		protected virtual void ApplyParameterMap
+			( IDalSession session, IDbCommand command,
+			RequestScope request, IStatement statement, object parameterObject )
+		{
+			ArrayList properties = request.PreparedStatement.DbParametersName;
+			ArrayList parameters = request.PreparedStatement.DbParameters;
+			object parameterValue = null;
+
+			for ( int i = 0; i < properties.Count; ++i )
+			{
+				IDataParameter sqlParameter = (IDataParameter)parameters[i];
+				string propertyName = (string)properties[i];
+
+				if (command.CommandType == CommandType.Text)
+				{
+					if ( propertyName != "value" ) // Inline Parameters && Parameters via ParameterMap
+					{
+						ParameterProperty property = request.ParameterMap.GetProperty(i);
+
+						parameterValue = request.ParameterMap.GetValueOfProperty(parameterObject,
+							property.PropertyName);
+					}
+					else // 'value' parameter
+					{
+						parameterValue = parameterObject;
+					}
+				}
+				else // CommandType.StoredProcedure
+				{
+					// A store procedure must always use a ParameterMap 
+					// to indicate the mapping order of the properties to the columns
+					if (request.ParameterMap == null) // Inline Parameters
+					{
+						throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribut, which is not the case for the procedure '"+statement.Id+"'."); 
+					}
+					else // Parameters via ParameterMap
+					{
+						ParameterProperty property = request.ParameterMap.GetProperty(i);
+
+						if (property.DirectionAttribut.Length == 0)
+						{
+							property.Direction = sqlParameter.Direction;
+						}
+
+						//						IDbDataParameter dataParameter = (IDbDataParameter)parameters[i];
+						//						property.Precision = dataParameter.Precision;
+						//						property.Scale = dataParameter.Scale;
+						//						property.Size = dataParameter.Size;
+
+						sqlParameter.Direction = property.Direction;
+						parameterValue = request.ParameterMap.GetValueOfProperty( parameterObject, property.PropertyName );
+					}
+				}
+
+				IDataParameter parameterCopy = command.CreateParameter();
+				// Fix JIRA 20
+				sqlParameter.Value = parameterValue;
+				parameterCopy.Value = parameterValue;
+				
+				parameterCopy.Direction = sqlParameter.Direction;
+
+				// With a ParameterMap, we could specify the ParameterDbTypeProperty
+				if (statement.ParameterMap != null)
+				{
+					if (request.ParameterMap.GetProperty(i).DbType.Length >0)
+					{
+						string dbTypePropertyName = session.DataSource.Provider.ParameterDbTypeProperty;
+
+						ObjectProbe.SetPropertyValue(parameterCopy, dbTypePropertyName, ObjectProbe.GetPropertyValue(sqlParameter, dbTypePropertyName));
+					}
+					else
+					{
+						//parameterCopy.DbType = sqlParameter.DbType;
+					}
+				}
+				else
+				{
+					//parameterCopy.DbType = sqlParameter.DbType;
+				}
+
+				((IDbDataParameter)parameterCopy).Size = ((IDbDataParameter)sqlParameter).Size;
+				((IDbDataParameter)parameterCopy).Precision = ((IDbDataParameter)sqlParameter).Precision;
+				((IDbDataParameter)parameterCopy).Scale = ((IDbDataParameter)sqlParameter).Scale;
+
+				parameterCopy.ParameterName = sqlParameter.ParameterName;
+
+				command.Parameters.Add( parameterCopy );
+			}
+		}
+
+		#endregion
+	}
+}

Added: incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/EmbedParamsPreparedCommand.cs
URL: http://svn.apache.org/viewcvs/incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/EmbedParamsPreparedCommand.cs?view=auto&rev=159375
==============================================================================
--- incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/EmbedParamsPreparedCommand.cs (added)
+++ incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/EmbedParamsPreparedCommand.cs Tue Mar 29 06:28:55 2005
@@ -0,0 +1,210 @@
+#region Apache Notice
+
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2005 - 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 Imports
+
+using System;
+using System.Data;
+using System.Collections;
+using IBatisNet.Common;
+using IBatisNet.Common.Utilities.Objects;
+using IBatisNet.DataMapper.Configuration.Statements;
+using IBatisNet.DataMapper.Configuration.ParameterMapping;
+using IBatisNet.DataMapper.Exceptions;
+using IBatisNet.DataMapper.Scope;
+
+#endregion
+
+namespace IBatisNet.DataMapper.Commands
+{
+	/// <summary>
+	/// Summary description for EmbedParamsPreparedCommand.
+	/// </summary>
+	public class EmbedParamsPreparedCommand : DefaultPreparedCommand
+	{
+		#region EmbedParamsPreparedCommand Members
+
+		/// <summary>
+		/// 
+		/// </summary>
+		/// <param name="session"></param>
+		/// <param name="command"></param>
+		/// <param name="request"></param>
+		/// <param name="statement"></param>
+		/// <param name="parameterObject"></param>
+		protected override void ApplyParameterMap
+			(IDalSession session, IDbCommand command,
+			 RequestScope request, IStatement statement, object parameterObject)
+		{
+			ArrayList properties = request.PreparedStatement.DbParametersName;
+			ArrayList parameters = request.PreparedStatement.DbParameters;
+			object parameterValue = null;
+
+			for (int i = 0; i < properties.Count; ++i)
+			{
+				IDataParameter sqlParameter = (IDataParameter) parameters[i];
+				string propertyName = (string) properties[i];
+
+				if (command.CommandType == CommandType.Text)
+				{
+					if (propertyName != "value") // Inline Parameters && Parameters via ParameterMap
+					{
+						ParameterProperty property = request.ParameterMap.GetProperty(i);
+
+						parameterValue = request.ParameterMap.GetValueOfProperty(parameterObject,
+							property.PropertyName);
+					}
+					else // 'value' parameter
+					{
+						parameterValue = parameterObject;
+					}
+				}
+				else // CommandType.StoredProcedure
+				{
+					// A store procedure must always use a ParameterMap 
+					// to indicate the mapping order of the properties to the columns
+					if (request.ParameterMap == null) // Inline Parameters
+					{
+						throw new DataMapperException("A procedure statement tag must alway have a parameterMap attribut, which is not the case for the procedure '" + statement.Id + "'.");
+					}
+					else // Parameters via ParameterMap
+					{
+						ParameterProperty property = request.ParameterMap.GetProperty(i);
+
+						if (property.DirectionAttribut.Length == 0)
+						{
+							property.Direction = sqlParameter.Direction;
+						}
+
+						//						IDbDataParameter dataParameter = (IDbDataParameter)parameters[i];
+						//						property.Precision = dataParameter.Precision;
+						//						property.Scale = dataParameter.Scale;
+						//						property.Size = dataParameter.Size;
+
+						sqlParameter.Direction = property.Direction;
+						parameterValue = request.ParameterMap.GetValueOfProperty(parameterObject, property.PropertyName);
+					}
+				}
+
+				IDataParameter parameterCopy = command.CreateParameter();
+				// Fix JIRA 20
+				sqlParameter.Value = parameterValue;
+				parameterCopy.Value = parameterValue;
+
+				parameterCopy.Direction = sqlParameter.Direction;
+
+				// With a ParameterMap, we could specify the ParameterDbTypeProperty
+				if (statement.ParameterMap != null)
+				{
+					if (request.ParameterMap.GetProperty(i).DbType.Length > 0)
+					{
+						string dbTypePropertyName = session.DataSource.Provider.ParameterDbTypeProperty;
+
+						ObjectProbe.SetPropertyValue(parameterCopy, dbTypePropertyName, ObjectProbe.GetPropertyValue(sqlParameter, dbTypePropertyName));
+					}
+					else
+					{
+						//parameterCopy.DbType = sqlParameter.DbType;
+					}
+				}
+				else
+				{
+					//parameterCopy.DbType = sqlParameter.DbType;
+				}
+
+				((IDbDataParameter) parameterCopy).Size = ((IDbDataParameter) sqlParameter).Size;
+				((IDbDataParameter) parameterCopy).Precision = ((IDbDataParameter) sqlParameter).Precision;
+				((IDbDataParameter) parameterCopy).Scale = ((IDbDataParameter) sqlParameter).Scale;
+
+				parameterCopy.ParameterName = sqlParameter.ParameterName;
+
+				command.Parameters.Add(parameterCopy);
+
+
+				// NOTE: 
+				// Code from Oleksa Borodie to embed parameter values 
+				// into command text/sql statement.
+				// NEED TO MERGE WITH ABOVE AFTER INITIAL TESTING
+				// TO REMOVE REDUNDANT LOOPING!
+
+				// replace parameter names with parameter values
+				// only for parameters with names, parameterMaps will be ignored
+				IDataParameter p;
+				for (int iCnt = command.Parameters.Count - 1; iCnt >= 0; iCnt--)
+				{
+					p = (IDataParameter) command.Parameters[iCnt];
+					if (p.Direction == ParameterDirection.Input &&
+						command.CommandText.IndexOf(p.ParameterName) > 0)
+					{
+						switch (p.DbType)
+						{
+							case DbType.String:
+							case DbType.AnsiString:
+							case
+							DbType.AnsiStringFixedLength:
+							case DbType.StringFixedLength:
+								command.CommandText =
+									command.CommandText.Replace(p.ParameterName,
+									"\'" + p.Value.ToString().Replace("\'", "\'\'") + "\'");
+								break;
+							case DbType.Date:
+							case DbType.DateTime:
+								DateTime v =
+									Convert.ToDateTime(p.Value);
+								command.CommandText =
+									command.CommandText.Replace(p.ParameterName,
+									String.Format("\'{0}.{1}.{2} {3}:{4}:{5}.{6}\'", v.Year, v.Month,
+									v.Day, v.Hour, v.Minute, v.Second, v.Millisecond));
+								//                                                      command.CommandText =
+								command.CommandText.Replace(p.ParameterName,
+									"\'" + p.Value.ToString() + "\'");
+								break;
+							case DbType.Double:
+							case DbType.Decimal:
+							case DbType.Currency:
+							case DbType.Single:
+								command.CommandText =
+									command.CommandText.Replace(p.ParameterName,
+									p.Value.ToString().Replace(',', '.'));
+								break;
+							default:
+								command.CommandText =
+									command.CommandText.Replace(p.ParameterName, p.Value.ToString());
+								break;
+						}
+						command.Parameters.RemoveAt(iCnt);
+					}
+				}
+			
+			
+			}
+		}
+
+		#endregion
+	}
+}
+

Added: incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/IPreparedCommand.cs
URL: http://svn.apache.org/viewcvs/incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/IPreparedCommand.cs?view=auto&rev=159375
==============================================================================
--- incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/IPreparedCommand.cs (added)
+++ incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/IPreparedCommand.cs Tue Mar 29 06:28:55 2005
@@ -0,0 +1,57 @@
+
+#region Apache Notice
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2005 - 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 Imports
+using System.Data;
+
+using IBatisNet.Common;
+using IBatisNet.DataMapper.Configuration.Statements;
+using IBatisNet.DataMapper.Scope;
+
+using log4net;
+#endregion
+
+namespace IBatisNet.DataMapper.Commands
+{
+	/// <summary>
+	/// Summary description for IPreparedCommand.
+	/// </summary>
+	public interface IPreparedCommand
+	{
+		/// <summary>
+		/// Create an IDbCommand for the IDalSession and the current SQL Statement
+		/// and fill IDbCommand IDataParameter's with the parameterObject.
+		/// </summary>
+		/// <param name="request"></param>
+		/// <param name="session">The IDalSession</param>
+		/// <param name="statement">The IStatement</param>
+		/// <param name="parameterObject">
+		/// The parameter object that will fill the sql parameter
+		/// </param>
+		/// <returns>An IDbCommand with all the IDataParameter filled.</returns>
+		IDbCommand Create (RequestScope request, IDalSession session, IStatement statement, object parameterObject );
+	}
+}

Added: incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/PreparedCommandFactory.cs
URL: http://svn.apache.org/viewcvs/incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/PreparedCommandFactory.cs?view=auto&rev=159375
==============================================================================
--- incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/PreparedCommandFactory.cs (added)
+++ incubator/ibatis/trunk/cs/mapper/IBatisNet.DataMapper/Commands/PreparedCommandFactory.cs Tue Mar 29 06:28:55 2005
@@ -0,0 +1,54 @@
+
+#region Apache Notice
+/*****************************************************************************
+ * $Header: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * iBATIS.NET Data Mapper
+ * Copyright (C) 2005 - 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 Imports
+
+#endregion
+
+namespace IBatisNet.DataMapper.Commands
+{
+	/// <summary>
+	/// Summary description for PreparedCommandFactory.
+	/// </summary>
+	public class PreparedCommandFactory
+	{
+		/// <summary>
+		/// Get an IPreparedCommand.
+		/// </summary>
+		/// <returns></returns>
+		static public IPreparedCommand GetPreparedCommand(bool isEmbedStatementParams)
+		{
+			if (isEmbedStatementParams)
+			{
+				return new EmbedParamsPreparedCommand();
+			}
+			else
+			{
+				return new DefaultPreparedCommand();				
+			}
+		}
+
+	}
+}