Skip to content

Miscellaneous Checks for Invalid SQL #516

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

Merged
merged 3 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ For examples of global and statement configuration, see the "Configuration of th
4. Added the ability to configure the library and change some default behaviors. Currently, this is limited to changing
the behavior of the library in regard to where clauses that will not render. See the "Configuration of the Library"
page for details. ([#515](https://github.com/mybatis/mybatis-dynamic-sql/pull/515))
5. Added several checks for invalid SQL ([#516](https://github.com/mybatis/mybatis-dynamic-sql/pull/516))

## Release 1.4.0 - March 3, 2022

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public abstract class AbstractListValueCondition<T> implements VisitableConditio
protected final Collection<T> values;

/**
* Callback to execute when the list is empty.
*
* @deprecated in favor of the statement configuration functions
*/
@Deprecated
Expand All @@ -38,9 +40,11 @@ protected AbstractListValueCondition(Collection<T> values) {
}

/**
* @deprecated in favor of the statement configuration functions
* Construct a new condition with a callback.
*
* @param values values
* @param emptyCallback empty callback
* @deprecated in favor of the statement configuration functions
*/
@Deprecated
protected AbstractListValueCondition(Collection<T> values, Callback emptyCallback) {
Expand Down Expand Up @@ -110,9 +114,9 @@ protected <R, S extends AbstractListValueCondition<R>> S mapSupport(Function<? s
/**
* Specifies a callback function to be called if the value list is empty when rendered.
*
* @deprecated in favor of the statement configuration functions
* @param callback a callback function - typically throws an exception to block the statement from executing
* @return this condition
* @deprecated in favor of the statement configuration functions
*/
@Deprecated
public abstract AbstractListValueCondition<T> withListEmptyCallback(Callback callback);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/Callback.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.function.Function;

/**
* Provides a callback function for empty "in" conditions.
*
* @deprecated in favor of the new statement configuration functionality
*/
@Deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2016-2022 the original author or 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
*
* http://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.mybatis.dynamic.sql.exception;

public class InvalidSqlException extends RuntimeException {
public InvalidSqlException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
public abstract class AbstractMultiRowInsertModel<T> {
private final SqlTable table;
private final List<T> records;
private final List<AbstractColumnMapping> columnMappings;
protected final List<AbstractColumnMapping> columnMappings;

protected AbstractMultiRowInsertModel(AbstractBuilder<T, ?> builder) {
table = Objects.requireNonNull(builder.table);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import java.util.Collection;

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

private BatchInsertModel(Builder<T> builder) {
super(builder);
if (columnMappings.isEmpty()) {
throw new InvalidSqlException("Batch insert statements must have at least one column mapping");
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.insert.render.GeneralInsertRenderer;
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
Expand All @@ -35,6 +36,9 @@ public class GeneralInsertModel {

private GeneralInsertModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
if (builder.insertMappings.isEmpty()) {
throw new InvalidSqlException("General insert statements must have at least one column mapping");
}
insertMappings = builder.insertMappings;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,22 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;

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

private InsertColumnListModel(List<SqlColumn<?>> columns) {
Objects.requireNonNull(columns);
if (columns.isEmpty()) {
throw new InvalidSqlException("Insert select statements require at least one column in the column list");
}

this.columns.addAll(columns);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.insert.render.InsertRenderer;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
Expand All @@ -37,6 +38,9 @@ private InsertModel(Builder<T> builder) {
table = Objects.requireNonNull(builder.table);
row = Objects.requireNonNull(builder.row);
columnMappings = Objects.requireNonNull(builder.columnMappings);
if (columnMappings.isEmpty()) {
throw new InvalidSqlException("Insert statements must have at least one column mapping"); // $NON-NLS-1$
}
}

public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import java.util.Collection;

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

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

@NotNull
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,15 +18,21 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;

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

private GroupByModel(Collection<BasicColumn> columns) {
Objects.requireNonNull(columns);
if (columns.isEmpty()) {
throw new InvalidSqlException("Group by expressions must have at least one column");
}
this.columns.addAll(columns);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,15 +18,21 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;

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

private OrderByModel(Collection<SortSpecification> columns) {
Objects.requireNonNull(columns);
if (columns.isEmpty()) {
throw new InvalidSqlException("Order by expressions must have at least one column");
}
this.columns.addAll(columns);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.TableExpression;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.select.join.JoinModel;
import org.mybatis.dynamic.sql.where.WhereModel;

Expand All @@ -49,6 +50,10 @@ private QueryExpressionModel(Builder builder) {
tableAliases = builder.tableAliases;
whereModel = builder.whereModel;
groupByModel = builder.groupByModel;

if (selectList.isEmpty()) {
throw new InvalidSqlException("Query expressions must have at least one column in the select list");
}
}

public Optional<String> connector() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,7 @@
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
Expand All @@ -34,6 +35,10 @@ public class SelectModel {

private SelectModel(Builder builder) {
queryExpressions = Objects.requireNonNull(builder.queryExpressions);
if (queryExpressions.isEmpty()) {
throw new InvalidSqlException("Select statements must have at least one query expression");
}

orderByModel = builder.orderByModel;
pagingModel = builder.pagingModel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,15 @@
*/
package org.mybatis.dynamic.sql.select.join;

import java.util.Objects;

import org.mybatis.dynamic.sql.BasicColumn;

public abstract class JoinCondition {
private final BasicColumn rightColumn;

protected JoinCondition(BasicColumn rightColumn) {
this.rightColumn = rightColumn;
this.rightColumn = Objects.requireNonNull(rightColumn);
}

public BasicColumn rightColumn() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,15 +17,22 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.TableExpression;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;

public class JoinModel {
private final List<JoinSpecification> joinSpecifications = new ArrayList<>();

private JoinModel(List<JoinSpecification> joinSpecifications) {
Objects.requireNonNull(joinSpecifications);
if (joinSpecifications.isEmpty()) {
throw new InvalidSqlException("Joins must have at least one join specification");
}

this.joinSpecifications.addAll(joinSpecifications);
}

Expand Down
Loading