Skip to content

Commit e4a62e1

Browse files
committed
Add many checks for invalid SQL in weird circumstances
1 parent c9e3801 commit e4a62e1

16 files changed

+279
-14
lines changed
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

src/main/java/org/mybatis/dynamic/sql/select/GroupByModel.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.
@@ -18,15 +18,21 @@
1818
import java.util.ArrayList;
1919
import java.util.Collection;
2020
import java.util.List;
21+
import java.util.Objects;
2122
import java.util.function.Function;
2223
import java.util.stream.Stream;
2324

2425
import org.mybatis.dynamic.sql.BasicColumn;
26+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2527

2628
public class GroupByModel {
2729
private final List<BasicColumn> columns = new ArrayList<>();
2830

2931
private GroupByModel(Collection<BasicColumn> columns) {
32+
Objects.requireNonNull(columns);
33+
if (columns.isEmpty()) {
34+
throw new InvalidSqlException("Group by expressions must have at least one column");
35+
}
3036
this.columns.addAll(columns);
3137
}
3238

src/main/java/org/mybatis/dynamic/sql/select/OrderByModel.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.
@@ -18,15 +18,21 @@
1818
import java.util.ArrayList;
1919
import java.util.Collection;
2020
import java.util.List;
21+
import java.util.Objects;
2122
import java.util.function.Function;
2223
import java.util.stream.Stream;
2324

2425
import org.mybatis.dynamic.sql.SortSpecification;
26+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
2527

2628
public class OrderByModel {
2729
private final List<SortSpecification> columns = new ArrayList<>();
2830

2931
private OrderByModel(Collection<SortSpecification> columns) {
32+
Objects.requireNonNull(columns);
33+
if (columns.isEmpty()) {
34+
throw new InvalidSqlException("Order by expressions must have at least one column");
35+
}
3036
this.columns.addAll(columns);
3137
}
3238

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionModel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.mybatis.dynamic.sql.BasicColumn;
2828
import org.mybatis.dynamic.sql.SqlTable;
2929
import org.mybatis.dynamic.sql.TableExpression;
30+
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
3031
import org.mybatis.dynamic.sql.select.join.JoinModel;
3132
import org.mybatis.dynamic.sql.where.WhereModel;
3233

@@ -49,6 +50,10 @@ private QueryExpressionModel(Builder builder) {
4950
tableAliases = builder.tableAliases;
5051
whereModel = builder.whereModel;
5152
groupByModel = builder.groupByModel;
53+
54+
if (selectList.isEmpty()) {
55+
throw new InvalidSqlException("Query expressions must have at least one column in the select list");
56+
}
5257
}
5358

5459
public Optional<String> connector() {

0 commit comments

Comments
 (0)