use of org.hibernate.sql.ast.tree.from.TableGroup in project hibernate-orm by hibernate.
the class AbstractCollectionPersister method selectFragment.
/**
* Generate a list of collection index, key and element columns
*/
@Override
public String selectFragment(String alias, String columnSuffix) {
final PluralAttributeMapping attributeMapping = getAttributeMapping();
final QuerySpec rootQuerySpec = new QuerySpec(true);
final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), LockOptions.NONE, (fetchParent, querySpec, creationState) -> new ArrayList<>(), true, getFactory());
final NavigablePath entityPath = new NavigablePath(attributeMapping.getRootPathName());
final TableGroup rootTableGroup = attributeMapping.createRootTableGroup(true, entityPath, null, () -> p -> {
}, new SqlAliasBaseConstant(alias), sqlAstCreationState.getSqlExpressionResolver(), sqlAstCreationState.getFromClauseAccess(), getFactory());
rootQuerySpec.getFromClause().addRoot(rootTableGroup);
sqlAstCreationState.getFromClauseAccess().registerTableGroup(entityPath, rootTableGroup);
attributeMapping.createDomainResult(entityPath, rootTableGroup, null, sqlAstCreationState);
// Wrap expressions with aliases
final SelectClause selectClause = rootQuerySpec.getSelectClause();
final java.util.List<SqlSelection> sqlSelections = selectClause.getSqlSelections();
int i = 0;
for (String keyAlias : keyColumnAliases) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), keyAlias + columnSuffix)));
i++;
}
if (hasIndex) {
for (String indexAlias : indexColumnAliases) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), indexAlias + columnSuffix)));
i++;
}
}
if (hasIdentifier) {
sqlSelections.set(i, new SqlSelectionImpl(i, i + 1, new AliasedExpression(sqlSelections.get(i).getExpression(), identifierColumnAlias + columnSuffix)));
i++;
}
for (int columnIndex = 0; i < sqlSelections.size(); i++, columnIndex++) {
final SqlSelection sqlSelection = sqlSelections.get(i);
sqlSelections.set(i, new SqlSelectionImpl(sqlSelection.getValuesArrayPosition(), sqlSelection.getJdbcResultSetIndex(), new AliasedExpression(sqlSelection.getExpression(), elementColumnAliases[columnIndex] + columnSuffix)));
}
final String sql = getFactory().getJdbcServices().getDialect().getSqlAstTranslatorFactory().buildSelectTranslator(getFactory(), new SelectStatement(rootQuerySpec)).translate(null, QueryOptions.NONE).getSql();
final int fromIndex = sql.lastIndexOf(" from");
final String expression;
if (fromIndex != -1) {
expression = sql.substring("select ".length(), fromIndex);
} else {
expression = sql.substring("select ".length());
}
return expression;
}
use of org.hibernate.sql.ast.tree.from.TableGroup in project hibernate-orm by hibernate.
the class MappingMetamodelImpl method resolveMappingExpressible.
@Override
public MappingModelExpressible<?> resolveMappingExpressible(SqmExpressible<?> sqmExpressible, Function<NavigablePath, TableGroup> tableGroupLocator) {
if (sqmExpressible instanceof SqmPath) {
final SqmPath<?> sqmPath = (SqmPath<?>) sqmExpressible;
final NavigablePath navigablePath = sqmPath.getNavigablePath();
if (navigablePath.getParent() != null) {
final TableGroup parentTableGroup = tableGroupLocator.apply(navigablePath.getParent());
return parentTableGroup.getModelPart().findSubPart(navigablePath.getLocalName(), null);
}
return tableGroupLocator.apply(navigablePath.getParent()).getModelPart();
}
if (sqmExpressible instanceof BasicType<?>) {
return (BasicType<?>) sqmExpressible;
}
if (sqmExpressible instanceof BasicSqmPathSource<?>) {
return getTypeConfiguration().getBasicTypeForJavaType(((BasicSqmPathSource<?>) sqmExpressible).getJavaType());
}
if (sqmExpressible instanceof SqmFieldLiteral) {
return getTypeConfiguration().getBasicTypeForJavaType(((SqmFieldLiteral<?>) sqmExpressible).getJavaType());
}
if (sqmExpressible instanceof CompositeSqmPathSource) {
throw new NotYetImplementedFor6Exception("Resolution of embedded-valued SqmExpressible nodes not yet implemented");
}
if (sqmExpressible instanceof EmbeddableTypeImpl) {
return (MappingModelExpressible<?>) sqmExpressible;
}
if (sqmExpressible instanceof EntityDomainType<?>) {
return getEntityDescriptor(((EntityDomainType<?>) sqmExpressible).getHibernateEntityName());
}
if (sqmExpressible instanceof TupleType<?>) {
final MappingModelExpressible<?> mappingModelExpressible = tupleTypeCache.get(sqmExpressible);
if (mappingModelExpressible != null) {
return mappingModelExpressible;
}
final TupleType<?> tupleType = (TupleType<?>) sqmExpressible;
final MappingModelExpressible<?>[] components = new MappingModelExpressible<?>[tupleType.componentCount()];
for (int i = 0; i < components.length; i++) {
components[i] = resolveMappingExpressible(tupleType.get(i), tableGroupLocator);
}
final MappingModelExpressible<?> createdMappingModelExpressible = new TupleMappingModelExpressible(components);
final MappingModelExpressible<?> existingMappingModelExpressible = tupleTypeCache.putIfAbsent(tupleType, createdMappingModelExpressible);
return existingMappingModelExpressible == null ? createdMappingModelExpressible : existingMappingModelExpressible;
}
return null;
}
use of org.hibernate.sql.ast.tree.from.TableGroup in project hibernate-orm by hibernate.
the class DiscriminatorSqmPath method interpret.
@Override
public SqmPathInterpretation<?> interpret(SqlAstCreationState sqlAstCreationState, SemanticQueryWalker sqmWalker, boolean jpaQueryComplianceEnabled) {
assert entityDescriptor.hasSubclasses();
final TableGroup tableGroup = sqlAstCreationState.getFromClauseAccess().getTableGroup(getLhs().getNavigablePath());
final ModelPartContainer modelPart = tableGroup.getModelPart();
final EntityMappingType entityMapping;
if (modelPart instanceof EntityValuedModelPart) {
entityMapping = ((EntityValuedModelPart) modelPart).getEntityMappingType();
} else {
entityMapping = (EntityMappingType) ((PluralAttributeMapping) modelPart).getElementDescriptor().getPartMappingType();
}
return new DiscriminatorPathInterpretation(getNavigablePath(), entityMapping, tableGroup, sqlAstCreationState);
}
use of org.hibernate.sql.ast.tree.from.TableGroup in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method generateSelect.
private SelectStatement generateSelect() {
if (loadable instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
if (pluralAttributeMapping.getMappedType().getCollectionSemantics() instanceof BagSemantics) {
currentBagRole = pluralAttributeMapping.getNavigableRole().getNavigableName();
}
}
final NavigablePath rootNavigablePath = new NavigablePath(loadable.getRootPathName());
final QuerySpec rootQuerySpec = new QuerySpec(true);
final List<DomainResult<?>> domainResults;
final LoaderSqlAstCreationState sqlAstCreationState = new LoaderSqlAstCreationState(rootQuerySpec, new SqlAliasBaseManager(), new SimpleFromClauseAccessImpl(), lockOptions, this::visitFetches, forceIdentifierSelection, creationContext);
final TableGroup rootTableGroup = loadable.createRootTableGroup(true, rootNavigablePath, null, () -> rootQuerySpec::applyPredicate, sqlAstCreationState, creationContext);
rootQuerySpec.getFromClause().addRoot(rootTableGroup);
sqlAstCreationState.getFromClauseAccess().registerTableGroup(rootNavigablePath, rootTableGroup);
registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), rootTableGroup);
if (partsToSelect != null && !partsToSelect.isEmpty()) {
domainResults = new ArrayList<>(partsToSelect.size());
for (ModelPart part : partsToSelect) {
final NavigablePath navigablePath = rootNavigablePath.append(part.getPartName());
final TableGroup tableGroup;
if (part instanceof TableGroupJoinProducer) {
final TableGroupJoinProducer tableGroupJoinProducer = (TableGroupJoinProducer) part;
final TableGroupJoin tableGroupJoin = tableGroupJoinProducer.createTableGroupJoin(navigablePath, rootTableGroup, null, SqlAstJoinType.LEFT, true, false, sqlAstCreationState);
rootTableGroup.addTableGroupJoin(tableGroupJoin);
tableGroup = tableGroupJoin.getJoinedGroup();
sqlAstCreationState.getFromClauseAccess().registerTableGroup(navigablePath, tableGroup);
registerPluralTableGroupParts(sqlAstCreationState.getFromClauseAccess(), tableGroup);
} else {
tableGroup = rootTableGroup;
}
domainResults.add(part.createDomainResult(navigablePath, tableGroup, null, sqlAstCreationState));
}
} else {
// use the one passed to the constructor or create one (maybe always create and pass?)
// allows re-use as they can be re-used to save on memory - they
// do not share state between
// noinspection rawtypes
final DomainResult domainResult;
if (this.cachedDomainResult != null) {
// used the one passed to the constructor
domainResult = this.cachedDomainResult;
} else {
// create one
domainResult = loadable.createDomainResult(rootNavigablePath, rootTableGroup, null, sqlAstCreationState);
}
// noinspection unchecked
domainResults = Collections.singletonList(domainResult);
}
for (ModelPart restrictedPart : restrictedParts) {
final int numberOfRestrictionColumns = restrictedPart.getJdbcTypeCount();
applyRestriction(rootQuerySpec, rootNavigablePath, rootTableGroup, restrictedPart, numberOfRestrictionColumns, jdbcParameterConsumer, sqlAstCreationState);
}
if (loadable instanceof PluralAttributeMapping) {
final PluralAttributeMapping pluralAttributeMapping = (PluralAttributeMapping) loadable;
applyFiltering(rootQuerySpec, rootTableGroup, pluralAttributeMapping, sqlAstCreationState);
applyOrdering(rootTableGroup, pluralAttributeMapping);
} else {
applyFiltering(rootQuerySpec, rootTableGroup, (Restrictable) loadable, sqlAstCreationState);
}
if (orderByFragments != null) {
orderByFragments.forEach(entry -> entry.getKey().apply(rootQuerySpec, entry.getValue(), sqlAstCreationState));
}
return new SelectStatement(rootQuerySpec, domainResults);
}
use of org.hibernate.sql.ast.tree.from.TableGroup in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method applyRestriction.
private void applyRestriction(QuerySpec rootQuerySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, ModelPart modelPart, int numberColumns, Consumer<JdbcParameter> jdbcParameterConsumer, LoaderSqlAstCreationState sqlAstCreationState) {
final SqlExpressionResolver sqlExpressionResolver = sqlAstCreationState.getSqlExpressionResolver();
final NavigablePath navigablePath = rootNavigablePath.append(modelPart.getNavigableRole().getNavigableName());
if (numberColumns == 1) {
modelPart.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final ColumnReference columnRef = (ColumnReference) sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), p -> new ColumnReference(tableReference, selection, creationContext.getSessionFactory()));
if (numberOfKeysToLoad == 1) {
final JdbcParameter jdbcParameter = new JdbcParameterImpl(selection.getJdbcMapping());
jdbcParameterConsumer.accept(jdbcParameter);
rootQuerySpec.applyPredicate(new ComparisonPredicate(columnRef, ComparisonOperator.EQUAL, jdbcParameter));
} else {
final InListPredicate predicate = new InListPredicate(columnRef);
for (int i = 0; i < numberOfKeysToLoad; i++) {
for (int j = 0; j < numberColumns; j++) {
final JdbcParameter jdbcParameter = new JdbcParameterImpl(columnRef.getJdbcMapping());
jdbcParameterConsumer.accept(jdbcParameter);
predicate.addExpression(jdbcParameter);
}
}
rootQuerySpec.applyPredicate(predicate);
}
});
} else {
final List<ColumnReference> columnReferences = new ArrayList<>(numberColumns);
modelPart.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
columnReferences.add((ColumnReference) sqlExpressionResolver.resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), p -> new ColumnReference(tableReference, selection, creationContext.getSessionFactory())));
});
final SqlTuple tuple = new SqlTuple(columnReferences, modelPart);
final InListPredicate predicate = new InListPredicate(tuple);
for (int i = 0; i < numberOfKeysToLoad; i++) {
final List<JdbcParameter> tupleParams = new ArrayList<>(numberColumns);
for (int j = 0; j < numberColumns; j++) {
final ColumnReference columnReference = columnReferences.get(j);
final JdbcParameter jdbcParameter = new JdbcParameterImpl(columnReference.getJdbcMapping());
jdbcParameterConsumer.accept(jdbcParameter);
tupleParams.add(jdbcParameter);
}
final SqlTuple paramTuple = new SqlTuple(tupleParams, modelPart);
predicate.addExpression(paramTuple);
}
rootQuerySpec.applyPredicate(predicate);
}
}
Aggregations