#region Apache Notice
/*****************************************************************************
* $Header: $
* $Revision: 513043 $
* $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
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Apache.Ibatis.Common.Contracts;
using Apache.Ibatis.DataMapper.Session;
using Apache.Ibatis.DataMapper.Model;
using Apache.Ibatis.DataMapper.MappedStatements;
using Apache.Ibatis.DataMapper.Exceptions;
namespace Apache.Ibatis.DataMapper
{
///
/// The default implementation of the
///
public class DataMapper : IDataMapper, IModelStoreAccessor
{
private readonly IModelStore modelStore = null;
private readonly ISessionStore sessionStore = null;
private readonly ISessionFactory sessionFactory = null;
///
/// Initializes a new instance of the class.
///
/// The model store.
public DataMapper(IModelStore modelStore)
{
Contract.Require.That(modelStore, Is.Not.Null).When("retrieving argument modelStore in DataMapper constructor");
this.modelStore = modelStore;
this.modelStore.DataMapper = this;
sessionStore = modelStore.SessionStore;
sessionFactory = modelStore.SessionFactory;
}
#region IDataMapper Members
///
/// Executes a Sql INSERT statement.
/// Insert is a bit different from other update methods, as it
/// provides facilities for returning the primary key of the
/// newly inserted row (rather than the effected rows). This
/// functionality is of course optional.
///
/// The parameter object is generally used to supply the input
/// data for the INSERT values.
///
/// The name of the statement to execute.
/// The parameter object.
///
/// The primary key of the newly inserted row.
/// This might be automatically generated by the RDBMS,
/// or selected from a sequence table or other source.
///
public object Insert(string statementId, object parameterObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteInsert(sessionScope.Session, parameterObject);
}
}
///
/// Alias to QueryForMap, .NET spirit.
/// Feature idea by Ted Husted.
///
/// The name of the sql statement to execute.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
/// The property of the result object to be used as the value (or null)
///
/// A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.
///
/// If a transaction is not in progress, or the database throws an exception.
public IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty, string valueProperty)
{
return QueryForMap(statementName, parameterObject, keyProperty, valueProperty);
}
///
/// Alias to QueryForMap, .NET spirit.
/// Feature idea by Ted Husted.
///
/// The name of the sql statement to execute.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
///
/// A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.
///
public IDictionary QueryForDictionary(string statementName, object parameterObject, string keyProperty)
{
return QueryForMap(statementName, parameterObject, keyProperty);
}
///
/// Executes a Sql SELECT statement that returns data to populate
/// a number of result objects.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// An Ilist object used to hold the objects.
public void QueryForList(string statementId, object parameterObject, IList resultObject)
{
if (resultObject == null)
{
throw new DataMapperException("resultObject parameter must be instantiated before being passed to QueryForList");
}
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
statement.ExecuteQueryForList(sessionScope.Session, parameterObject, resultObject);
}
}
///
/// Executes a Sql SELECT statement that returns data to populate
/// a number of result objects.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// A List of result objects.
public IList QueryForList(string statementId, object parameterObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForList(sessionScope.Session, parameterObject);
}
}
///
/// 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 entire result object.
///
/// The name of the sql statement to execute.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
///
/// A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.
///
public IDictionary QueryForMap(string statementName, object parameterObject, string keyProperty)
{
return QueryForMap(statementName, parameterObject, keyProperty, null);
}
///
/// 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.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
/// The property of the result object to be used as the value (or null)
///
/// A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.
///
/// If a transaction is not in progress, or the database throws an exception.
public IDictionary QueryForMap(string statementId, object parameterObject, string keyProperty, string valueProperty)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForMap(sessionScope.Session, parameterObject, keyProperty, valueProperty);
}
}
///
/// Runs a query with a custom object that gets a chance to deal
/// with each row as it is processed.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
/// The property of the result object to be used as the value (or null)
/// The row delegate.
///
/// A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.
///
/// If a transaction is not in progress, or the database throws an exception.
public IDictionary QueryForMapWithRowDelegate(string statementId, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate rowDelegate)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForMapWithRowDelegate(sessionScope.Session, parameterObject, keyProperty, valueProperty, rowDelegate);
}
}
///
/// Executes a Sql SELECT statement that returns a single object of the type of the
/// resultObject parameter.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// An object of the type to be returned.
///
/// The single result object populated with the result set data.
///
public object QueryForObject(string statementId, object parameterObject, object resultObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForObject(sessionScope.Session, parameterObject, resultObject);
}
}
///
/// Executes a Sql SELECT statement that returns that returns data
/// to populate a single object instance.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
///
/// The single result object populated with the result set data.
///
public object QueryForObject(string statementId, object parameterObject)
{
return QueryForObject(statementId, parameterObject, null);
}
///
/// Runs a query for list with a custom object that gets a chance to deal
/// with each row as it is processed.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The row delegate.
/// A List of result objects.
public IList QueryWithRowDelegate(string statementId, object parameterObject, RowDelegate rowDelegate)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForRowDelegate(sessionScope.Session, parameterObject, rowDelegate);
}
}
///
/// Executes a Sql UPDATE statement.
/// Update can also be used for any other update statement type,
/// such as inserts and deletes. Update returns the number of
/// rows effected.
///
/// The parameter object is generally used to supply the input
/// data for the UPDATE values as well as the WHERE clause parameter(s).
///
/// The statement id.
/// The parameter object.
/// The number of rows effected.
public int Update(string statementId, object parameterObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteUpdate(sessionScope.Session, parameterObject);
}
}
///
/// 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.
///
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
/// The property of the result object to be used as the value (or null)
///
/// A IDictionary of object containing the rows keyed by keyProperty.
///
/// If a transaction is not in progress, or the database throws an exception.
public IDictionary QueryForDictionary(string statementId, object parameterObject, string keyProperty, string valueProperty)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForDictionary(sessionScope.Session, parameterObject, keyProperty, valueProperty);
}
}
///
/// 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 entire result object.
///
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
///
/// A IDictionary of object containing the rows keyed by keyProperty.
///
public IDictionary QueryForDictionary(string statementId, object parameterObject, string keyProperty)
{
return QueryForDictionary(statementId, parameterObject, keyProperty, null);
}
///
/// Runs a query with a custom object that gets a chance to deal
/// with each row as it is processed.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The property of the result object to be used as the key.
/// The property of the result object to be used as the value (or null)
/// A delegate called once per row in the QueryForDictionary method>
///
/// A IDictionary (Hashtable) of object containing the rows keyed by keyProperty.
///
/// If a transaction is not in progress, or the database throws an exception.
public IDictionary QueryForDictionary(string statementId, object parameterObject, string keyProperty, string valueProperty, DictionaryRowDelegate rowDelegate)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForDictionary(sessionScope.Session, parameterObject, keyProperty, valueProperty, rowDelegate);
}
}
///
/// Executes a Sql SELECT statement that returns a single object of the type of the
/// resultObject parameter.
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// An object of the type to be returned.
///
/// The single result object populated with the result set data.
///
public T QueryForObject(string statementId, object parameterObject, T instanceObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForObject(sessionScope.Session, parameterObject, instanceObject);
}
}
///
/// Executes a Sql SELECT statement that returns that returns data
/// to populate a single object instance.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
///
/// The single result object populated with the result set data.
///
public T QueryForObject(string statementId, object parameterObject)
{
return QueryForObject(statementId, parameterObject, default(T));
}
///
/// Executes a Sql SELECT statement that returns data to populate
/// a number of result objects.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// A List of result objects.
public IList QueryForList(string statementId, object parameterObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForList(sessionScope.Session, parameterObject);
}
}
///
/// Executes a Sql SELECT statement that returns data to populate
/// a number of result objects.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// An Ilist object used to hold the objects.
public void QueryForList(string statementId, object parameterObject, IList resultObject)
{
if (resultObject == null)
{
throw new DataMapperException("resultObject parameter must be instantiated before being passed to SqlMapper.QueryForList");
}
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
statement.ExecuteQueryForList(sessionScope.Session, parameterObject, resultObject);
}
}
///
/// Runs a query for list with a custom object that gets a chance to deal
/// with each row as it is processed.
///
/// The parameter object is generally used to supply the input
/// data for the WHERE clause parameter(s) of the SELECT statement.
///
///
/// The statement id.
/// The object used to set the parameters in the SQL.
/// The row delegate.
/// A List of result objects.
public IList QueryWithRowDelegate(string statementId, object parameterObject, RowDelegate rowDelegate)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForRowDelegate(sessionScope.Session, parameterObject, rowDelegate);
}
}
///
/// Executes a Sql DELETE statement.
/// Delete returns the number of rows effected.
///
/// The statement id.
/// The parameter object.
/// The number of rows effected.
public int Delete(string statementId, object parameterObject)
{
return Update(statementId, parameterObject);
}
///
/// Executes a SQL SELECT statement that returns data
/// to populate a DataTable.
/// If a resultMap is specified, the column name will be the result property name.
///
/// The statement id.
/// The parameter object.
/// A DataTable
public DataTable QueryForDataTable(string statementId, object parameterObject)
{
using (var sessionScope = new DataMapperLocalSessionScope(sessionStore, sessionFactory))
{
IMappedStatement statement = GetMappedStatement(statementId);
return statement.ExecuteQueryForDataTable(sessionScope.Session, parameterObject);
}
}
#endregion
#region IModelStoreAccessor Members
///
/// Gets the
///
/// The model store.
IModelStore IModelStoreAccessor.ModelStore
{
get { return modelStore; }
}
#endregion
private IMappedStatement GetMappedStatement(string statementId)
{
return modelStore.GetMappedStatement(statementId);
}
}
}