
io.github.hiskrtapps.apocalypse.dao.api.statements.common.Union Maven / Gradle / Ivy
/*
* © 2020 Ceppi Productions.
*/
package io.github.hiskrtapps.apocalypse.dao.api.statements.common;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import io.github.hiskrtapps.apocalypse.dao.api.Entity;
import io.github.hiskrtapps.apocalypse.dao.api.statements.Find;
import io.github.hiskrtapps.apocalypse.dao.api.statements.Statement;
/**
* Find composed of several finds of the same entity and join them by an UNION or UNION ALL
*/
public class Union implements Find {
private static final String COMMAND_UNION_ALL = "%s union all (%s)";
private static final String COMMAND_UNION = "%s union (%s)";
private Find firstFind;
private List> otherFinds;
private boolean unionAll;
public Union(boolean unionAll, Find firstFind, Find ... otherFind) {
this.unionAll = unionAll;
this.firstFind = firstFind;
this.otherFinds = Arrays.asList(otherFind);
}
@Override
public Class entityClass() {
return firstFind.entityClass();
}
@Override
public Map valuesMap() {
Map allValuesMap = new HashMap<>();
allValuesMap.putAll(firstFind.valuesMap());
for (Find otherFind : otherFinds) {
allValuesMap.putAll(otherFind.valuesMap());
}
return allValuesMap;
}
@Override
public String name() {
return firstFind.name() + otherFinds.stream().map(Statement::name).collect(Collectors.joining("|"));
}
@Override
public String command() {
final String unionCommand = unionAll ? COMMAND_UNION_ALL : COMMAND_UNION;
String command = String.format("(" + firstFind.command() + ")");
for (Find otherFind : otherFinds) {
command = String.format(unionCommand, command, otherFind.command());
}
return command;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy