use of org.hibernate.metamodel.mapping.Bindable in project hibernate-orm by hibernate.
the class SqmUtil method createJdbcParameterBindings.
// public static JdbcParameterBindings buildJdbcParameterBindings(
// SqmStatement sqmStatement,
// JdbcParameterBySqmParameterAccess sqmInterpretation,
// ExecutionContext executionContext) {
// final DomainParameterXref domainParameterXref = DomainParameterXref.from( sqmStatement );
// final Map<QueryParameterImplementor<?>, Map<SqmParameter, List<JdbcParameter>>> jdbcParamsXref =
// generateJdbcParamsXref( domainParameterXref, sqmInterpretation );
// return createJdbcParameterBindings(
// executionContext.getDomainParameterBindingContext().getQueryParameterBindings(),
// domainParameterXref,
// jdbcParamsXref,
// executionContext.getSession()
// );
// }
// public static JdbcParameterBindings buildJdbcParameterBindings(
// SqmStatement sqmStatement,
// Map<QueryParameterImplementor<?>, Map<SqmParameter, List<JdbcParameter>>> jdbcParamsXref,
// ExecutionContext executionContext) {
// final DomainParameterXref domainParameterXref = DomainParameterXref.from( sqmStatement );
// return createJdbcParameterBindings(
// executionContext.getDomainParameterBindingContext().getQueryParameterBindings(),
// domainParameterXref,
// jdbcParamsXref,
// executionContext.getSession()
// );
// }
// public static JdbcParameterBindings buildJdbcParameterBindings(
// DomainParameterXref domainParameterXref,
// Map<QueryParameterImplementor<?>, Map<SqmParameter, List<JdbcParameter>>> jdbcParamsXref,
// ExecutionContext executionContext) {
// return createJdbcParameterBindings(
// executionContext.getDomainParameterBindingContext().getQueryParameterBindings(),
// domainParameterXref,
// jdbcParamsXref,
// executionContext.getSession()
// );
// }
public static JdbcParameterBindings createJdbcParameterBindings(QueryParameterBindings domainParamBindings, DomainParameterXref domainParameterXref, Map<QueryParameterImplementor<?>, Map<SqmParameter<?>, List<List<JdbcParameter>>>> jdbcParamXref, MappingMetamodel domainModel, Function<NavigablePath, TableGroup> tableGroupLocator, SqmParameterMappingModelResolutionAccess mappingModelResolutionAccess, SharedSessionContractImplementor session) {
final JdbcParameterBindings jdbcParameterBindings = new JdbcParameterBindingsImpl(domainParameterXref.getSqmParameterCount());
for (Map.Entry<QueryParameterImplementor<?>, List<SqmParameter<?>>> entry : domainParameterXref.getSqmParamByQueryParam().entrySet()) {
final QueryParameterImplementor<?> queryParam = entry.getKey();
final List<SqmParameter<?>> sqmParameters = entry.getValue();
final QueryParameterBinding<?> domainParamBinding = domainParamBindings.getBinding(queryParam);
final Map<SqmParameter<?>, List<List<JdbcParameter>>> jdbcParamMap = jdbcParamXref.get(queryParam);
for (SqmParameter<?> sqmParameter : sqmParameters) {
final Bindable parameterType = determineParameterType(domainParamBinding, queryParam, sqmParameters, mappingModelResolutionAccess, session.getFactory());
final List<List<JdbcParameter>> jdbcParamsBinds = jdbcParamMap.get(sqmParameter);
if (jdbcParamsBinds == null) {
// is replaced with an alias reference expression, which can happen for JPA Criteria queries
continue;
}
if (!domainParamBinding.isBound()) {
for (int i = 0; i < jdbcParamsBinds.size(); i++) {
final List<JdbcParameter> jdbcParams = jdbcParamsBinds.get(i);
parameterType.forEachJdbcType((position, jdbcType) -> {
jdbcParameterBindings.addBinding(jdbcParams.get(position), new JdbcParameterBindingImpl(jdbcType, null));
});
}
} else if (domainParamBinding.isMultiValued()) {
final Collection<?> bindValues = domainParamBinding.getBindValues();
final Iterator<?> valueItr = bindValues.iterator();
// the original SqmParameter is the one we are processing.. create a binding for it..
for (int i = 0; i < jdbcParamsBinds.size(); i++) {
final List<JdbcParameter> jdbcParams = jdbcParamsBinds.get(i);
createValueBindings(jdbcParameterBindings, queryParam, domainParamBinding, parameterType, jdbcParams, valueItr.next(), tableGroupLocator, session);
}
// an then one for each of the expansions
final List<SqmParameter<?>> expansions = domainParameterXref.getExpansions(sqmParameter);
assert expansions.size() == bindValues.size() - 1;
int expansionPosition = 0;
while (valueItr.hasNext()) {
final SqmParameter<?> expansionSqmParam = expansions.get(expansionPosition++);
final List<List<JdbcParameter>> jdbcParamBinds = jdbcParamMap.get(expansionSqmParam);
for (int i = 0; i < jdbcParamBinds.size(); i++) {
List<JdbcParameter> expansionJdbcParams = jdbcParamBinds.get(i);
createValueBindings(jdbcParameterBindings, queryParam, domainParamBinding, parameterType, expansionJdbcParams, valueItr.next(), tableGroupLocator, session);
}
}
} else if (domainParamBinding.getBindValue() == null) {
for (int i = 0; i < jdbcParamsBinds.size(); i++) {
final List<JdbcParameter> jdbcParams = jdbcParamsBinds.get(i);
for (int j = 0; j < jdbcParams.size(); j++) {
final JdbcParameter jdbcParameter = jdbcParams.get(j);
jdbcParameterBindings.addBinding(jdbcParameter, new JdbcParameterBindingImpl(null, null));
}
}
} else {
if (domainParamBinding.getType() instanceof AttributeConverterTypeAdapter || domainParamBinding.getType() instanceof ConvertibleModelPart) {
final BasicValueConverter valueConverter;
final JdbcMapping jdbcMapping;
if (domainParamBinding.getType() instanceof AttributeConverterTypeAdapter) {
final AttributeConverterTypeAdapter<?> adapter = (AttributeConverterTypeAdapter<?>) domainParamBinding.getType();
valueConverter = adapter.getAttributeConverter();
jdbcMapping = adapter.getJdbcMapping();
} else {
final ConvertibleModelPart convertibleModelPart = (ConvertibleModelPart) domainParamBinding.getType();
valueConverter = convertibleModelPart.getValueConverter();
jdbcMapping = convertibleModelPart.getJdbcMapping();
}
if (valueConverter != null) {
final Object convertedValue = valueConverter.toRelationalValue(domainParamBinding.getBindValue());
for (int i = 0; i < jdbcParamsBinds.size(); i++) {
final List<JdbcParameter> jdbcParams = jdbcParamsBinds.get(i);
assert jdbcParams.size() == 1;
final JdbcParameter jdbcParameter = jdbcParams.get(0);
jdbcParameterBindings.addBinding(jdbcParameter, new JdbcParameterBindingImpl(jdbcMapping, convertedValue));
}
continue;
}
}
final Object bindValue = domainParamBinding.getBindValue();
for (int i = 0; i < jdbcParamsBinds.size(); i++) {
final List<JdbcParameter> jdbcParams = jdbcParamsBinds.get(i);
createValueBindings(jdbcParameterBindings, queryParam, domainParamBinding, parameterType, jdbcParams, bindValue, tableGroupLocator, session);
}
}
}
}
return jdbcParameterBindings;
}
use of org.hibernate.metamodel.mapping.Bindable 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