Index: IBatisNet.DataMapper.Test/IBatisNet.DataMapper.Test.2005.csproj
===================================================================
--- IBatisNet.DataMapper.Test/IBatisNet.DataMapper.Test.2005.csproj (revision 417192)
+++ IBatisNet.DataMapper.Test/IBatisNet.DataMapper.Test.2005.csproj (working copy)
@@ -409,6 +409,7 @@
+
Index: IBatisNet.DataMapper.Test/Maps/MSSQL/SqlClient/Category.xml
===================================================================
--- IBatisNet.DataMapper.Test/Maps/MSSQL/SqlClient/Category.xml (revision 417192)
+++ IBatisNet.DataMapper.Test/Maps/MSSQL/SqlClient/Category.xml (working copy)
@@ -114,6 +114,10 @@
ps_InsertCategorie
+
+
+ ps_InsertCategorieWithReturnValue
+
Index: IBatisNet.DataMapper.Test/NUnit/SqlMapTests/MSSQL/ProcedureTest.cs
===================================================================
--- IBatisNet.DataMapper.Test/NUnit/SqlMapTests/MSSQL/ProcedureTest.cs (revision 417192)
+++ IBatisNet.DataMapper.Test/NUnit/SqlMapTests/MSSQL/ProcedureTest.cs (working copy)
@@ -25,8 +25,10 @@
InitScript( sqlMap.DataSource, ScriptDirectory + "category-procedure.sql" );
InitScript( sqlMap.DataSource, ScriptDirectory + "account-init.sql" );
InitScript( sqlMap.DataSource, ScriptDirectory + "account-procedure.sql", false );
+ InitScript( sqlMap.DataSource, ScriptDirectory + "category-procedureWithReturn.sql", false );
InitScript( sqlMap.DataSource, ScriptDirectory + "ps_SelectAccount.sql", false );
InitScript( sqlMap.DataSource, ScriptDirectory + "swap-procedure.sql" );
+
}
///
@@ -91,6 +93,34 @@
Assert.AreEqual(2, category.Id );
}
+ ///
+ /// Test an insert with via a store procedure and getting the generatedKey from a t-sql return statement
+ ///
+ [Test]
+ public void InsertTestIdentityViaProcedureWithReturn ( )
+ {
+ Category category = new Category ( );
+ category.Name = "Mapping object relational";
+
+ int categoryID = ( int ) sqlMap.Insert ( "InsertCategoryViaStoreProcedureWithReturn", category );
+ Assert.AreEqual ( 1, categoryID );
+
+ Category category2 = new Category ( );
+ category2.Name = "Nausicaa";
+
+ int categoryID2 = ( int ) sqlMap.Insert ( "InsertCategoryViaStoreProcedureWithReturn", category2 );
+ Assert.AreEqual ( 2, categoryID2 );
+
+ Category category3 = sqlMap.QueryForObject ( "GetCategory", categoryID2 ) ;
+ Category category4 = sqlMap.QueryForObject ( "GetCategory", categoryID );
+
+ Assert.AreEqual ( categoryID2, category3.Id );
+ Assert.AreEqual ( category2.Name, category3.Name );
+
+ Assert.AreEqual ( categoryID, category4.Id );
+ Assert.AreEqual ( category.Name, category4.Name );
+ }
+
///
/// Test store procedure with output parameters
///
Index: IBatisNet.DataMapper.Test/Scripts/MSSQL/category-init.sql
===================================================================
--- IBatisNet.DataMapper.Test/Scripts/MSSQL/category-init.sql (revision 417192)
+++ IBatisNet.DataMapper.Test/Scripts/MSSQL/category-init.sql (working copy)
@@ -15,3 +15,6 @@
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ps_InsertCategorie]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[ps_InsertCategorie]
+
+if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ps_InsertCategorieWithReturnValue]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
+drop procedure [dbo].[ps_InsertCategorieWithReturnValue]
\ No newline at end of file
Index: IBatisNet.DataMapper/Configuration/Statements/Procedure.cs
===================================================================
--- IBatisNet.DataMapper/Configuration/Statements/Procedure.cs (revision 417192)
+++ IBatisNet.DataMapper/Configuration/Statements/Procedure.cs (working copy)
@@ -2,7 +2,7 @@
#region Apache Notice
/*****************************************************************************
* $Header: $
- * $Revision: $
+ * $Revision$
* $Date$
*
* iBATIS.NET Data Mapper
@@ -27,6 +27,7 @@
#region Using
using System;
using System.Data;
+using System.Xml ;
using System.Xml.Serialization;
using IBatisNet.Common.Exceptions;
@@ -42,10 +43,15 @@
[Serializable]
[XmlRoot("procedure", Namespace="http://ibatis.apache.org/mapping")]
public class Procedure : Statement
- {
+ {
+ #region Fields
- #region Properties
- ///
+ [NonSerialized]
+ private bool _useReturnValue = false;
+ #endregion
+
+ #region Properties
+ ///
/// The type of the statement StoredProcedure.
///
[XmlIgnoreAttribute]
@@ -63,7 +69,21 @@
get { return string.Empty; }
set { }
}
+
+ ///
+ /// Should procedure return the return value from the procedure
+ ///
+ [XmlIgnore]
+ public bool UseReturnValue
+ {
+ get { return _useReturnValue; }
+ set { _useReturnValue = value; }
+ }
+
+
#endregion
+
+
#region Constructor (s) / Destructor
///
@@ -86,6 +106,12 @@
{
throw new ConfigurationException("The parameterMap attribute is required in the procedure tag named '"+ this.Id +"'.");
}
+
+ XmlAttribute attrUseReturnValue = configurationScope.NodeContext.Attributes[ "useReturnValue" ];
+ if ( attrUseReturnValue != null )
+ {
+ _useReturnValue = bool.Parse ( attrUseReturnValue.Value );
+ }
}
#endregion
Index: IBatisNet.DataMapper/MappedStatements/MappedStatement.cs
===================================================================
--- IBatisNet.DataMapper/MappedStatements/MappedStatement.cs (revision 417192)
+++ IBatisNet.DataMapper/MappedStatements/MappedStatement.cs (working copy)
@@ -766,7 +766,21 @@
{
command.ExecuteNonQuery();
}
- else
+ else if (_statement is Procedure && ((Procedure) _statement).UseReturnValue)
+ {
+ IDataParameter rvp = command.CreateParameter ( );
+ rvp.Direction = ParameterDirection.ReturnValue;
+ command.Parameters.Add ( rvp );
+ command.ExecuteNonQuery ( );
+ generatedKey = rvp.Value ;
+ if ( ( _statement.ResultClass != null ) &&
+ _sqlMap.TypeHandlerFactory.IsSimpleType ( _statement.ResultClass ) )
+ {
+ ITypeHandler typeHandler = _sqlMap.TypeHandlerFactory.GetTypeHandler ( _statement.ResultClass );
+ generatedKey = typeHandler.GetDataBaseValue ( generatedKey, _statement.ResultClass );
+ }
+ }
+ else
{
generatedKey = command.ExecuteScalar();
if ( (_statement.ResultClass!=null) &&
Index: IBatisNet.DataMapper/SqlMap.xsd
===================================================================
--- IBatisNet.DataMapper/SqlMap.xsd (revision 417192)
+++ IBatisNet.DataMapper/SqlMap.xsd (working copy)
@@ -78,6 +78,7 @@
+