88
99namespace NHibernate . Hql . Ast . ANTLR
1010{
11- internal class PolymorphicQuerySourceDetector
12- {
13- private readonly ISessionFactoryImplementor _sfi ;
14- private readonly Dictionary < IASTNode , IASTNode [ ] > _map = new Dictionary < IASTNode , IASTNode [ ] > ( ) ;
15- private readonly SessionFactoryHelper _sessionFactoryHelper ;
11+ internal class PolymorphicQuerySourceDetector
12+ {
13+ private readonly ISessionFactoryImplementor _sfi ;
14+ private readonly Dictionary < IASTNode , IASTNode [ ] > _map = new Dictionary < IASTNode , IASTNode [ ] > ( ) ;
15+ private readonly SessionFactoryHelper _sessionFactoryHelper ;
1616
17- public PolymorphicQuerySourceDetector ( ISessionFactoryImplementor sfi )
18- {
19- _sfi = sfi ;
20- _sessionFactoryHelper = new SessionFactoryHelper ( sfi ) ;
21- }
17+ public PolymorphicQuerySourceDetector ( ISessionFactoryImplementor sfi )
18+ {
19+ _sfi = sfi ;
20+ _sessionFactoryHelper = new SessionFactoryHelper ( sfi ) ;
21+ }
2222
23- public Dictionary < IASTNode , IASTNode [ ] > Process ( IASTNode tree )
24- {
25- foreach ( var querySource in new QuerySourceDetector ( tree ) . LocateQuerySources ( ) )
26- {
27- var className = GetClassName ( querySource ) ;
28- string [ ] implementors = _sfi . GetImplementors ( className ) ;
29- AddImplementorsToMap ( querySource , className , implementors ) ;
30- }
23+ public Dictionary < IASTNode , IASTNode [ ] > Process ( IASTNode tree )
24+ {
25+ foreach ( var querySource in new QuerySourceDetector ( tree ) . LocateQuerySources ( ) )
26+ {
27+ var className = GetClassName ( querySource ) ;
28+ string [ ] implementors = _sfi . GetImplementors ( className ) ;
29+ AddImplementorsToMap ( querySource , className , implementors ) ;
30+ }
3131
32- return _map ;
33- }
32+ return _map ;
33+ }
3434
35- private void AddImplementorsToMap ( IASTNode querySource , string className , string [ ] implementors )
36- {
37- if ( implementors . Length == 1 && implementors [ 0 ] == className )
38- {
39- // No need to change things
40- return ;
41- }
35+ private void AddImplementorsToMap ( IASTNode querySource , string className , string [ ] implementors )
36+ {
37+ if ( implementors . Length == 1 && implementors [ 0 ] == className )
38+ {
39+ // No need to change things
40+ return ;
41+ }
4242
43- _map . Add ( querySource ,
44- implementors . Select ( implementor => MakeIdent ( querySource , implementor ) ) . ToArray ( ) ) ;
45- }
43+ _map . Add ( querySource ,
44+ implementors . Select ( implementor => MakeIdent ( querySource , implementor ) ) . ToArray ( ) ) ;
45+ }
4646
47- private static string GetClassName ( IASTNode querySource )
48- {
49- switch ( querySource . Type )
50- {
51- case HqlSqlWalker . IDENT :
52- return querySource . Text ;
53- case HqlSqlWalker . DOT :
54- return BuildPath ( querySource ) ;
55- }
47+ private static string GetClassName ( IASTNode querySource )
48+ {
49+ switch ( querySource . Type )
50+ {
51+ case HqlSqlWalker . IDENT :
52+ return querySource . Text ;
53+ case HqlSqlWalker . DOT :
54+ return BuildPath ( querySource ) ;
55+ default :
56+ // TODO
57+ throw new NotSupportedException ( $ "{ querySource . ToString ( ) } { querySource . ToStringTree ( ) } ") ;
58+ }
5659
57- // TODO
58- throw new NotSupportedException ( ) ;
59- }
60+ }
6061
61- private static IASTNode MakeIdent ( IASTNode source , string text )
62- {
63- var ident = source . DupNode ( ) ;
64- ident . Type = HqlSqlWalker . IDENT ;
65- ident . Text = text ;
66- return ident ;
67- }
62+ private static IASTNode MakeIdent ( IASTNode source , string text )
63+ {
64+ var ident = source . DupNode ( ) ;
65+ ident . Type = HqlSqlWalker . IDENT ;
66+ ident . Text = text ;
67+ return ident ;
68+ }
6869
69- private static string BuildPath ( IASTNode node )
70- {
71- var sb = new StringBuilder ( ) ;
72- BuildPath ( node , sb ) ;
73- return sb . ToString ( ) ;
74- }
70+ private static string BuildPath ( IASTNode node )
71+ {
72+ var sb = new StringBuilder ( ) ;
73+ BuildPath ( node , sb ) ;
74+ return sb . ToString ( ) ;
75+ }
7576
76- private static void BuildPath ( IASTNode node , StringBuilder sb )
77- {
78- if ( node . Type == HqlSqlWalker . DOT )
79- {
80- BuildPath ( node . GetChild ( 0 ) , sb ) ;
77+ private static void BuildPath ( IASTNode node , StringBuilder sb )
78+ {
79+ if ( node . Type == HqlSqlWalker . DOT )
80+ {
81+ BuildPath ( node . GetChild ( 0 ) , sb ) ;
8182
82- sb . Append ( '.' ) ;
83- sb . Append ( node . GetChild ( 1 ) . Text ) ;
84- }
85- else
86- {
87- sb . Append ( node . Text ) ;
88- }
89- }
83+ sb . Append ( '.' ) ;
84+ sb . Append ( node . GetChild ( 1 ) . Text ) ;
85+ }
86+ else
87+ {
88+ sb . Append ( node . Text ) ;
89+ }
90+ }
9091
91- }
92- }
92+ }
93+ }
0 commit comments