Skip to content

Commit 06ad6ac

Browse files
committed
NH-1316 - Fix returing generated identity for PostgreSQL
Fixes #1201
1 parent 1024b71 commit 06ad6ac

File tree

12 files changed

+91
-23
lines changed

12 files changed

+91
-23
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH2204/Fixture.cs renamed to src/NHibernate.Test/Async/NHSpecificTest/NH1316/Fixture.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using NHibernate.Dialect;
1515

1616

17-
namespace NHibernate.Test.NHSpecificTest.NH2204
17+
namespace NHibernate.Test.NHSpecificTest.NH1316
1818
{
1919
using System.Threading.Tasks;
2020
[TestFixture]
@@ -66,7 +66,7 @@ protected override void OnTearDown()
6666

6767

6868
[Test]
69-
public async Task KnownFailure_Correct_Id_Returned_When_Using_TriggerAsync()
69+
public async Task Correct_Id_Returned_When_Using_TriggerAsync()
7070
{
7171
//We expected this test to fail - if the problem has been fixed, clean-up the test.
7272
var entity1 = new Parent {Name = "Parent1_0"}; // when saved this entity should have the id of 1
@@ -100,13 +100,10 @@ public async Task KnownFailure_Correct_Id_Returned_When_Using_TriggerAsync()
100100
await (s.SaveAsync(entity3));
101101
await (s.FlushAsync());
102102

103-
Warn.Unless(
103+
Assert.That(
104104
entity3.Id,
105105
Is.EqualTo(3),
106106
"oh uh - it would appear that lastval() is not our friend when a trigger updates other sequences.");
107-
108-
// now would be a good time to look at the data in the tables and see that they have the IDs as expected
109-
// which are not the same as those returned by nhibernate
110107
}
111108
}
112109
}

src/NHibernate.Test/NHSpecificTest/NH2204/Fixture.cs renamed to src/NHibernate.Test/NHSpecificTest/NH1316/Fixture.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
2-
using System.Data.Common;
3-
using NUnit.Framework;
42
using NHibernate.Dialect;
3+
using NUnit.Framework;
54

6-
7-
namespace NHibernate.Test.NHSpecificTest.NH2204
5+
namespace NHibernate.Test.NHSpecificTest.NH1316
86
{
97
[TestFixture]
108
public class Fixture : BugTestCase
@@ -55,7 +53,7 @@ protected override void OnTearDown()
5553

5654

5755
[Test]
58-
public void KnownFailure_Correct_Id_Returned_When_Using_Trigger()
56+
public void Correct_Id_Returned_When_Using_Trigger()
5957
{
6058
//We expected this test to fail - if the problem has been fixed, clean-up the test.
6159
var entity1 = new Parent {Name = "Parent1_0"}; // when saved this entity should have the id of 1
@@ -89,13 +87,10 @@ public void KnownFailure_Correct_Id_Returned_When_Using_Trigger()
8987
s.Save(entity3);
9088
s.Flush();
9189

92-
Warn.Unless(
90+
Assert.That(
9391
entity3.Id,
9492
Is.EqualTo(3),
9593
"oh uh - it would appear that lastval() is not our friend when a trigger updates other sequences.");
96-
97-
// now would be a good time to look at the data in the tables and see that they have the IDs as expected
98-
// which are not the same as those returned by nhibernate
9994
}
10095
}
10196
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
4+
assembly="NHibernate.Test"
5+
namespace="NHibernate.Test.NHSpecificTest.NH1316">
6+
7+
<class name="Parent">
8+
<id name="Id">
9+
<generator class="native" />
10+
11+
</id>
12+
<property name="Name" />
13+
14+
</class>
15+
16+
17+
<class name="ParentHistory" table="parent_history">
18+
<id name="HistId">
19+
<generator class="native" />
20+
</id>
21+
22+
<property name="HistWhen" />
23+
<property name="Id" />
24+
<property name="Name" />
25+
</class>
26+
27+
</hibernate-mapping>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH1316
4+
{
5+
public class Parent
6+
{
7+
public virtual int Id { get; set; }
8+
public virtual string Name { get; set; }
9+
}
10+
11+
public class ParentHistory
12+
{
13+
public virtual int HistId { get; set; }
14+
public virtual DateTime HistWhen { get; set; }
15+
16+
public virtual int Id { get; set; }
17+
public virtual string Name { get; set; }
18+
}
19+
}

src/NHibernate.Test/NHSpecificTest/NH2204/Mappings.hbm.xml

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/NHibernate.Test/NHSpecificTest/NH2204/Model.cs

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/NHibernate/Dialect/Dialect.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ public virtual bool HasDataTypeInIdentityColumn
976976
}
977977

978978
/// <summary>
979-
/// Provided we <see cref="SupportsInsertSelectIdentity"/>, then attch the
979+
/// Provided we <see cref="SupportsInsertSelectIdentity"/>, then attach the
980980
/// "select identity" clause to the insert statement.
981981
/// </summary>
982982
/// <param name="insertString">The insert command </param>
@@ -990,6 +990,22 @@ public virtual SqlString AppendIdentitySelectToInsert(SqlString insertString)
990990
return insertString;
991991
}
992992

993+
/// <summary>
994+
/// Provided we <see cref="SupportsInsertSelectIdentity"/>, then attach the
995+
/// "select identity" clause to the insert statement.
996+
/// </summary>
997+
/// <param name="insertString">The insert command </param>
998+
/// <param name="identifierColumnName">The identifier name</param>
999+
/// <returns>
1000+
/// The insert command with any necessary identity select clause attached.
1001+
/// Note, if <see cref="SupportsInsertSelectIdentity"/> == false then
1002+
/// the insert-string should be returned without modification.
1003+
/// </returns>
1004+
public virtual SqlString AppendIdentitySelectToInsert(SqlString insertString, string identifierColumnName)
1005+
{
1006+
return AppendIdentitySelectToInsert(insertString);
1007+
}
1008+
9931009
/// <summary>
9941010
/// Get the select command to use to retrieve the last generated IDENTITY
9951011
/// value for a particular table.

src/NHibernate/Dialect/PostgreSQL81Dialect.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public override SqlString AppendIdentitySelectToInsert(SqlString insertSql)
109109
return insertSql.Append("; " + IdentitySelectString);
110110
}
111111

112+
public override SqlString AppendIdentitySelectToInsert(SqlString insertString, string identifierColumnName)
113+
{
114+
return insertString.Append(" returning ").Append(identifierColumnName);
115+
}
116+
112117
public override bool SupportsInsertSelectIdentity
113118
{
114119
get { return true; }

src/NHibernate/Dialect/PostgreSQLDialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public override string GetDropSequenceString(string sequenceName)
175175

176176
public override SqlString AddIdentifierOutParameterToInsert(SqlString insertString, string identifierColumnName, string parameterName)
177177
{
178-
return insertString.Append(" returning " + identifierColumnName);
178+
return insertString.Append(" returning ").Append(identifierColumnName);
179179
}
180180

181181
public override InsertGeneratedIdentifierRetrievalMethod InsertGeneratedIdentifierRetrievalMethod

src/NHibernate/Id/IdentityGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public InsertSelectDelegate(IPostInsertIdentityPersister persister, ISessionFact
6060

6161
public override IdentifierGeneratingInsert PrepareIdentifierGeneratingInsert()
6262
{
63-
InsertSelectIdentityInsert insert = new InsertSelectIdentityInsert(factory);
63+
var insert = new InsertSelectIdentityInsert(factory, persister.RootTableKeyColumnNames[0]);
6464
insert.AddIdentityColumn(persister.RootTableKeyColumnNames[0]);
6565
return insert;
6666
}

src/NHibernate/Id/Insert/InsertSelectIdentityInsert.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NHibernate.Engine;
23
using NHibernate.SqlCommand;
34

@@ -10,14 +11,24 @@ namespace NHibernate.Id.Insert
1011
/// </summary>
1112
public class InsertSelectIdentityInsert : IdentifierGeneratingInsert
1213
{
14+
private readonly string _identifierColumnName;
15+
16+
//Since v5.2
17+
[Obsolete("Please use constructor accepting identifierColumnName parameter.")]
1318
public InsertSelectIdentityInsert(ISessionFactoryImplementor factory)
1419
: base(factory)
1520
{
1621
}
1722

23+
public InsertSelectIdentityInsert(ISessionFactoryImplementor factory, string identifierColumnName)
24+
: base(factory)
25+
{
26+
_identifierColumnName = identifierColumnName;
27+
}
28+
1829
public override SqlString ToSqlString()
1930
{
20-
return Dialect.AppendIdentitySelectToInsert(base.ToSqlString());
31+
return Dialect.AppendIdentitySelectToInsert(base.ToSqlString(), _identifierColumnName);
2132
}
2233
}
2334
}

src/NHibernate/Id/Insert/ReturningIdentifierInsert.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public class ReturningIdentifierInsert : NoCommentsInsert
1515
private readonly string identifierColumnName;
1616
private readonly string returnParameterName;
1717

18-
public ReturningIdentifierInsert(ISessionFactoryImplementor factory, string identifierColumnName,
19-
string returnParameterName) : base(factory)
18+
public ReturningIdentifierInsert(ISessionFactoryImplementor factory, string identifierColumnName, string returnParameterName)
19+
: base(factory)
2020
{
2121
this.returnParameterName = returnParameterName;
2222
this.identifierColumnName = identifierColumnName;

0 commit comments

Comments
 (0)