Skip to content

Fix invalid DML statements for static where in mapping #2062

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/NHibernate.Test/Async/NHSpecificTest/GH2053/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Linq;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2053
{
using System.Threading.Tasks;
[TestFixture]
public class FixtureAsync : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Description = "DESCRIPTION", Status = 0 };
session.Save(e1);

var e2 = new Entity { Description = "DESCRIPTION", Status = 1 };
session.Save(e2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Entity").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public async Task TestAsync()
{
var descriptions = new [] { "DESCRIPTION" };

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = await (session.Query<Entity>()
.Where(x => descriptions.Contains(x.Description))
.UpdateAsync(x => new Entity {Description = "DESCRIPTION_UPDATED"}));
Assert.That(result, Is.EqualTo(1));

await (transaction.CommitAsync());
}
}
}
}
13 changes: 13 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2053/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;

namespace NHibernate.Test.NHSpecificTest.GH2053
{
public class Entity
{
public virtual int Id { get; protected set; }

public virtual int Status { get; set; }

public virtual string Description { get; set; }
}
}
53 changes: 53 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2053/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Linq;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.NHSpecificTest.GH2053
{
[TestFixture]
public class Fixture : BugTestCase
{
protected override void OnSetUp()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var e1 = new Entity { Description = "DESCRIPTION", Status = 0 };
session.Save(e1);

var e2 = new Entity { Description = "DESCRIPTION", Status = 1 };
session.Save(e2);

transaction.Commit();
}
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
session.CreateQuery("delete from Entity").ExecuteUpdate();

transaction.Commit();
}
}

[Test]
public void Test()
{
var descriptions = new [] { "DESCRIPTION" };

using (var session = OpenSession())
using (var transaction = session.BeginTransaction())
{
var result = session.Query<Entity>()
.Where(x => descriptions.Contains(x.Description))
.Update(x => new Entity {Description = "DESCRIPTION_UPDATED"});
Assert.That(result, Is.EqualTo(1));

transaction.Commit();
}
}
}
}
11 changes: 11 additions & 0 deletions src/NHibernate.Test/NHSpecificTest/GH2053/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
namespace="NHibernate.Test.NHSpecificTest.GH2053">
<class name="Entity" where="STATUS = 1">
<id name="Id">
<generator class="native" />
</id>
<property name="Status" />
<property name="Description" />
</class>
</hibernate-mapping>
Original file line number Diff line number Diff line change
Expand Up @@ -638,19 +638,19 @@ public bool HasCache

public string GetSQLWhereString(string alias)
{
return sqlWhereStringTemplate?.Replace(Template.Placeholder, alias);
return Template.ReplacePlaceholder(sqlWhereStringTemplate, alias);
}

public string GetSQLOrderByString(string alias)
{
return HasOrdering ? sqlOrderByStringTemplate?.Replace(Template.Placeholder, alias) : string.Empty;
return HasOrdering ? Template.ReplacePlaceholder(sqlOrderByStringTemplate, alias) : string.Empty;
}

public string GetManyToManyOrderByString(string alias)
{
if (IsManyToMany && manyToManyOrderByString != null)
{
return manyToManyOrderByTemplate?.Replace(Template.Placeholder, alias);
return Template.ReplacePlaceholder(manyToManyOrderByTemplate, alias);
}
else
{
Expand Down Expand Up @@ -1020,7 +1020,7 @@ private static string[] Qualify(string alias, string[] columnNames, string[] for
{
if (columnNames[i] == null)
{
result[i] = formulaTemplates[i]?.Replace(Template.Placeholder, alias);
result[i] = Template.ReplacePlaceholder(formulaTemplates[i], alias);
}
else
{
Expand Down Expand Up @@ -1375,7 +1375,7 @@ public string GetManyToManyFilterFragment(string alias, IDictionary<string, IFil

if (manyToManyWhereString != null)
{
buffer.Append(" and ").Append(manyToManyWhereTemplate?.Replace(Template.Placeholder, alias));
buffer.Append(" and ").Append(Template.ReplacePlaceholder(manyToManyWhereTemplate, alias));
}

return buffer.ToString();
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Persister/Entity/AbstractEntityPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,7 +2169,7 @@ public string[] ToColumns(string name, int i)
{
if (cols[j] == null)
{
result[j] = templates[j]?.Replace(Template.Placeholder, alias);
result[j] = Template.ReplacePlaceholder(templates[j], alias);
}
else
{
Expand Down Expand Up @@ -2432,7 +2432,7 @@ private EntityLoader CreateUniqueKeyLoader(IType uniqueKeyType, string[] columns

protected string GetSQLWhereString(string alias)
{
return sqlWhereStringTemplate?.Replace(Template.Placeholder, alias);
return Template.ReplacePlaceholder(sqlWhereStringTemplate, alias);
}

protected bool HasWhere
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Persister/Entity/AbstractPropertyMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public virtual string[] ToColumns(string alias, string propertyName)
for (int i = 0; i < columns.Length; i++)
{
if (columns[i] == null)
result[i] = templates[i]?.Replace(Template.Placeholder, alias);
result[i] = Template.ReplacePlaceholder(templates[i], alias);
else
result[i] = StringHelper.Qualify(alias, columns[i]);
}
Expand All @@ -81,7 +81,7 @@ public virtual string[] ToColumns(string propertyName)
for (int i = 0; i < columns.Length; i++)
{
if (columns[i] == null)
result[i] = templates[i]?.Replace(Template.Placeholder, string.Empty);
result[i] = Template.ReplacePlaceholder(templates[i], string.Empty);
else
result[i] = columns[i];
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/SqlCommand/InFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public InFragment SetColumn(string alias, string colName)

public InFragment SetFormula(string alias, string formulaTemplate)
{
columnName = formulaTemplate?.Replace(Template.Placeholder, alias);
columnName = Template.ReplacePlaceholder(formulaTemplate, alias);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/SqlCommand/SelectFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public SelectFragment AddFormula(string tableAlias, string formula, string formu
{
AddColumn(
null,
formula?.Replace(Template.Placeholder, tableAlias),
Template.ReplacePlaceholder(formula, tableAlias),
formulaAlias);

return this;
Expand Down
8 changes: 8 additions & 0 deletions src/NHibernate/SqlCommand/Template.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ public static string RenderOrderByStringTemplate(string sqlOrderByString, Dialec
return result.ToString();
}

internal static string ReplacePlaceholder(string template, string alias)
{
// We have to remove the dot when the alias is empty in order to avoid generating an invalid sql statement.
// Alias will be empty for DML statements that do not support aliases.
var toReplace = !string.IsNullOrEmpty(alias) ? Placeholder : Placeholder + StringHelper.Dot;
return template?.Replace(toReplace, alias);
}

private static bool IsNamedParameter(string token)
{
return token.StartsWith(':');
Expand Down