use of org.hibernate.metamodel.model.domain.internal.BasicSqmPathSource 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