Skip to content

Commit 2d77480

Browse files
authoredAug 6, 2023
Remove hql ConstantConverter (#3395)
Removed logic handled in DotNode class (see LiteralProcessor.LookupConstant)
1 parent a7deeb0 commit 2d77480

File tree

5 files changed

+57
-158
lines changed

5 files changed

+57
-158
lines changed
 

‎src/NHibernate/Hql/Ast/ANTLR/QueryTranslatorImpl.cs

-51
Original file line numberDiff line numberDiff line change
@@ -547,64 +547,13 @@ public IASTNode Parse()
547547
try
548548
{
549549
var ast = (IASTNode) parser.statement().Tree;
550-
551-
var walker = new NodeTraverser(new ConstantConverter(_sfi));
552-
walker.TraverseDepthFirst(ast);
553-
554550
return ast;
555551
}
556552
finally
557553
{
558554
parser.ParseErrorHandler.ThrowQueryException();
559555
}
560556
}
561-
562-
class ConstantConverter : IVisitationStrategy
563-
{
564-
private IASTNode _dotRoot;
565-
private readonly ISessionFactoryImplementor _sfi;
566-
567-
public ConstantConverter(ISessionFactoryImplementor sfi)
568-
{
569-
_sfi = sfi;
570-
}
571-
572-
public void Visit(IASTNode node)
573-
{
574-
if (_dotRoot != null)
575-
{
576-
// we are already processing a dot-structure
577-
if (ASTUtil.IsSubtreeChild(_dotRoot, node))
578-
{
579-
// ignore it...
580-
return;
581-
}
582-
583-
// we are now at a new tree level
584-
_dotRoot = null;
585-
}
586-
587-
if (_dotRoot == null && node.Type == HqlSqlWalker.DOT)
588-
{
589-
_dotRoot = node;
590-
HandleDotStructure(_dotRoot);
591-
}
592-
}
593-
594-
private void HandleDotStructure(IASTNode dotStructureRoot)
595-
{
596-
var expression = ASTUtil.GetPathText(dotStructureRoot);
597-
598-
var constant = ReflectHelper.GetConstantValue(expression, _sfi);
599-
600-
if (constant != null)
601-
{
602-
dotStructureRoot.ClearChildren();
603-
dotStructureRoot.Type = HqlSqlWalker.JAVA_CONSTANT;
604-
dotStructureRoot.Text = expression;
605-
}
606-
}
607-
}
608557
}
609558

610559
internal class HqlSqlTranslator

‎src/NHibernate/Hql/Ast/ANTLR/Tree/DotNode.cs

+26-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace NHibernate.Hql.Ast.ANTLR.Tree
1818
/// Ported by: Steve Strong
1919
/// </summary>
2020
[CLSCompliant(false)]
21-
public class DotNode : FromReferenceNode
21+
public class DotNode : FromReferenceNode, IExpectedTypeAwareNode
2222
{
2323
private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof(DotNode));
2424

@@ -72,6 +72,8 @@ public class DotNode : FromReferenceNode
7272
/// </summary>
7373
private JoinType _joinType = JoinType.InnerJoin;
7474

75+
private object _constantValue;
76+
7577
public DotNode(IToken token) : base(token)
7678
{
7779
}
@@ -287,11 +289,14 @@ private IType GetDataType()
287289
return DataType;
288290
}
289291

290-
public void SetResolvedConstant(string text)
292+
public void SetResolvedConstant(string text) => SetResolvedConstant(text, null);
293+
294+
public void SetResolvedConstant(string text, object value)
291295
{
292296
_path = text;
293297
_dereferenceType = DerefJavaConstant;
294298
IsResolved = true; // Don't resolve the node again.
299+
_constantValue = value;
295300
}
296301

297302
private static QueryException BuildIllegalCollectionDereferenceException(string propertyName, IASTNode lhs)
@@ -772,5 +777,24 @@ public void ResolveSelectExpression()
772777
lhs = (FromReferenceNode)lhs.GetChild(0);
773778
}
774779
}
780+
781+
public IType ExpectedType
782+
{
783+
get => DataType;
784+
set
785+
{
786+
if (Type != HqlSqlWalker.JAVA_CONSTANT)
787+
return;
788+
789+
DataType = value;
790+
}
791+
}
792+
793+
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
794+
{
795+
return Type == HqlSqlWalker.JAVA_CONSTANT
796+
? JavaConstantNode.ResolveToLiteralString(DataType, _constantValue, sessionFactory.Dialect)
797+
: base.RenderText(sessionFactory);
798+
}
775799
}
776800
}

‎src/NHibernate/Hql/Ast/ANTLR/Tree/JavaConstantNode.cs

+20-17
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,30 @@ public ISessionFactoryImplementor SessionFactory
3838
set { _factory = value; }
3939
}
4040

41-
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
42-
{
43-
ProcessText();
41+
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
42+
{
43+
ProcessText();
4444

4545
IType type = _expectedType ?? _heuristicType;
46-
return new SqlString(ResolveToLiteralString( type ));
46+
return ResolveToLiteralString(type);
4747
}
4848

49-
private string ResolveToLiteralString(IType type)
50-
{
51-
try
52-
{
53-
ILiteralType literalType = (ILiteralType)type;
54-
Dialect.Dialect dialect = _factory.Dialect;
55-
return literalType.ObjectToSQLString(_constantValue, dialect);
56-
}
57-
catch (Exception t)
58-
{
59-
throw new QueryException(LiteralProcessor.ErrorCannotFormatLiteral + Text, t);
60-
}
61-
}
49+
private SqlString ResolveToLiteralString(IType type)
50+
{
51+
return ResolveToLiteralString(type, _constantValue, _factory.Dialect);
52+
}
53+
54+
internal static SqlString ResolveToLiteralString(IType type, object constantValue, Dialect.Dialect dialect)
55+
{
56+
try
57+
{
58+
return new SqlString(((ILiteralType) type).ObjectToSQLString(constantValue, dialect));
59+
}
60+
catch (Exception t)
61+
{
62+
throw new QueryException(LiteralProcessor.ErrorCannotFormatLiteral + constantValue, t);
63+
}
64+
}
6265

6366
private void ProcessText()
6467
{

‎src/NHibernate/Hql/Ast/ANTLR/Util/LiteralProcessor.cs

+5-69
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void LookupConstant(DotNode node)
7070
}
7171
else
7272
{
73-
Object value = ReflectHelper.GetConstantValue(text);
73+
var value = ReflectHelper.GetConstantValue(text, _walker.SessionFactoryHelper.Factory);
7474
if (value == null)
7575
{
7676
throw new InvalidPathException("Invalid path: '" + text + "'");
@@ -149,6 +149,7 @@ public void ProcessConstant(SqlNode constant, bool resolveIdent)
149149
if (isIdent && queryable != null)
150150
{
151151
constant.Text = queryable.DiscriminatorSQLValue;
152+
constant.DataType = queryable.DiscriminatorType;
152153
}
153154
// Otherwise, it's a literal.
154155
else
@@ -275,74 +276,9 @@ private void SetConstantValue(DotNode node, string text, object value)
275276

276277
node.ClearChildren(); // Chop off the rest of the tree.
277278

278-
if (value is string)
279-
{
280-
node.Type = HqlSqlWalker.QUOTED_String;
281-
}
282-
else if (value is char)
283-
{
284-
node.Type = HqlSqlWalker.QUOTED_String;
285-
}
286-
else if (value is byte)
287-
{
288-
node.Type = HqlSqlWalker.NUM_INT;
289-
}
290-
else if (value is short)
291-
{
292-
node.Type = HqlSqlWalker.NUM_INT;
293-
}
294-
else if (value is int)
295-
{
296-
node.Type = HqlSqlWalker.NUM_INT;
297-
}
298-
else if (value is long)
299-
{
300-
node.Type = HqlSqlWalker.NUM_LONG;
301-
}
302-
else if (value is double)
303-
{
304-
node.Type = HqlSqlWalker.NUM_DOUBLE;
305-
}
306-
else if (value is decimal)
307-
{
308-
node.Type = HqlSqlWalker.NUM_DECIMAL;
309-
}
310-
else if (value is float)
311-
{
312-
node.Type = HqlSqlWalker.NUM_FLOAT;
313-
}
314-
else
315-
{
316-
node.Type = HqlSqlWalker.CONSTANT;
317-
}
318-
319-
IType type;
320-
try
321-
{
322-
type = TypeFactory.HeuristicType(value.GetType().Name);
323-
}
324-
catch (MappingException me)
325-
{
326-
throw new QueryException(me);
327-
}
328-
329-
if (type == null)
330-
{
331-
throw new QueryException(LiteralProcessor.ErrorCannotDetermineType + node.Text);
332-
}
333-
try
334-
{
335-
ILiteralType literalType = (ILiteralType)type;
336-
NHibernate.Dialect.Dialect dialect = _walker.SessionFactoryHelper.Factory.Dialect;
337-
node.Text = literalType.ObjectToSQLString(value, dialect);
338-
}
339-
catch (Exception e)
340-
{
341-
throw new QueryException(LiteralProcessor.ErrorCannotFormatLiteral + node.Text, e);
342-
}
343-
344-
node.DataType = type;
345-
node.SetResolvedConstant(text);
279+
node.Type = HqlSqlWalker.JAVA_CONSTANT;
280+
node.DataType = TypeFactory.HeuristicType(value.GetType().Name);
281+
node.SetResolvedConstant(text, value);
346282
}
347283

348284
interface IDecimalFormatter

‎src/NHibernate/Util/ReflectHelper.cs

+6-19
Original file line numberDiff line numberDiff line change
@@ -820,31 +820,18 @@ private static MethodInfo SafeGetMethod(System.Type type, MethodInfo method, Sys
820820
return foundMethod;
821821
}
822822

823-
internal static object GetConstantValue(string qualifiedName)
824-
{
825-
return GetConstantValue(qualifiedName, null);
826-
}
827-
828823
internal static object GetConstantValue(string qualifiedName, ISessionFactoryImplementor sfi)
829824
{
830825
string className = StringHelper.Qualifier(qualifiedName);
831826

832-
if (!string.IsNullOrEmpty(className))
833-
{
834-
System.Type t = System.Type.GetType(className);
835-
836-
if (t == null && sfi != null)
837-
{
838-
t = System.Type.GetType(sfi.GetImportedClassName(className));
839-
}
827+
if (string.IsNullOrEmpty(className))
828+
return null;
840829

841-
if (t != null)
842-
{
843-
return GetConstantValue(t, StringHelper.Unqualify(qualifiedName));
844-
}
845-
}
830+
var t = System.Type.GetType(sfi?.GetImportedClassName(className) ?? className);
846831

847-
return null;
832+
return t == null
833+
? null
834+
: GetConstantValue(t, StringHelper.Unqualify(qualifiedName));
848835
}
849836

850837
// Since v5

0 commit comments

Comments
 (0)