|
| 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.relational.core.dialect.Dialect; |
| 26 | +import org.springframework.data.relational.core.mapping.AggregatePath; |
| 27 | +import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 28 | +import org.springframework.data.relational.core.mapping.RelationalPersistentEntity; |
| 29 | +import org.springframework.data.relational.core.sqlgeneration.AliasFactory; |
| 30 | +import org.springframework.data.relational.core.sqlgeneration.CachingSqlGenerator; |
| 31 | +import org.springframework.data.relational.core.sqlgeneration.SingleQuerySqlGenerator; |
| 32 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 33 | +import org.springframework.util.Assert; |
| 34 | + |
| 35 | +/** |
| 36 | + * Reads complete Aggregates from the database, by generating appropriate SQL using a {@link SingleQuerySqlGenerator} |
| 37 | + * and a matching {@link AggregateResultSetExtractor} and invoking a |
| 38 | + * {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate} |
| 39 | + * |
| 40 | + * @param <T> the type of aggregate produced by this reader. |
| 41 | + * @since 3.2 |
| 42 | + * @author Jens Schauder |
| 43 | + */ |
| 44 | +class AggregateReader<T> { |
| 45 | + |
| 46 | + private final RelationalMappingContext mappingContext; |
| 47 | + private final RelationalPersistentEntity<T> aggregate; |
| 48 | + private final AliasFactory aliasFactory; |
| 49 | + private final org.springframework.data.relational.core.sqlgeneration.SqlGenerator sqlGenerator; |
| 50 | + private final JdbcConverter converter; |
| 51 | + private final NamedParameterJdbcOperations jdbcTemplate; |
| 52 | + |
| 53 | + AggregateReader(RelationalMappingContext mappingContext, Dialect dialect, JdbcConverter converter, |
| 54 | + NamedParameterJdbcOperations jdbcTemplate, RelationalPersistentEntity<T> aggregate) { |
| 55 | + |
| 56 | + this.mappingContext = mappingContext; |
| 57 | + |
| 58 | + this.aggregate = aggregate; |
| 59 | + this.converter = converter; |
| 60 | + this.jdbcTemplate = jdbcTemplate; |
| 61 | + |
| 62 | + this.sqlGenerator = new CachingSqlGenerator(new SingleQuerySqlGenerator(mappingContext, dialect, aggregate)); |
| 63 | + this.aliasFactory = sqlGenerator.getAliasFactory(); |
| 64 | + } |
| 65 | + |
| 66 | + public List<T> findAll() { |
| 67 | + |
| 68 | + String sql = sqlGenerator.findAll(); |
| 69 | + |
| 70 | + PathToColumnMapping pathToColumn = createPathToColumnMapping(aliasFactory); |
| 71 | + AggregateResultSetExtractor<T> extractor = new AggregateResultSetExtractor<>(mappingContext, aggregate, converter, |
| 72 | + pathToColumn); |
| 73 | + |
| 74 | + Iterable<T> result = jdbcTemplate.query(sql, extractor); |
| 75 | + |
| 76 | + Assert.state(result != null, "result is null"); |
| 77 | + |
| 78 | + return (List<T>) result; |
| 79 | + } |
| 80 | + |
| 81 | + public T findById(Object id) { |
| 82 | + |
| 83 | + PathToColumnMapping pathToColumn = createPathToColumnMapping(aliasFactory); |
| 84 | + AggregateResultSetExtractor<T> extractor = new AggregateResultSetExtractor<>(mappingContext, aggregate, converter, |
| 85 | + pathToColumn); |
| 86 | + |
| 87 | + String sql = sqlGenerator.findById(); |
| 88 | + |
| 89 | + id = converter.writeValue(id, aggregate.getRequiredIdProperty().getTypeInformation()); |
| 90 | + |
| 91 | + Iterator<T> result = jdbcTemplate.query(sql, Map.of("id", id), extractor).iterator(); |
| 92 | + |
| 93 | + T returnValue = result.hasNext() ? result.next() : null; |
| 94 | + |
| 95 | + if (result.hasNext()) { |
| 96 | + throw new IncorrectResultSizeDataAccessException(1); |
| 97 | + } |
| 98 | + |
| 99 | + return returnValue; |
| 100 | + } |
| 101 | + |
| 102 | + public Iterable<T> findAllById(Iterable<?> ids) { |
| 103 | + |
| 104 | + PathToColumnMapping pathToColumn = createPathToColumnMapping(aliasFactory); |
| 105 | + AggregateResultSetExtractor<T> extractor = new AggregateResultSetExtractor<>(mappingContext, aggregate, converter, |
| 106 | + pathToColumn); |
| 107 | + |
| 108 | + String sql = sqlGenerator.findAllById(); |
| 109 | + |
| 110 | + List<Object> convertedIds = new ArrayList<>(); |
| 111 | + for (Object id : ids) { |
| 112 | + convertedIds.add(converter.writeValue(id, aggregate.getRequiredIdProperty().getTypeInformation())); |
| 113 | + } |
| 114 | + |
| 115 | + return jdbcTemplate.query(sql, Map.of("ids", convertedIds), extractor); |
| 116 | + } |
| 117 | + |
| 118 | + private PathToColumnMapping createPathToColumnMapping(AliasFactory aliasFactory) { |
| 119 | + return new PathToColumnMapping() { |
| 120 | + @Override |
| 121 | + public String column(AggregatePath path) { |
| 122 | + |
| 123 | + String alias = aliasFactory.getColumnAlias(path); |
| 124 | + Assert.notNull(alias, () -> "alias for >" + path + "<must not be null"); |
| 125 | + return alias; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public String keyColumn(AggregatePath path) { |
| 130 | + return aliasFactory.getKeyAlias(path); |
| 131 | + } |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
0 commit comments