@@ -68,6 +68,11 @@ public string ColumnExpression
6868 return string . Format ( "{0}.{1} AS {2}" , TableAlias , ColumnName , Alias ) ;
6969 }
7070 }
71+
72+ public override string ToString ( )
73+ {
74+ return ColumnExpression ;
75+ }
7176 }
7277
7378 private DbmsRelationTree _RootTable ;
@@ -89,7 +94,7 @@ private void TraverseRelationTree(DbmsRelationTree nodeTable, string parentAlias
8994 {
9095 nodeTable . LinkProperty = new TableLink ( this , parentAlias , position ) ;
9196
92- foreach ( DbmsColumn col in nodeTable . Columns )
97+ foreach ( DbmsColumn col in nodeTable . DisplayColumns )
9398 _ColumnAliases . Add ( new ColumnAlias ( nodeTable , col ) ) ;
9499
95100 for ( byte pos = 0 ; pos < nodeTable . ForeignKeys . Count ; pos ++ )
@@ -98,24 +103,32 @@ private void TraverseRelationTree(DbmsRelationTree nodeTable, string parentAlias
98103
99104 public string BuildJoinClause ( )
100105 {
101- return BuildJoinClause ( _RootTable ) ;
106+ int cntJoin ;
107+ return BuildJoinClause ( _RootTable , out cntJoin ) ;
102108 }
103109
104- private string BuildJoinClause ( DbmsRelationTree startTable )
110+ private string BuildJoinClause ( DbmsRelationTree startTable , out int cntJoin )
105111 {
106112 StringBuilder joinClause = new StringBuilder ( ) ;
107113
108114 joinClause . AppendFormat ( startTable . TableName ) ;
109115 joinClause . Append ( " " ) ;
110116 joinClause . AppendLine ( startTable . LinkProperty . Alias ) ;
117+ cntJoin = 1 ;
111118
112119 foreach ( DbmsForeignKey fk in startTable . ForeignKeys ) // INNER JOIN first
113120 if ( ! fk . IsNullable ) // ForeignKeyColumns is not null
121+ {
114122 AppendJoinSource ( joinClause , fk ) ;
123+ cntJoin ++ ;
124+ }
115125
116126 foreach ( DbmsForeignKey fk in startTable . ForeignKeys ) // Then LEFT JOIN
117127 if ( fk . IsNullable ) // ForeignKeyColumns is nullable
128+ {
118129 AppendJoinSource ( joinClause , fk ) ;
130+ cntJoin ++ ;
131+ }
119132
120133 return joinClause . ToString ( ) ;
121134 }
@@ -126,16 +139,25 @@ private void AppendJoinSource(StringBuilder joinClause, DbmsForeignKey foreignKe
126139 string pkTableAlias = foreignKey . PrimaryUniqueKeyBaseTable . LinkProperty . Alias ;
127140 List < DbmsColumn > fkColumns = foreignKey . ForeignKeyColumns ;
128141 List < DbmsColumn > pkColumns = foreignKey . PrimaryUniqueKeyColumns ;
129- int nCols = foreignKey . ForeignKeyColumns . Count ;
142+ int cntJoin , nCols = foreignKey . ForeignKeyColumns . Count ;
143+ string insideJoin = BuildJoinClause ( foreignKey . PrimaryUniqueKeyBaseTable , out cntJoin ) ;
130144
131145 if ( foreignKey . IsNullable )
132- joinClause . AppendLine ( "LEFT JOIN ( " ) ;
146+ joinClause . Append ( "LEFT JOIN" ) ;
133147 else
134- joinClause . AppendLine ( "INNER JOIN (" ) ;
148+ joinClause . Append ( "INNER JOIN" ) ;
149+
150+ if ( cntJoin > 1 )
151+ joinClause . AppendLine ( " (" ) ;
152+ else
153+ joinClause . AppendLine ( ) ;
154+
155+ joinClause . Append ( insideJoin . PushIndent ( ) ) ;
135156
136- joinClause . Append ( BuildJoinClause ( foreignKey . PrimaryUniqueKeyBaseTable ) . PushIndent ( ) ) ;
157+ if ( cntJoin > 1 )
158+ joinClause . Append ( ") " ) ;
137159
138- joinClause . Append ( ") ON (" ) ;
160+ joinClause . Append ( "ON (" ) ;
139161
140162 for ( int i = 0 ; i < nCols ; i ++ )
141163 {
@@ -145,7 +167,7 @@ private void AppendJoinSource(StringBuilder joinClause, DbmsForeignKey foreignKe
145167 joinClause . AppendFormat ( "{0}.{1} = {2}.{3}" , fkTableAlias , fkColumns [ i ] . ColumnName , pkTableAlias , pkColumns [ i ] . ColumnName ) ;
146168 }
147169
148- joinClause . Append ( ")" ) ;
170+ joinClause . AppendLine ( ")" ) ;
149171 }
150172
151173 public bool SolveNameRepetition ( string renameFormat ) // renameFormat: "{0}${1}" - {0}: Table; {1}: ColumnName
0 commit comments