package io.ebeaninternal.server.query;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.deploy.DbSqlContext;
import io.ebeaninternal.server.deploy.TableJoin;
import java.util.List;
import java.util.Set;
final class SqlTreeNodeRoot extends SqlTreeNodeBean {
private final TableJoin includeJoin;
private final boolean sqlDistinct;
private final String baseTable;
SqlTreeNodeRoot(STreeType desc, SqlTreeProperties props, List<SqlTreeNode> myList, boolean withId, TableJoin includeJoin,
STreePropertyAssocMany many, SpiQuery.TemporalMode temporalMode, boolean disableLazyLoad, boolean sqlDistinct, String baseTable) {
super(desc, props, myList, withId, many, temporalMode, disableLazyLoad);
this.includeJoin = includeJoin;
this.sqlDistinct = sqlDistinct;
this.baseTable = baseTable;
}
@Override
protected boolean isRoot() {
return true;
}
@Override
public boolean isSqlDistinct() {
return sqlDistinct;
}
@Override
public void appendDistinctOn(DbSqlContext ctx, boolean subQuery) {
if (readId) {
ctx.pushTableAlias(prefix);
appendSelectId(ctx, idBinder.getBeanProperty());
ctx.popTableAlias();
super.appendDistinctOn(ctx, subQuery);
}
}
@Override
public void addAsOfTableAlias(SpiQuery<?> query) {
if (desc.isHistorySupport()) {
query.setAsOfBaseTable();
query.incrementAsOfTableCount();
}
if (lazyLoadParent != null && lazyLoadParent.isManyToManyWithHistory()) {
query.incrementAsOfTableCount();
}
for (SqlTreeNode aChildren : children) {
aChildren.addAsOfTableAlias(query);
}
}
@Override
public SqlJoinType appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) {
ctx.append(baseTable);
ctx.append(" ").append(baseTableAlias);
ctx.appendFromForUpdate();
if (includeJoin != null) {
String a1 = baseTableAlias;
String a2 = "int_";
includeJoin.addJoin(joinType, a1, a2, ctx);
}
return joinType;
}
@Override
public void dependentTables(Set<String> tables) {
for (SqlTreeNode child : children) {
child.dependentTables(tables);
}
}
}