use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class LoaderSelectBuilder method applySubSelectRestriction.
private void applySubSelectRestriction(QuerySpec querySpec, NavigablePath rootNavigablePath, TableGroup rootTableGroup, SubselectFetch subselect, LoaderSqlAstCreationState sqlAstCreationState) {
final SqlAstCreationContext sqlAstCreationContext = sqlAstCreationState.getCreationContext();
final SessionFactoryImplementor sessionFactory = sqlAstCreationContext.getSessionFactory();
assert loadable instanceof PluralAttributeMapping;
final PluralAttributeMapping attributeMapping = (PluralAttributeMapping) loadable;
final ForeignKeyDescriptor fkDescriptor = attributeMapping.getKeyDescriptor();
final NavigablePath navigablePath = rootNavigablePath.append(attributeMapping.getAttributeName());
final Expression fkExpression;
final int jdbcTypeCount = fkDescriptor.getJdbcTypeCount();
if (jdbcTypeCount == 1) {
assert fkDescriptor instanceof SimpleForeignKeyDescriptor;
final SimpleForeignKeyDescriptor simpleFkDescriptor = (SimpleForeignKeyDescriptor) fkDescriptor;
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, simpleFkDescriptor.getContainingTableExpression());
fkExpression = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, simpleFkDescriptor.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, simpleFkDescriptor.getSelectionExpression(), false, null, null, simpleFkDescriptor.getJdbcMapping(), this.creationContext.getSessionFactory()));
} else {
final List<ColumnReference> columnReferences = new ArrayList<>(jdbcTypeCount);
fkDescriptor.forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = rootTableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
columnReferences.add((ColumnReference) sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, this.creationContext.getSessionFactory())));
});
fkExpression = new SqlTuple(columnReferences, fkDescriptor);
}
querySpec.applyPredicate(new InSubQueryPredicate(fkExpression, generateSubSelect(attributeMapping, rootTableGroup, subselect, jdbcTypeCount, sqlAstCreationState, sessionFactory), false));
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class NonAggregatedIdentifierMappingImpl method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
if (hasContainingClass()) {
final SelectableMappings selectableMappings = getEmbeddableTypeDescriptor();
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(selectableMappings.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
int offset = 0;
for (AttributeMapping attributeMapping : identifierValueMapper.getAttributeMappings()) {
offset += attributeMapping.forEachSelectable(offset, (columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add((ColumnReference) columnReference);
});
}
return new SqlTuple(columnReferences, this);
}
return super.toSqlExpression(tableGroup, clause, walker, sqlAstCreationState);
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class EmbeddedAttributeMapping method toSqlExpression.
@Override
public SqlTuple toSqlExpression(TableGroup tableGroup, Clause clause, SqmToSqlAstConverter walker, SqlAstCreationState sqlAstCreationState) {
final List<ColumnReference> columnReferences = CollectionHelper.arrayList(embeddableMappingType.getJdbcTypeCount());
final NavigablePath navigablePath = tableGroup.getNavigablePath().append(getNavigableRole().getNavigableName());
final TableReference defaultTableReference = tableGroup.resolveTableReference(navigablePath, getContainingTableExpression());
getEmbeddableTypeDescriptor().forEachSelectable((columnIndex, selection) -> {
final TableReference tableReference = defaultTableReference.resolveTableReference(selection.getContainingTableExpression()) != null ? defaultTableReference : tableGroup.resolveTableReference(navigablePath, selection.getContainingTableExpression());
final Expression columnReference = sqlAstCreationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference.getIdentificationVariable(), selection, sqlAstCreationState.getCreationContext().getSessionFactory()));
columnReferences.add(columnReference.getColumnReference());
});
return new SqlTuple(columnReferences, this);
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class AbstractDomainPath method resolve.
public Expression resolve(ModelPart referenceModelPart, QuerySpec ast, TableGroup tableGroup, String modelPartName, SqlAstCreationState creationState) {
if (referenceModelPart instanceof BasicValuedModelPart) {
final BasicValuedModelPart selection = (BasicValuedModelPart) referenceModelPart;
final TableReference tableReference = tableGroup.resolveTableReference(getNavigablePath(), selection.getContainingTableExpression());
return creationState.getSqlExpressionResolver().resolveSqlExpression(SqlExpressionResolver.createColumnReferenceKey(tableReference, selection.getSelectionExpression()), sqlAstProcessingState -> new ColumnReference(tableReference, selection, creationState.getCreationContext().getSessionFactory()));
} else if (referenceModelPart instanceof EntityValuedModelPart) {
final ModelPart subPart;
if (ELEMENT_TOKEN.equals(modelPartName)) {
subPart = ((EntityValuedModelPart) referenceModelPart).getEntityMappingType().getIdentifierMapping();
} else {
subPart = ((EntityValuedModelPart) referenceModelPart).findSubPart(modelPartName);
}
return resolve(subPart, ast, tableGroup, modelPartName, creationState);
} else if (referenceModelPart instanceof EmbeddableValuedModelPart) {
final EmbeddableValuedModelPart embeddableValuedModelPart = (EmbeddableValuedModelPart) referenceModelPart;
if (embeddableValuedModelPart.getFetchableName().equals(modelPartName) || ELEMENT_TOKEN.equals(modelPartName)) {
final List<Expression> expressions = new ArrayList<>(embeddableValuedModelPart.getNumberOfFetchables());
embeddableValuedModelPart.visitFetchables(fetchable -> {
expressions.add(resolve(fetchable, ast, tableGroup, modelPartName, creationState));
}, null);
return new SqlTuple(expressions, embeddableValuedModelPart);
} else {
ModelPart subPart = embeddableValuedModelPart.findSubPart(modelPartName, null);
assert subPart instanceof BasicValuedModelPart;
return resolve(subPart, ast, tableGroup, modelPartName, creationState);
}
} else {
// sure it can happen
throw new NotYetImplementedFor6Exception("Ordering for " + referenceModelPart + " not supported");
}
}
use of org.hibernate.sql.ast.tree.expression.SqlTuple in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitLiteral.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// General expressions
@Override
public Expression visitLiteral(SqmLiteral<?> literal) {
if (literal instanceof SqmLiteralNull) {
MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
if (mappingModelExpressible == null) {
mappingModelExpressible = determineCurrentExpressible(literal);
}
if (mappingModelExpressible instanceof BasicValuedMapping) {
return new QueryLiteral<>(null, (BasicValuedMapping) mappingModelExpressible);
}
final MappingModelExpressible<?> keyExpressible = getKeyExpressible(mappingModelExpressible);
if (keyExpressible == null) {
// Default to the Object type
return new QueryLiteral<>(null, basicType(Object.class));
}
final List<Expression> expressions = new ArrayList<>(keyExpressible.getJdbcTypeCount());
keyExpressible.forEachJdbcType((index, jdbcMapping) -> expressions.add(new QueryLiteral<>(null, (BasicValuedMapping) jdbcMapping)));
return new SqlTuple(expressions, mappingModelExpressible);
}
final MappingModelExpressible<?> inferableExpressible = resolveInferredType();
if (inferableExpressible instanceof ConvertibleModelPart) {
final ConvertibleModelPart convertibleModelPart = (ConvertibleModelPart) inferableExpressible;
if (convertibleModelPart.getValueConverter() != null) {
return new QueryLiteral<>(literal.getLiteralValue(), convertibleModelPart);
}
} else // Special case for when we create an entity literal through the JPA CriteriaBuilder.literal API
if (inferableExpressible instanceof EntityDiscriminatorMapping) {
final EntityDiscriminatorMapping discriminatorMapping = (EntityDiscriminatorMapping) inferableExpressible;
final Object literalValue = literal.getLiteralValue();
final EntityPersister mappingDescriptor;
if (literalValue instanceof Class<?>) {
mappingDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor((Class<?>) literalValue);
} else {
final JavaType<?> javaType = discriminatorMapping.getJdbcMapping().getJavaTypeDescriptor();
final Object discriminatorValue;
if (javaType.getJavaTypeClass().isInstance(literalValue)) {
discriminatorValue = literalValue;
} else if (literalValue instanceof CharSequence) {
discriminatorValue = javaType.fromString((CharSequence) literalValue);
} else if (creationContext.getSessionFactory().getJpaMetamodel().getJpaCompliance().isLoadByIdComplianceEnabled()) {
discriminatorValue = literalValue;
} else {
discriminatorValue = javaType.coerce(literalValue, null);
}
final String entityName = discriminatorMapping.getConcreteEntityNameForDiscriminatorValue(discriminatorValue);
mappingDescriptor = creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(entityName);
}
return new EntityTypeLiteral(mappingDescriptor);
}
final MappingModelExpressible<?> expressible;
final MappingModelExpressible<?> localExpressible = SqmMappingModelHelper.resolveMappingModelExpressible(literal, creationContext.getSessionFactory().getRuntimeMetamodels().getMappingMetamodel(), getFromClauseAccess()::findTableGroup);
if (localExpressible == null) {
expressible = getElementExpressible(inferableExpressible);
} else {
final MappingModelExpressible<?> elementExpressible = getElementExpressible(localExpressible);
if (elementExpressible instanceof BasicType) {
expressible = InferredBasicValueResolver.resolveSqlTypeIndicators(this, (BasicType) elementExpressible, literal.getJavaTypeDescriptor());
} else {
expressible = elementExpressible;
}
}
if (expressible instanceof EntityIdentifierMapping && literal.getNodeType() instanceof EntityTypeImpl) {
return new QueryLiteral<>(((EntityIdentifierMapping) expressible).getIdentifier(literal.getLiteralValue()), (BasicValuedMapping) expressible);
}
if (expressible instanceof BasicValuedMapping) {
return new QueryLiteral<>(literal.getLiteralValue(), (BasicValuedMapping) expressible);
}
// Handling other values might seem unnecessary, but with JPA Criteria it is totally possible to have such literals
if (expressible instanceof EmbeddableValuedModelPart) {
final EmbeddableValuedModelPart embeddableValuedModelPart = (EmbeddableValuedModelPart) expressible;
final List<Expression> list = new ArrayList<>(embeddableValuedModelPart.getJdbcTypeCount());
embeddableValuedModelPart.forEachJdbcValue(literal.getLiteralValue(), null, (selectionIndex, value, jdbcMapping) -> list.add(new QueryLiteral<>(value, (BasicValuedMapping) jdbcMapping)), null);
return new SqlTuple(list, expressible);
} else if (expressible instanceof EntityValuedModelPart) {
final EntityValuedModelPart entityValuedModelPart = (EntityValuedModelPart) expressible;
final Object associationKey;
final ModelPart associationKeyPart;
if (entityValuedModelPart instanceof Association) {
final Association association = (Association) entityValuedModelPart;
final ForeignKeyDescriptor foreignKeyDescriptor = association.getForeignKeyDescriptor();
associationKey = foreignKeyDescriptor.getAssociationKeyFromSide(literal.getLiteralValue(), association.getSideNature().inverse(), null);
associationKeyPart = foreignKeyDescriptor.getPart(association.getSideNature());
} else {
final EntityIdentifierMapping identifierMapping = entityValuedModelPart.getEntityMappingType().getIdentifierMapping();
associationKeyPart = identifierMapping;
associationKey = identifierMapping.getIdentifier(literal.getLiteralValue(), null);
}
if (associationKeyPart instanceof BasicValuedMapping) {
return new QueryLiteral<>(associationKey, (BasicValuedMapping) associationKeyPart);
} else {
final List<Expression> list = new ArrayList<>(associationKeyPart.getJdbcTypeCount());
associationKeyPart.forEachJdbcValue(associationKey, null, (selectionIndex, value, jdbcMapping) -> list.add(new QueryLiteral<>(value, (BasicValuedMapping) jdbcMapping)), null);
return new SqlTuple(list, associationKeyPart);
}
} else {
return new QueryLiteral<>(literal.getLiteralValue(), creationContext.getSessionFactory().getTypeConfiguration().getBasicTypeRegistry().getRegisteredType(((BasicSqmPathSource<?>) literal.getNodeType()).getSqmPathType().getJavaType().getName()));
}
}
Aggregations