#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: 476843 $
* $Date$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2008/2005 - The Apache Software Foundation
*
*
* 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.Data;
using System.Runtime.CompilerServices;
using Apache.Ibatis.DataMapper.Model.ParameterMapping;
using Apache.Ibatis.DataMapper.Model.ResultMapping;
using Apache.Ibatis.DataMapper.Model.Statements;
using Apache.Ibatis.DataMapper.DataExchange;
using Apache.Ibatis.DataMapper.MappedStatements;
using Apache.Ibatis.DataMapper.Session;
using System.Collections.Generic;
#endregion
namespace Apache.Ibatis.DataMapper.Scope
{
///
/// Hold data during the process of a mapped statement.
///
public class RequestScope : IScope
{
#region Fields
private readonly IStatement statement = null;
private readonly ErrorContext errorContext = null;
private ParameterMap parameterMap = null;
private PreparedStatement preparedStatement = null;
private IDbCommand command = null;
private Queue selects = new Queue();
private bool rowDataFound = false;
private static long nextId = 0;
private readonly long id = 0;
private readonly DataExchangeFactory dataExchangeFactory = null;
private readonly ISession session = null;
private IMappedStatement mappedStatement = null;
private int currentResultMapIndex = -1;
// Used by N+1 Select solution
// Holds [IResultMap, IDictionary] couple where the IDictionary holds [string key,object result]
private IDictionary> uniqueKeys = null;
// Holds [IResultMap, IDictionary] couple where the IDictionary holds [string key,object result]
// to resolve circular reference
private IDictionary> cirularKeys = null;
#endregion
#region Properties
///
/// Gets the circular keys.
///
/// The ResultMap.
///
/// Returns [string key, object result] which holds the result objects that have
/// already been build during this request with this
///
public IDictionary GetCirularKeys(IResultMap map)
{
if (cirularKeys == null)
{
return null;
}
IDictionary keys = null;
cirularKeys.TryGetValue(map, out keys);
return keys;
}
///
/// Sets the cirular keys.
///
/// The map.
/// The keys.
public void SetCirularKeys(IResultMap map, IDictionary keys)
{
if (cirularKeys == null)
{
cirularKeys = new Dictionary>();
}
cirularKeys.Add(map, keys);
}
///
/// Gets the unique keys, used to resolve groupBy
///
/// The ResultMap.
///
/// Returns [string key, object result] 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;
}
IDictionary keys = null;
uniqueKeys.TryGetValue(map, out keys);
return keys;
}
///
/// Sets the unique keys.
///
/// The map.
/// The keys.
public void SetUniqueKeys(IResultMap map, IDictionary keys)
{
if (uniqueKeys == null)
{
uniqueKeys = new Dictionary>();
}
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 ISession 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 DelayedLoad
{
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,
ISession session,
IStatement statement
)
{
errorContext = new ErrorContext();
this.statement = statement;
parameterMap = statement.ParameterMap;
this.session = session;
this.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
}
}