Mule-Creating dynamic where condition for sql query through DB connector

前端 未结 3 1338
终归单人心
终归单人心 2021-01-25 06:56

I need to create dynamic query wherein the where condition will be changed based on the request coming in to mule.The request will be always get with query parameter. Here goes

3条回答
  •  情书的邮戳
    2021-01-25 07:25

    I had this in my mind of using Custom Transformers. So I used java transformer for this.

    The logic looks something like this :

    public class QueryBuilder extends AbstractMessageTransformer {
    
    @Override
    public Object transformMessage(MuleMessage message, String outputEncoding)
            throws TransformerException {
    
        System.out.println("Query Params : "
                + message.getInboundProperty("http.query.params").getClass()
                        .getName());
    
        Map map = message.getInboundProperty("http.query.params");
    
        System.out.println("Map keys : " + map.keySet());
        String where = "";
        for (Map.Entry entry : map.entrySet()) {
            System.out.println(entry.getKey() + "/" + entry.getValue());
            where = where+" "+entry.getKey()+"="+"'"+entry.getValue()+"'"+" and";
        }
        String whereCondition = where.substring(0, where.lastIndexOf(" "));
        System.out.println("Where condition is : "+ where.substring(0, where.lastIndexOf(" ")));
        return whereCondition;
    }}
    

    Now this returns the payload which is string type.

    In DB connector, select Query type as Dynamic. After WHERE condition add #[payload].

    Cheers

提交回复
热议问题