use of org.hibernate.metamodel.mapping.ValueMapping in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method consumeSqmParameter.
protected Expression consumeSqmParameter(SqmParameter<?> sqmParameter, MappingModelExpressible<?> valueMapping, BiConsumer<Integer, JdbcParameter> jdbcParameterConsumer) {
final List<JdbcParameter> jdbcParametersForSqm = new ArrayList<>();
resolveSqmParameter(sqmParameter, valueMapping, (index, jdbcParameter) -> {
jdbcParameterConsumer.accept(index, jdbcParameter);
jdbcParametersForSqm.add(jdbcParameter);
});
this.jdbcParameters.addParameters(jdbcParametersForSqm);
this.jdbcParamsBySqmParam.computeIfAbsent(sqmParameter, k -> new ArrayList<>(1)).add(jdbcParametersForSqm);
final QueryParameterImplementor<?> queryParameter = domainParameterXref.getQueryParameter(sqmParameter);
final QueryParameterBinding binding = domainParameterBindings.getBinding(queryParameter);
if (binding.setType(valueMapping)) {
replaceJdbcParametersType(sqmParameter, domainParameterXref.getSqmParameters(queryParameter), valueMapping);
}
return new SqmParameterInterpretation(sqmParameter, queryParameter, jdbcParametersForSqm, valueMapping, qp -> binding);
}
use of org.hibernate.metamodel.mapping.ValueMapping in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method resolveSqmParameter.
private void resolveSqmParameter(SqmParameter<?> expression, MappingModelExpressible<?> valueMapping, BiConsumer<Integer, JdbcParameter> jdbcParameterConsumer) {
sqmParameterMappingModelTypes.put(expression, valueMapping);
final Bindable bindable;
if (valueMapping instanceof EntityAssociationMapping) {
final EntityAssociationMapping mapping = (EntityAssociationMapping) valueMapping;
bindable = mapping.getForeignKeyDescriptor().getPart(mapping.getSideNature());
} else if (valueMapping instanceof EntityMappingType) {
bindable = ((EntityMappingType) valueMapping).getIdentifierMapping();
} else {
bindable = valueMapping;
}
if (bindable instanceof SelectableMappings) {
((SelectableMappings) bindable).forEachSelectable((index, selectableMapping) -> jdbcParameterConsumer.accept(index, new SqlTypedMappingJdbcParameter(selectableMapping)));
} else if (bindable instanceof SelectableMapping) {
jdbcParameterConsumer.accept(0, new SqlTypedMappingJdbcParameter((SelectableMapping) bindable));
} else {
SqlTypedMapping sqlTypedMapping = null;
if (bindable instanceof BasicType<?>) {
final int sqlTypeCode = ((BasicType<?>) bindable).getJdbcType().getDefaultSqlTypeCode();
if (sqlTypeCode == SqlTypes.NUMERIC || sqlTypeCode == SqlTypes.DECIMAL) {
// For numeric and decimal parameter types we must determine the precision/scale of the value.
// When we need to cast the parameter later, it is necessary to know the size to avoid truncation.
final QueryParameterBinding<?> binding = domainParameterBindings.getBinding(domainParameterXref.getQueryParameter(expression));
final Object bindValue = binding.getBindValue();
if (bindValue != null) {
if (bindValue instanceof BigInteger) {
int precision = bindValue.toString().length() - (((BigInteger) bindValue).signum() < 0 ? 1 : 0);
sqlTypedMapping = new SqlTypedMappingImpl(null, null, precision, 0, ((BasicType<?>) bindable).getJdbcMapping());
} else if (bindValue instanceof BigDecimal) {
final BigDecimal bigDecimal = (BigDecimal) bindValue;
sqlTypedMapping = new SqlTypedMappingImpl(null, null, bigDecimal.precision(), bigDecimal.scale(), ((BasicType<?>) bindable).getJdbcMapping());
}
}
}
}
if (sqlTypedMapping == null) {
bindable.forEachJdbcType((index, jdbcMapping) -> jdbcParameterConsumer.accept(index, new JdbcParameterImpl(jdbcMapping)));
} else {
jdbcParameterConsumer.accept(0, new SqlTypedMappingJdbcParameter(sqlTypedMapping));
}
}
}
use of org.hibernate.metamodel.mapping.ValueMapping in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method consumeSingleSqmParameter.
protected Expression consumeSingleSqmParameter(SqmParameter<?> sqmParameter) {
final MappingModelExpressible<?> valueMapping = determineValueMapping(sqmParameter);
final List<JdbcParameter> jdbcParametersForSqm = new ArrayList<>();
resolveSqmParameter(sqmParameter, valueMapping, jdbcParametersForSqm::add);
this.jdbcParameters.addParameters(jdbcParametersForSqm);
this.jdbcParamsBySqmParam.computeIfAbsent(sqmParameter, k -> new ArrayList<>(1)).add(jdbcParametersForSqm);
final QueryParameterImplementor<?> queryParameter = domainParameterXref.getQueryParameter(sqmParameter);
final QueryParameterBinding binding = domainParameterBindings.getBinding(queryParameter);
if (binding.setType(valueMapping)) {
replaceJdbcParametersType(sqmParameter, domainParameterXref.getSqmParameters(queryParameter), valueMapping);
}
return new SqmParameterInterpretation(sqmParameter, queryParameter, jdbcParametersForSqm, valueMapping, qp -> binding);
}
use of org.hibernate.metamodel.mapping.ValueMapping in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method createCteTable.
public static CteTable createCteTable(SqmCteTable sqmCteTable, List<SqmCteTableColumn> sqmCteColumns, SessionFactoryImplementor factory) {
final List<CteColumn> sqlCteColumns = new ArrayList<>(sqmCteColumns.size());
for (int i = 0; i < sqmCteColumns.size(); i++) {
final SqmCteTableColumn sqmCteTableColumn = sqmCteColumns.get(i);
ValueMapping valueMapping = sqmCteTableColumn.getType();
if (valueMapping instanceof Association) {
valueMapping = ((Association) valueMapping).getForeignKeyDescriptor();
}
if (valueMapping instanceof EmbeddableValuedModelPart) {
valueMapping.forEachJdbcType((index, jdbcMapping) -> sqlCteColumns.add(new CteColumn(sqmCteTableColumn.getColumnName() + "_" + index, jdbcMapping)));
} else {
sqlCteColumns.add(new CteColumn(sqmCteTableColumn.getColumnName(), ((BasicValuedMapping) valueMapping).getJdbcMapping()));
}
}
return new CteTable(sqmCteTable.getCteName(), sqlCteColumns, factory);
}
use of org.hibernate.metamodel.mapping.ValueMapping in project hibernate-orm by hibernate.
the class BaseSqmToSqlAstConverter method visitTuple.
@Override
public Object visitTuple(SqmTuple<?> sqmTuple) {
final List<SqmExpression<?>> groupedExpressions = sqmTuple.getGroupedExpressions();
final int size = groupedExpressions.size();
final List<Expression> expressions = new ArrayList<>(size);
final MappingModelExpressible<?> mappingModelExpressible = resolveInferredType();
final EmbeddableMappingType embeddableMappingType;
if (mappingModelExpressible instanceof ValueMapping) {
embeddableMappingType = (EmbeddableMappingType) ((ValueMapping) mappingModelExpressible).getMappedType();
} else {
embeddableMappingType = null;
}
if (embeddableMappingType == null) {
try {
inferrableTypeAccessStack.push(() -> null);
for (int i = 0; i < size; i++) {
expressions.add((Expression) groupedExpressions.get(i).accept(this));
}
} finally {
inferrableTypeAccessStack.pop();
}
} else {
for (int i = 0; i < size; i++) {
final AttributeMapping attributeMapping = embeddableMappingType.getAttributeMappings().get(i);
inferrableTypeAccessStack.push(() -> attributeMapping);
try {
expressions.add((Expression) groupedExpressions.get(i).accept(this));
} finally {
inferrableTypeAccessStack.pop();
}
}
}
final MappingModelExpressible<?> valueMapping;
if (mappingModelExpressible != null) {
valueMapping = mappingModelExpressible;
} else {
final SqmExpressible<?> expressible = sqmTuple.getExpressible();
if (expressible instanceof MappingModelExpressible<?>) {
valueMapping = (MappingModelExpressible<?>) expressible;
} else {
valueMapping = null;
}
}
return new SqlTuple(expressions, valueMapping);
}
Aggregations