Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NH-2408 - Fix pessimistic locking of union subclasses in Microsoft SQ…
…L Server dialects
- Loading branch information
Showing
6 changed files
with
216 additions
and
42 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
src/NHibernate.Test/DialectTest/LockHintAppenderFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Collections.Generic; | ||
using NHibernate.Dialect; | ||
using NHibernate.SqlCommand; | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.DialectTest | ||
{ | ||
[TestFixture] | ||
public class LockHintAppenderFixture | ||
{ | ||
private const string MsSql2000LockHint = " with (updlock, rowlock)"; | ||
private MsSql2000Dialect.LockHintAppender _appender; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_appender = new MsSql2000Dialect.LockHintAppender(new MsSql2000Dialect(), new Dictionary<string, LockMode> { {"person", LockMode.Upgrade} }); | ||
} | ||
|
||
[Test] | ||
public void AppendHintToSingleTableAlias() | ||
{ | ||
const string expectedQuery1 = "select * from Person person with (updlock, rowlock)"; | ||
const string expectedQuery2 = "select * from Person as person with (updlock, rowlock)"; | ||
|
||
var result1 = _appender.AppendLockHint(new SqlString(expectedQuery1.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result1.ToString(), Is.EqualTo(expectedQuery1)); | ||
|
||
var result2 = _appender.AppendLockHint(new SqlString(expectedQuery2.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result2.ToString(), Is.EqualTo(expectedQuery2)); | ||
} | ||
|
||
[Test] | ||
public void AppendHintToJoinedTableAlias() | ||
{ | ||
const string expectedQuery = | ||
"select * from Person person with (updlock, rowlock) inner join Country country on person.Id = country.Id"; | ||
|
||
var result = _appender.AppendLockHint(new SqlString(expectedQuery.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result.ToString(), Is.EqualTo(expectedQuery)); | ||
} | ||
|
||
[Test] | ||
public void AppendHintToUnionTableAlias() | ||
{ | ||
const string expectedQuery = | ||
"select Id, Name from (select Id, CONCAT(FirstName, LastName) from Employee with (updlock, rowlock) union all select Id, CONCAT(FirstName, LastName) from Manager with (updlock, rowlock)) as person"; | ||
|
||
var result = _appender.AppendLockHint(new SqlString(expectedQuery.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result.ToString(), Is.EqualTo(expectedQuery)); | ||
} | ||
|
||
[Test] | ||
public void ShouldIgnoreCasing() | ||
{ | ||
const string expectedQuery = | ||
"select Id, Name FROM (select Id, Name FROM Employee with (updlock, rowlock) union all select Id, Name from Manager with (updlock, rowlock)) as person"; | ||
|
||
var result = _appender.AppendLockHint(new SqlString(expectedQuery.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result.ToString(), Is.EqualTo(expectedQuery)); | ||
} | ||
|
||
[Test] | ||
public void ShouldHandleEscapingInSubselect() | ||
{ | ||
const string expectedQuery = | ||
"select Id, Name from (select Id, Name from [Employee] with (updlock, rowlock) union all select Id, Name from [Manager] with (updlock, rowlock)) as person"; | ||
|
||
var result = _appender.AppendLockHint(new SqlString(expectedQuery.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result.ToString(), Is.EqualTo(expectedQuery)); | ||
} | ||
|
||
[Test] | ||
public void ShouldHandleMultilineQuery() | ||
{ | ||
const string expectedQuery = @" | ||
select Id, Name from | ||
(select Id, Name from Employee with (updlock, rowlock) union all | ||
select Id, Name from Manager with (updlock, rowlock)) | ||
as person"; | ||
|
||
var result = _appender.AppendLockHint(new SqlString(expectedQuery.Replace(MsSql2000LockHint, string.Empty))); | ||
Assert.That(result.ToString(), Is.EqualTo(expectedQuery)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using NUnit.Framework; | ||
|
||
namespace NHibernate.Test.NHSpecificTest.NH2408 | ||
{ | ||
public class Fixture : BugTestCase | ||
{ | ||
[Test] | ||
public void ShouldGenerateCorrectSqlStatement() | ||
{ | ||
using (var session = OpenSession()) | ||
{ | ||
var query = session.CreateQuery("from Animal a where a.Name = ?"); | ||
query.SetParameter(0, "Prince"); | ||
|
||
query.SetLockMode("a", LockMode.Upgrade); | ||
|
||
Assert.DoesNotThrow(() => query.List()); | ||
} | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/NHibernate.Test/NHSpecificTest/NH2408/Mappings.hbm.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" | ||
namespace="NHibernate.Test.NHSpecificTest.NH2408" | ||
assembly="NHibernate.Test" | ||
> | ||
<class name="Animal"> | ||
<id name="Id"> | ||
<generator class="increment"/> | ||
</id> | ||
<property name="Name"/> | ||
|
||
<union-subclass name="Dog" table="`Dog`"> | ||
</union-subclass> | ||
|
||
<union-subclass name="Cat"> | ||
</union-subclass> | ||
|
||
</class> | ||
|
||
</hibernate-mapping> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace NHibernate.Test.NHSpecificTest.NH2408 | ||
{ | ||
public class Animal | ||
{ | ||
public virtual int Id { get; set; } | ||
|
||
public virtual string Name { get; set; } | ||
} | ||
|
||
public class Dog : Animal | ||
{ | ||
} | ||
|
||
public class Cat : Animal | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters