io.ebeaninternal.server.expression.AllEqualsExpression Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ebean Show documentation
Show all versions of ebean Show documentation
composite of common runtime dependencies for all platforms
package io.ebeaninternal.server.expression;
import io.ebeaninternal.api.ManyWhereJoins;
import io.ebeaninternal.api.SpiExpression;
import io.ebeaninternal.api.SpiExpressionRequest;
import io.ebeaninternal.api.SpiExpressionValidation;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.el.ElPropertyDeploy;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
class AllEqualsExpression extends NonPrepareExpression {
private final Map propMap;
AllEqualsExpression(Map propMap) {
this.propMap = propMap;
}
protected String name(String propName) {
return propName;
}
@Override
public void writeDocQuery(DocQueryContext context) throws IOException {
context.writeAllEquals(propMap);
}
@Override
public String nestedPath(BeanDescriptor> desc) {
return null;
}
@Override
public void containsMany(BeanDescriptor> desc, ManyWhereJoins manyWhereJoin) {
if (propMap != null) {
for (String propertyName : propMap.keySet()) {
ElPropertyDeploy elProp = desc.getElPropertyDeploy(name(propertyName));
if (elProp != null && elProp.containsMany()) {
manyWhereJoin.add(elProp);
}
}
}
}
@Override
public void validate(SpiExpressionValidation validation) {
for (String propName : propMap.keySet()) {
validation.validate(propName);
}
}
@Override
public void addBindValues(SpiExpressionRequest request) {
if (propMap.isEmpty()) {
return;
}
for (Object value : propMap.values()) {
// null value uses is null clause
if (value != null) {
request.addBindValue(value);
}
}
}
@Override
public void addSql(SpiExpressionRequest request) {
if (propMap.isEmpty()) {
return;
}
request.append("(");
int count = 0;
for (Map.Entry entry : propMap.entrySet()) {
Object value = entry.getValue();
String propName = entry.getKey();
if (count > 0) {
request.append("and ");
}
request.append(name(propName));
if (value == null) {
request.append(" is null ");
} else {
request.append(" = ? ");
}
count++;
}
request.append(")");
}
/**
* Based on the properties and whether they are null.
*
* The null check is required due to the "is null" sql being generated.
*
*/
@Override
public void queryPlanHash(StringBuilder builder) {
builder.append("AllEquals[");
for (Entry entry : propMap.entrySet()) {
Object value = entry.getValue();
String propName = entry.getKey();
builder.append(propName);
if (value == null) {
builder.append(" isNull");
} else {
builder.append(" =?");
}
builder.append(",");
}
builder.append("]");
}
@Override
public int queryBindHash() {
int hc = 92821;
for (Object value : propMap.values()) {
hc = hc * 92821 + (value == null ? 0 : value.hashCode());
}
return hc;
}
@Override
public boolean isSameByBind(SpiExpression other) {
if (!(other instanceof AllEqualsExpression)) {
return false;
}
AllEqualsExpression that = (AllEqualsExpression) other;
return isSameByValue(that, true);
}
private boolean isSameByValue(AllEqualsExpression that, boolean byValue) {
if (propMap.size() != that.propMap.size()) {
return false;
}
Iterator> thisIt = propMap.entrySet().iterator();
Iterator> thatIt = that.propMap.entrySet().iterator();
while (thisIt.hasNext() && thatIt.hasNext()) {
Entry thisNext = thisIt.next();
Entry thatNext = thatIt.next();
if (!thisNext.getKey().equals(thatNext.getKey())) {
return false;
}
if (!Same.sameBy(byValue, thisNext.getValue(), thatNext.getValue())) {
return false;
}
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy