Skip to content

Commit 71cb955

Browse files
authored
Merge pull request #516 from jeffgbutler/invalid-sql
Miscellaneous Checks for Invalid SQL
2 parents c9e3801 + 0ea9717 commit 71cb955

23 files changed

+323
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ For examples of global and statement configuration, see the "Configuration of th
3838
4. Added the ability to configure the library and change some default behaviors. Currently, this is limited to changing
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))
41+
5. Added several checks for invalid SQL ([#516](https://github.com/mybatis/mybatis-dynamic-sql/pull/516))
4142

4243
## Release 1.4.0 - March 3, 2022
4344

src/main/java/org/mybatis/dynamic/sql/AbstractListValueCondition.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public abstract class AbstractListValueCondition<T> implements VisitableConditio
2828
protected final Collection<T> values;
2929

3030
/**
31+
* Callback to execute when the list is empty.
32+
*
3133
* @deprecated in favor of the statement configuration functions
3234
*/
3335
@Deprecated
@@ -38,9 +40,11 @@ protected AbstractListValueCondition(Collection<T> values) {
3840
}
3941

4042
/**
41-
* @deprecated in favor of the statement configuration functions
43+
* Construct a new condition with a callback.
44+
*
4245
* @param values values
4346
* @param emptyCallback empty callback
47+
* @deprecated in favor of the statement configuration functions
4448
*/
4549
@Deprecated
4650
protected AbstractListValueCondition(Collection<T> values, Callback emptyCallback) {
@@ -110,9 +114,9 @@ protected <R, S extends AbstractListValueCondition<R>> S mapSupport(Function<? s
110114
/**
111115
* Specifies a callback function to be called if the value list is empty when rendered.
112116
*
113-
* @deprecated in favor of the statement configuration functions
114117
* @param callback a callback function - typically throws an exception to block the statement from executing
115118
* @return this condition
119+
* @deprecated in favor of the statement configuration functions
116120
*/
117121
@Deprecated
118122
public abstract AbstractListValueCondition<T> withListEmptyCallback(Callback callback);

src/main/java/org/mybatis/dynamic/sql/Callback.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import java.util.function.Function;
1919

2020
/**
21+
* Provides a callback function for empty "in" conditions.
22+
*
2123
* @deprecated in favor of the new statement configuration functionality
2224
*/
2325
@Deprecated
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 InvalidSqlException extends RuntimeException {
19+
public InvalidSqlException(String message) {
20+
super(message);
21+
}
22+
}

src/main/java/org/mybatis/dynamic/sql/insert/AbstractMultiRowInsertModel.java

Lines changed: 2 additions & 2 deletions
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.
@@ -29,7 +29,7 @@
2929
public abstract class AbstractMultiRowInsertModel<T> {
3030
private final SqlTable table;
3131
private final List<T> records;
32-
private final List<AbstractColumnMapping> columnMappings;
32+
protected final List<AbstractColumnMapping> columnMappings;
3333

3434
protected AbstractMultiRowInsertModel(AbstractBuilder<T, ?> builder) {
3535
table = Objects.requireNonNull(builder.table);

src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertModel.java

Lines changed: 5 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.
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919

2020
import org.jetbrains.annotations.NotNull;
21+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2122
import org.mybatis.dynamic.sql.insert.render.BatchInsert;
2223
import org.mybatis.dynamic.sql.insert.render.BatchInsertRenderer;
2324
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -26,6 +27,9 @@ public class BatchInsertModel<T> extends AbstractMultiRowInsertModel<T> {
2627

2728
private BatchInsertModel(Builder<T> builder) {
2829
super(builder);
30+
if (columnMappings.isEmpty()) {
31+
throw new InvalidSqlException("Batch insert statements must have at least one column mapping");
32+
}
2933
}
3034

3135
@NotNull

src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertModel.java

Lines changed: 5 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.
@@ -23,6 +23,7 @@
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.mybatis.dynamic.sql.SqlTable;
26+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2627
import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer;
2728
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
2829
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -35,6 +36,9 @@ public class GeneralInsertModel {
3536

3637
private GeneralInsertModel(Builder builder) {
3738
table = Objects.requireNonNull(builder.table);
39+
if (builder.insertMappings.isEmpty()) {
40+
throw new InvalidSqlException("General insert statements must have at least one column mapping");
41+
}
3842
insertMappings = builder.insertMappings;
3943
}
4044

src/main/java/org/mybatis/dynamic/sql/insert/InsertColumnListModel.java

Lines changed: 8 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.
@@ -17,15 +17,22 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20+
import java.util.Objects;
2021
import java.util.function.Function;
2122
import java.util.stream.Stream;
2223

2324
import org.mybatis.dynamic.sql.SqlColumn;
25+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2426

2527
public class InsertColumnListModel {
2628
private final List<SqlColumn<?>> columns = new ArrayList<>();
2729

2830
private InsertColumnListModel(List<SqlColumn<?>> columns) {
31+
Objects.requireNonNull(columns);
32+
if (columns.isEmpty()) {
33+
throw new InvalidSqlException("Insert select statements require at least one column in the column list");
34+
}
35+
2936
this.columns.addAll(columns);
3037
}
3138

src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java

Lines changed: 5 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.
@@ -23,6 +23,7 @@
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.mybatis.dynamic.sql.SqlTable;
26+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2627
import org.mybatis.dynamic.sql.insert.render.InsertRenderer;
2728
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
2829
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -37,6 +38,9 @@ private InsertModel(Builder<T> builder) {
3738
table = Objects.requireNonNull(builder.table);
3839
row = Objects.requireNonNull(builder.row);
3940
columnMappings = Objects.requireNonNull(builder.columnMappings);
41+
if (columnMappings.isEmpty()) {
42+
throw new InvalidSqlException("Insert statements must have at least one column mapping"); // $NON-NLS-1$
43+
}
4044
}
4145

4246
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {

src/main/java/org/mybatis/dynamic/sql/insert/MultiRowInsertModel.java

Lines changed: 5 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.
@@ -18,6 +18,7 @@
1818
import java.util.Collection;
1919

2020
import org.jetbrains.annotations.NotNull;
21+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2122
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertRenderer;
2223
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
2324
import org.mybatis.dynamic.sql.render.RenderingStrategy;
@@ -26,6 +27,9 @@ public class MultiRowInsertModel<T> extends AbstractMultiRowInsertModel<T> {
2627

2728
private MultiRowInsertModel(Builder<T> builder) {
2829
super(builder);
30+
if (columnMappings.isEmpty()) {
31+
throw new InvalidSqlException("Multi row insert statements must have at least one column mapping");
32+
}
2933
}
3034

3135
@NotNull

0 commit comments

Comments
 (0)