|
| 1 | +/* |
| 2 | + * Copyright Doma 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 | +package org.seasar.doma.internal.jdbc.command; |
| 17 | + |
| 18 | +import static org.seasar.doma.internal.Constants.ROWNUMBER_COLUMN_NAME; |
| 19 | + |
| 20 | +import java.sql.ResultSetMetaData; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | +import java.util.Set; |
| 30 | +import java.util.function.BiFunction; |
| 31 | +import org.seasar.doma.jdbc.DuplicateColumnHandler; |
| 32 | +import org.seasar.doma.jdbc.Naming; |
| 33 | +import org.seasar.doma.jdbc.ResultMappingException; |
| 34 | +import org.seasar.doma.jdbc.Sql; |
| 35 | +import org.seasar.doma.jdbc.UnknownColumnHandler; |
| 36 | +import org.seasar.doma.jdbc.entity.EntityPropertyType; |
| 37 | +import org.seasar.doma.jdbc.entity.EntityType; |
| 38 | +import org.seasar.doma.jdbc.entity.NamingType; |
| 39 | +import org.seasar.doma.jdbc.entity.Property; |
| 40 | +import org.seasar.doma.jdbc.query.Query; |
| 41 | +import org.seasar.doma.wrapper.Wrapper; |
| 42 | + |
| 43 | +public class MappingSupport { |
| 44 | + |
| 45 | + private final EntityType<?> entityType; |
| 46 | + private final Query query; |
| 47 | + private final boolean resultMappingEnsured; |
| 48 | + private final UnknownColumnHandler unknownColumnHandler; |
| 49 | + private final DuplicateColumnHandler duplicateColumnHandler; |
| 50 | + |
| 51 | + public MappingSupport( |
| 52 | + EntityType<?> entityType, |
| 53 | + Query query, |
| 54 | + boolean resultMappingEnsured, |
| 55 | + UnknownColumnHandler unknownColumnHandler, |
| 56 | + DuplicateColumnHandler duplicateColumnHandler) { |
| 57 | + this.entityType = Objects.requireNonNull(entityType); |
| 58 | + this.query = Objects.requireNonNull(query); |
| 59 | + this.resultMappingEnsured = resultMappingEnsured; |
| 60 | + this.unknownColumnHandler = Objects.requireNonNull(unknownColumnHandler); |
| 61 | + this.duplicateColumnHandler = Objects.requireNonNull(duplicateColumnHandler); |
| 62 | + } |
| 63 | + |
| 64 | + public Map<Integer, MappingSupport.PropType> createIndexMap( |
| 65 | + ResultSetMetaData resultSetMeta, Map<String, PropType> columnNameMap) throws SQLException { |
| 66 | + HashMap<Integer, PropType> indexMap = new HashMap<>(); |
| 67 | + Set<PropType> unmappedPropertySet = |
| 68 | + resultMappingEnsured ? new HashSet<>(columnNameMap.values()) : new HashSet<>(); |
| 69 | + Set<String> seenColumnNames = new HashSet<>(); |
| 70 | + int count = resultSetMeta.getColumnCount(); |
| 71 | + for (int i = 1; i < count + 1; i++) { |
| 72 | + String columnName = resultSetMeta.getColumnLabel(i); |
| 73 | + String lowerCaseColumnName = columnName.toLowerCase(); |
| 74 | + if (!seenColumnNames.add(lowerCaseColumnName)) { |
| 75 | + duplicateColumnHandler.handle(query, lowerCaseColumnName); |
| 76 | + } |
| 77 | + PropType propertyType = columnNameMap.get(lowerCaseColumnName); |
| 78 | + if (propertyType == null) { |
| 79 | + if (ROWNUMBER_COLUMN_NAME.equals(lowerCaseColumnName)) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + unknownColumnHandler.handle(query, entityType, lowerCaseColumnName); |
| 83 | + } else { |
| 84 | + unmappedPropertySet.remove(propertyType); |
| 85 | + indexMap.put(i, propertyType); |
| 86 | + } |
| 87 | + } |
| 88 | + if (resultMappingEnsured && !unmappedPropertySet.isEmpty()) { |
| 89 | + throwResultMappingException(unmappedPropertySet); |
| 90 | + } |
| 91 | + return indexMap; |
| 92 | + } |
| 93 | + |
| 94 | + private void throwResultMappingException(Set<PropType> unmappedPropertySet) { |
| 95 | + Naming naming = query.getConfig().getNaming(); |
| 96 | + int size = unmappedPropertySet.size(); |
| 97 | + List<String> unmappedPropertyNames = new ArrayList<>(size); |
| 98 | + List<String> expectedColumnNames = new ArrayList<>(size); |
| 99 | + for (PropType propType : unmappedPropertySet) { |
| 100 | + unmappedPropertyNames.add(propType.name()); |
| 101 | + expectedColumnNames.add(propType.columnName(naming::apply)); |
| 102 | + } |
| 103 | + Class<?> entityClass; |
| 104 | + Iterator<PropType> iterator = unmappedPropertySet.iterator(); |
| 105 | + if (iterator.hasNext()) { |
| 106 | + entityClass = iterator.next().entityType().getEntityClass(); |
| 107 | + } else { |
| 108 | + entityClass = this.entityType.getEntityClass(); |
| 109 | + } |
| 110 | + Sql<?> sql = query.getSql(); |
| 111 | + throw new ResultMappingException( |
| 112 | + query.getConfig().getExceptionSqlLogType(), |
| 113 | + entityClass.getName(), |
| 114 | + unmappedPropertyNames, |
| 115 | + expectedColumnNames, |
| 116 | + sql.getKind(), |
| 117 | + sql.getRawSql(), |
| 118 | + sql.getFormattedSql(), |
| 119 | + sql.getSqlFilePath()); |
| 120 | + } |
| 121 | + |
| 122 | + public record PropType( |
| 123 | + EntityType<?> entityType, EntityPropertyType<?, ?> propertyType, String propertyPath) { |
| 124 | + public String name() { |
| 125 | + return propertyType.getName(); |
| 126 | + } |
| 127 | + |
| 128 | + public String columnName(BiFunction<NamingType, String, String> namingFunction) { |
| 129 | + return propertyType.getColumnName(namingFunction); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public record Prop(PropType propType, Property<Object, ?> property, Object rawValue) { |
| 134 | + |
| 135 | + public EntityType<?> entityType() { |
| 136 | + return propType.entityType(); |
| 137 | + } |
| 138 | + |
| 139 | + public String propertyPath() { |
| 140 | + return propType.propertyPath(); |
| 141 | + } |
| 142 | + |
| 143 | + public String name() { |
| 144 | + return propType.propertyType().getName(); |
| 145 | + } |
| 146 | + |
| 147 | + public boolean isId() { |
| 148 | + return propType.propertyType().isId(); |
| 149 | + } |
| 150 | + |
| 151 | + public Wrapper<?> wrapper() { |
| 152 | + return property.getWrapper(); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments