@@ -1036,25 +1036,27 @@ public String getGroupString(boolean hasPrefix) {
10361036//加上子表的 group
10371037String joinGroup = "" ;
10381038if (joinList != null ) {
1039- SQLConfig cfg ;
1040- String c ;
10411039boolean first = true ;
10421040for (Join j : joinList ) {
10431041if (j .isAppJoin ()) {
10441042continue ;
10451043}
10461044
1047- cfg = j .isLeftOrRightJoin () ? j .getOuterConfig () : j .getJoinConfig ();
1048- if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1049- cfg .setAlias (cfg .getTable ());
1050- }
1045+ SQLConfig ocfg = j .getOuterConfig ();
1046+ SQLConfig cfg = (ocfg != null && ocfg .getGroup () != null ) || j .isLeftOrRightJoin () ? ocfg : j .getJoinConfig ();
10511047
1052- c = ((AbstractSQLConfig ) cfg ).getGroupString (false );
1053- if (StringUtil .isEmpty (c , true ) == false ) {
1054- joinGroup += (first ? "" : ", " ) + c ;
1055- first = false ;
1056- }
1048+ if (cfg != null ) {
1049+ cfg .setMain (false ).setKeyPrefix (true );
1050+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1051+ cfg .setAlias (cfg .getTable ());
1052+ }
1053+ String c = ((AbstractSQLConfig ) cfg ).getGroupString (false );
10571054
1055+ if (StringUtil .isEmpty (c , true ) == false ) {
1056+ joinGroup += (first ? "" : ", " ) + c ;
1057+ first = false ;
1058+ }
1059+ }
10581060}
10591061}
10601062
@@ -1098,25 +1100,27 @@ public String getHavingString(boolean hasPrefix) {
10981100//加上子表的 having
10991101String joinHaving = "" ;
11001102if (joinList != null ) {
1101- SQLConfig cfg ;
1102- String c ;
11031103boolean first = true ;
11041104for (Join j : joinList ) {
11051105if (j .isAppJoin ()) {
11061106continue ;
11071107}
11081108
1109- cfg = j .isLeftOrRightJoin () ? j .getOuterConfig () : j .getJoinConfig ();
1110- if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1111- cfg .setAlias (cfg .getTable ());
1112- }
1109+ SQLConfig ocfg = j .getOuterConfig ();
1110+ SQLConfig cfg = (ocfg != null && ocfg .getHaving () != null ) || j .isLeftOrRightJoin () ? ocfg : j .getJoinConfig ();
1111+
1112+ if (cfg != null ) {
1113+ cfg .setMain (false ).setKeyPrefix (true );
1114+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1115+ cfg .setAlias (cfg .getTable ());
1116+ }
1117+ String c = ((AbstractSQLConfig ) cfg ).getHavingString (false );
11131118
1114- c = (( AbstractSQLConfig ) cfg ). getHavingString ( false );
1115- if ( StringUtil . isEmpty ( c , true ) == false ) {
1116- joinHaving += ( first ? "" : ", " ) + c ;
1117- first = false ;
1119+ if ( StringUtil . isEmpty ( c , true ) == false ) {
1120+ joinHaving += ( first ? "" : ", " ) + c ;
1121+ first = false ;
1122+ }
11181123}
1119-
11201124}
11211125}
11221126
@@ -1255,25 +1259,27 @@ public String getOrderString(boolean hasPrefix) {
12551259//加上子表的 order
12561260String joinOrder = "" ;
12571261if (joinList != null ) {
1258- SQLConfig cfg ;
1259- String c ;
12601262boolean first = true ;
12611263for (Join j : joinList ) {
12621264if (j .isAppJoin ()) {
12631265continue ;
12641266}
12651267
1266- cfg = j .isLeftOrRightJoin () ? j .getOuterConfig () : j .getJoinConfig ();
1267- if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1268- cfg .setAlias (cfg .getTable ());
1269- }
1268+ SQLConfig ocfg = j .getOuterConfig ();
1269+ SQLConfig cfg = (ocfg != null && ocfg .getOrder () != null ) || j .isLeftOrRightJoin () ? ocfg : j .getJoinConfig ();
12701270
1271- c = ((AbstractSQLConfig ) cfg ).getOrderString (false );
1272- if (StringUtil .isEmpty (c , true ) == false ) {
1273- joinOrder += (first ? "" : ", " ) + c ;
1274- first = false ;
1275- }
1271+ if (cfg != null ) {
1272+ cfg .setMain (false ).setKeyPrefix (true );
1273+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1274+ cfg .setAlias (cfg .getTable ());
1275+ }
1276+ String c = ((AbstractSQLConfig ) cfg ).getOrderString (false );
12761277
1278+ if (StringUtil .isEmpty (c , true ) == false ) {
1279+ joinOrder += (first ? "" : ", " ) + c ;
1280+ first = false ;
1281+ }
1282+ }
12771283}
12781284}
12791285
@@ -1499,41 +1505,38 @@ public String getColumnString(boolean inSQLJoin) throws Exception {
14991505case GETS :
15001506String joinColumn = "" ;
15011507if (joinList != null ) {
1502- SQLConfig ecfg ;
1503- SQLConfig cfg ;
1504- String c ;
15051508boolean first = true ;
15061509for (Join j : joinList ) {
15071510if (j .isAppJoin ()) {
15081511continue ;
15091512}
15101513
1511- if (j .isLeftOrRightJoin ()) {
1514+ SQLConfig ocfg = j .getOuterConfig ();
1515+ boolean isEmpty = ocfg == null || ocfg .getColumn () == null ;
1516+ boolean isLeftOrRightJoin = j .isLeftOrRightJoin ();
1517+
1518+ if (isEmpty && isLeftOrRightJoin ) {
15121519// 改为 SELECT ViceTable.* 解决 SELECT sum(ViceTable.id) LEFT/RIGHT JOIN (SELECT sum(id) FROM ViceTable...) AS ViceTable
15131520// 不仅导致 SQL 函数重复计算,还有时导致 SQL 报错或对应字段未返回
15141521String quote = getQuote ();
15151522joinColumn += (first ? "" : ", " ) + quote + (StringUtil .isEmpty (j .getAlias (), true ) ? j .getTable () : j .getAlias ()) + quote + ".*" ;
15161523first = false ;
15171524} else {
1518- ecfg = j .getOuterConfig ();
1519- if (ecfg != null && ecfg .getColumn () != null ) { //优先级更高
1520- cfg = ecfg ;
1521- }
1522- else {
1523- cfg = j .getJoinConfig ();
1524- }
1525-
1526- if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1527- cfg .setAlias (cfg .getTable ());
1528- }
1529-
1530- c = ((AbstractSQLConfig ) cfg ).getColumnString (true );
1531- if (StringUtil .isEmpty (c , true ) == false ) {
1532- joinColumn += (first ? "" : ", " ) + c ;
1533- first = false ;
1525+ SQLConfig cfg = isLeftOrRightJoin == false && isEmpty ? j .getJoinConfig () : ocfg ;
1526+ if (cfg != null ) {
1527+ cfg .setMain (false ).setKeyPrefix (true );
1528+ if (StringUtil .isEmpty (cfg .getAlias (), true )) {
1529+ cfg .setAlias (cfg .getTable ());
1530+ }
1531+
1532+ String c = ((AbstractSQLConfig ) cfg ).getColumnString (true );
1533+ if (StringUtil .isEmpty (c , true ) == false ) {
1534+ joinColumn += (first ? "" : ", " ) + c ;
1535+ first = false ;
1536+ }
15341537}
15351538}
1536-
1539+
15371540inSQLJoin = true ;
15381541}
15391542}
0 commit comments