未验证 提交 2d774801 编写于 作者: R Roman Artiukhin 提交者: GitHub

Remove hql ConstantConverter (#3395)

Removed logic handled in DotNode class (see LiteralProcessor.LookupConstant)
上级 a7deeb06
......@@ -547,10 +547,6 @@ public IASTNode Parse()
try
{
var ast = (IASTNode) parser.statement().Tree;
var walker = new NodeTraverser(new ConstantConverter(_sfi));
walker.TraverseDepthFirst(ast);
return ast;
}
finally
......@@ -558,53 +554,6 @@ public IASTNode Parse()
parser.ParseErrorHandler.ThrowQueryException();
}
}
class ConstantConverter : IVisitationStrategy
{
private IASTNode _dotRoot;
private readonly ISessionFactoryImplementor _sfi;
public ConstantConverter(ISessionFactoryImplementor sfi)
{
_sfi = sfi;
}
public void Visit(IASTNode node)
{
if (_dotRoot != null)
{
// we are already processing a dot-structure
if (ASTUtil.IsSubtreeChild(_dotRoot, node))
{
// ignore it...
return;
}
// we are now at a new tree level
_dotRoot = null;
}
if (_dotRoot == null && node.Type == HqlSqlWalker.DOT)
{
_dotRoot = node;
HandleDotStructure(_dotRoot);
}
}
private void HandleDotStructure(IASTNode dotStructureRoot)
{
var expression = ASTUtil.GetPathText(dotStructureRoot);
var constant = ReflectHelper.GetConstantValue(expression, _sfi);
if (constant != null)
{
dotStructureRoot.ClearChildren();
dotStructureRoot.Type = HqlSqlWalker.JAVA_CONSTANT;
dotStructureRoot.Text = expression;
}
}
}
}
internal class HqlSqlTranslator
......
......@@ -18,7 +18,7 @@ namespace NHibernate.Hql.Ast.ANTLR.Tree
/// Ported by: Steve Strong
/// </summary>
[CLSCompliant(false)]
public class DotNode : FromReferenceNode
public class DotNode : FromReferenceNode, IExpectedTypeAwareNode
{
private static readonly INHibernateLogger Log = NHibernateLogger.For(typeof(DotNode));
......@@ -72,6 +72,8 @@ public class DotNode : FromReferenceNode
/// </summary>
private JoinType _joinType = JoinType.InnerJoin;
private object _constantValue;
public DotNode(IToken token) : base(token)
{
}
......@@ -287,11 +289,14 @@ private IType GetDataType()
return DataType;
}
public void SetResolvedConstant(string text)
public void SetResolvedConstant(string text) => SetResolvedConstant(text, null);
public void SetResolvedConstant(string text, object value)
{
_path = text;
_dereferenceType = DerefJavaConstant;
IsResolved = true; // Don't resolve the node again.
_constantValue = value;
}
private static QueryException BuildIllegalCollectionDereferenceException(string propertyName, IASTNode lhs)
......@@ -772,5 +777,24 @@ public void ResolveSelectExpression()
lhs = (FromReferenceNode)lhs.GetChild(0);
}
}
public IType ExpectedType
{
get => DataType;
set
{
if (Type != HqlSqlWalker.JAVA_CONSTANT)
return;
DataType = value;
}
}
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
{
return Type == HqlSqlWalker.JAVA_CONSTANT
? JavaConstantNode.ResolveToLiteralString(DataType, _constantValue, sessionFactory.Dialect)
: base.RenderText(sessionFactory);
}
}
}
......@@ -38,27 +38,30 @@ public ISessionFactoryImplementor SessionFactory
set { _factory = value; }
}
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
{
ProcessText();
public override SqlString RenderText(ISessionFactoryImplementor sessionFactory)
{
ProcessText();
IType type = _expectedType ?? _heuristicType;
return new SqlString(ResolveToLiteralString( type ));
return ResolveToLiteralString(type);
}
private string ResolveToLiteralString(IType type)
{
try
{
ILiteralType literalType = (ILiteralType)type;
Dialect.Dialect dialect = _factory.Dialect;
return literalType.ObjectToSQLString(_constantValue, dialect);
}
catch (Exception t)
{
throw new QueryException(LiteralProcessor.ErrorCannotFormatLiteral + Text, t);
}
}
private SqlString ResolveToLiteralString(IType type)
{
return ResolveToLiteralString(type, _constantValue, _factory.Dialect);
}
internal static SqlString ResolveToLiteralString(IType type, object constantValue, Dialect.Dialect dialect)
{
try
{
return new SqlString(((ILiteralType) type).ObjectToSQLString(constantValue, dialect));
}
catch (Exception t)
{
throw new QueryException(LiteralProcessor.ErrorCannotFormatLiteral + constantValue, t);
}
}
private void ProcessText()
{
......
......@@ -70,7 +70,7 @@ public void LookupConstant(DotNode node)
}
else
{
Object value = ReflectHelper.GetConstantValue(text);
var value = ReflectHelper.GetConstantValue(text, _walker.SessionFactoryHelper.Factory);
if (value == null)
{
throw new InvalidPathException("Invalid path: '" + text + "'");
......@@ -149,6 +149,7 @@ public void ProcessConstant(SqlNode constant, bool resolveIdent)
if (isIdent && queryable != null)
{
constant.Text = queryable.DiscriminatorSQLValue;
constant.DataType = queryable.DiscriminatorType;
}
// Otherwise, it's a literal.
else
......@@ -275,74 +276,9 @@ private void SetConstantValue(DotNode node, string text, object value)
node.ClearChildren(); // Chop off the rest of the tree.
if (value is string)
{
node.Type = HqlSqlWalker.QUOTED_String;
}
else if (value is char)
{
node.Type = HqlSqlWalker.QUOTED_String;
}
else if (value is byte)
{
node.Type = HqlSqlWalker.NUM_INT;
}
else if (value is short)
{
node.Type = HqlSqlWalker.NUM_INT;
}
else if (value is int)
{
node.Type = HqlSqlWalker.NUM_INT;
}
else if (value is long)
{
node.Type = HqlSqlWalker.NUM_LONG;
}
else if (value is double)
{
node.Type = HqlSqlWalker.NUM_DOUBLE;
}
else if (value is decimal)
{
node.Type = HqlSqlWalker.NUM_DECIMAL;
}
else if (value is float)
{
node.Type = HqlSqlWalker.NUM_FLOAT;
}
else
{
node.Type = HqlSqlWalker.CONSTANT;
}
IType type;
try
{
type = TypeFactory.HeuristicType(value.GetType().Name);
}
catch (MappingException me)
{
throw new QueryException(me);
}
if (type == null)
{
throw new QueryException(LiteralProcessor.ErrorCannotDetermineType + node.Text);
}
try
{
ILiteralType literalType = (ILiteralType)type;
NHibernate.Dialect.Dialect dialect = _walker.SessionFactoryHelper.Factory.Dialect;
node.Text = literalType.ObjectToSQLString(value, dialect);
}
catch (Exception e)
{
throw new QueryException(LiteralProcessor.ErrorCannotFormatLiteral + node.Text, e);
}
node.DataType = type;
node.SetResolvedConstant(text);
node.Type = HqlSqlWalker.JAVA_CONSTANT;
node.DataType = TypeFactory.HeuristicType(value.GetType().Name);
node.SetResolvedConstant(text, value);
}
interface IDecimalFormatter
......
......@@ -820,31 +820,18 @@ private static MethodInfo SafeGetMethod(System.Type type, MethodInfo method, Sys
return foundMethod;
}
internal static object GetConstantValue(string qualifiedName)
{
return GetConstantValue(qualifiedName, null);
}
internal static object GetConstantValue(string qualifiedName, ISessionFactoryImplementor sfi)
{
string className = StringHelper.Qualifier(qualifiedName);
if (!string.IsNullOrEmpty(className))
{
System.Type t = System.Type.GetType(className);
if (t == null && sfi != null)
{
t = System.Type.GetType(sfi.GetImportedClassName(className));
}
if (string.IsNullOrEmpty(className))
return null;
if (t != null)
{
return GetConstantValue(t, StringHelper.Unqualify(qualifiedName));
}
}
var t = System.Type.GetType(sfi?.GetImportedClassName(className) ?? className);
return null;
return t == null
? null
: GetConstantValue(t, StringHelper.Unqualify(qualifiedName));
}
// Since v5
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册