com.jaregu.database.queries.compiling.expr.ParsingContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of queries Show documentation
Show all versions of queries Show documentation
Java based SQL templating project. Store your queries in *.sql files and build queries for execution. Supports simple expressions and conditional clauses and interface proxying for java-sql bridge.
package com.jaregu.database.queries.compiling.expr;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
final class ParsingContext {
private static final ThreadLocal CONTEXT = new ThreadLocal<>();
private String expression;
public static Builder forExpression(String expression) {
return new Builder(expression);
}
private ParsingContext(String expression) {
this.expression = expression;
}
public String getExpression() {
return expression;
}
public T withContext(Supplier work) {
return withContext(this, work);
}
public static Optional peekCurrent() {
return Optional.ofNullable(CONTEXT.get());
}
public static ParsingContext getCurrent() {
return peekCurrent().orElseThrow(() -> new ExpressionParseException(
"Trying to parse expression without " + ParsingContext.class.getSimpleName() + "!"));
}
public static T withContext(ParsingContext context, Supplier work) {
ParsingContext oldContext = CONTEXT.get();
try {
CONTEXT.set(Objects.requireNonNull(context));
return work.get();
} finally {
CONTEXT.set(oldContext);
}
}
public static class Builder {
private ParsingContext context;
private Builder(String expression) {
context = new ParsingContext(expression);
}
public ParsingContext build() {
return context;
}
}
}