use of org.hibernate.metamodel.mapping.SqlTypedMapping in project hibernate-orm by hibernate.
the class AbstractSqlAstTranslator method renderCasted.
protected void renderCasted(Expression expression) {
final List<SqlAstNode> arguments = new ArrayList<>(2);
arguments.add(expression);
if (expression instanceof SqlTypedMappingJdbcParameter) {
final SqlTypedMappingJdbcParameter parameter = (SqlTypedMappingJdbcParameter) expression;
final SqlTypedMapping sqlTypedMapping = parameter.getSqlTypedMapping();
arguments.add(new CastTarget(parameter.getJdbcMapping(), sqlTypedMapping.getColumnDefinition(), sqlTypedMapping.getLength(), sqlTypedMapping.getPrecision(), sqlTypedMapping.getScale()));
} else {
arguments.add(new CastTarget(expression.getExpressionType().getJdbcMappings().get(0)));
}
castFunction().render(this, arguments, this);
}
use of org.hibernate.metamodel.mapping.SqlTypedMapping 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));
}
}
}
Aggregations