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

io.ebeaninternal.server.expression.AllEqualsExpression Maven / Gradle / Ivy

There is a newer version: 15.8.1
Show newest version
package io.ebeaninternal.server.expression;

import io.ebeaninternal.api.*;
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;

final class AllEqualsExpression extends NonPrepareExpression {

  private final Map propMap;

  AllEqualsExpression(Map propMap) {
    this.propMap = propMap;
  }

  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.elPropertyDeploy(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(SpiExpressionBind 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.property(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 void queryBindKey(BindValuesKey key) { key.add(propMap.size()); for (Object value : propMap.values()) { key.add(value); } } @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