-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add duplicate column detection in EntityProvider (#1267)
* Add duplicate column detection in EntityProvider * Add DuplicateColumnHandler interface and its implementations to make DuplicateColumnException optional * Replace wildcard imports with explicit imports * current behavior set as the default implementation and DuplicateColumnException optional * Change duplicate column handler to use lower case column name * update test method name
- Loading branch information
1 parent
e2b6180
commit 03272c7
Showing
9 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
doma-core/src/main/java/org/seasar/doma/jdbc/DuplicateColumnException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
doma-core/src/main/java/org/seasar/doma/jdbc/DuplicateColumnHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
40 changes: 40 additions & 0 deletions
40
doma-core/src/main/java/org/seasar/doma/jdbc/ThrowingDuplicateColumnHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
doma-core/src/test/java/org/seasar/doma/jdbc/DuplicateColumnExceptionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |