Hi
Suppose you have a query with the following criteria:
[...]where [...] and (@between(f.lastUpdate, now-:lastdays, now+1, day) or @between(f.updateTs, now-:lastdays, now+1, day) ) [...]
Entity with alias ‘f’ being an entity with a Date property ‘lastUpdate’ which is a functional date (as opposed to ‘updateTs’ which is technical).
Runningsuch query will produce the following error.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 2: unexpected token: @ line 2, column 2.
This comes from TimeBetweenMacroQueryHandler
which does not detect that the parameter ‘lastDays’ in this case is used twice. However the macro handler is able to detect several occurences of his macro.
So this issue can be fixed by extending TimeBetweenMacroQueryHandler
this way.
@Override
public String replaceQueryParams(String queryString, Map<String, Object> params) {
Matcher matcher = MACRO_PATTERN.matcher(queryString);
StringBuffer sb = new StringBuffer();
Set<String> paramToRemoves = new HashSet<>();
while (matcher.find()) {
String macros = matcher.group(0);
macros = replaceParamsInMacros(macros, params, paramToRemoves);
matcher.appendReplacement(sb, macros);
}
matcher.appendTail(sb);
for(String paramName : paramToRemoves)
params.remove(paramName);
return sb.toString();
}
@Override
protected String replaceParamsInMacros(String macros, Map<String, Object> params) {
throw new IllegalStateException("do not use this impl any more");
}
protected String replaceParamsInMacros(String macros, Map<String, Object> params, Set<String> paramToRemoves) {
Matcher matcher = QUERY_PARAM_PATTERN.matcher(macros);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String paramName = matcher.group(1);
if (params.containsKey(paramName)) {
matcher.appendReplacement(sb, params.get(paramName).toString());
paramToRemoves.add(paramName);
}
}
matcher.appendTail(sb);
return sb.toString();
}
Best Regards
Michael