Skip to content

Commit beb7b4b

Browse files
bahusoidfredericDelaporte
authored andcommitted
Keep guessed parameter type in hql
1 parent d6ff0a0 commit beb7b4b

File tree

7 files changed

+40
-6
lines changed

7 files changed

+40
-6
lines changed

src/NHibernate.Test/Async/Linq/LinqQuerySamples.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1385,5 +1385,15 @@ public void ReplaceFunctionWithNullArgumentAsync()
13851385
}, Throws.Nothing, "Expected REPLACE(FirstName, LastName, NULL) to be supported");
13861386
Assert.That(results, Is.Not.Null);
13871387
}
1388+
1389+
[Test(Description = "GH-2860")]
1390+
public async Task StringFormatWithTrimAsync()
1391+
{
1392+
var q =
1393+
from e in db.Employees
1394+
select new {Name = $"{e.FirstName} {e.LastName}".Trim(), Phone = e.Address.PhoneNumber};
1395+
var items = await (q.ToListAsync());
1396+
Assert.AreEqual(9, items.Count);
1397+
}
13881398
}
13891399
}

src/NHibernate.Test/Linq/LinqQuerySamples.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,16 @@ public void ReplaceFunctionWithNullArgument()
19701970
}, Throws.Nothing, "Expected REPLACE(FirstName, LastName, NULL) to be supported");
19711971
Assert.That(results, Is.Not.Null);
19721972
}
1973+
1974+
[Test(Description = "GH-2860")]
1975+
public void StringFormatWithTrim()
1976+
{
1977+
var q =
1978+
from e in db.Employees
1979+
select new {Name = $"{e.FirstName} {e.LastName}".Trim(), Phone = e.Address.PhoneNumber};
1980+
var items = q.ToList();
1981+
Assert.AreEqual(9, items.Count);
1982+
}
19731983
}
19741984

19751985
public class ParentChildBatch<T, TKey, TSub>

src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,10 @@ IASTNode GenerateNamedParameter(IASTNode delimiterNode, IASTNode nameNode)
10701070
{
10711071
// Add the parameter type information so that we are able to calculate functions return types
10721072
// when the parameter is used as an argument.
1073-
parameter.ExpectedType = namedParameter.Type;
1073+
if (namedParameter.IsGuessedType)
1074+
parameter.GuessedType = namedParameter.Type;
1075+
else
1076+
parameter.ExpectedType = namedParameter.Type;
10741077
}
10751078

10761079
_parameters.Add(paramSpec);

src/NHibernate/Hql/Ast/ANTLR/Tree/ParameterNode.cs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public IType ExpectedType
4949
}
5050
}
5151

52+
internal IType GuessedType { get; set; }
53+
5254
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
5355
{
5456
int count;

src/NHibernate/Hql/Ast/ANTLR/Tree/SelectClause.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,14 @@ public void InitializeExplicitSelectClause(FromClause fromClause)
153153
else
154154
{
155155
IType type = expr.DataType;
156-
if (type == null && !(expr is ParameterNode))
156+
if (type == null)
157157
{
158-
throw new QueryException("No data type for node: " + expr.GetType().Name + " " + new ASTPrinter().ShowAsString((IASTNode)expr, ""));
158+
if (expr is ParameterNode param)
159+
{
160+
type = param.GuessedType;
161+
}
162+
else
163+
throw new QueryException("No data type for node: " + expr.GetType().Name + " " + new ASTPrinter().ShowAsString((IASTNode)expr, ""));
159164
}
160165
//sqlResultTypeList.add( type );
161166

src/NHibernate/Linq/Visitors/ParameterTypeLocator.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ internal static void SetParameterTypes(
9393
continue;
9494
}
9595

96-
namedParameter.Type = GetParameterType(sessionFactory, constantExpressions, visitor, namedParameter);
96+
namedParameter.Type = GetParameterType(sessionFactory, constantExpressions, visitor, namedParameter, out var tryProcessInHql);
97+
namedParameter.IsGuessedType = tryProcessInHql;
9798
}
9899
}
99100

@@ -145,8 +146,10 @@ private static IType GetParameterType(
145146
ISessionFactoryImplementor sessionFactory,
146147
HashSet<ConstantExpression> constantExpressions,
147148
ConstantTypeLocatorVisitor visitor,
148-
NamedParameter namedParameter)
149+
NamedParameter namedParameter,
150+
out bool tryProcessInHql)
149151
{
152+
tryProcessInHql = false;
150153
// All constant expressions have the same type/value
151154
var constantExpression = constantExpressions.First();
152155
var constantType = constantExpression.Type.UnwrapIfNullable();
@@ -158,7 +161,7 @@ private static IType GetParameterType(
158161

159162
if (visitor.NotGuessableConstants.Contains(constantExpression) && constantExpression.Value != null)
160163
{
161-
return null;
164+
tryProcessInHql = true;
162165
}
163166

164167
// No related MemberExpressions was found, guess the type by value or its type when null.

src/NHibernate/Param/NamedParameter.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal NamedParameter(string name, object value, IType type, bool isCollection
2020
public string Name { get; private set; }
2121
public object Value { get; internal set; }
2222
public IType Type { get; internal set; }
23+
internal bool IsGuessedType { get; set; }
2324

2425
public virtual bool IsCollection { get; }
2526

0 commit comments

Comments
 (0)