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

com.jaregu.database.queries.building.ParametersResolverImpl 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.building;

import java.util.function.Supplier;

final class ParametersResolverImpl implements ParametersResolver {

	private final Object lock = new Object();
	private final Supplier namedSupplier;
	private final Supplier iteratorSupplier;

	private boolean resolved = false;
	private NamedResolver namedParameters;
	private IteratorResolver iteratorParameters;

	public ParametersResolverImpl(Supplier namedSupplier, Supplier iteratorSupplier) {
		this.namedSupplier = namedSupplier;
		this.iteratorSupplier = iteratorSupplier;
	}

	@Override
	public NamedResolver toNamed() {
		resolveIfNotResolved(() -> namedParameters = namedSupplier.get());
		if (namedParameters == null) {
			throw new QueryBuildException("Parameter resolver was already resolved as iterator resolver!"
					+ " Probably query contains mixed named (:name) and anonymous (?) parameters!");
		}
		return namedParameters;
	}

	@Override
	public IteratorResolver toIterator() {
		resolveIfNotResolved(() -> iteratorParameters = iteratorSupplier.get());
		if (iteratorParameters == null) {
			throw new QueryBuildException("Parameter resolver was already resolved as named resolver!"
					+ " Probably query contains mixed named (:name) and anonymous (?) parameters!");
		}
		return iteratorParameters;
	}

	private void resolveIfNotResolved(Runnable runnable) {
		if (!resolved) {
			synchronized (lock) {
				if (!resolved) {
					runnable.run();
					resolved = true;
				}
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy