簡體   English   中英

使用字符串生成器在Java中生成動態查詢

[英]Generate a dynamic query in java using string builder

我正在嘗試在Java中生成動態查詢,我需要生成相同的表Cos_Class_Allocation和相同的列class_name = ? AND CLASS_ALLOCATION = ? class_name = ? AND CLASS_ALLOCATION = ? 如何注意到我聲明了alloc但沒有調用alloc的循環中的代碼,是否有更好的方法編寫循環,以及如何從第一個foreach循環中刪除最后一個逗號,最后刪除最后一個“來自第二個foreach循環。

我的查詢應如下所示:

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();
}

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

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 

我能夠通過使用for循環而不是foreach循環來生成動態查詢。

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM