|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.data.jdbc.core.convert; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Iterator; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.springframework.dao.IncorrectResultSizeDataAccessException; |
| 25 | +import org.springframework.data.jdbc.core.convert.sqlgeneration.AliasFactory; |
| 26 | +import org.springframework.data.jdbc.core.convert.sqlgeneration.SingleQuerySqlGenerator; |
| 27 | +import org.springframework.data.mapping.PersistentPropertyPath; |
| 28 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 29 | +import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension; |
| 30 | +import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 31 | +import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; |
| 32 | +import org.springframework.data.relational.core.mapping.RelationalPersistentProperty; |
| 33 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 34 | +import org.springframework.util.Assert; |
| 35 | + |
| 36 | +public class AggregateReader<T> { |
| 37 | + |
| 38 | + private final RelationalMappingContext mappingContext; |
| 39 | + private final RelationalPersistentEntity<T> aggregate; |
| 40 | + private final AliasFactory aliasFactory = new AliasFactory(); |
| 41 | + private final SingleQuerySqlGenerator sqlGenerator; |
| 42 | + private final JdbcConverter converter; |
| 43 | + private final NamedParameterJdbcOperations jdbcTemplate; |
| 44 | + |
| 45 | + AggregateReader(RelationalMappingContext mappingContext, Dialect dialect, JdbcConverter converter, |
| 46 | + NamedParameterJdbcOperations jdbcTemplate, RelationalPersistentEntity<T> aggregate) { |
| 47 | + |
| 48 | + this.mappingContext = mappingContext; |
| 49 | + |
| 50 | + this.aggregate = aggregate; |
| 51 | + this.converter = converter; |
| 52 | + this.jdbcTemplate = jdbcTemplate; |
| 53 | + |
| 54 | + this.sqlGenerator = new SingleQuerySqlGenerator(mappingContext, dialect, aggregate); |
| 55 | + } |
| 56 | + |
| 57 | + public List<T> findAll() { |
| 58 | + |
| 59 | + String sql = sqlGenerator.findAll(); |
| 60 | + |
| 61 | + PathToColumnMapping pathToColumn = createPathToColumnMapping(aliasFactory); |
| 62 | + AggregateResultSetExtractor<T> extractor = new AggregateResultSetExtractor<>(mappingContext, aggregate, converter, |
| 63 | + pathToColumn); |
| 64 | + |
| 65 | + Iterable<T> result = jdbcTemplate.query(sql, extractor); |
| 66 | + |
| 67 | + return (List<T>) result; |
| 68 | + } |
| 69 | + |
| 70 | + public T findById(Object id) { |
| 71 | + |
| 72 | + PathToColumnMapping pathToColumn = createPathToColumnMapping(aliasFactory); |
| 73 | + AggregateResultSetExtractor<T> extractor = new AggregateResultSetExtractor<>(mappingContext, aggregate, converter, |
| 74 | + pathToColumn); |
| 75 | + |
| 76 | + String sql = sqlGenerator.findById(); |
| 77 | + |
| 78 | + id = converter.writeValue(id, aggregate.getIdProperty().getTypeInformation()); |
| 79 | + |
| 80 | + Iterator<T> result = jdbcTemplate.query(sql, Map.of("id", id), extractor).iterator(); |
| 81 | + |
| 82 | + T returnValue = result.hasNext() ? result.next() : null; |
| 83 | + |
| 84 | + if (result.hasNext()) { |
| 85 | + throw new IncorrectResultSizeDataAccessException(1); |
| 86 | + } |
| 87 | + |
| 88 | + return returnValue; |
| 89 | + } |
| 90 | + |
| 91 | + public Iterable<T> findAllById(Iterable<?> ids) { |
| 92 | + |
| 93 | + PathToColumnMapping pathToColumn = createPathToColumnMapping(aliasFactory); |
| 94 | + AggregateResultSetExtractor<T> extractor = new AggregateResultSetExtractor<>(mappingContext, aggregate, converter, |
| 95 | + pathToColumn); |
| 96 | + |
| 97 | + String sql = sqlGenerator.findAllById(); |
| 98 | + |
| 99 | + List<Object> convertedIds = new ArrayList<>(); |
| 100 | + for (Object id : ids) { |
| 101 | + convertedIds.add(converter.writeValue(id, aggregate.getIdProperty().getTypeInformation())); |
| 102 | + } |
| 103 | + |
| 104 | + return jdbcTemplate.query(sql, Map.of("ids", convertedIds), extractor); |
| 105 | + } |
| 106 | + |
| 107 | + private PathToColumnMapping createPathToColumnMapping(AliasFactory aliasFactory) { |
| 108 | + return new PathToColumnMapping() { |
| 109 | + @Override |
| 110 | + public String column(PersistentPropertyPath<RelationalPersistentProperty> propertyPath) { |
| 111 | + |
| 112 | + PersistentPropertyPathExtension aliasLookUpKey = new PersistentPropertyPathExtension(mappingContext, propertyPath); |
| 113 | + |
| 114 | + String alias = aliasFactory.getAlias(aliasLookUpKey); |
| 115 | + Assert.notNull(alias, () -> "alias for >" + aliasLookUpKey + "<must not be null"); |
| 116 | + return alias; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public String keyColumn(PersistentPropertyPath<RelationalPersistentProperty> propertyPath) { |
| 121 | + return aliasFactory.getKeyAlias(propertyPath); |
| 122 | + } |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments