-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add duplicate column detection in EntityProvider #1267
Changes from 4 commits
444c6e4
f4d19de
1639737
7f5af4c
b6806bb
79d2869
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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.message.Message; | ||
|
||
/** Thrown to indicate that a column is duplicated in a result set. */ | ||
public class DuplicateColumnException extends JdbcException { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
protected final String columnName; | ||
|
||
protected final String rawSql; | ||
|
||
protected final String formattedSql; | ||
|
||
protected final String sqlFilePath; | ||
|
||
public DuplicateColumnException( | ||
SqlLogType logType, | ||
String columnName, | ||
String rawSql, | ||
String formattedSql, | ||
String sqlFilePath) { | ||
super(Message.DOMA2237, columnName, sqlFilePath, choiceSql(logType, rawSql, formattedSql)); | ||
this.columnName = columnName; | ||
this.rawSql = rawSql; | ||
this.formattedSql = formattedSql; | ||
this.sqlFilePath = sqlFilePath; | ||
} | ||
|
||
/** | ||
* Returns the unknown column name. | ||
* | ||
* @return the unknown column name | ||
*/ | ||
public String getColumnName() { | ||
return columnName; | ||
} | ||
|
||
/** | ||
* Returns the raw SQL string. | ||
* | ||
* @return the raw SQL string | ||
*/ | ||
public String getRawSql() { | ||
return rawSql; | ||
} | ||
|
||
/** | ||
* Returns the formatted SQL string | ||
* | ||
* @return the formatted SQL or {@code null} if this exception is thrown in the batch process | ||
*/ | ||
public String getFormattedSql() { | ||
return formattedSql; | ||
} | ||
|
||
/** | ||
* Returns the SQL file path. | ||
* | ||
* @return the SQL file path or {@code null} if the SQL is auto generated | ||
*/ | ||
public String getSqlFilePath() { | ||
return sqlFilePath; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 | ||
*/ | ||
default void handle(Query query, String duplicateColumnName) {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* 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 class ThrowingDuplicateColumnHandler implements DuplicateColumnHandler { | ||
|
||
/** | ||
* Handles the duplicate column. | ||
* | ||
* @param query the query | ||
* @param duplicateColumnName the name of the duplicate column | ||
* @throws DuplicateColumnException if this handler does not allow the duplicate column | ||
*/ | ||
@Override | ||
public 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 |
---|---|---|
|
@@ -15,13 +15,15 @@ | |
*/ | ||
package org.seasar.doma.internal.jdbc.command; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.seasar.doma.internal.util.AssertionUtil.assertEquals; | ||
|
||
import example.entity.Emp; | ||
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; | ||
|
@@ -31,10 +33,13 @@ | |
import org.seasar.doma.internal.jdbc.mock.MockResultSetMetaData; | ||
import org.seasar.doma.internal.jdbc.mock.RowData; | ||
import org.seasar.doma.jdbc.Config; | ||
import org.seasar.doma.jdbc.DuplicateColumnException; | ||
import org.seasar.doma.jdbc.DuplicateColumnHandler; | ||
import org.seasar.doma.jdbc.PreparedSql; | ||
import org.seasar.doma.jdbc.SelectOptions; | ||
import org.seasar.doma.jdbc.SqlKind; | ||
import org.seasar.doma.jdbc.SqlLogType; | ||
import org.seasar.doma.jdbc.ThrowingDuplicateColumnHandler; | ||
import org.seasar.doma.jdbc.UnknownColumnException; | ||
import org.seasar.doma.jdbc.UnknownColumnHandler; | ||
import org.seasar.doma.jdbc.entity.EntityType; | ||
|
@@ -111,6 +116,46 @@ public void testGetEntity_EmptyUnknownColumnHandler() throws Exception { | |
assertEquals(100, emp.getVersion()); | ||
} | ||
|
||
@Test | ||
public void testCreateIndexMap_DuplicateColumnNameException() throws SQLException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test case name includes "DuplicateColumnNameException," but the test does not throw a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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 MockConfig()), false); | ||
|
||
provider.createIndexMap(metaData, entityType); | ||
Emp emp = provider.get(resultSet); | ||
|
||
assertEquals(1, emp.getId()); | ||
assertEquals("bbb", emp.getName()); | ||
assertEquals(100, emp.getVersion()); | ||
} | ||
|
||
@Test | ||
public void testCreateIndexMap_DuplicateColumnHandler() 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")); | ||
|
||
_Emp entityType = _Emp.getSingletonInternal(); | ||
EntityProvider<Emp> provider = | ||
new EntityProvider<>( | ||
entityType, new MySelectQuery(new SetDuplicateColumnHandlerConfig()), false); | ||
|
||
assertThrows( | ||
DuplicateColumnException.class, () -> provider.createIndexMap(metaData, entityType)); | ||
} | ||
|
||
protected static class MySelectQuery implements SelectQuery { | ||
|
||
private final Config config; | ||
|
@@ -213,4 +258,11 @@ public UnknownColumnHandler getUnknownColumnHandler() { | |
return new EmptyUnknownColumnHandler(); | ||
} | ||
} | ||
|
||
protected static class SetDuplicateColumnHandlerConfig extends MockConfig { | ||
@Override | ||
public DuplicateColumnHandler getDuplicateColumnHandler() { | ||
return new ThrowingDuplicateColumnHandler(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class DuplicateColumnExceptionTest { | ||
|
||
@Test | ||
public void test() { | ||
DuplicateColumnException e = | ||
new DuplicateColumnException(SqlLogType.FORMATTED, "aaa", "bbb", "ccc", "ddd"); | ||
System.out.println(e.getMessage()); | ||
assertEquals("aaa", e.getColumnName()); | ||
assertEquals("bbb", e.getRawSql()); | ||
assertEquals("ccc", e.getFormattedSql()); | ||
assertEquals("ddd", e.getSqlFilePath()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you pass
lowerCaseColumnName
instead ofcolumnName
? The reason for this request is to align with the invocation ofunknownColumnHandler.handle
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b6806bb