Skip to content

Commit

Permalink
Add DuplicateColumnHandler interface and its implementations to make …
Browse files Browse the repository at this point in the history
…DuplicateColumnException optional
  • Loading branch information
okurashoichi committed Jan 14, 2025
1 parent 444c6e4 commit f4d19de
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.seasar.doma.jdbc.*;
import org.seasar.doma.jdbc.DuplicateColumnHandler;
import org.seasar.doma.jdbc.JdbcMappingVisitor;
import org.seasar.doma.jdbc.Naming;
import org.seasar.doma.jdbc.ResultMappingException;
import org.seasar.doma.jdbc.Sql;
import org.seasar.doma.jdbc.UnknownColumnHandler;
import org.seasar.doma.jdbc.entity.EntityPropertyType;
import org.seasar.doma.jdbc.entity.EntityType;
import org.seasar.doma.jdbc.entity.Property;
Expand All @@ -45,6 +50,8 @@ public class EntityProvider<ENTITY> extends AbstractObjectProvider<ENTITY> {

protected final UnknownColumnHandler unknownColumnHandler;

protected final DuplicateColumnHandler duplicateColumnHandler;

protected Map<Integer, EntityPropertyType<ENTITY, ?>> indexMap;

public EntityProvider(EntityType<ENTITY> entityType, Query query, boolean resultMappingEnsured) {
Expand All @@ -54,6 +61,7 @@ public EntityProvider(EntityType<ENTITY> entityType, Query query, boolean result
this.resultMappingEnsured = resultMappingEnsured;
this.jdbcMappingVisitor = query.getConfig().getDialect().getJdbcMappingVisitor();
this.unknownColumnHandler = query.getConfig().getUnknownColumnHandler();
this.duplicateColumnHandler = query.getConfig().getDuplicateColumnHandler();
}

@Override
Expand Down Expand Up @@ -93,12 +101,7 @@ protected ENTITY build(ResultSet resultSet) throws SQLException {
String columnName = resultSetMeta.getColumnLabel(i);
String lowerCaseColumnName = columnName.toLowerCase();
if (!seenColumnNames.add(lowerCaseColumnName)) {
throw new DuplicateColumnException(
query.getConfig().getExceptionSqlLogType(),
columnName,
query.getSql().getRawSql(),
query.getSql().getFormattedSql(),
query.getSql().getSqlFilePath());
duplicateColumnHandler.handle(query, columnName);
}
EntityPropertyType<ENTITY, ?> propertyType = columnNameMap.get(lowerCaseColumnName);
if (propertyType == null) {
Expand Down
9 changes: 9 additions & 0 deletions doma-core/src/main/java/org/seasar/doma/jdbc/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ default UnknownColumnHandler getUnknownColumnHandler() {
return ConfigSupport.defaultUnknownColumnHandler;
}

/**
* Returns the duplicate column handler.
*
* @return the duplicate column handler
*/
default DuplicateColumnHandler getDuplicateColumnHandler() {
return ConfigSupport.defaultDuplicateColumnHandler;
}

/**
* Returns the naming convention controller.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public final class ConfigSupport {
public static final UnknownColumnHandler defaultUnknownColumnHandler =
new UnknownColumnHandler() {};

public static final DuplicateColumnHandler defaultDuplicateColumnHandler =
new DuplicateColumnHandler() {};

public static final Naming defaultNaming = Naming.DEFAULT;

public static final MapKeyNaming defaultMapKeyNaming = new MapKeyNaming() {};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright Doma 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.seasar.doma.jdbc;

import org.seasar.doma.jdbc.query.Query;

/** A handler for the column that is duplicated in a result set. */
public interface DuplicateColumnHandler {

/**
* Handles the duplicate column.
*
* @param query the query
* @param duplicateColumnName the name of the unknown column
* @throws DuplicateColumnException if this handler does not allow the duplicate column
*/
default void handle(Query query, String duplicateColumnName) {
Sql<?> sql = query.getSql();
throw new DuplicateColumnException(
query.getConfig().getExceptionSqlLogType(),
duplicateColumnName,
sql.getRawSql(),
sql.getFormattedSql(),
sql.getSqlFilePath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import example.entity._Emp;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Collections;
import org.junit.jupiter.api.Test;
import org.seasar.doma.FetchType;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void testGetEntity_EmptyUnknownColumnHandler() throws Exception {
}

@Test
public void testCreateIndexMap_DuplicateColumnName() throws Exception {
public void testCreateIndexMap_DuplicateColumnNameException() {
MockResultSetMetaData metaData = new MockResultSetMetaData();
metaData.columns.add(new ColumnMetaData("id"));
metaData.columns.add(new ColumnMetaData("name"));
Expand All @@ -119,10 +120,31 @@ public void testCreateIndexMap_DuplicateColumnName() throws Exception {
new EntityProvider<>(entityType, new MySelectQuery(new MockConfig()), false);

assertThrows(
DuplicateColumnException.class,
() -> {
provider.createIndexMap(metaData, entityType);
});
DuplicateColumnException.class, () -> provider.createIndexMap(metaData, entityType));
}

@Test
public void testCreateIndexMap_EmptyDuplicateColumnHandler() throws SQLException {
MockResultSetMetaData metaData = new MockResultSetMetaData();
metaData.columns.add(new ColumnMetaData("id"));
metaData.columns.add(new ColumnMetaData("name"));
metaData.columns.add(new ColumnMetaData("name")); // Duplicate column name
metaData.columns.add(new ColumnMetaData("version"));
MockResultSet resultSet = new MockResultSet(metaData);
resultSet.rows.add(new RowData(1, "aaa", "bbb", 100));
resultSet.next();

_Emp entityType = _Emp.getSingletonInternal();
EntityProvider<Emp> provider =
new EntityProvider<>(
entityType, new MySelectQuery(new EmptyDuplicateColumnHandlerConfig()), false);

provider.createIndexMap(metaData, entityType);
Emp emp = provider.get(resultSet);

assertEquals(1, emp.getId());
assertEquals("bbb", emp.getName());
assertEquals(100, emp.getVersion());
}

protected static class MySelectQuery implements SelectQuery {
Expand Down Expand Up @@ -227,4 +249,16 @@ public UnknownColumnHandler getUnknownColumnHandler() {
return new EmptyUnknownColumnHandler();
}
}

protected static class EmptyDuplicateColumnHandler implements DuplicateColumnHandler {
@Override
public void handle(Query query, String unknownColumnName) {}
}

protected static class EmptyDuplicateColumnHandlerConfig extends MockConfig {
@Override
public DuplicateColumnHandler getDuplicateColumnHandler() {
return new EmptyDuplicateColumnHandler();
}
}
}

0 comments on commit f4d19de

Please sign in to comment.