All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.runner.runnertarget.GrqlResolver Maven / Gradle / Ivy

The newest version!
package com.xlrit.gears.runner.runnertarget;

import java.util.regex.Pattern;

public class GrqlResolver {
	private static final Pattern variablePattern = Pattern.compile("(@\\w+)|(\\{(d|t|dt|ts) '([^']+)'})"); // `ts` is the official prefix for datetimes (aka timestamps)

	private final AbstractRunnerTarget runnerTarget;

	public GrqlResolver(AbstractRunnerTarget runnerTarget) {
		this.runnerTarget = runnerTarget;
	}

	public String toJpql(String grql) {
		return variablePattern.matcher(grql).replaceAll(matchResult -> {
			String key = matchResult.group(1);
			if (key != null) {
				return resolveVariable(key);
			}
			String literal = matchResult.group(2);
			if (literal != null) {
				return toTemporalLiteral(matchResult.group(3), matchResult.group(4));
			}
			throw new IllegalStateException();
		});
	}

	private String resolveVariable(String variable) {
		if (variable.substring(1).equals("startTime")) {
			return toTemporalLiteral("ts", runnerTarget.getStartTime());
		}
		throw new InterpreterException(String.format("No resolver available for variable '%s' in GRQL", variable));
	}

	// see https://docs.jboss.org/hibernate/orm/6.2/userguide/html_single/Hibernate_User_Guide.html#hql-datetime-literals
	private static String toTemporalLiteral(String type, String value) {
		if ("dt".equals(type)) type = "ts";
		return "{" + type + " '" + value + "'}";
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy