@@ -2347,6 +2347,16 @@ public String gainColumnString(boolean inSQLJoin) throws Exception {
23472347 if (joinList != null ) {
23482348 boolean first = true ;
23492349 for (Join <T , M , L > join : joinList ) {
2350+ if (join == null ) {
2351+ continue ;
2352+ }
2353+ if (join .getJoinType () == null ) {
2354+ SQLConfig <T , M , L > cfg = join .getJoinConfig ();
2355+ List <String > col = cfg == null ? null : cfg .getColumn ();
2356+ column = col == null ? column : col ;
2357+ continue ;
2358+ }
2359+
23502360 if (join .isAppJoin ()) {
23512361 continue ;
23522362 }
@@ -3315,11 +3325,23 @@ else if (key.equals(userIdInKey)) {
33153325 */
33163326 @ Override
33173327 public String gainWhereString (boolean hasPrefix ) throws Exception {
3318- String combineExpr = getCombine ();
3328+ Join <T , M , L > join = joinList == null || joinList .isEmpty () ? null : joinList .get (0 );
3329+ SQLConfig <T , M , L > cfg = join == null || join .getJoinType () != null ? null : join .getJoinConfig ();
3330+ Map <String , Object > joinWhere = cfg == null ? null : cfg .getWhere ();
3331+ //String ws = cfg == null ? null : cfg.gainWhereString(hasPrefix);
3332+ //if (StringUtil.isNotEmpty(ws, true)) {
3333+ // return ws;
3334+ //}
3335+
3336+ String joinCombine = cfg == null ? null : cfg .getCombine ();
3337+ Map <String , Object > where = joinWhere == null ? getWhere () : joinWhere ;
3338+ String combineExpr = StringUtil .isEmpty (joinCombine , true ) ? getCombine () : joinCombine ;
33193339 if (StringUtil .isEmpty (combineExpr , false )) {
3320- return getWhereString (hasPrefix , getMethod (), getWhere (), getCombineMap (), getJoinList (), ! isTest ());
3340+ Map <String , List <String >> joinCombineMap = cfg == null ? null : cfg .getCombineMap ();
3341+ Map <String , List <String >> combineMap = joinCombineMap == null ? getCombineMap () : joinCombineMap ;
3342+ return getWhereString (hasPrefix , getMethod (), where , combineMap , getJoinList (), ! isTest ());
33213343 }
3322- return getWhereString (hasPrefix , getMethod (), getWhere () , combineExpr , getJoinList (), ! isTest ());
3344+ return getWhereString (hasPrefix , getMethod (), where , combineExpr , getJoinList (), ! isTest ());
33233345 }
33243346 /**获取WHERE
33253347 * @param method
@@ -3741,7 +3763,10 @@ protected String concatJoinWhereString(String whereString) throws Exception {
37413763 boolean changed = false ;
37423764 // 各种 JOIN 没办法统一用 & | !连接,只能按优先级,和 @combine 一样?
37433765 for (Join <T , M , L > j : joinList ) {
3744- String jt = j .getJoinType ();
3766+ String jt = j == null ? null : j .getJoinType ();
3767+ if (jt == null ) {
3768+ continue ;
3769+ }
37453770
37463771 switch (jt ) {
37473772 case "*" : // CROSS JOIN
@@ -3752,7 +3777,7 @@ protected String concatJoinWhereString(String whereString) throws Exception {
37523777 if (outerConfig == null ){
37533778 break ;
37543779 }
3755- boolean isMain1 = outerConfig . isMain ();
3780+
37563781 outerConfig .setMain (false ).setPrepared (isPrepared ()).setPreparedValueList (new ArrayList <Object >());
37573782 String outerWhere = outerConfig .gainWhereString (false );
37583783
@@ -4021,7 +4046,10 @@ else if (isTest()) {
40214046 return gainSQLValue (key ).toString ();
40224047 }
40234048
4024- Map <String , String > keyMap = getKeyMap ();
4049+ Join <T , M , L > join = joinList == null || joinList .isEmpty () ? null : joinList .get (0 );
4050+ SQLConfig <T , M , L > cfg = join == null || join .getJoinType () != null ? null : join .getJoinConfig ();
4051+ Map <String , String > joinKeyMap = cfg == null ? null : cfg .getKeyMap ();
4052+ Map <String , String > keyMap = joinKeyMap == null ? getKeyMap () : joinKeyMap ;
40254053 String expression = keyMap == null ? null : keyMap .get (key );
40264054 if (expression == null ) {
40274055 expression = COLUMN_KEY_MAP == null ? null : COLUMN_KEY_MAP .get (key );
@@ -5162,13 +5190,23 @@ public String gainJoinString() throws Exception {
51625190 List <Object > pvl = getPreparedValueList (); // new ArrayList<>();
51635191 //boolean changed = false;
51645192
5165- // 主表不用别名 String ta;
5166- for (Join j : joinList ) {
5193+ // 主表不用别名 String ta;
5194+ boolean first = true ;
5195+ for (Join <T , M , L > j : joinList ) {
5196+ if (j == null ) {
5197+ continue ;
5198+ }
5199+ if (first ) {
5200+ first = false ;
5201+ continue ;
5202+ }
5203+
51675204 onGainJoinString (j );
51685205
51695206 if (j .isAppJoin ()) { // APP JOIN,只是作为一个标记,执行完主表的查询后自动执行副表的查询 User.id IN($commentIdList)
51705207 continue ;
51715208 }
5209+
51725210 String type = j .getJoinType ();
51735211
51745212 //LEFT JOIN sys.apijson_user AS User ON User.id = Moment.userId, 都是用 = ,通过relateType处理缓存
@@ -5490,7 +5528,7 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> SQLConf
54905528 String catalog = getString (request , KEY_CATALOG );
54915529 String schema = getString (request , KEY_SCHEMA );
54925530
5493- SQLConfig <T , M , L > config = ( SQLConfig < T , M , L >) callback .getSQLConfig (method , database , schema , datasource , table );
5531+ SQLConfig <T , M , L > config = callback .getSQLConfig (method , database , schema , datasource , table );
54945532 config .setAlias (alias );
54955533
54965534 config .setDatabase (database ); // 不删,后面表对象还要用的,必须放在 parseJoin 前
@@ -6306,6 +6344,11 @@ public static <T, M extends Map<String, Object>, L extends List<Object>> SQLConf
63066344 for (Join <T , M , L > join : joinList ) {
63076345 table = join .getTable ();
63086346 alias = join .getAlias ();
6347+
6348+ if (table == null && join .getJoinType () == null ) {
6349+ table = config .getTable ();
6350+ }
6351+
63096352 //JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误
63106353 SQLConfig <T , M , L > joinConfig = newSQLConfig (method , table , alias , join .getRequest (), null , false , callback );
63116354 SQLConfig <T , M , L > cacheConfig = join .canCacheViceTable () == false ? null : newSQLConfig (method , table , alias
@@ -6319,6 +6362,7 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
63196362 throw new IllegalArgumentException ("主表 " + config .getTable () + " 的 @database:" + config .getDatabase ()
63206363 + " 和它 SQL JOIN 的副表 " + table + " 的 @database:" + joinConfig .getDatabase () + " 不一致!" );
63216364 }
6365+
63226366 if (joinConfig .getSchema () == null ) {
63236367 joinConfig .setSchema (config .getSchema ()); //主表 JOIN 副表,默认 schema 一致
63246368 }
@@ -6327,16 +6371,17 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
63276371 cacheConfig .setDatabase (joinConfig .getDatabase ()).setSchema (joinConfig .getSchema ()); //解决主表 JOIN 副表,引号不一致
63286372 }
63296373
6330- if (isQuery ) {
6331- config .setKeyPrefix (true );
6374+ if (join .getJoinType () != null ) {
6375+ if (isQuery ) {
6376+ config .setKeyPrefix (true );
6377+ }
6378+ joinConfig .setMain (false ).setKeyPrefix (true );
63326379 }
63336380
6334- joinConfig .setMain (false ).setKeyPrefix (true );
6335-
63366381 if (join .getOn () != null ) {
63376382 SQLConfig <T , M , L > onConfig = newSQLConfig (method , table , alias , join .getOn (), null , false , callback );
6338- onConfig .setMain (false )
6339- .setKeyPrefix (true )
6383+ onConfig .setMain (joinConfig . isMain () )
6384+ .setKeyPrefix (joinConfig . isKeyPrefix () )
63406385 .setDatabase (joinConfig .getDatabase ())
63416386 .setSchema (joinConfig .getSchema ()); //解决主表 JOIN 副表,引号不一致
63426387
@@ -6345,8 +6390,8 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) {
63456390
63466391 if (join .getOuter () != null ) {
63476392 SQLConfig <T , M , L > outerConfig = newSQLConfig (method , table , alias , join .getOuter (), null , false , callback );
6348- outerConfig .setMain (false )
6349- .setKeyPrefix (true )
6393+ outerConfig .setMain (joinConfig . isMain () )
6394+ .setKeyPrefix (joinConfig . isKeyPrefix () )
63506395 .setDatabase (joinConfig .getDatabase ())
63516396 .setSchema (joinConfig .getSchema ()); //解决主表 JOIN 副表,引号不一致
63526397 join .setOuterConfig (outerConfig );
0 commit comments