use of org.hibernate.query.spi.QueryParameterImplementor 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.query.spi.QueryParameterImplementor in project hibernate-orm by hibernate.
the class ConcreteSqmSelectQueryPlan method buildCacheableSqmInterpretation.
private static CacheableSqmInterpretation buildCacheableSqmInterpretation(SqmSelectStatement<?> sqm, DomainParameterXref domainParameterXref, DomainQueryExecutionContext executionContext) {
final SharedSessionContractImplementor session = executionContext.getSession();
final SessionFactoryImplementor sessionFactory = session.getFactory();
final QueryEngine queryEngine = sessionFactory.getQueryEngine();
final SqmTranslatorFactory sqmTranslatorFactory = queryEngine.getSqmTranslatorFactory();
final SqmTranslator<SelectStatement> sqmConverter = sqmTranslatorFactory.createSelectTranslator(sqm, executionContext.getQueryOptions(), domainParameterXref, executionContext.getQueryParameterBindings(), executionContext.getSession().getLoadQueryInfluencers(), sessionFactory, true);
// tableGroupAccess = sqmConverter.getFromClauseAccess();
final SqmTranslation<SelectStatement> sqmInterpretation = sqmConverter.translate();
final FromClauseAccess tableGroupAccess = sqmConverter.getFromClauseAccess();
final JdbcServices jdbcServices = sessionFactory.getJdbcServices();
final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment.getSqlAstTranslatorFactory();
final SqlAstTranslator<JdbcSelect> selectTranslator = sqlAstTranslatorFactory.buildSelectTranslator(sessionFactory, sqmInterpretation.getSqlAst());
final Map<QueryParameterImplementor<?>, Map<SqmParameter<?>, List<List<JdbcParameter>>>> jdbcParamsXref = SqmUtil.generateJdbcParamsXref(domainParameterXref, sqmInterpretation::getJdbcParamsBySqmParam);
final JdbcParameterBindings jdbcParameterBindings = SqmUtil.createJdbcParameterBindings(executionContext.getQueryParameterBindings(), domainParameterXref, jdbcParamsXref, session.getFactory().getRuntimeMetamodels().getMappingMetamodel(), tableGroupAccess::findTableGroup, new SqmParameterMappingModelResolutionAccess() {
@Override
@SuppressWarnings("unchecked")
public <T> MappingModelExpressible<T> getResolvedMappingModelType(SqmParameter<T> parameter) {
return (MappingModelExpressible<T>) sqmInterpretation.getSqmParameterMappingModelTypeResolutions().get(parameter);
}
}, session);
final JdbcSelect jdbcSelect = selectTranslator.translate(jdbcParameterBindings, executionContext.getQueryOptions());
return new CacheableSqmInterpretation(sqmInterpretation.getSqlAst(), jdbcSelect, tableGroupAccess, jdbcParamsXref, sqmInterpretation.getSqmParameterMappingModelTypeResolutions(), jdbcParameterBindings);
}
use of org.hibernate.query.spi.QueryParameterImplementor 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.query.spi.QueryParameterImplementor 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);
}
Aggregations