2015-12-09 68 views
0

我想在java中生成一個動態查詢,我需要生成相同的表Cos_Class_Allocation和同樣的列class_name = ? AND CLASS_ALLOCATION = ?。我怎樣才能在循環中清理我的代碼我注意到我聲明瞭alloc,但沒有調用它,是否有更好的方法來編寫循環,以及如何從我的第一個foreach循環中刪除最後一個逗號,最後刪除最後一個「And 「來自第二個foreach循環。使用字符串生成器在java中生成一個動態查詢

我的查詢應該是這樣的:

SELECT * FROM TRAFFIC_PROFILE 
     WHERE COS_MODEL = 'cos4' AND DIRECTION = 'Egress' 

      AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y') 

     and TRAFFIC_PROFILE_ID IN (

     select distinct (C1.Traffic_PROFILE_ID) from Cos_Class_Allocation C1, 
     Cos_Class_Allocation C2, Cos_Class_Allocation C3, 
     Cos_Class_Allocation C4, Cos_Class_Allocation C5, Cos_Class_Allocation C6 
     where c1.class_name = ? AND c1.CLASS_ALLOCATION = ? 
     and c2.class_name = ? AND c2.CLASS_ALLOCATION = ? 
     and c3.class_name = ? AND c3.CLASS_ALLOCATION = ? 
     and c4.class_name = ? AND c4.CLASS_ALLOCATION = ? 
     and c5.class_name = ? AND c5.CLASS_ALLOCATION = ? 
     and c6.class_name = ? AND c6.CLASS_ALLOCATION = ? 
     and C1.TRAFFIC_PROFILE_ID = c2.TRAFFIC_PROFILE_ID 
     and C1.TRAFFIC_PROFILE_ID = c3.TRAFFIC_PROFILE_ID 
     and C1.TRAFFIC_PROFILE_ID = c4.TRAFFIC_PROFILE_ID 
     and C1.TRAFFIC_PROFILE_ID = c5.TRAFFIC_PROFILE_ID 
     and C1.TRAFFIC_PROFILE_ID = c6.TRAFFIC_PROFILE_ID) ; 

代碼:

private String buildTrafficProfileByCosClassAllocationQuery(TrafficProfileExtension.CosModel cosModel, Direction direction, List<ClassOfServiceAllocation> cosAllocation, RouterType routerType){ 
    StringBuilder builder = new StringBuilder(); 

    builder.append(queryByDirectionRouterTypeAndCosClassAllocation); 
    builder.append(buildRouterQuery(routerType)); 
    builder.append("and TRAFFIC_PROFILE_ID IN (select distinct (C1.Traffic_PROFILE_ID) from "); 
// builder.append(buildCosModelQuery(cosModel)); 


    int i = 1; 

    for(ClassOfServiceAllocation alloc : cosAllocation){ 
     builder.append(" Cos_Class_Allocation c" + i++ +","); 

    } 
    builder.append(" where " ); 
    int n = 1; 
    int a =1; 

    for(ClassOfServiceAllocation alloc : cosAllocation){ 
    builder.append("c" + n++ + ".class_name = ? AND c" + a++ + ".CLASS_ALLOCATION = ? AND "); 

    } 

    int tp = 2; 
    for(ClassOfServiceAllocation alloc : cosAllocation){ 
     builder.append("and C1.TRAFFIC_PROFILE_ID = c" + tp++ + ".TRAFFIC_PROFILE_ID "); 
    } 


    return builder.toString(); 
} 

這是下面的輸出:(我有一個額外的AND和額外的最後一個表comma後)

SELECT * FROM TRAFFIC_PROFILE 
WHERE COS_MODEL = ? AND DIRECTION = ? 
AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y') 
and TRAFFIC_PROFILE_ID IN ( 
select distinct (C1.Traffic_PROFILE_ID) 
from Cos_Class_Allocation c1, Cos_Class_Allocation c2, 
Cos_Class_Allocation c3, Cos_Class_Allocation c4, 
Cos_Class_Allocation c5, Cos_Class_Allocation c6, 
where c1.class_name = ? AND c1.CLASS_ALLOCATION = ? 
AND c2.class_name = ? AND c2.CLASS_ALLOCATION = ? 
AND c3.class_name = ? AND c3.CLASS_ALLOCATION = ? 
AND c4.class_name = ? AND c4.CLASS_ALLOCATION = ? 
AND c5.class_name = ? AND c5.CLASS_ALLOCATION = ? 
AND c6.class_name = ? AND c6.CLASS_ALLOCATION = ? 
AND and C1.TRAFFIC_PROFILE_ID = c2.TRAFFIC_PROFILE_ID 
and C1.TRAFFIC_PROFILE_ID = c3.TRAFFIC_PROFILE_ID 
and C1.TRAFFIC_PROFILE_ID = c4.TRAFFIC_PROFILE_ID 
and C1.TRAFFIC_PROFILE_ID = c5.TRAFFIC_PROFILE_ID 
and C1.TRAFFIC_PROFILE_ID = c6.TRAFFIC_PROFILE_ID 
+1

請問您可以發佈代碼。 –

+0

剛剛發佈了代碼 – Gio

+0

上面的代碼是否生成正確的字符串?如果是這樣,你應該去CodeReview.stackexchange –

回答

0

我能夠通過使用for循環而不是先行生成我的動態查詢ach循環。

private String buildTrafficProfileByCosClassAllocationQuery(TrafficProfileExtension.CosModel cosModel, Direction direction, List<ClassOfServiceAllocation> cosAllocation, RouterType routerType){ 
    StringBuilder builder = new StringBuilder(); 

    builder.append(queryByDirectionRouterTypeAndCosClassAllocation); 
    builder.append(buildRouterQuery(routerType)); 
    builder.append("and TRAFFIC_PROFILE_ID IN (select distinct (C1.Traffic_PROFILE_ID) from "); 
// builder.append(buildCosModelQuery(cosModel)); 


    for (int i = 1; i <= cosAllocation.size(); i++) { 
      if (i > 1) builder.append(","); 
     builder.append(" Cos_Class_Allocation c").append(i); 
    } 

    for (int i = 1; i <= cosAllocation.size(); i++) { 
      builder.append(i == 1 ? " where " : " and "); 
      builder.append("c").append(i).append(".class_name = ? AND c").append(i).append(".CLASS_ALLOCATION = ?"); 
     if (i > 1) { 
       builder.append(" and C1.TRAFFIC_PROFILE_ID = c").append(i).append(".TRAFFIC_PROFILE_ID"); 
     } 
    } 

    builder.append(")"); 

    return builder.toString(); 
}