Skip to content
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

Various Refactorings #716

Merged
merged 13 commits into from
Dec 6, 2023
8 changes: 4 additions & 4 deletions src/main/java/org/mybatis/dynamic/sql/SqlColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private SqlColumn(Builder<T> builder) {
alias = builder.alias;
typeHandler = builder.typeHandler;
renderingStrategy = builder.renderingStrategy;
parameterTypeConverter = builder.parameterTypeConverter;
parameterTypeConverter = Objects.requireNonNull(builder.parameterTypeConverter);
tableQualifier = builder.tableQualifier;
javaType = builder.javaType;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public Optional<Class<T>> javaType() {

@Override
public Object convertParameterType(T value) {
return parameterTypeConverter == null ? value : parameterTypeConverter.convert(value);
return parameterTypeConverter.convert(value);
}

@Override
Expand Down Expand Up @@ -110,7 +110,7 @@ public SqlColumn<T> qualifiedWith(String tableQualifier) {
}

/**
* Set an alias with a camel cased string based on the column name. The can be useful for queries using
* Set an alias with a camel cased string based on the column name. This can be useful for queries using
* the {@link org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper} where the columns are placed into
* a map based on the column name returned from the database.
*
Expand Down Expand Up @@ -218,7 +218,7 @@ public static class Builder<T> {
protected String alias;
protected String typeHandler;
protected RenderingStrategy renderingStrategy;
protected ParameterTypeConverter<T, ?> parameterTypeConverter;
protected ParameterTypeConverter<T, ?> parameterTypeConverter = v -> v;
protected String tableQualifier;
protected Class<T> javaType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
import org.mybatis.dynamic.sql.ExistsPredicate;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.VisitableCondition;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public abstract class AbstractBooleanExpressionDSL<T extends AbstractBooleanExpressionDSL<T>> {
private SqlCriterion initialCriterion; // WARNING - may be null!
Expand Down Expand Up @@ -146,10 +145,7 @@ private void addSubCriteria(String connector, List<AndOrCriteriaGroup> criteria)
}

protected void setInitialCriterion(SqlCriterion initialCriterion, StatementType statementType) {
if (this.initialCriterion != null) {
throw new InvalidSqlException(Messages.getString(statementType.messageNumber())); //$NON-NLS-1$
}

Validator.assertTrue(this.initialCriterion == null, statementType.messageNumber());
this.initialCriterion = initialCriterion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

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

private OrderByModel(Collection<? extends SortSpecification> columns) {
Objects.requireNonNull(columns);
if (columns.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.12")); //$NON-NLS-1$
}
Validator.assertNotEmpty(columns, "ERROR.12"); //$NON-NLS-1$
this.columns.addAll(columns);
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ public DeleteDSL<R> orderBy(Collection<? extends SortSpecification> columns) {
@NotNull
@Override
public R build() {
DeleteModel.Builder deleteModelBuilder = DeleteModel.withTable(table)
DeleteModel deleteModel = DeleteModel.withTable(table)
.withTableAlias(tableAlias)
.withLimit(limit)
.withOrderByModel(orderByModel);
if (whereBuilder != null) {
deleteModelBuilder.withWhereModel(whereBuilder.buildWhereModel());
}
.withOrderByModel(orderByModel)
.withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
.build();

return adapterFunction.apply(deleteModelBuilder.build());
return adapterFunction.apply(deleteModel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
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;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public class BatchInsertModel<T> extends AbstractMultiRowInsertModel<T> {

private BatchInsertModel(Builder<T> builder) {
super(builder);
if (records().isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.19")); //$NON-NLS-1$
}
if (columnMappings.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.5")); //$NON-NLS-1$
}
Validator.assertNotEmpty(records(), "ERROR.19"); //$NON-NLS-1$
Validator.assertNotEmpty(columnMappings, "ERROR.5"); //$NON-NLS-1$
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

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;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public class GeneralInsertModel {

Expand All @@ -37,9 +36,7 @@ public class GeneralInsertModel {

private GeneralInsertModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
if (builder.insertMappings.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.6")); //$NON-NLS-1$
}
Validator.assertNotEmpty(builder.insertMappings, "ERROR.6"); //$NON-NLS-1$
insertMappings = builder.insertMappings;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

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

private InsertColumnListModel(List<SqlColumn<?>> columns) {
Objects.requireNonNull(columns);
if (columns.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.4")); //$NON-NLS-1$
}

Validator.assertNotEmpty(columns, "ERROR.4"); //$NON-NLS-1$
this.columns.addAll(columns);
}

Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@

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;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public class InsertModel<T> {
private final SqlTable table;
Expand All @@ -39,9 +38,7 @@ 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(Messages.getString("ERROR.7")); //$NON-NLS-1$
}
Validator.assertNotEmpty(columnMappings, "ERROR.7"); //$NON-NLS-1$
}

public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
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;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public class MultiRowInsertModel<T> extends AbstractMultiRowInsertModel<T> {

private MultiRowInsertModel(Builder<T> builder) {
super(builder);
if (records().isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.20")); //$NON-NLS-1$
}
if (columnMappings.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.8")); //$NON-NLS-1$
}
Validator.assertNotEmpty(records(), "ERROR.20"); //$NON-NLS-1$
Validator.assertNotEmpty(columnMappings, "ERROR.8"); //$NON-NLS-1$
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.insert.GeneralInsertModel;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public class GeneralInsertRenderer {

Expand All @@ -43,9 +42,7 @@ public GeneralInsertStatementProvider render() {
.map(Optional::get)
.collect(FieldAndValueCollector.collect());

if (collector.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.9")); //$NON-NLS-1$
}
Validator.assertFalse(collector.isEmpty(), "ERROR.9"); //$NON-NLS-1$

String insertStatement = InsertRenderingUtilities.calculateInsertStatement(model.table(), collector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.insert.InsertModel;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

public class InsertRenderer<T> {

Expand All @@ -41,9 +40,7 @@ public InsertStatementProvider<T> render() {
.map(Optional::get)
.collect(FieldAndValueCollector.collect());

if (collector.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.10")); //$NON-NLS-1$
}
Validator.assertFalse(collector.isEmpty(), "ERROR.10"); //$NON-NLS-1$

String insertStatement = InsertRenderingUtilities.calculateInsertStatement(model.table(), collector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public class RenderingContext {
private RenderingContext(Builder builder) {
renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
configuredParameterName = builder.parameterName;
tableAliasCalculator = Objects.requireNonNull(builder.tableAliasCalculator);

// reasonable defaults
sequence = builder.sequence == null ? new AtomicInteger(1) : builder.sequence;
tableAliasCalculator = builder.tableAliasCalculator == null ? TableAliasCalculator.empty()
: builder.tableAliasCalculator;
calculatedParameterName = builder.parameterName == null ? RenderingStrategy.DEFAULT_PARAMETER_PREFIX
: builder.parameterName + "." + RenderingStrategy.DEFAULT_PARAMETER_PREFIX; //$NON-NLS-1$
}
Expand Down Expand Up @@ -96,7 +95,7 @@ public String aliasedTableName(SqlTable table) {
}

/**
* Crete a new rendering context based on this, with the table alias calculator modified to include the
* Create a new rendering context based on this, with the table alias calculator modified to include the
* specified child table alias calculator. This is used by the query expression renderer when the alias calculator
* may change during rendering.
*
Expand Down Expand Up @@ -125,7 +124,7 @@ public static Builder withRenderingStrategy(RenderingStrategy renderingStrategy)
public static class Builder {
private RenderingStrategy renderingStrategy;
private AtomicInteger sequence;
private TableAliasCalculator tableAliasCalculator;
private TableAliasCalculator tableAliasCalculator = TableAliasCalculator.empty();
private String parameterName;

public Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/org/mybatis/dynamic/sql/select/CountDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.Utilities;
import org.mybatis.dynamic.sql.where.AbstractWhereFinisher;
import org.mybatis.dynamic.sql.where.WhereModel;

Expand All @@ -33,7 +34,7 @@
* clauses, but not the other parts of a select (group by, order by, etc.) Count queries always return
* a long. If these restrictions are not acceptable, then use the Select DSL for an unrestricted select statement.
*
* @param <R> the type of model built by this Builder. Typically SelectModel.
* @param <R> the type of model built by this Builder. Typically, SelectModel.
*
* @author Jeff Butler
*/
Expand All @@ -53,9 +54,7 @@ private CountDSL(BasicColumn countColumn, SqlTable table, Function<SelectModel,

@Override
public CountWhereBuilder where() {
if (whereBuilder == null) {
whereBuilder = new CountWhereBuilder();
}
whereBuilder = Utilities.buildIfNecessary(whereBuilder, CountWhereBuilder::new);
return whereBuilder;
}

Expand All @@ -72,19 +71,16 @@ public CountDSL<R> configureStatement(Consumer<StatementConfiguration> consumer)
}

private SelectModel buildModel() {
QueryExpressionModel.Builder b = new QueryExpressionModel.Builder()
QueryExpressionModel queryExpressionModel = new QueryExpressionModel.Builder()
.withSelectColumn(countColumn)
.withTable(table())
.withTableAliases(tableAliases());

if (whereBuilder != null) {
b.withWhereModel(whereBuilder.buildWhereModel());
}

buildJoinModel().ifPresent(b::withJoinModel);
.withTableAliases(tableAliases())
.withJoinModel(buildJoinModel().orElse(null))
.withWhereModel(whereBuilder == null ? null : whereBuilder.buildWhereModel())
.build();

return new SelectModel.Builder()
.withQueryExpression(b.build())
.withQueryExpression(queryExpressionModel)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
import org.mybatis.dynamic.sql.util.Messages;
import org.mybatis.dynamic.sql.util.Validator;

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

private GroupByModel(Collection<? extends BasicColumn> columns) {
Objects.requireNonNull(columns);
if (columns.isEmpty()) {
throw new InvalidSqlException(Messages.getString("ERROR.11")); //$NON-NLS-1$
}
Validator.assertNotEmpty(columns, "ERROR.11"); //$NON-NLS-1$
this.columns.addAll(columns);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SortSpecification;
Expand Down Expand Up @@ -78,15 +79,11 @@ public MultiSelectModel build() {
.withInitialSelect(initialSelect)
.withUnionQueries(unionQueries)
.withOrderByModel(orderByModel)
.withPagingModel(buildPagingModel())
.withPagingModel(buildPagingModel().orElse(null))
.build();
}

private PagingModel buildPagingModel() {
if (limit == null && offset == null && fetchFirstRows == null) {
return null;
}

private Optional<PagingModel> buildPagingModel() {
return new PagingModel.Builder()
.withLimit(limit)
.withOffset(offset)
Expand Down
Loading