#region Apache Notice
/*****************************************************************************
* $Revision: 476843 $
* $LastChangedDate$
* $LastChangedBy$
*
* 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
using System.Data;
using Apache.Ibatis.Common.Contracts;
using Apache.Ibatis.DataMapper.Data;
using Apache.Ibatis.DataMapper.DataExchange;
using Apache.Ibatis.DataMapper.MappedStatements;
using Apache.Ibatis.DataMapper.Model.ParameterMapping;
using Apache.Ibatis.DataMapper.Model.Sql.SimpleDynamic;
using Apache.Ibatis.DataMapper.Model.Statements;
using Apache.Ibatis.DataMapper.Scope;
using Apache.Ibatis.DataMapper.Session;
namespace Apache.Ibatis.DataMapper.Model.Sql.External
{
///
/// Represents ths SQL of a mapped statement with an external .
///
public class ExternalSql :ISql
{
private readonly IStatement statement = null;
private readonly InlineParemeterMapBuilder inlineParemeterMapBuilder = null;
private readonly DataExchangeFactory dataExchangeFactory = null;
private readonly DBHelperParameterCache dbHelperParameterCache = null;
private readonly string commandText = string.Empty;
///
/// Gets the command text.
///
/// The command text.
public string CommandText
{
get { return commandText; }
}
///
/// Initializes a new instance of the class.
///
/// The model store.
/// The statement.
/// The command text.
public ExternalSql(
IModelStore modelStore,
IStatement statement,
string commandText)
{
Contract.Require.That(modelStore, Is.Not.Null).When("retrieving argument modelStore in ExternalSql constructor");
Contract.Require.That(statement, Is.Not.Null).When("retrieving argument statement in ExternalSql constructor");
this.statement = statement;
this.commandText = commandText;
dataExchangeFactory = modelStore.DataExchangeFactory;
dbHelperParameterCache = modelStore.DBHelperParameterCache;
inlineParemeterMapBuilder = new InlineParemeterMapBuilder(modelStore);
}
#region ISql Members
///
/// Builds a new and the text to execute.
///
/// The .
/// The parameter object (used by DynamicSql/SimpleDynamicSql).
/// Use to complete the sql statement.
/// The current session
/// A new .
public RequestScope GetRequestScope(IMappedStatement mappedStatement, object parameterObject, ISession session)
{
RequestScope request = new RequestScope(dataExchangeFactory, session, statement);
string sqlCommandText = statement.SqlSource.GetSql(mappedStatement, parameterObject);
string newSqlCommandText = string.Empty;
if (request.ParameterMap==null)
{
request.ParameterMap = inlineParemeterMapBuilder.BuildInlineParemeterMap(statement, sqlCommandText, out newSqlCommandText);
}
// Processes $substitutions$ after DynamicSql
if (SimpleDynamicSql.IsSimpleDynamicSql(newSqlCommandText))
{
newSqlCommandText = new SimpleDynamicSql(
dataExchangeFactory,
dbHelperParameterCache,
newSqlCommandText,
statement).GetSql(parameterObject);
}
request.PreparedStatement = BuildPreparedStatement(session, request, newSqlCommandText);
return request;
}
#endregion
///
/// Builds the prepared statement.
///
/// The session.
/// The request.
/// The SQL statement.
///
private PreparedStatement BuildPreparedStatement(ISession session, RequestScope request, string sqlStatement)
{
PreparedStatementFactory factory = new PreparedStatementFactory(session, dbHelperParameterCache, request, statement, sqlStatement);
return factory.Prepare(true);
}
}
}