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

com.jaregu.database.queries.compiling.PreparedQueryImpl 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;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.jaregu.database.queries.QueryId;
import com.jaregu.database.queries.building.ParametersResolver;
import com.jaregu.database.queries.building.Query;
import com.jaregu.database.queries.building.QueryImpl;
import com.jaregu.database.queries.compiling.PreparedQueryPart.Result;
import com.jaregu.database.queries.dialect.Dialect;

final class PreparedQueryImpl implements PreparedQuery {

	final private QueryId id;
	final private List parts;
	final private Dialect dialect;

	PreparedQueryImpl(QueryId id, List parts, Dialect dialect) {
		this.id = id;
		this.parts = parts;
		this.dialect = dialect;
	}

	@Override
	public QueryId getQueryId() {
		return id;
	}

	@Override
	public Query build(ParametersResolver resolver) {
		StringBuilder sql = new StringBuilder();
		List allParameters = new ArrayList<>();
		Map allAttributes = new HashMap<>();
		for (PreparedQueryPart part : parts) {
			Result result = part.build(resolver);
			result.getSql().ifPresent(sql::append);
			allParameters.addAll(result.getParameters());
			allAttributes.putAll(result.getAttributes());
		}
		return new QueryImpl(sql.toString(), allParameters, allAttributes, dialect);
	}

	@Override
	public String toString() {
		String idToString = id.toString();
		StringBuilder sb = new StringBuilder(idToString.length() + 20);
		sb.append("PreparedQuery{id: ").append(idToString).append("}");
		return sb.toString();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;

		if (obj instanceof PreparedQueryImpl) {
			PreparedQueryImpl other = (PreparedQueryImpl) obj;
			return id.equals(other.id);
		}

		return false;
	}

	@Override
	public int hashCode() {
		return Objects.hash(id);
	}
}