简体   繁体   English

使用字符串生成器在Java中生成动态查询

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

I am trying to generate a dynamic query in java, I need to generate the same table Cos_Class_Allocation and also the same columns class_name = ? AND CLASS_ALLOCATION = ? 我正在尝试在Java中生成动态查询,我需要生成相同的表Cos_Class_Allocation和相同的列class_name = ? AND CLASS_ALLOCATION = ? class_name = ? AND CLASS_ALLOCATION = ? . How can I clean my code in the loops I notice I declare alloc but did not called it, is there a better way to write the loops and also how can I remove the last comma from my first foreach loop and finally remove the last "And " from the second foreach loop. 如何注意到我声明了alloc但没有调用alloc的循环中的代码,是否有更好的方法编写循环,以及如何从第一个foreach循环中删除最后一个逗号,最后删除最后一个“来自第二个foreach循环。

My Query should look like this: 我的查询应如下所示:

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

code: 码:

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

this is the output below: (I have an extra AND and extra after last table comma ) 这是下面的输出:(我在最后一个表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 

I was able to generate my dynamic query by using for loop instead of a foreach loop. 我能够通过使用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