io.ebeaninternal.server.deploy.id.IdBinderSimple Maven / Gradle / Ivy
package io.ebeaninternal.server.deploy.id;
import io.ebean.bean.EntityBean;
import io.ebean.core.type.ScalarType;
import io.ebeaninternal.api.SpiExpressionBind;
import io.ebeaninternal.api.SpiExpressionRequest;
import io.ebeaninternal.server.core.DefaultSqlUpdate;
import io.ebeaninternal.server.core.InternString;
import io.ebeaninternal.server.deploy.BeanProperty;
import io.ebeaninternal.server.deploy.DbReadContext;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.persist.MultiValueWrapper;
import io.ebeaninternal.server.persist.platform.MultiValueBind;
import io.ebeaninternal.server.bind.DataBind;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Bind an Id where the Id is made of a single property (not embedded).
*/
public final class IdBinderSimple implements IdBinder {
private final BeanProperty idProperty;
private final String bindIdSql;
private final Class> expectedType;
private final MultiValueBind multiValueBind;
@SuppressWarnings("rawtypes")
private final ScalarType scalarType;
public IdBinderSimple(BeanProperty idProperty, MultiValueBind multiValueBind) {
this.idProperty = idProperty;
this.scalarType = idProperty.scalarType();
this.expectedType = idProperty.type();
bindIdSql = InternString.intern(idProperty.dbColumn() + " = ?");
this.multiValueBind = multiValueBind;
}
@Override
public void initialise() {
// do nothing
}
@Override
public String idSelect() {
return idProperty.name();
}
@Override
public boolean isIdInExpandedForm() {
return false;
}
@Override
public String orderBy(String pathPrefix, boolean ascending) {
StringBuilder sb = new StringBuilder();
if (pathPrefix != null) {
sb.append(pathPrefix).append('.');
}
sb.append(idProperty.name());
if (!ascending) {
sb.append(" desc");
}
return sb.toString();
}
@Override
public void buildRawSqlSelectChain(String prefix, List selectChain) {
idProperty.buildRawSqlSelectChain(prefix, selectChain);
}
@Override
public BeanProperty beanProperty() {
return idProperty;
}
@Override
public BeanProperty findBeanProperty(String dbColumnName) {
if (dbColumnName.equalsIgnoreCase(idProperty.dbColumn())) {
return idProperty;
}
return null;
}
@Override
public boolean isComplexId() {
return false;
}
@Override
public String orderBy() {
return idProperty.name();
}
@Override
public String bindInSql(String baseTableAlias) {
if (baseTableAlias == null) {
return idProperty.dbColumn();
} else {
return baseTableAlias + "." + idProperty.dbColumn();
}
}
@Override
public String bindEqSql(String baseTableAlias) {
if (baseTableAlias == null) {
return bindIdSql;
} else {
return baseTableAlias + "." + bindIdSql;
}
}
@Override
public Object[] values(EntityBean bean) {
return new Object[]{idProperty.getValue(bean)};
}
@Override
public Object[] bindValues(Object idValue) {
return new Object[]{idValue};
}
@Override
public String idInValueExprDelete(int size) {
return idInValueExpr(false, size);
}
@Override
public String idInValueExpr(boolean not, int size) {
if (size <= 0) {
throw new IndexOutOfBoundsException("The size must be at least 1");
}
return multiValueBind.getInExpression(not, scalarType, size);
}
@Override
public void addBindValues(DefaultSqlUpdate sqlUpdate, Collection> ids) {
sqlUpdate.setParameter(new MultiValueWrapper(ids));
}
@Override
public void addBindValues(SpiExpressionBind request, Collection> values) {
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy