Skip to content

Commit 0bf17ff

Browse files
authored
Merge pull request #150 from DataObjects-NET/6.0-nulls-replacement-issue
Null constants replacement issue fixed
2 parents 04804b8 + ff63de2 commit 0bf17ff

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (C) 2021 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using NUnit.Framework;
10+
using Xtensive.Orm.Configuration;
11+
using Xtensive.Orm.Tests.Issues.IssueGithub0150_ClosureProblemModel;
12+
13+
namespace Xtensive.Orm.Tests.Issues.IssueGithub0150_ClosureProblemModel
14+
{
15+
[HierarchyRoot]
16+
public class TestOperation : Entity
17+
{
18+
[Key, Field]
19+
public long ID { get; private set; }
20+
21+
[Field]
22+
[Association(OnOwnerRemove = OnRemoveAction.Clear, OnTargetRemove = OnRemoveAction.Deny, PairTo = "Operation")]
23+
public EntitySet<TestWorkOrder> WorkOrders { get; private set; }
24+
25+
public string GetErpOrderReference()
26+
{
27+
var erpReferences = Session.Query.Execute(q => q.All<TestWorkOrder>()
28+
.Where(w => w.Operation == this)
29+
.Select(w => w.Str));
30+
31+
var result = string.Join(" / ", erpReferences.ToArray());
32+
return result;
33+
}
34+
35+
public TestOperation(TestWorkOrder wo)
36+
{
37+
if (wo == null)
38+
throw new ArgumentNullException(nameof(wo));
39+
_ = WorkOrders.Add(wo);
40+
}
41+
}
42+
43+
[HierarchyRoot]
44+
public class TestWorkOrder : Entity
45+
{
46+
[Key, Field]
47+
public long ID { get; private set; }
48+
49+
[Field]
50+
public TestOperation Operation { get; private set; }
51+
52+
[Field]
53+
public string Str { get; set; }
54+
}
55+
}
56+
57+
namespace Xtensive.Orm.Tests.Issues
58+
{
59+
public class IssueGithub0149_ParameterReplacerHandlesNullConstsIncorrectly : AutoBuildTest
60+
{
61+
protected override DomainConfiguration BuildConfiguration()
62+
{
63+
var config = base.BuildConfiguration();
64+
config.Types.Register(typeof(TestWorkOrder));
65+
config.Types.Register(typeof(TestOperation));
66+
return config;
67+
}
68+
69+
[Test]
70+
public void MainTest()
71+
{
72+
using(var session = Domain.OpenSession())
73+
using(var tx = session.OpenTransaction()) {
74+
var wo = new TestWorkOrder {
75+
Str = "A"
76+
};
77+
var op = new TestOperation(wo);
78+
var erpOrder = op.GetErpOrderReference();
79+
Assert.AreEqual(wo.Str, erpOrder);
80+
}
81+
}
82+
}
83+
}

Orm/Xtensive.Orm/Orm/Internals/CompiledQueryRunner.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2012-2020 Xtensive LLC.
1+
// Copyright (C) 2012-2021 Xtensive LLC.
22
// This code is distributed under MIT license terms.
33
// See the License.txt file in the project root for more information.
44
// Created by: Denis Krjuchkov
@@ -156,6 +156,9 @@ private void AllocateParameterAndReplacer()
156156
queryParameter = (Parameter) System.Activator.CreateInstance(parameterType, "pClosure");
157157
queryParameterReplacer = new ExtendedExpressionReplacer(expression => {
158158
if (expression.NodeType == ExpressionType.Constant) {
159+
if ((expression as ConstantExpression).Value == null) {
160+
return null;
161+
}
159162
if (expression.Type.IsClosure()) {
160163
if (expression.Type == closureType) {
161164
return Expression.MakeMemberAccess(Expression.Constant(queryParameter, parameterType), valueMemberInfo);

0 commit comments

Comments
 (0)