1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
2
4
using Antlr . Runtime ;
3
5
using NHibernate . Hql . Ast . ANTLR . Util ;
4
6
using NHibernate . Type ;
@@ -12,8 +14,10 @@ namespace NHibernate.Hql.Ast.ANTLR.Tree
12
14
/// Ported by: Steve Strong
13
15
/// </summary>
14
16
[ CLSCompliant ( false ) ]
15
- public class CaseNode : AbstractSelectExpression , ISelectExpression
17
+ public class CaseNode : AbstractSelectExpression , ISelectExpression , IExpectedTypeAwareNode
16
18
{
19
+ private IType _expectedType ;
20
+
17
21
public CaseNode ( IToken token ) : base ( token )
18
22
{
19
23
}
@@ -22,46 +26,58 @@ public override IType DataType
22
26
{
23
27
get
24
28
{
25
- for ( int i = 0 ; i < ChildCount ; i ++ )
29
+ if ( ExpectedType != null )
30
+ return ExpectedType ;
31
+
32
+ foreach ( var node in GetResultNodes ( ) )
26
33
{
27
- IASTNode whenOrElseClause = GetChild ( i ) ;
28
- if ( whenOrElseClause . Type == HqlParser . WHEN )
29
- {
30
- // WHEN Child(0) THEN Child(1)
31
- IASTNode thenClause = whenOrElseClause . GetChild ( 1 ) ;
32
- if ( thenClause is ISelectExpression )
33
- {
34
- if ( ! ( thenClause is ParameterNode ) )
35
- {
36
- return ( thenClause as ISelectExpression ) . DataType ;
37
- }
38
- }
39
- }
40
- else if ( whenOrElseClause . Type == HqlParser . ELSE )
41
- {
42
- // ELSE Child(0)
43
- IASTNode elseClause = whenOrElseClause . GetChild ( 0 ) ;
44
- if ( elseClause is ISelectExpression )
45
- {
46
- if ( ! ( elseClause is ParameterNode ) )
47
- {
48
- return ( elseClause as ISelectExpression ) . DataType ;
49
- }
50
- }
51
- }
52
- else
53
- {
54
- throw new HibernateException ( "Was expecting a WHEN or ELSE, but found a: " + whenOrElseClause . Text ) ;
55
- }
34
+ if ( node is ISelectExpression select && ! ( node is ParameterNode ) )
35
+ return select . DataType ;
56
36
}
37
+
57
38
throw new HibernateException ( "Unable to determine data type of CASE statement." ) ;
58
39
}
59
40
set { base . DataType = value ; }
60
41
}
61
42
43
+ public IEnumerable < IASTNode > GetResultNodes ( )
44
+ {
45
+ for ( int i = 0 ; i < ChildCount ; i ++ )
46
+ {
47
+ IASTNode whenOrElseClause = GetChild ( i ) ;
48
+ if ( whenOrElseClause . Type == HqlParser . WHEN )
49
+ {
50
+ // WHEN Child(0) THEN Child(1)
51
+ yield return whenOrElseClause . GetChild ( 1 ) ;
52
+ }
53
+ else if ( whenOrElseClause . Type == HqlParser . ELSE )
54
+ {
55
+ // ELSE Child(0)
56
+ yield return whenOrElseClause . GetChild ( 0 ) ;
57
+ }
58
+ else
59
+ {
60
+ throw new HibernateException ( "Was expecting a WHEN or ELSE, but found a: " + whenOrElseClause . Text ) ;
61
+ }
62
+ }
63
+ }
64
+
62
65
public override void SetScalarColumnText ( int i )
63
66
{
64
67
ColumnHelper . GenerateSingleScalarColumn ( ASTFactory , this , i ) ;
65
68
}
69
+
70
+ public IType ExpectedType
71
+ {
72
+ get => _expectedType ;
73
+ set
74
+ {
75
+ _expectedType = value ;
76
+ foreach ( var node in GetResultNodes ( ) . OfType < IExpectedTypeAwareNode > ( ) )
77
+ {
78
+ node . ExpectedType = ExpectedType ;
79
+ }
80
+ }
81
+ }
66
82
}
67
83
}
0 commit comments