io.ebeaninternal.server.deploy.BeanPropertyAssocManySqlHelp 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.deploy;
import io.ebean.Transaction;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiSqlUpdate;
import io.ebeaninternal.dbmigration.model.visitor.BaseTablePropertyVisitor;
import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing;
import io.ebeaninternal.server.core.DefaultSqlUpdate;
import java.util.List;
class BeanPropertyAssocManySqlHelp {
private final BeanPropertyAssocMany many;
private final ExportedProperty[] exportedProperties;
private final boolean hasJoinTable;
private final BeanDescriptor> descriptor;
private final String exportedPropertyBindProto;
private final String deleteByParentIdSql;
private final String deleteByParentIdInSql;
private final String elementCollectionInsertSql;
BeanPropertyAssocManySqlHelp(BeanPropertyAssocMany many, ExportedProperty[] exportedProperties) {
this.many = many;
this.exportedProperties = exportedProperties;
this.hasJoinTable = many.hasJoinTable();
this.descriptor = many.getBeanDescriptor();
this.exportedPropertyBindProto = deriveExportedPropertyBindProto();
String delStmt;
if (hasJoinTable) {
delStmt = "delete from " + many.inverseJoin.getTable() + " where ";
} else {
delStmt = "delete from " + many.targetTable() + " where ";
}
deleteByParentIdSql = delStmt + deriveWhereParentIdSql(false, "");
deleteByParentIdInSql = delStmt + deriveWhereParentIdSql(true, "");
if (many.isElementCollection()) {
elementCollectionInsertSql = elementCollectionInsert();
} else {
elementCollectionInsertSql = null;
}
}
private String elementCollectionInsert() {
StringBuilder sb = new StringBuilder(200);
sb.append("insert into ").append(many.targetTable()).append(" (");
append(sb);
Cols cols = new Cols(sb);
VisitAllUsing.visitOne(many.targetDescriptor, cols);
sb.append(") values (");
appendBind(sb, exportedProperties.length, true);
appendBind(sb, cols.colCount, false);
sb.append(")");
return sb.toString();
}
SpiSqlUpdate insertElementCollection() {
return new DefaultSqlUpdate(elementCollectionInsertSql);
}
private static class Cols extends BaseTablePropertyVisitor {
int colCount;
private final StringBuilder sb;
private Cols(StringBuilder sb) {
this.sb = sb;
}
@Override
public void visitEmbeddedScalar(BeanProperty p, BeanPropertyAssocOne> embedded) {
sb.append(",").append(p.getDbColumn());
colCount++;
}
@Override
public void visitOneImported(BeanPropertyAssocOne> p) {
}
@Override
public void visitScalar(BeanProperty p) {
sb.append(",").append(p.getDbColumn());
colCount++;
}
@Override
public void visitEnd() {
}
}
String lazyFetchOrderBy(String fetchOrderBy) {
// derive lazyFetchOrderBy
StringBuilder sb = new StringBuilder(50);
for (int i = 0; i < exportedProperties.length; i++) {
if (i > 0) {
sb.append(", ");
}
// these fk columns are either on the intersection (int_) or base table (t0)
String fkTableAlias = hasJoinTable ? "int_" : "t0";
sb.append(fkTableAlias).append(".").append(exportedProperties[i].getForeignDbColumn());
}
sb.append(", ").append(fetchOrderBy);
return sb.toString().trim();
}
/**
* Add a where clause to the query for a given list of parent Id's.
*/
void addWhereParentIdIn(SpiQuery> query, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy