Skip to content

Commit cfda284

Browse files
authored
Merge pull request #517 from jeffgbutler/exception-consolidation
Exception Consolidation and Documentation
2 parents 71cb955 + 245555b commit cfda284

File tree

14 files changed

+168
-46
lines changed

14 files changed

+168
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ For examples of global and statement configuration, see the "Configuration of th
3939
the behavior of the library in regard to where clauses that will not render. See the "Configuration of the Library"
4040
page for details. ([#515](https://github.com/mybatis/mybatis-dynamic-sql/pull/515))
4141
5. Added several checks for invalid SQL ([#516](https://github.com/mybatis/mybatis-dynamic-sql/pull/516))
42+
6. Added documentation for the various exceptions thrown by the library ([#517](https://github.com/mybatis/mybatis-dynamic-sql/pull/517))
4243

4344
## Release 1.4.0 - March 3, 2022
4445

src/main/java/org/mybatis/dynamic/sql/configuration/GlobalConfiguration.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.util.Properties;
21-
import java.util.logging.Level;
22-
import java.util.logging.Logger;
21+
22+
import org.mybatis.dynamic.sql.exception.DynamicSqlException;
2323

2424
public class GlobalConfiguration {
2525
public static final String CONFIGURATION_FILE_PROPERTY = "mybatis-dynamic-sql.configurationFile"; //$NON-NLS-1$
@@ -57,8 +57,7 @@ void loadProperties(InputStream inputStream, String propertyFile) {
5757
try {
5858
properties.load(inputStream);
5959
} catch (IOException e) {
60-
Logger logger = Logger.getLogger(GlobalConfiguration.class.getName());
61-
logger.log(Level.SEVERE, e, () -> "IOException reading property file \"" + propertyFile + "\"");
60+
throw new DynamicSqlException("IOException reading property file \"" + propertyFile + "\"", e);
6261
}
6362
}
6463

src/main/java/org/mybatis/dynamic/sql/exception/DuplicateTableAliasException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
* @since 1.3.1
3131
* @author Jeff Butler
3232
*/
33-
public class DuplicateTableAliasException extends RuntimeException {
33+
public class DuplicateTableAliasException extends DynamicSqlException {
3434

3535
public DuplicateTableAliasException(SqlTable table, String newAlias, String existingAlias) {
3636
super(generateMessage(Objects.requireNonNull(table),
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016-2022 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+
* http://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.mybatis.dynamic.sql.exception;
17+
18+
public class DynamicSqlException extends RuntimeException {
19+
public DynamicSqlException(String message) {
20+
super(message);
21+
}
22+
23+
public DynamicSqlException(String message, Throwable cause) {
24+
super(message, cause);
25+
}
26+
}

src/main/java/org/mybatis/dynamic/sql/exception/InvalidSqlException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.mybatis.dynamic.sql.exception;
1717

18-
public class InvalidSqlException extends RuntimeException {
18+
public class InvalidSqlException extends DynamicSqlException {
1919
public InvalidSqlException(String message) {
2020
super(message);
2121
}

src/main/java/org/mybatis/dynamic/sql/exception/NonRenderingWhereClauseException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @since 1.4.1
3737
* @author Jeff Butler
3838
*/
39-
public class NonRenderingWhereClauseException extends RuntimeException {
39+
public class NonRenderingWhereClauseException extends DynamicSqlException {
4040
public NonRenderingWhereClauseException() {
4141
super("A where clause was specified, but failed to render."); //$NON-NLS-1$
4242
}

src/main/java/org/mybatis/dynamic/sql/insert/render/GeneralInsertRenderer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import java.util.Optional;
2525
import java.util.stream.Collectors;
2626

27+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2728
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
2829
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2930

@@ -42,6 +43,11 @@ public GeneralInsertStatementProvider render() {
4243
List<Optional<FieldAndValueAndParameters>> fieldsAndValues = model.mapColumnMappings(m -> m.accept(visitor))
4344
.collect(Collectors.toList());
4445

46+
if (fieldsAndValues.stream().noneMatch(Optional::isPresent)) {
47+
throw new InvalidSqlException(
48+
"All optional set phrases were dropped when rendering the general insert statement");
49+
}
50+
4551
return DefaultGeneralInsertStatementProvider.withInsertStatement(calculateInsertStatement(fieldsAndValues))
4652
.withParameters(calculateParameters(fieldsAndValues))
4753
.build();

src/main/java/org/mybatis/dynamic/sql/insert/render/InsertRenderer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.Optional;
2323
import java.util.stream.Collectors;
2424

25+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2526
import org.mybatis.dynamic.sql.insert.InsertModel;
2627
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2728

@@ -41,6 +42,11 @@ public InsertStatementProvider<T> render() {
4142
List<Optional<FieldAndValue>> fieldsAndValues = model.mapColumnMappings(m -> m.accept(visitor))
4243
.collect(Collectors.toList());
4344

45+
if (fieldsAndValues.stream().noneMatch(Optional::isPresent)) {
46+
throw new InvalidSqlException(
47+
"All optional column mappings were dropped when rendering the insert statement");
48+
}
49+
4450
return DefaultInsertStatementProvider.withRow(model.row())
4551
.withInsertStatement(calculateInsertStatement(fieldsAndValues))
4652
.build();

src/main/java/org/mybatis/dynamic/sql/update/render/UpdateRenderer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.stream.Collectors;
2727

2828
import org.mybatis.dynamic.sql.SqlTable;
29+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2930
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
3031
import org.mybatis.dynamic.sql.render.RenderingStrategy;
3132
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
@@ -56,6 +57,10 @@ public UpdateStatementProvider render() {
5657
updateModel.mapColumnMappings(m -> m.accept(visitor))
5758
.collect(Collectors.toList());
5859

60+
if (fragmentsAndParameters.stream().noneMatch(Optional::isPresent)) {
61+
throw new InvalidSqlException("All optional set phrases were dropped when rendering the update statement");
62+
}
63+
5964
return updateModel.whereModel()
6065
.flatMap(this::renderWhereClause)
6166
.map(wc -> renderWithWhereClause(fragmentsAndParameters, wc))

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KInvalidSQLException.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package org.mybatis.dynamic.sql.util.kotlin
1717

18+
import org.mybatis.dynamic.sql.exception.InvalidSqlException
19+
1820
/**
1921
* This exception is thrown if the library detects misuse of the Kotlin DSL that would result in invalid SQL
2022
*/
21-
class KInvalidSQLException(message: String) : RuntimeException(message)
23+
class KInvalidSQLException(message: String) : InvalidSqlException(message)

0 commit comments

Comments
 (0)