Skip to content

Commit

Permalink
Merge pull request #768 from jeffgbutler/duplicate-code-removal
Browse files Browse the repository at this point in the history
Remove duplicate code through abstraction
  • Loading branch information
jeffgbutler authored Apr 8, 2024
2 parents a79770d + 08cbae3 commit 9773e37
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2016-2024 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
*
* https://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.select;

import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;

public abstract class AbstractSelectModel {
private final OrderByModel orderByModel;
private final PagingModel pagingModel;
protected final StatementConfiguration statementConfiguration;

protected AbstractSelectModel(AbstractBuilder<?> builder) {
orderByModel = builder.orderByModel;
pagingModel = builder.pagingModel;
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
}

public Optional<OrderByModel> orderByModel() {
return Optional.ofNullable(orderByModel);
}

public Optional<PagingModel> pagingModel() {
return Optional.ofNullable(pagingModel);
}

public abstract static class AbstractBuilder<T extends AbstractBuilder<T>> {
private OrderByModel orderByModel;
private PagingModel pagingModel;
private StatementConfiguration statementConfiguration;

public T withOrderByModel(OrderByModel orderByModel) {
this.orderByModel = orderByModel;
return getThis();
}

public T withPagingModel(PagingModel pagingModel) {
this.pagingModel = pagingModel;
return getThis();
}

public T withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
return getThis();
}

protected abstract T getThis();
}
}
39 changes: 5 additions & 34 deletions src/main/java/org/mybatis/dynamic/sql/select/MultiSelectModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,22 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.render.MultiSelectRenderer;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.util.Validator;

public class MultiSelectModel {
public class MultiSelectModel extends AbstractSelectModel {
private final SelectModel initialSelect;
private final List<UnionQuery> unionQueries;
private final OrderByModel orderByModel;
private final PagingModel pagingModel;
private final StatementConfiguration statementConfiguration;

private MultiSelectModel(Builder builder) {
super(builder);
initialSelect = Objects.requireNonNull(builder.initialSelect);
unionQueries = builder.unionQueries;
orderByModel = builder.orderByModel;
pagingModel = builder.pagingModel;
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
Validator.assertNotEmpty(unionQueries, "ERROR.35"); //$NON-NLS-1$
}

Expand All @@ -53,14 +45,6 @@ public Stream<UnionQuery> unionQueries() {
return unionQueries.stream();
}

public Optional<OrderByModel> orderByModel() {
return Optional.ofNullable(orderByModel);
}

public Optional<PagingModel> pagingModel() {
return Optional.ofNullable(pagingModel);
}

@NotNull
public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
return new MultiSelectRenderer.Builder()
Expand All @@ -71,12 +55,9 @@ public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
.render();
}

public static class Builder {
public static class Builder extends AbstractBuilder<Builder> {
private SelectModel initialSelect;
private final List<UnionQuery> unionQueries = new ArrayList<>();
private OrderByModel orderByModel;
private PagingModel pagingModel;
private StatementConfiguration statementConfiguration;

public Builder withInitialSelect(SelectModel initialSelect) {
this.initialSelect = initialSelect;
Expand All @@ -88,18 +69,8 @@ public Builder withUnionQueries(List<UnionQuery> unionQueries) {
return this;
}

public Builder withOrderByModel(OrderByModel orderByModel) {
this.orderByModel = orderByModel;
return this;
}

public Builder withPagingModel(PagingModel pagingModel) {
this.pagingModel = pagingModel;
return this;
}

public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
@Override
protected Builder getThis() {
return this;
}

Expand Down
39 changes: 5 additions & 34 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,28 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.util.Validator;

public class SelectModel {
public class SelectModel extends AbstractSelectModel {
private final List<QueryExpressionModel> queryExpressions;
private final OrderByModel orderByModel;
private final PagingModel pagingModel;
private final StatementConfiguration statementConfiguration;

private SelectModel(Builder builder) {
super(builder);
queryExpressions = Objects.requireNonNull(builder.queryExpressions);
Validator.assertNotEmpty(queryExpressions, "ERROR.14"); //$NON-NLS-1$
orderByModel = builder.orderByModel;
pagingModel = builder.pagingModel;
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
}

public Stream<QueryExpressionModel> queryExpressions() {
return queryExpressions.stream();
}

public Optional<OrderByModel> orderByModel() {
return Optional.ofNullable(orderByModel);
}

public Optional<PagingModel> pagingModel() {
return Optional.ofNullable(pagingModel);
}

@NotNull
public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
RenderingContext renderingContext = RenderingContext.withRenderingStrategy(renderingStrategy)
Expand All @@ -82,11 +66,8 @@ public static Builder withQueryExpressions(List<QueryExpressionModel> queryExpre
return new Builder().withQueryExpressions(queryExpressions);
}

public static class Builder {
public static class Builder extends AbstractBuilder<Builder> {
private final List<QueryExpressionModel> queryExpressions = new ArrayList<>();
private OrderByModel orderByModel;
private PagingModel pagingModel;
private StatementConfiguration statementConfiguration;

public Builder withQueryExpression(QueryExpressionModel queryExpression) {
this.queryExpressions.add(queryExpression);
Expand All @@ -98,18 +79,8 @@ public Builder withQueryExpressions(List<QueryExpressionModel> queryExpressions)
return this;
}

public Builder withOrderByModel(OrderByModel orderByModel) {
this.orderByModel = orderByModel;
return this;
}

public Builder withPagingModel(PagingModel pagingModel) {
this.pagingModel = pagingModel;
return this;
}

public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
@Override
protected Builder getThis() {
return this;
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/mybatis/dynamic/sql/InvalidSQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ void testInvalidNullInsertColumnList() {

@Test
void testInvalidSelectStatementWithoutQueryExpressions() {
SelectModel.Builder builder = new SelectModel.Builder();
SelectModel.Builder builder =
new SelectModel.Builder().withStatementConfiguration(new StatementConfiguration());

assertThatExceptionOfType(InvalidSqlException.class).isThrownBy(builder::build)
.withMessage(Messages.getString("ERROR.14"));
Expand Down

0 comments on commit 9773e37

Please sign in to comment.