Search code examples
javafreemarker

Freemarker applying join after another join


I got such a data:

{ "key1" : [ "value1", "value2" ],
  "key2" : [ "value3", "value4" ],
  "key3" : [ "value5", "value6" ] }

And i want to create sql query WHERE clause from them like this:

WHERE
column1 in (value1, value2)
OR
column2 in (value3, value4)
OR
column3 in (value5, value6)

I try to make an assign <#assign column1 = <#if key1??> ${key1?join(", ")} </#if> >, but it seems that it is not possible

The question is: how to set OR delimiter between clauses? How to apply join from join? Maybe some else solutions without using assign?


Solution

  • Use can use #list, and #sep. Something like this:

    WHERE
    <#list map as k, v>
    ${k} in (${v?join(", ")})
    <#sep>
    OR
    </#list>
    

    Also, you might want to use ?join in simpler cases instead, like I did above for the nested listing. If that listing has to do something complicated, you you probably want to use <#list v as it>complex stuff for ${it}<#sep>, </#list> there.