#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.Collections;
using System.Data;
using System.Runtime.CompilerServices;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration.ParameterMapping;
using IBatisNet.DataMapper.Configuration.ResultMapping;
using IBatisNet.DataMapper.Configuration.Statements;
using IBatisNet.DataMapper.DataExchange;
using IBatisNet.DataMapper.MappedStatements;
#endregion
namespace IBatisNet.DataMapper.Scope
{
///
/// Hold data during the process of a mapped statement.
///
public class RequestScope : IScope
{
#region Fields
private IStatement _statement = null;
private ErrorContext _errorContext = null;
private ParameterMap _parameterMap = null;
private PreparedStatement _preparedStatement = null;
private IDbCommand _command = null;
private Queue _selects = new Queue();
bool _rowDataFound = false;
private static long _nextId = 0;
private long _id = 0;
private DataExchangeFactory _dataExchangeFactory = null;
private ISqlMapSession _session = null;
private IMappedStatement _mappedStatement = null;
private int _currentResultMapIndex = -1;
// Used by N+1 Select solution
// Holds [IResultMap, IDictionary] couple where the IDictionary holds [key, result object]
private IDictionary _uniqueKeys = null;
#endregion
#region Properties
///
/// Gets the unique keys.
///
/// The ResultMap.
///
/// Returns [key, result object] which holds the result objects that have
/// already been build during this request with this
///
public IDictionary GetUniqueKeys(IResultMap map)
{
if (_uniqueKeys == null)
{
return null;
}
return (IDictionary)_uniqueKeys[map];
}
///
/// Sets the unique keys.
///
/// The map.
/// The keys.
public void SetUniqueKeys(IResultMap map, IDictionary keys)
{
if (_uniqueKeys == null)
{
_uniqueKeys = new Hashtable();
}
_uniqueKeys.Add(map, keys);
}
///
/// The current .
///
public IMappedStatement MappedStatement
{
set { _mappedStatement = value; }
get { return _mappedStatement; }
}
///
/// Gets the current .
///
/// The statement.
public IStatement Statement
{
get { return _statement; }
}
///
/// The current .
///
public ISqlMapSession Session
{
get { return _session; }
}
///
/// The to execute
///
public IDbCommand IDbCommand
{
set { _command = value; }
get { return _command; }
}
///
/// Indicate if the statement have find data
///
public bool IsRowDataFound
{
set { _rowDataFound = value; }
get { return _rowDataFound; }
}
///
/// The 'select' result property to process after having process the main properties.
///
public Queue QueueSelect
{
get { return _selects; }
set { _selects = value; }
}
///
/// The current used by this request.
///
public IResultMap CurrentResultMap
{
get { return _statement.ResultsMap[_currentResultMapIndex]; }
}
///
/// Moves to the next result map.
///
///
public bool MoveNextResultMap()
{
if (_currentResultMapIndex < _statement.ResultsMap.Count - 1)
{
_currentResultMapIndex++;
return true;
}
return false;
}
///
/// The used by this request.
///
public ParameterMap ParameterMap
{
set { _parameterMap = value; }
get { return _parameterMap; }
}
///
/// The used by this request.
///
public PreparedStatement PreparedStatement
{
get { return _preparedStatement; }
set { _preparedStatement = value; }
}
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The data exchange factory.
/// The session.
/// The statement
public RequestScope(
DataExchangeFactory dataExchangeFactory,
ISqlMapSession session,
IStatement statement
)
{
_errorContext = new ErrorContext();
_statement = statement;
_parameterMap = statement.ParameterMap;
_session = session;
_dataExchangeFactory = dataExchangeFactory;
_id = GetNextId();
}
#endregion
#region Method
///
/// Check if the specify object is equal to the current object.
///
///
///
public override bool Equals(object obj)
{
if (this == obj) { return true; }
if (!(obj is RequestScope)) { return false; }
RequestScope scope = (RequestScope)obj;
if (_id != scope._id) return false;
return true;
}
///
/// Get the HashCode for this RequestScope
///
///
public override int GetHashCode()
{
return (int)(_id ^ (_id >> 32));
}
///
/// Method to get a unique ID
///
/// The new ID
[MethodImpl(MethodImplOptions.Synchronized)]
public static long GetNextId()
{
return _nextId++;
}
#endregion
#region IScope Members
///
/// A factory for DataExchange objects
///
public DataExchangeFactory DataExchangeFactory
{
get { return _dataExchangeFactory; }
}
///
/// Get the request's error context
///
public ErrorContext ErrorContext
{
get { return _errorContext; }
}
#endregion
}
}