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

com.jaregu.database.queries.compiling.expr.ParsingContext Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.4.1
Show newest version
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;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy