17 changed files with 1447 additions and 12 deletions
@ -0,0 +1,124 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2022 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.repository.support; |
||||||
|
|
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
import java.util.function.UnaryOperator; |
||||||
|
import java.util.stream.Collectors; |
||||||
|
import java.util.stream.Stream; |
||||||
|
import java.util.stream.StreamSupport; |
||||||
|
|
||||||
|
import org.springframework.data.domain.Example; |
||||||
|
import org.springframework.data.domain.Page; |
||||||
|
import org.springframework.data.domain.Pageable; |
||||||
|
import org.springframework.data.domain.Sort; |
||||||
|
import org.springframework.data.jdbc.core.JdbcAggregateOperations; |
||||||
|
import org.springframework.data.relational.core.query.Query; |
||||||
|
import org.springframework.data.relational.repository.query.RelationalExampleMapper; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@link org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery} using {@link Example}. |
||||||
|
* |
||||||
|
* @author Diego Krupitza |
||||||
|
*/ |
||||||
|
class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> { |
||||||
|
|
||||||
|
private final RelationalExampleMapper exampleMapper; |
||||||
|
private final JdbcAggregateOperations entityOperations; |
||||||
|
|
||||||
|
FetchableFluentQueryByExample(Example<S> example, Class<R> resultType, RelationalExampleMapper exampleMapper, |
||||||
|
JdbcAggregateOperations entityOperations) { |
||||||
|
this(example, Sort.unsorted(), resultType, Collections.emptyList(), exampleMapper, entityOperations); |
||||||
|
} |
||||||
|
|
||||||
|
FetchableFluentQueryByExample(Example<S> example, Sort sort, Class<R> resultType, List<String> fieldsToInclude, |
||||||
|
RelationalExampleMapper exampleMapper, JdbcAggregateOperations entityOperations) { |
||||||
|
super(example, sort, resultType, fieldsToInclude); |
||||||
|
this.exampleMapper = exampleMapper; |
||||||
|
this.entityOperations = entityOperations; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public R oneValue() { |
||||||
|
return this.entityOperations.selectOne(createQuery(), getExampleType()) |
||||||
|
.map(item -> this.getConversionFunction().apply(item)).get(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public R firstValue() { |
||||||
|
return this.getConversionFunction() |
||||||
|
.apply(this.entityOperations.select(createQuery(), getExampleType(), getSort()).iterator().next()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public List<R> all() { |
||||||
|
return StreamSupport |
||||||
|
.stream(this.entityOperations.select(createQuery(), getExampleType(), getSort()).spliterator(), false) |
||||||
|
.map(item -> this.getConversionFunction().apply(item)).collect(Collectors.toList()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Page<R> page(Pageable pageable) { |
||||||
|
return this.entityOperations.select(createQuery(p -> p.with(pageable)), getExampleType(), pageable) |
||||||
|
.map(item -> this.getConversionFunction().apply(item)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Stream<R> stream() { |
||||||
|
return StreamSupport |
||||||
|
.stream(this.entityOperations.select(createQuery(), getExampleType(), getSort()).spliterator(), false) |
||||||
|
.map(item -> this.getConversionFunction().apply(item)); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public long count() { |
||||||
|
return this.entityOperations.count(createQuery(), getExampleType()); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean exists() { |
||||||
|
return this.entityOperations.exists(createQuery(), getExampleType()); |
||||||
|
} |
||||||
|
|
||||||
|
private Query createQuery() { |
||||||
|
return createQuery(UnaryOperator.identity()); |
||||||
|
} |
||||||
|
|
||||||
|
private Query createQuery(UnaryOperator<Query> queryCustomizer) { |
||||||
|
|
||||||
|
Query query = exampleMapper.getMappedExample(getExample()); |
||||||
|
|
||||||
|
if (getSort().isSorted()) { |
||||||
|
query = query.sort(getSort()); |
||||||
|
} |
||||||
|
|
||||||
|
if (!getFieldsToInclude().isEmpty()) { |
||||||
|
query = query.columns(getFieldsToInclude().toArray(new String[0])); |
||||||
|
} |
||||||
|
|
||||||
|
query = queryCustomizer.apply(query); |
||||||
|
|
||||||
|
return query; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected <R> FluentQuerySupport<S, R> create(Example<S> example, Sort sort, Class<R> resultType, |
||||||
|
List<String> fieldsToInclude) { |
||||||
|
return new FetchableFluentQueryByExample<>(example, sort, resultType, fieldsToInclude, this.exampleMapper, |
||||||
|
this.entityOperations); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,127 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2022 the original author or authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.springframework.data.jdbc.repository.support; |
||||||
|
|
||||||
|
import org.springframework.core.convert.support.DefaultConversionService; |
||||||
|
import org.springframework.data.domain.Example; |
||||||
|
import org.springframework.data.domain.Sort; |
||||||
|
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
||||||
|
import org.springframework.data.repository.query.FluentQuery; |
||||||
|
import org.springframework.util.Assert; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.List; |
||||||
|
import java.util.function.Function; |
||||||
|
|
||||||
|
/** |
||||||
|
* Support class for {@link FluentQuery.FetchableFluentQuery} implementations. |
||||||
|
* |
||||||
|
* @author Diego Krupitza |
||||||
|
*/ |
||||||
|
abstract class FluentQuerySupport<S, R> implements FluentQuery.FetchableFluentQuery<R> { |
||||||
|
|
||||||
|
private final Example<S> example; |
||||||
|
private final Sort sort; |
||||||
|
private final Class<R> resultType; |
||||||
|
private final List<String> fieldsToInclude; |
||||||
|
|
||||||
|
private final SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory(); |
||||||
|
|
||||||
|
FluentQuerySupport(Example<S> example, Sort sort, Class<R> resultType, List<String> fieldsToInclude) { |
||||||
|
this.example = example; |
||||||
|
this.sort = sort; |
||||||
|
this.resultType = resultType; |
||||||
|
this.fieldsToInclude = fieldsToInclude; |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.query.FluentQuery.ReactiveFluentQuery#sortBy(org.springframework.data.domain.Sort) |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public FetchableFluentQuery<R> sortBy(Sort sort) { |
||||||
|
|
||||||
|
Assert.notNull(sort, "Sort must not be null!"); |
||||||
|
|
||||||
|
return create(example, sort, resultType, fieldsToInclude); |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.query.FluentQuery.ReactiveFluentQuery#as(java.lang.Class) |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public <R> FetchableFluentQuery<R> as(Class<R> projection) { |
||||||
|
|
||||||
|
Assert.notNull(projection, "Projection target type must not be null!"); |
||||||
|
|
||||||
|
return create(example, sort, projection, fieldsToInclude); |
||||||
|
} |
||||||
|
|
||||||
|
/* |
||||||
|
* (non-Javadoc) |
||||||
|
* @see org.springframework.data.repository.query.FluentQuery.ReactiveFluentQuery#project(java.util.Collection) |
||||||
|
*/ |
||||||
|
@Override |
||||||
|
public FetchableFluentQuery<R> project(Collection<String> properties) { |
||||||
|
|
||||||
|
Assert.notNull(properties, "Projection properties must not be null!"); |
||||||
|
|
||||||
|
return create(example, sort, resultType, new ArrayList<>(properties)); |
||||||
|
} |
||||||
|
|
||||||
|
protected abstract <R> FluentQuerySupport<S, R> create(Example<S> example, Sort sort, Class<R> resultType, |
||||||
|
List<String> fieldsToInclude); |
||||||
|
|
||||||
|
Class<S> getExampleType() { |
||||||
|
return this.example.getProbeType(); |
||||||
|
} |
||||||
|
|
||||||
|
Example<S> getExample() { |
||||||
|
return this.example; |
||||||
|
} |
||||||
|
|
||||||
|
Sort getSort() { |
||||||
|
return sort; |
||||||
|
} |
||||||
|
|
||||||
|
Class<R> getResultType() { |
||||||
|
return resultType; |
||||||
|
} |
||||||
|
|
||||||
|
List<String> getFieldsToInclude() { |
||||||
|
return fieldsToInclude; |
||||||
|
} |
||||||
|
|
||||||
|
private Function<Object, R> getConversionFunction(Class<S> inputType, Class<R> targetType) { |
||||||
|
|
||||||
|
if (targetType.isAssignableFrom(inputType)) { |
||||||
|
return (Function<Object, R>) Function.identity(); |
||||||
|
} |
||||||
|
|
||||||
|
if (targetType.isInterface()) { |
||||||
|
return o -> projectionFactory.createProjection(targetType, o); |
||||||
|
} |
||||||
|
|
||||||
|
return o -> DefaultConversionService.getSharedInstance().convert(o, targetType); |
||||||
|
} |
||||||
|
|
||||||
|
protected Function<Object, R> getConversionFunction() { |
||||||
|
return getConversionFunction(this.example.getProbeType(), getResultType()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue