@@ -67,16 +67,22 @@ public IEnumerable<string> GetJoinClauses(IEnumerable<JoinClause> joins, IComman
6767 {
6868 var builder = new StringBuilder ( JoinTypeToKeyword ( join . JoinType ) ) ;
6969 var joinExpression = join . JoinExpression ?? InferJoinExpression ( join . Table ) ;
70- builder . AppendFormat ( " JOIN {0}{1} ON ({2})" ,
71- _schema . FindTable ( _schema . BuildObjectName ( join . Table . ToString ( ) ) ) . QualifiedName ,
72- string . IsNullOrWhiteSpace ( join . Table . GetAlias ( ) ) ? string . Empty : " " + _schema . QuoteObjectName ( join . Table . GetAlias ( ) ) ,
73- expressionFormatter . Format ( joinExpression ) ) ;
74- yield return builder . ToString ( ) . Trim ( ) ;
70+ if ( ! ReferenceEquals ( joinExpression , null ) )
71+ {
72+ builder . AppendFormat ( " JOIN {0}{1} ON ({2})" ,
73+ _schema . FindTable ( _schema . BuildObjectName ( join . Table . ToString ( ) ) ) . QualifiedName ,
74+ string . IsNullOrWhiteSpace ( join . Table . GetAlias ( ) )
75+ ? string . Empty
76+ : " " + _schema . QuoteObjectName ( join . Table . GetAlias ( ) ) ,
77+ expressionFormatter . Format ( joinExpression ) ) ;
78+ yield return builder . ToString ( ) . Trim ( ) ;
79+ }
7580 }
7681 }
7782
7883 private SimpleExpression InferJoinExpression ( ObjectReference table )
7984 {
85+ if ( table . GetOwner ( ) . IsNull ( ) ) return null ;
8086 var table1 = _schema . FindTable ( table . GetOwner ( ) . GetName ( ) ) ;
8187 var table2 = _schema . FindTable ( table . GetName ( ) ) ;
8288 var foreignKey = GetForeignKey ( table1 , table2 ) ;
@@ -90,7 +96,7 @@ private void AddJoin(ObjectName table1Name, ObjectName table2Name, JoinType join
9096 var table1 = _schema . FindTable ( table1Name ) ;
9197 var table2 = _schema . FindTable ( table2Name ) ;
9298 var foreignKey = GetForeignKey ( table1 , table2 ) ;
93- return MakeJoinText ( table2 , foreignKey , joinType ) ;
99+ return MakeJoinText ( table2 , table2Name . Alias , foreignKey , joinType ) ;
94100 } ) ;
95101 }
96102
@@ -109,16 +115,18 @@ private static ForeignKey GetForeignKey(Table table1, Table table2)
109115 return foreignKey ;
110116 }
111117
112- private string MakeJoinText ( Table rightTable , ForeignKey foreignKey , JoinType joinType )
118+ private string MakeJoinText ( Table rightTable , string alias , ForeignKey foreignKey , JoinType joinType )
113119 {
114120 var builder = new StringBuilder ( JoinKeywordFor ( joinType ) ) ;
115- builder . AppendFormat ( " JOIN {0} ON (" , rightTable . QualifiedName ) ;
116- builder . Append ( FormatJoinExpression ( foreignKey , 0 ) ) ;
121+ builder . AppendFormat ( " JOIN {0}" , rightTable . QualifiedName ) ;
122+ if ( ! string . IsNullOrWhiteSpace ( alias ) ) builder . Append ( " " + _schema . QuoteObjectName ( alias ) ) ;
123+ builder . Append ( " ON (" ) ;
124+ builder . Append ( FormatJoinExpression ( foreignKey , 0 , alias ) ) ;
117125
118126 for ( int i = 1 ; i < foreignKey . Columns . Length ; i ++ )
119127 {
120128 builder . Append ( " AND " ) ;
121- builder . Append ( FormatJoinExpression ( foreignKey , i ) ) ;
129+ builder . Append ( FormatJoinExpression ( foreignKey , i , alias ) ) ;
122130 }
123131 builder . Append ( ")" ) ;
124132 return builder . ToString ( ) ;
@@ -159,9 +167,12 @@ private SimpleExpression CreateJoinExpression(ObjectReference table, ForeignKey
159167 return masterObjectReference == detailObjectReference ;
160168 }
161169
162- private string FormatJoinExpression ( ForeignKey foreignKey , int columnIndex )
170+ private string FormatJoinExpression ( ForeignKey foreignKey , int columnIndex , string alias )
163171 {
164- return string . Format ( "{0}.{1} = {2}.{3}" , _schema . QuoteObjectName ( foreignKey . MasterTable ) , _schema . QuoteObjectName ( foreignKey . UniqueColumns [ columnIndex ] ) ,
172+ var leftTable = string . IsNullOrWhiteSpace ( alias )
173+ ? _schema . QuoteObjectName ( foreignKey . MasterTable )
174+ : _schema . QuoteObjectName ( alias ) ;
175+ return string . Format ( "{0}.{1} = {2}.{3}" , leftTable , _schema . QuoteObjectName ( foreignKey . UniqueColumns [ columnIndex ] ) ,
165176 _schema . QuoteObjectName ( foreignKey . DetailTable ) , _schema . QuoteObjectName ( foreignKey . Columns [ columnIndex ] ) ) ;
166177 }
167178
@@ -178,7 +189,7 @@ private string JoinTypeToKeyword(JoinType joinType)
178189 private static IEnumerable < Tuple < ObjectName , ObjectName > > GetTableNames ( IEnumerable < ObjectReference > references , string schema )
179190 {
180191 return references . SelectMany ( r => DynamicReferenceToTuplePairs ( r , schema ) )
181- . TupleSelect ( ( table1 , table2 ) => Tuple . Create ( new ObjectName ( schema , table1 ) , new ObjectName ( schema , table2 ) ) )
192+ . TupleSelect ( ( table1 , table2 ) => Tuple . Create ( new ObjectName ( schema , table1 . Item1 , table1 . Item2 ) , new ObjectName ( schema , table2 . Item1 , table2 . Item2 ) ) )
182193 . Distinct ( ) ;
183194 }
184195
@@ -187,10 +198,10 @@ private static IEnumerable<Tuple<ObjectName, ObjectName>> GetTableNames(SimpleEx
187198 return expression == null ? Enumerable . Empty < Tuple < ObjectName , ObjectName > > ( ) : GetTableNames ( GetReferencesFromExpression ( expression ) , schema ) ;
188199 }
189200
190- private static IEnumerable < Tuple < string , string > > DynamicReferenceToTuplePairs ( ObjectReference reference , string schema )
201+ private static IEnumerable < Tuple < Tuple < string , string > , Tuple < string , string > > > DynamicReferenceToTuplePairs ( ObjectReference reference , string schema )
191202 {
192- return reference . GetAllObjectNames ( )
193- . SkipWhile ( s => s . Equals ( schema , StringComparison . OrdinalIgnoreCase ) )
203+ return reference . GetAllObjectNamesAndAliases ( )
204+ . SkipWhile ( s => s . Item1 . Equals ( schema , StringComparison . OrdinalIgnoreCase ) )
194205 . SkipLast ( )
195206 . ToTuplePairs ( ) ;
196207 }
0 commit comments