You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ibatis.apache.org by rg...@apache.org on 2009/06/28 09:03:35 UTC
svn commit: r789059 [2/2] - in /ibatis/trunk/cs/V3/src:
Apache.Ibatis.DataMapper.SqlClient.Test/
Apache.Ibatis.DataMapper.SqlClient.Test/Fixtures/
Apache.Ibatis.DataMapper.SqlClient.Test/Fixtures/Mapping/
Apache.Ibatis.DataMapper/ Apache.Ibatis.DataMap...
Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs
URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs?rev=789059&r1=789058&r2=789059&view=diff
==============================================================================
--- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs (original)
+++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatement.cs Sun Jun 28 07:03:34 2009
@@ -48,17 +48,16 @@
namespace Apache.Ibatis.DataMapper.MappedStatements
{
-
/// <summary>
/// Base implementation of <see cref="IMappedStatement"/>.
/// </summary>
[DebuggerDisplay("MappedStatement: {Id}")]
- public class MappedStatement : MappedStatementEventSupport, IMappedStatement
+ public partial class MappedStatement : MappedStatementEventSupport, IMappedStatement
{
/// <summary>
/// Event launch on execute query
/// </summary>
- public event EventHandler<ExecuteEventArgs> Execute = delegate { };
+ public event EventHandler<ExecuteEventArgs> Executed = delegate { };
#region Fields
private readonly IStatement statement = null;
@@ -80,8 +79,6 @@
resultStrategy = ResultStrategyFactory.Get(this.statement);
}
- #region IDataMapper Members
-
#region properties
/// <summary>
/// The IPreparedCommand to use
@@ -118,999 +115,6 @@
}
#endregion
- #region ExecuteForObject
-
-
- /// <summary>
- /// Executes an SQL statement that returns a single row as an Object of the type of
- /// the resultObject passed in as a parameter.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">The result object.</param>
- /// <returns>The object</returns>
- public virtual object ExecuteQueryForObject(ISession session, object parameterObject, object resultObject)
- {
- object obj = null;
-
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
- preparedCommand.Create(request, session, Statement, param);
-
- obj = RunQueryForObject(request, session, param, resultObject);
-
- return RaisePostEvent<object, PostSelectEventArgs>(PostSelectEvent, param, obj);
- }
-
- /// <summary>
- /// Executes an SQL statement that returns a single row as an Object of the type of
- /// the resultObject passed in as a parameter.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">The result object.</param>
- /// <returns>The object</returns>
- internal object RunQueryForObject(RequestScope request, ISession session, object parameterObject, object resultObject)
- {
- object result = resultObject;
-
- using (IDbCommand command = request.IDbCommand)
- {
- IDataReader reader = command.ExecuteReader();
- try
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, resultObject);
- if (obj != BaseStrategy.SKIP)
- {
- result = obj;
- }
- }
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- ExecuteDelayedLoad(request);
-
- #region remark
- // If you are using the OleDb data provider (as you are), you need to close the
- // DataReader before output parameters are visible.
- #endregion
-
- RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- RaiseExecuteEvent();
-
- return result;
- }
-
- #endregion
-
- #region ExecuteForObject .NET 2.0
-
- /// <summary>
- /// Executes an SQL statement that returns a single row as an Object of the type of
- /// the resultObject passed in as a parameter.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">The result object.</param>
- /// <returns>The object</returns>
- public virtual T ExecuteQueryForObject<T>(ISession session, object parameterObject, T resultObject)
- {
- T obj = default(T);
-
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- obj = RunQueryForObject<T>(request, session, param, resultObject);
-
- return RaisePostEvent<T, PostSelectEventArgs>(PostSelectEvent, param, obj);
- }
-
-
- /// <summary>
- /// Executes an SQL statement that returns a single row as an Object of the type of
- /// the resultObject passed in as a parameter.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">The result object.</param>
- /// <returns>The object</returns>
- internal T RunQueryForObject<T>(RequestScope request, ISession session, object parameterObject, T resultObject)
- {
- T result = resultObject;
-
- using (IDbCommand command = request.IDbCommand)
- {
- IDataReader reader = command.ExecuteReader();
- try
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, resultObject);
- if (obj != BaseStrategy.SKIP)
- {
- result = (T)obj;
- }
- }
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- ExecuteDelayedLoad(request);
-
- #region remark
- // If you are using the OleDb data provider, you need to close the
- // DataReader before output parameters are visible.
- #endregion
-
- RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- RaiseExecuteEvent();
-
- return result;
- }
-
- #endregion
-
- #region ExecuteQueryForList
-
-
- /// <summary>
- /// Runs a query with a custom object that gets a chance
- /// to deal with each row as it is processed.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="rowDelegate"></param>
- public virtual IList ExecuteQueryForRowDelegate(ISession session, object parameterObject, RowDelegate rowDelegate)
- {
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- if (rowDelegate == null)
- {
- throw new DataMapperException("A null RowDelegate was passed to QueryForRowDelegate.");
- }
-
- IList list = RunQueryForList(request, session, param, null, rowDelegate);
-
- return RaisePostEvent<IList, PostSelectEventArgs>(PostSelectEvent, param, list);
- }
-
- /// <summary>
- /// Runs a query with a custom object that gets a chance
- /// to deal with each row as it is processed.
- /// </summary>
- /// <param name="session">The session used to execute the statement</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL. </param>
- /// <param name="keyProperty">The property of the result object to be used as the key. </param>
- /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
- /// <param name="rowDelegate"></param>
- /// <returns>A hashtable of object containing the rows keyed by keyProperty.</returns>
- ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
- public virtual IDictionary ExecuteQueryForMapWithRowDelegate(ISession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate rowDelegate)
- {
- RequestScope request = statement.Sql.GetRequestScope(this, parameterObject, session);
-
- if (rowDelegate == null)
- {
- throw new DataMapperException("A null DictionaryRowDelegate was passed to QueryForMapWithRowDelegate.");
- }
-
- preparedCommand.Create(request, session, Statement, parameterObject);
-
- return RunQueryForMap(request, session, parameterObject, keyProperty, valueProperty, rowDelegate);
- }
-
-
- /// <summary>
- /// Executes the SQL and retuns all rows selected.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <returns>A List of result objects.</returns>
- public virtual IList ExecuteQueryForList(ISession session, object parameterObject)
- {
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- IList list = RunQueryForList(request, session, param);
-
- return RaisePostEvent<IList, PostSelectEventArgs>(PostSelectEvent, param, list);
- }
-
- /// <summary>
- /// Executes the SQL and and fill a strongly typed collection.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">A strongly typed collection of result objects.</param>
- public virtual void ExecuteQueryForList(ISession session, object parameterObject, IList resultObject)
- {
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- RunQueryForList(request, session, param, resultObject, null);
-
- RaisePostEvent<IList, PostSelectEventArgs>(PostSelectEvent, param, resultObject);
- }
-
- /// <summary>
- /// Runs the query for list.
- /// </summary>
- /// <param name="request">The request.</param>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <returns>A List of result objects.</returns>
- internal IList RunQueryForList(RequestScope request, ISession session, object parameterObject)
- {
- IList list = null;
-
- using (IDbCommand command = request.IDbCommand)
- {
- if (statement.ListClass == null)
- {
- list = new ArrayList();
- }
- else
- {
- list = statement.CreateInstanceOfListClass();
- }
-
- IDataReader reader = command.ExecuteReader();
-
- try
- {
- do
- {
- IList currentList = null;
- if (request.Statement.ResultsMap.Count == 1)
- {
- currentList = list;
- }
- else
- {
- if (request.CurrentResultMap != null)
- {
- Type genericListType = typeof(List<>).MakeGenericType(new Type[] { request.CurrentResultMap.Class });
- currentList = (IList)Activator.CreateInstance(genericListType);
- }
- else
- {
- currentList = new ArrayList();
- }
- list.Add(currentList);
-
- }
- // Get Results
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- if (obj != BaseStrategy.SKIP)
- {
- currentList.Add(obj);
- }
- }
- }
- while (reader.NextResult());
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- ExecuteDelayedLoad(request);
-
- RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- return list;
- }
-
- /// <summary>
- /// Executes the SQL and retuns a List of result objects.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">A strongly typed collection of result objects.</param>
- /// <param name="rowDelegate"></param>
- /// <returns>A List of result objects.</returns>
- internal IList RunQueryForList(RequestScope request, ISession session, object parameterObject, IList resultObject, RowDelegate rowDelegate)
- {
- IList list = resultObject;
-
- using (IDbCommand command = request.IDbCommand)
- {
- if (resultObject==null)
- {
- if (statement.ListClass == null)
- {
- list = new ArrayList();
- }
- else
- {
- list = statement.CreateInstanceOfListClass();
- }
- }
-
- IDataReader reader = command.ExecuteReader();
-
- try
- {
- do
- {
- if (rowDelegate == null)
- {
- //***
- IList currentList = null;
- if (request.Statement.ResultsMap.Count == 1)
- {
- currentList = list;
- }
- else
- {
- if (request.CurrentResultMap != null)
- {
- Type genericListType = typeof(List<>).MakeGenericType(new Type[] { request.CurrentResultMap.Class });
- currentList = (IList)Activator.CreateInstance(genericListType);
- }
- else
- {
- currentList = new ArrayList();
- }
- list.Add(currentList);
-
- }
- //***
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- if (obj != BaseStrategy.SKIP)
- {
- //list.Add(obj);
- currentList.Add(obj);
- }
- }
- }
- else
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- rowDelegate(obj, parameterObject, list);
- }
- }
- }
- while (reader.NextResult());
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- ExecuteDelayedLoad(request);
- RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- return list;
- }
-
-
- #endregion
-
- #region ExecuteQueryForList .NET 2.0
-
- /// <summary>
- /// Runs a query with a custom object that gets a chance
- /// to deal with each row as it is processed.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="rowDelegate"></param>
- public virtual IList<T> ExecuteQueryForRowDelegate<T>(ISession session, object parameterObject, RowDelegate<T> rowDelegate)
- {
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- if (rowDelegate == null)
- {
- throw new DataMapperException("A null RowDelegate was passed to QueryForRowDelegate.");
- }
- IList<T> list = RunQueryForList<T>(request, session, param, null, rowDelegate);
-
- return RaisePostEvent<IList<T>, PostSelectEventArgs>(PostSelectEvent, param, list);
- }
-
-
- /// <summary>
- /// Executes the SQL and retuns all rows selected.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <returns>A List of result objects.</returns>
- public virtual IList<T> ExecuteQueryForList<T>(ISession session, object parameterObject)
- {
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- IList<T> list = RunQueryForList<T>(request, session, param, null, null);
-
- return RaisePostEvent<IList<T>, PostSelectEventArgs>(PostSelectEvent, param, list);
- }
-
- /// <summary>
- /// Executes the SQL and and fill a strongly typed collection.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">A strongly typed collection of result objects.</param>
- public virtual void ExecuteQueryForList<T>(ISession session, object parameterObject, IList<T> resultObject)
- {
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- RunQueryForList<T>(request, session, param, resultObject, null);
-
- RaisePostEvent<IList<T>, PostSelectEventArgs>(PostSelectEvent, param, resultObject);
- }
-
- /// <summary>
- /// Executes the SQL and retuns a List of result objects.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <returns>A List of result objects.</returns>
- internal IList<T> RunQueryForList<T>(RequestScope request, ISession session, object parameterObject)
- {
- IList<T> list = null;
-
- using (IDbCommand command = request.IDbCommand)
- {
- if (statement.ListClass == null)
- {
- list = new List<T>();
- }
- else
- {
- list = statement.CreateInstanceOfGenericListClass<T>();
- }
-
- IDataReader reader = command.ExecuteReader();
- try
- {
- do
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- if (obj != BaseStrategy.SKIP)
- {
- list.Add((T) obj);
- }
- }
- }
- while (reader.NextResult());
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- ExecuteDelayedLoad(request);
-
- RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- return list;
- }
-
- /// <summary>
- /// Executes the SQL and retuns a List of result objects.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="resultObject">The result object</param>
- /// <param name="rowDelegate"></param>
- /// <returns>A List of result objects.</returns>
- internal IList<T> RunQueryForList<T>(RequestScope request, ISession session,
- object parameterObject, IList<T> resultObject, RowDelegate<T> rowDelegate)
- {
- IList<T> list = resultObject;
-
- using (IDbCommand command = request.IDbCommand)
- {
- if (resultObject == null)
- {
- if (statement.ListClass == null)
- {
- list = new List<T>();
- }
- else
- {
- list = statement.CreateInstanceOfGenericListClass<T>();
- }
- }
-
- IDataReader reader = command.ExecuteReader();
- try
- {
- do
- {
- if (rowDelegate == null)
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- if (obj != BaseStrategy.SKIP)
- {
- list.Add((T)obj);
- }
- }
- }
- else
- {
- while (reader.Read())
- {
- T obj = (T)resultStrategy.Process(request, ref reader, null);
- rowDelegate(obj, parameterObject, list);
- }
- }
- }
- while (reader.NextResult());
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- ExecuteDelayedLoad(request);
- RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- return list;
- }
-
- #endregion
-
- #region ExecuteUpdate, ExecuteInsert
-
- /// <summary>
- /// Execute an update statement. Also used for delete statement.
- /// Return the number of row effected.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <returns>The number of row effected.</returns>
- public virtual int ExecuteUpdate(ISession session, object parameterObject)
- {
- int rows = 0; // the number of rows affected
-
- object param = RaisePreEvent<PreUpdateOrDeleteEventArgs>(PreUpdateOrDeleteEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- preparedCommand.Create(request, session, Statement, param);
-
- using (IDbCommand command = request.IDbCommand)
- {
- rows = command.ExecuteNonQuery();
-
- RetrieveOutputParameters(request, session, command, param);
- }
-
- RaiseExecuteEvent();
-
- return RaisePostEvent<int, PostUpdateOrDeleteEventArgs>(PostUpdateOrDeleteEvent, param, rows);
- }
-
-
- /// <summary>
- /// Execute an insert statement. Fill the parameter object with
- /// the ouput parameters if any, also could return the insert generated key
- /// </summary>
- /// <param name="session">The session</param>
- /// <param name="parameterObject">The parameter object used to fill the statement.</param>
- /// <returns>Can return the insert generated key.</returns>
- public virtual object ExecuteInsert(ISession session, object parameterObject)
- {
- object generatedKey = null;
- SelectKey selectKeyStatement = null;
-
- object param = RaisePreEvent<PreInsertEventArgs>(PreInsertEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
-
- if (statement is Insert)
- {
- selectKeyStatement = ((Insert)statement).SelectKey;
- }
-
- if (selectKeyStatement != null && !selectKeyStatement.isAfter)
- {
- IMappedStatement mappedStatement = modelStore.GetMappedStatement(selectKeyStatement.Id);
- generatedKey = mappedStatement.ExecuteQueryForObject(session, param, null);
-
- ObjectProbe.SetMemberValue(param, selectKeyStatement.PropertyName, generatedKey,
- request.DataExchangeFactory.ObjectFactory,
- request.DataExchangeFactory.AccessorFactory);
- }
-
- preparedCommand.Create(request, session, Statement, param);
-
- using (IDbCommand command = request.IDbCommand)
- {
- if (statement is Insert)
- {
- command.ExecuteNonQuery();
- }
- // Retrieve output parameter if the result class is specified
- else if (statement is Procedure && (statement.ResultClass != null) &&
- modelStore.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(statement.ResultClass))
- {
- IDataParameter returnValueParameter = command.CreateParameter();
- returnValueParameter.Direction = ParameterDirection.ReturnValue;
- command.Parameters.Add(returnValueParameter);
-
- command.ExecuteNonQuery();
- generatedKey = returnValueParameter.Value;
-
- ITypeHandler typeHandler = modelStore.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(statement.ResultClass);
- generatedKey = typeHandler.GetDataBaseValue(generatedKey, statement.ResultClass);
- }
- else
- {
- generatedKey = command.ExecuteScalar();
- if ((statement.ResultClass != null) &&
- modelStore.DataExchangeFactory.TypeHandlerFactory.IsSimpleType(statement.ResultClass))
- {
- ITypeHandler typeHandler = modelStore.DataExchangeFactory.TypeHandlerFactory.GetTypeHandler(statement.ResultClass);
- generatedKey = typeHandler.GetDataBaseValue(generatedKey, statement.ResultClass);
- }
- }
-
- if (selectKeyStatement != null && selectKeyStatement.isAfter)
- {
- IMappedStatement mappedStatement = modelStore.GetMappedStatement(selectKeyStatement.Id);
- generatedKey = mappedStatement.ExecuteQueryForObject(session, param, null);
-
- ObjectProbe.SetMemberValue(param, selectKeyStatement.PropertyName, generatedKey,
- request.DataExchangeFactory.ObjectFactory,
- request.DataExchangeFactory.AccessorFactory);
- }
-
- RetrieveOutputParameters(request, session, command, param);
- }
-
- RaiseExecuteEvent();
-
- return RaisePostEvent<object, PostInsertEventArgs>(PostInsertEvent, param, generatedKey);
- }
-
- #endregion
-
- #region ExecuteQueryForMap
-
- /// <summary>
- /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
- /// in the keyProperty parameter. The value at each key will be the value of the property specified
- /// in the valueProperty parameter. If valueProperty is null, the entire result object will be entered.
- /// </summary>
- /// <param name="session">The session used to execute the statement</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL. </param>
- /// <param name="keyProperty">The property of the result object to be used as the key. </param>
- /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
- /// <returns>A hashtable of object containing the rows keyed by keyProperty.</returns>
- ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
- public virtual IDictionary ExecuteQueryForMap(ISession session, object parameterObject, string keyProperty, string valueProperty)
- {
- RequestScope request = statement.Sql.GetRequestScope(this, parameterObject, session);
-
- preparedCommand.Create(request, session, Statement, parameterObject);
-
- return RunQueryForMap(request, session, parameterObject, keyProperty, valueProperty, null);
- }
-
-
- /// <summary>
- /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
- /// in the keyProperty parameter. The value at each key will be the value of the property specified
- /// in the valueProperty parameter. If valueProperty is null, the entire result object will be entered.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="keyProperty">The property of the result object to be used as the key.</param>
- /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
- /// <param name="rowDelegate">A delegate called once per row in the QueryForMapWithRowDelegate method</param>
- /// <returns>A hashtable of object containing the rows keyed by keyProperty.</returns>
- ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
- internal IDictionary RunQueryForMap(RequestScope request,
- ISession session,
- object parameterObject,
- string keyProperty,
- string valueProperty,
- DictionaryRowDelegate rowDelegate)
- {
- IDictionary map = new Hashtable();
-
- using (IDbCommand command = request.IDbCommand)
- {
- IDataReader reader = command.ExecuteReader();
- try
- {
-
- if (rowDelegate == null)
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- object key = ObjectProbe.GetMemberValue(obj, keyProperty, request.DataExchangeFactory.AccessorFactory);
- object value = obj;
- if (valueProperty != null)
- {
- value = ObjectProbe.GetMemberValue(obj, valueProperty, request.DataExchangeFactory.AccessorFactory);
- }
- map.Add(key, value);
- }
- }
- else
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- object key = ObjectProbe.GetMemberValue(obj, keyProperty, request.DataExchangeFactory.AccessorFactory);
- object value = obj;
- if (valueProperty != null)
- {
- value = ObjectProbe.GetMemberValue(obj, valueProperty, request.DataExchangeFactory.AccessorFactory);
- }
- rowDelegate(key, value, parameterObject, map);
-
- }
- }
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
- ExecuteDelayedLoad(request);
- }
- return map;
-
- }
-
- /// <summary>
- /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
- /// in the keyProperty parameter. The value at each key will be the value of the property specified
- /// in the valueProperty parameter. If valueProperty is null, the entire result object will be entered.
- /// </summary>
- /// <param name="session">The session used to execute the statement</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL. </param>
- /// <param name="keyProperty">The property of the result object to be used as the key. </param>
- /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
- /// <returns>A IDictionary of object containing the rows keyed by keyProperty.</returns>
- ///<exception cref="Apache.Ibatis.DataMapper.Exceptions.DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
- public virtual IDictionary<K, V> ExecuteQueryForDictionary<K, V>(ISession session, object parameterObject, string keyProperty, string valueProperty)
- {
- RequestScope request = statement.Sql.GetRequestScope(this, parameterObject, session);
-
- preparedCommand.Create(request, session, Statement, parameterObject);
-
- return RunQueryForDictionary<K, V>(request, session, parameterObject, keyProperty, valueProperty, null);
-
- }
-
- /// <summary>
- /// Runs a query with a custom object that gets a chance
- /// to deal with each row as it is processed.
- /// </summary>
- /// <param name="session">The session used to execute the statement</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL. </param>
- /// <param name="keyProperty">The property of the result object to be used as the key. </param>
- /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
- /// <param name="rowDelegate">A delegate called once per row in the QueryForDictionary method</param>
- /// <returns>A hashtable of object containing the rows keyed by keyProperty.</returns>
- ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
- public virtual IDictionary<K, V> ExecuteQueryForDictionary<K, V>(ISession session, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate<K, V> rowDelegate)
- {
- RequestScope request = statement.Sql.GetRequestScope(this, parameterObject, session);
-
- if (rowDelegate == null)
- {
- throw new DataMapperException("A null DictionaryRowDelegate was passed to QueryForDictionary.");
- }
-
- preparedCommand.Create(request, session, Statement, parameterObject);
-
- return RunQueryForDictionary<K, V>(request, session, parameterObject, keyProperty, valueProperty, rowDelegate);
- }
-
- /// <summary>
- /// Executes the SQL and retuns all rows selected in a map that is keyed on the property named
- /// in the keyProperty parameter. The value at each key will be the value of the property specified
- /// in the valueProperty parameter. If valueProperty is null, the entire result object will be entered.
- /// </summary>
- /// <param name="request">The request scope.</param>
- /// <param name="session">The session used to execute the statement</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <param name="keyProperty">The property of the result object to be used as the key.</param>
- /// <param name="valueProperty">The property of the result object to be used as the value (or null)</param>
- /// <param name="rowDelegate">A delegate called once per row in the QueryForMapWithRowDelegate method</param>
- /// <returns>A IDictionary of object containing the rows keyed by keyProperty.</returns>
- ///<exception cref="DataMapperException">If a transaction is not in progress, or the database throws an exception.</exception>
- internal IDictionary<K, V> RunQueryForDictionary<K, V>(RequestScope request,
- ISession session,
- object parameterObject,
- string keyProperty,
- string valueProperty,
- DictionaryRowDelegate<K, V> rowDelegate)
- {
- IDictionary<K, V> map = new Dictionary<K, V>();
-
- using (IDbCommand command = request.IDbCommand)
- {
- IDataReader reader = command.ExecuteReader();
- try
- {
-
- if (rowDelegate == null)
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- K key = (K)ObjectProbe.GetMemberValue(obj, keyProperty, request.DataExchangeFactory.AccessorFactory);
- V value = default(V);
- if (valueProperty != null)
- {
- value = (V)ObjectProbe.GetMemberValue(obj, valueProperty, request.DataExchangeFactory.AccessorFactory);
- }
- else
- {
- value = (V)obj;
- }
- map.Add(key, value);
- }
- }
- else
- {
- while (reader.Read())
- {
- object obj = resultStrategy.Process(request, ref reader, null);
- K key = (K)ObjectProbe.GetMemberValue(obj, keyProperty, request.DataExchangeFactory.AccessorFactory);
- V value = default(V);
- if (valueProperty != null)
- {
- value = (V)ObjectProbe.GetMemberValue(obj, valueProperty, request.DataExchangeFactory.AccessorFactory);
- }
- else
- {
- value = (V)obj;
- }
- rowDelegate(key, value, parameterObject, map);
- }
- }
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
- ExecuteDelayedLoad(request);
- }
- return map;
-
- }
-
- #endregion
-
- #region ExecuteQueryForDataTable
- /// <summary>
- /// Executes an SQL statement that returns DataTable.
- /// </summary>
- /// <param name="session">The session used to execute the statement.</param>
- /// <param name="parameterObject">The object used to set the parameters in the SQL.</param>
- /// <returns>The object</returns>
- public virtual DataTable ExecuteQueryForDataTable(ISession session, object parameterObject)
- {
- DataTable dataTable = null;
-
- object param = RaisePreEvent<PreSelectEventArgs>(PreSelectEvent, parameterObject);
-
- RequestScope request = statement.Sql.GetRequestScope(this, param, session);
- preparedCommand.Create(request, session, Statement, param);
-
- dataTable = RunQueryForForDataTable(request, session, param);
-
- return RaisePostEvent<DataTable, PostSelectEventArgs>(PostSelectEvent, param, dataTable);
- }
-
- /// <summary>
- /// Runs the query for for data table.
- /// </summary>
- /// <param name="request">The request.</param>
- /// <param name="session">The session.</param>
- /// <param name="parameterObject">The parameter object.</param>
- /// <returns></returns>
- internal DataTable RunQueryForForDataTable(RequestScope request, ISession session, object parameterObject)
- {
- DataTable dataTable = new DataTable("DataTable");
-
- using (IDbCommand command = request.IDbCommand)
- {
- IDataReader reader = command.ExecuteReader();
-
- try
- {
- // Get Results
- while (reader.Read())
- {
- DataRow dataRow = dataTable.NewRow();
- dataTable.Rows.Add(dataRow);
- resultStrategy.Process(request, ref reader, dataRow);
- }
- }
- finally
- {
- reader.Close();
- reader.Dispose();
- }
-
- // do we need ??
- //ExecuteDelayedLoad(request);
-
- // do we need ??
- //RetrieveOutputParameters(request, session, command, parameterObject);
- }
-
- return dataTable;
- }
- #endregion
-
- #endregion
-
/// <summary>
/// Retrieve the output parameter and map them on the result object.
/// This routine is only use is you specified a ParameterMap and some output attribute
@@ -1209,9 +213,24 @@
{
ExecuteEventArgs e = new ExecuteEventArgs();
e.StatementName = statement.Id;
- Execute(this, e);
+ Executed(this, e);
}
+ /// <summary>
+ /// Ensures all the related Execute methods are run in a consistent manner with pre and post events.
+ /// </summary>
+ /// <returns></returns>
+ protected virtual T Execute<T>(object preEvent, object postEvent, ISession session, object parameterObject, Func<RequestScope, object, T> requestRunner)
+ {
+ object paramPreEvent = RaisePreEvent(preEvent, parameterObject);
+
+ RequestScope request = statement.Sql.GetRequestScope(this, paramPreEvent, session);
+ preparedCommand.Create(request, session, Statement, paramPreEvent);
+ T result = requestRunner(request, paramPreEvent);
+ RaiseExecuteEvent();
+
+ return RaisePostEvent(postEvent, paramPreEvent, result, false);
+ }
}
}
Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatementEventSupport.cs
URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatementEventSupport.cs?rev=789059&r1=789058&r2=789059&view=diff
==============================================================================
--- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatementEventSupport.cs (original)
+++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/MappedStatements/MappedStatementEventSupport.cs Sun Jun 28 07:03:34 2009
@@ -35,69 +35,69 @@
[Serializable]
public abstract class MappedStatementEventSupport : IMappedStatementEvents
{
- protected static readonly object PreInsertEvent = new object();
- protected static readonly object PreSelectEvent = new object();
- protected static readonly object PreUpdateOrDeleteEvent = new object();
- protected static readonly object PostInsertEvent = new object();
- protected static readonly object PostSelectEvent = new object();
- protected static readonly object PostUpdateOrDeleteEvent = new object();
+ protected static readonly object PreInsertEventKey = new object();
+ protected static readonly object PreSelectEventKey = new object();
+ protected static readonly object PreUpdateOrDeleteEventKey = new object();
+ protected static readonly object PostInsertEventKey = new object();
+ protected static readonly object PostSelectEventKey = new object();
+ protected static readonly object PostUpdateOrDeleteEventKey = new object();
private readonly EventHandlerList events = new EventHandlerList();
#region IMappedStatementEvents Members
/// <summary>
- /// Will handle an <see cref="PreInsertEventArgs"/>.
+ /// Will handle an <see cref="PreStatementEventArgs"/>.
/// </summary>
- public event EventHandler<PreInsertEventArgs> PreInsert
+ public event EventHandler<PreStatementEventArgs> PreInsert
{
- add { events.AddHandler(PreInsertEvent, value); }
- remove { events.RemoveHandler(PreInsertEvent, value); }
+ add { events.AddHandler(PreInsertEventKey, value); }
+ remove { events.RemoveHandler(PreInsertEventKey, value); }
}
/// <summary>
- /// Will handle an <see cref="PreSelectEventArgs"/>.
+ /// Will handle an <see cref="PreStatementEventArgs"/>.
/// </summary>
- public event EventHandler<PreSelectEventArgs> PreSelect
+ public event EventHandler<PreStatementEventArgs> PreSelect
{
- add { events.AddHandler(PreSelectEvent, value); }
- remove { events.RemoveHandler(PreSelectEvent, value); }
+ add { events.AddHandler(PreSelectEventKey, value); }
+ remove { events.RemoveHandler(PreSelectEventKey, value); }
}
/// <summary>
- /// Will handle an <see cref="PreUpdateOrDeleteEventArgs"/>.
+ /// Will handle an <see cref="PreStatementEventArgs"/>.
/// </summary>
- public event EventHandler<PreUpdateOrDeleteEventArgs> PreUpdateOrDelete
+ public event EventHandler<PreStatementEventArgs> PreUpdateOrDelete
{
- add { events.AddHandler(PreUpdateOrDeleteEvent, value); }
- remove { events.RemoveHandler(PreUpdateOrDeleteEvent, value); }
+ add { events.AddHandler(PreUpdateOrDeleteEventKey, value); }
+ remove { events.RemoveHandler(PreUpdateOrDeleteEventKey, value); }
}
/// <summary>
- /// Will handle an <see cref="PostInsertEventArgs"/>.
+ /// Will handle an <see cref="PostStatementEventArgs"/>.
/// </summary>
- public event EventHandler<PostInsertEventArgs> PostInsert
+ public event EventHandler<PostStatementEventArgs> PostInsert
{
- add { events.AddHandler(PostInsertEvent, value); }
- remove { events.RemoveHandler(PostInsertEvent, value); }
+ add { events.AddHandler(PostInsertEventKey, value); }
+ remove { events.RemoveHandler(PostInsertEventKey, value); }
}
/// <summary>
- /// Will handle an <see cref="PostSelectEventArgs"/>.
+ /// Will handle an <see cref="PostStatementEventArgs"/>.
/// </summary>
- public event EventHandler<PostSelectEventArgs> PostSelect
+ public event EventHandler<PostStatementEventArgs> PostSelect
{
- add { events.AddHandler(PostSelectEvent, value); }
- remove { events.RemoveHandler(PostSelectEvent, value); }
+ add { events.AddHandler(PostSelectEventKey, value); }
+ remove { events.RemoveHandler(PostSelectEventKey, value); }
}
/// <summary>
- /// Will handle an <see cref="PostUpdateOrDeleteEventArgs"/>.
+ /// Will handle an <see cref="PostStatementEventArgs"/>.
/// </summary>
- public event EventHandler<PostUpdateOrDeleteEventArgs> PostUpdateOrDelete
+ public event EventHandler<PostStatementEventArgs> PostUpdateOrDelete
{
- add { events.AddHandler(PostUpdateOrDeleteEvent, value); }
- remove { events.RemoveHandler(PostUpdateOrDeleteEvent, value); }
+ add { events.AddHandler(PostUpdateOrDeleteEventKey, value); }
+ remove { events.RemoveHandler(PostUpdateOrDeleteEventKey, value); }
}
#endregion
@@ -108,14 +108,13 @@
/// <param name="key">The key.</param>
/// <param name="parameterObject">The parameter object.</param>
/// <returns>Returns is used as the parameter object</returns>
- protected object RaisePreEvent<TEvent>(object key, object parameterObject)
- where TEvent : PreStatementEventArgs, new()
+ protected object RaisePreEvent(object key, object parameterObject)
{
- EventHandler<TEvent> handlers = (EventHandler<TEvent>)events[key];
+ var handlers = (EventHandler<PreStatementEventArgs>)events[key];
if (handlers != null)
{
- TEvent eventArgs = new TEvent();
+ var eventArgs = new PreStatementEventArgs();
eventArgs.ParameterObject = parameterObject;
handlers(this, eventArgs);
return eventArgs.ParameterObject;
@@ -129,17 +128,18 @@
/// <param name="key">The key.</param>
/// <param name="parameterObject">The parameter object.</param>
/// <param name="resultObject">The result object.</param>
+ /// <param name="cacheHit">Did the ResultObject come from cache?</param>
/// <returns>Returns is used as the result object</returns>
- protected TType RaisePostEvent<TType, TEvent>(object key, object parameterObject, TType resultObject)
- where TEvent : PostStatementEventArgs, new()
+ protected TType RaisePostEvent<TType>(object key, object parameterObject, TType resultObject, bool cacheHit)
{
- EventHandler<TEvent> handlers = (EventHandler<TEvent>)events[key];
+ var handlers = (EventHandler<PostStatementEventArgs>)events[key];
if (handlers != null)
{
- TEvent eventArgs = new TEvent();
+ var eventArgs = new PostStatementEventArgs();
eventArgs.ParameterObject = parameterObject;
eventArgs.ResultObject = resultObject;
+ eventArgs.CacheHit = cacheHit;
handlers(this, eventArgs);
return (TType)eventArgs.ResultObject;
}
Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Cache/CacheModel.cs
URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Cache/CacheModel.cs?rev=789059&r1=789058&r2=789059&view=diff
==============================================================================
--- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Cache/CacheModel.cs (original)
+++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Cache/CacheModel.cs Sun Jun 28 07:03:34 2009
@@ -155,7 +155,7 @@
/// <param name="mappedStatement">A MappedStatement on which we listen ExecuteEventArgs event.</param>
public void RegisterTriggerStatement(IMappedStatement mappedStatement)
{
- mappedStatement.Execute +=FlushHandler;
+ mappedStatement.Executed +=FlushHandler;
}
Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PostStatementEventArgs.cs
URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PostStatementEventArgs.cs?rev=789059&r1=789058&r2=789059&view=diff
==============================================================================
--- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PostStatementEventArgs.cs (original)
+++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PostStatementEventArgs.cs Sun Jun 28 07:03:34 2009
@@ -28,12 +28,17 @@
/// <summary>
/// Base class for post <see cref="BaseStatementEventArgs"/>
/// </summary>
- public abstract class PostStatementEventArgs : BaseStatementEventArgs
+ public class PostStatementEventArgs : BaseStatementEventArgs
{
/// <summary>
/// Gets or sets the result object.
/// </summary>
/// <value>The result object.</value>
public object ResultObject { get; set; }
+
+ /// <summary>
+ /// Was the ResultObject populated from cache?
+ /// </summary>
+ public bool CacheHit { get; set; }
}
}
Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PreStatementEventArgs.cs
URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PreStatementEventArgs.cs?rev=789059&r1=789058&r2=789059&view=diff
==============================================================================
--- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PreStatementEventArgs.cs (original)
+++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Events/PreStatementEventArgs.cs Sun Jun 28 07:03:34 2009
@@ -28,7 +28,7 @@
/// <summary>
/// Base class for pre <see cref="BaseStatementEventArgs"/>
/// </summary>
- public abstract class PreStatementEventArgs : BaseStatementEventArgs
+ public class PreStatementEventArgs : BaseStatementEventArgs
{
}
Modified: ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Statements/PreparedStatementFactory.cs
URL: http://svn.apache.org/viewvc/ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Statements/PreparedStatementFactory.cs?rev=789059&r1=789058&r2=789059&view=diff
==============================================================================
--- ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Statements/PreparedStatementFactory.cs (original)
+++ ibatis/trunk/cs/V3/src/Apache.Ibatis.DataMapper/Model/Statements/PreparedStatementFactory.cs Sun Jun 28 07:03:34 2009
@@ -114,10 +114,10 @@
{
preparedStatement = new PreparedStatement();
- preparedStatement.PreparedSql = commandText;
-
if (statement.CommandType == CommandType.Text)
{
+ preparedStatement.PreparedSql = commandText;
+
if (request.ParameterMap != null)
{
CreateParametersForTextCommand();
@@ -126,6 +126,10 @@
}
else if (statement.CommandType == CommandType.StoredProcedure) // StoredProcedure
{
+ // Necessary to prevent stored procedures with extra space around their name like
+ // " ps_SelectAccount " to be sent to the database as "ps_SelectAccount".
+ preparedStatement.PreparedSql = commandText.Trim();
+
if (request.ParameterMap == null) // No parameterMap --> error
{
throw new DataMapperException("A procedure statement tag must have a parameterMap attribute, which is not the case for the procedure '"+statement.Id+".");