Skip to content

Commit 21846c0

Browse files
maca88hazzik
authored andcommitted
Fix invalid DML statements for static where in mapping
1 parent cc61512 commit 21846c0

File tree

10 files changed

+160
-11
lines changed

10 files changed

+160
-11
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Linq;
12+
using NHibernate.Linq;
13+
using NUnit.Framework;
14+
15+
namespace NHibernate.Test.NHSpecificTest.GH2053
16+
{
17+
using System.Threading.Tasks;
18+
[TestFixture]
19+
public class FixtureAsync : BugTestCase
20+
{
21+
protected override void OnSetUp()
22+
{
23+
using (var session = OpenSession())
24+
using (var transaction = session.BeginTransaction())
25+
{
26+
var e1 = new Entity { Description = "DESCRIPTION", Status = 0 };
27+
session.Save(e1);
28+
29+
var e2 = new Entity { Description = "DESCRIPTION", Status = 1 };
30+
session.Save(e2);
31+
32+
transaction.Commit();
33+
}
34+
}
35+
36+
protected override void OnTearDown()
37+
{
38+
using (var session = OpenSession())
39+
using (var transaction = session.BeginTransaction())
40+
{
41+
session.CreateQuery("delete from Entity").ExecuteUpdate();
42+
43+
transaction.Commit();
44+
}
45+
}
46+
47+
[Test]
48+
public async Task TestAsync()
49+
{
50+
var descriptions = new [] { "DESCRIPTION" };
51+
52+
using (var session = OpenSession())
53+
using (var transaction = session.BeginTransaction())
54+
{
55+
var result = await (session.Query<Entity>()
56+
.Where(x => descriptions.Contains(x.Description))
57+
.UpdateAsync(x => new Entity {Description = "DESCRIPTION_UPDATED"}));
58+
Assert.That(result, Is.EqualTo(1));
59+
60+
await (transaction.CommitAsync());
61+
}
62+
}
63+
}
64+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
3+
namespace NHibernate.Test.NHSpecificTest.GH2053
4+
{
5+
public class Entity
6+
{
7+
public virtual int Id { get; protected set; }
8+
9+
public virtual int Status { get; set; }
10+
11+
public virtual string Description { get; set; }
12+
}
13+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Linq;
2+
using NHibernate.Linq;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.GH2053
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
protected override void OnSetUp()
11+
{
12+
using (var session = OpenSession())
13+
using (var transaction = session.BeginTransaction())
14+
{
15+
var e1 = new Entity { Description = "DESCRIPTION", Status = 0 };
16+
session.Save(e1);
17+
18+
var e2 = new Entity { Description = "DESCRIPTION", Status = 1 };
19+
session.Save(e2);
20+
21+
transaction.Commit();
22+
}
23+
}
24+
25+
protected override void OnTearDown()
26+
{
27+
using (var session = OpenSession())
28+
using (var transaction = session.BeginTransaction())
29+
{
30+
session.CreateQuery("delete from Entity").ExecuteUpdate();
31+
32+
transaction.Commit();
33+
}
34+
}
35+
36+
[Test]
37+
public void Test()
38+
{
39+
var descriptions = new [] { "DESCRIPTION" };
40+
41+
using (var session = OpenSession())
42+
using (var transaction = session.BeginTransaction())
43+
{
44+
var result = session.Query<Entity>()
45+
.Where(x => descriptions.Contains(x.Description))
46+
.Update(x => new Entity {Description = "DESCRIPTION_UPDATED"});
47+
Assert.That(result, Is.EqualTo(1));
48+
49+
transaction.Commit();
50+
}
51+
}
52+
}
53+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.GH2053">
4+
<class name="Entity" where="STATUS = 1">
5+
<id name="Id">
6+
<generator class="native" />
7+
</id>
8+
<property name="Status" />
9+
<property name="Description" />
10+
</class>
11+
</hibernate-mapping>

src/NHibernate/Persister/Collection/AbstractCollectionPersister.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,19 +638,19 @@ public bool HasCache
638638

639639
public string GetSQLWhereString(string alias)
640640
{
641-
return sqlWhereStringTemplate?.Replace(Template.Placeholder, alias);
641+
return Template.ReplacePlaceholder(sqlWhereStringTemplate, alias);
642642
}
643643

644644
public string GetSQLOrderByString(string alias)
645645
{
646-
return HasOrdering ? sqlOrderByStringTemplate?.Replace(Template.Placeholder, alias) : string.Empty;
646+
return HasOrdering ? Template.ReplacePlaceholder(sqlOrderByStringTemplate, alias) : string.Empty;
647647
}
648648

649649
public string GetManyToManyOrderByString(string alias)
650650
{
651651
if (IsManyToMany && manyToManyOrderByString != null)
652652
{
653-
return manyToManyOrderByTemplate?.Replace(Template.Placeholder, alias);
653+
return Template.ReplacePlaceholder(manyToManyOrderByTemplate, alias);
654654
}
655655
else
656656
{
@@ -1020,7 +1020,7 @@ private static string[] Qualify(string alias, string[] columnNames, string[] for
10201020
{
10211021
if (columnNames[i] == null)
10221022
{
1023-
result[i] = formulaTemplates[i]?.Replace(Template.Placeholder, alias);
1023+
result[i] = Template.ReplacePlaceholder(formulaTemplates[i], alias);
10241024
}
10251025
else
10261026
{
@@ -1375,7 +1375,7 @@ public string GetManyToManyFilterFragment(string alias, IDictionary<string, IFil
13751375

13761376
if (manyToManyWhereString != null)
13771377
{
1378-
buffer.Append(" and ").Append(manyToManyWhereTemplate?.Replace(Template.Placeholder, alias));
1378+
buffer.Append(" and ").Append(Template.ReplacePlaceholder(manyToManyWhereTemplate, alias));
13791379
}
13801380

13811381
return buffer.ToString();

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,7 +2169,7 @@ public string[] ToColumns(string name, int i)
21692169
{
21702170
if (cols[j] == null)
21712171
{
2172-
result[j] = templates[j]?.Replace(Template.Placeholder, alias);
2172+
result[j] = Template.ReplacePlaceholder(templates[j], alias);
21732173
}
21742174
else
21752175
{
@@ -2432,7 +2432,7 @@ private EntityLoader CreateUniqueKeyLoader(IType uniqueKeyType, string[] columns
24322432

24332433
protected string GetSQLWhereString(string alias)
24342434
{
2435-
return sqlWhereStringTemplate?.Replace(Template.Placeholder, alias);
2435+
return Template.ReplacePlaceholder(sqlWhereStringTemplate, alias);
24362436
}
24372437

24382438
protected bool HasWhere

src/NHibernate/Persister/Entity/AbstractPropertyMapping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public virtual string[] ToColumns(string alias, string propertyName)
5555
for (int i = 0; i < columns.Length; i++)
5656
{
5757
if (columns[i] == null)
58-
result[i] = templates[i]?.Replace(Template.Placeholder, alias);
58+
result[i] = Template.ReplacePlaceholder(templates[i], alias);
5959
else
6060
result[i] = StringHelper.Qualify(alias, columns[i]);
6161
}
@@ -81,7 +81,7 @@ public virtual string[] ToColumns(string propertyName)
8181
for (int i = 0; i < columns.Length; i++)
8282
{
8383
if (columns[i] == null)
84-
result[i] = templates[i]?.Replace(Template.Placeholder, string.Empty);
84+
result[i] = Template.ReplacePlaceholder(templates[i], string.Empty);
8585
else
8686
result[i] = columns[i];
8787
}

src/NHibernate/SqlCommand/InFragment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public InFragment SetColumn(string alias, string colName)
4444

4545
public InFragment SetFormula(string alias, string formulaTemplate)
4646
{
47-
columnName = formulaTemplate?.Replace(Template.Placeholder, alias);
47+
columnName = Template.ReplacePlaceholder(formulaTemplate, alias);
4848
return this;
4949
}
5050

src/NHibernate/SqlCommand/SelectFragment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public SelectFragment AddFormula(string tableAlias, string formula, string formu
105105
{
106106
AddColumn(
107107
null,
108-
formula?.Replace(Template.Placeholder, tableAlias),
108+
Template.ReplacePlaceholder(formula, tableAlias),
109109
formulaAlias);
110110

111111
return this;

src/NHibernate/SqlCommand/Template.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ public static string RenderOrderByStringTemplate(string sqlOrderByString, Dialec
297297
return result.ToString();
298298
}
299299

300+
internal static string ReplacePlaceholder(string template, string alias)
301+
{
302+
// We have to remove the dot when the alias is empty in order to avoid generating an invalid sql statement.
303+
// Alias will be empty for DML statements that do not support aliases.
304+
var toReplace = !string.IsNullOrEmpty(alias) ? Placeholder : Placeholder + StringHelper.Dot;
305+
return template?.Replace(toReplace, alias);
306+
}
307+
300308
private static bool IsNamedParameter(string token)
301309
{
302310
return token.StartsWith(':');

0 commit comments

Comments
 (0)