Search code examples
javamysqlquerydsl

Call mysql nested/inner functions in querydsl


I need to run the following query in QueryDsl Native MySQL.

SELECT cast(json_unquote(JSON_EXTRACT(vendor_master,'$.vendor.currency')) as char) as currency FROM vendor;

The above query has 3 mysql functions which are nested such as cast,json_unquote and json_extract. In QueryDsl, I have created separate Expression for each function as shown in the below code.

    SimpleExpression<String> exp1=Expressions.simpleTemplate(String.class,"json_extract({0},{1})",vendor.vendorMaster,"$.vendor.currency");
SimpleExpression<String> exp2=Expressions.simpleTemplate(String.class,"json_unquote({0})",exp2);
SimpleExpression<String> exp3=Expressions.simpleTemplate(String.class,"cast({0} as char) as currency",exp3);
List<Tuple> tuples=sqlQueryFactory.select(exp3).from(vendor).fetch();

I have to create more number of expressions as much as no of functions. Is it possible to call more than one function in single expression in order to reduce complexity?


Solution

  • I found the answer for the question.

    SimpleExpression<String> exp=Expressions.simpleTemplate(String.class,"cast(json_unquote(json_extract({0},{1})) as char) as currency",vendor.vendorMaster,"$.vendor.currency"); 
    List<Tuple> tuples=sqlQueryFactory.select(exp).from(vendor).fetch();
    

    Thanks for your response.