Skip to content

Refactor the renderers #81

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 9 commits into from
Apr 7, 2019
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 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,23 +15,16 @@
*/
package org.mybatis.dynamic.sql.delete.render;

import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.where.render.WhereClauseProvider;

public class DefaultDeleteStatementProvider implements DeleteStatementProvider {
private String tableName;
private Optional<String> whereClause;
private String deleteStatement;
private Map<String, Object> parameters;

private DefaultDeleteStatementProvider(Builder builder) {
tableName = Objects.requireNonNull(builder.tableName);
whereClause = Optional.ofNullable(builder.whereClause);
deleteStatement = Objects.requireNonNull(builder.deleteStatement);
parameters = Objects.requireNonNull(builder.parameters);
}

Expand All @@ -42,30 +35,24 @@ public Map<String, Object> getParameters() {

@Override
public String getDeleteStatement() {
return "delete from" //$NON-NLS-1$
+ spaceBefore(tableName)
+ spaceBefore(whereClause);
return deleteStatement;
}

public static Builder withTableName(String tableName) {
return new Builder().withTableName(tableName);
public static Builder withDeleteStatement(String deleteStatement) {
return new Builder().withDeleteStatement(deleteStatement);
}

public static class Builder {
private String tableName;
private String whereClause;
private String deleteStatement;
private Map<String, Object> parameters = new HashMap<>();

public Builder withTableName(String tableName) {
this.tableName = tableName;
public Builder withDeleteStatement(String deleteStatement) {
this.deleteStatement = deleteStatement;
return this;
}

public Builder withWhereClause(Optional<WhereClauseProvider> whereClauseProvider) {
whereClauseProvider.ifPresent(wcp -> {
whereClause = wcp.getWhereClause();
parameters.putAll(wcp.getParameters());
});
public Builder withParameters(Map<String, Object> parameters) {
this.parameters.putAll(parameters);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 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,6 +15,10 @@
*/
package org.mybatis.dynamic.sql.delete.render;

import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -36,11 +40,13 @@ private DeleteRenderer(Builder builder) {
}

public DeleteStatementProvider render() {
return DefaultDeleteStatementProvider.withTableName(deleteModel.table().name())
.withWhereClause(deleteModel.whereModel().flatMap(this::renderWhereClause))
Optional<WhereClauseProvider> whereClause = deleteModel.whereModel().flatMap(this::renderWhereClause);

return DefaultDeleteStatementProvider.withDeleteStatement(calculateDeleteStatement(whereClause))
.withParameters(calculateParameters(whereClause))
.build();
}

private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
return WhereRenderer.withWhereModel(whereModel)
.withRenderingStrategy(renderingStrategy)
Expand All @@ -50,6 +56,18 @@ private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
.render();
}

private String calculateDeleteStatement(Optional<WhereClauseProvider> whereClause) {
return "delete from" //$NON-NLS-1$
+ spaceBefore(deleteModel.table().name())
+ spaceBefore(whereClause.map(WhereClauseProvider::getWhereClause));
}

private Map<String, Object> calculateParameters(Optional<WhereClauseProvider> whereClause) {
return whereClause
.map(WhereClauseProvider::getParameters)
.orElse(Collections.emptyMap());
}

public static Builder withDeleteModel(DeleteModel deleteModel) {
return new Builder().withDeleteModel(deleteModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 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,7 +18,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
Expand All @@ -43,7 +42,7 @@ private InsertSelectDSL(SqlTable table, SelectModel selectModel) {

public InsertSelectModel build() {
return InsertSelectModel.withTable(table)
.withColumnList(Optional.ofNullable(columnList))
.withColumnList(columnList)
.withSelectModel(selectModel)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2019 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 @@ -26,12 +26,12 @@

public class InsertSelectModel {
private SqlTable table;
private Optional<InsertColumnListModel> columnList;
private InsertColumnListModel columnList;
private SelectModel selectModel;

private InsertSelectModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
columnList = Objects.requireNonNull(builder.columnList);
columnList = builder.columnList;
selectModel = Objects.requireNonNull(builder.selectModel);
}

Expand All @@ -44,7 +44,7 @@ public SelectModel selectModel() {
}

public Optional<InsertColumnListModel> columnList() {
return columnList;
return Optional.ofNullable(columnList);
}

public InsertSelectStatementProvider render(RenderingStrategy renderingStrategy) {
Expand All @@ -60,15 +60,15 @@ public static Builder withTable(SqlTable table) {

public static class Builder {
private SqlTable table;
private Optional<InsertColumnListModel> columnList = Optional.empty();
private InsertColumnListModel columnList;
private SelectModel selectModel;

public Builder withTable(SqlTable table) {
this.table = table;
return this;
}

public Builder withColumnList(Optional<InsertColumnListModel> columnList) {
public Builder withColumnList(InsertColumnListModel columnList) {
this.columnList = columnList;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* Copyright 2016-2019 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,24 +15,18 @@
*/
package org.mybatis.dynamic.sql.insert.render;

import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class BatchInsert<T> {
private String tableName;
private String columnsPhrase;
private String valuesPhrase;
private String insertStatement;
private List<T> records;

private BatchInsert(Builder<T> builder) {
tableName = Objects.requireNonNull(builder.tableName);
columnsPhrase = Objects.requireNonNull(builder.columnsPhrase);
valuesPhrase = Objects.requireNonNull(builder.valuesPhrase);
insertStatement = Objects.requireNonNull(builder.insertStatement);
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
}

Expand All @@ -49,9 +43,7 @@ public List<InsertStatementProvider<T>> insertStatements() {

private InsertStatementProvider<T> toInsertStatement(T record) {
return DefaultInsertStatementProvider.withRecord(record)
.withTableName(tableName)
.withColumnsPhrase(columnsPhrase)
.withValuesPhrase(valuesPhrase)
.withInsertStatement(insertStatement)
.build();
}

Expand All @@ -61,37 +53,22 @@ private InsertStatementProvider<T> toInsertStatement(T record) {
* @return the generated INSERT statement
*/
public String getInsertStatementSQL() {
return "insert into" //$NON-NLS-1$
+ spaceBefore(tableName)
+ spaceBefore(columnsPhrase)
+ spaceBefore(valuesPhrase);
return insertStatement;
}

public static <T> Builder<T> withRecords(List<T> records) {
return new Builder<T>().withRecords(records);
}

public static class Builder<T> {
private String tableName;
private String columnsPhrase;
private String valuesPhrase;
private String insertStatement;
private List<T> records = new ArrayList<>();

public Builder<T> withTableName(String tableName) {
this.tableName = tableName;
public Builder<T> withInsertStatement(String insertStatement) {
this.insertStatement = insertStatement;
return this;
}

public Builder<T> withColumnsPhrase(String columnsPhrase) {
this.columnsPhrase = columnsPhrase;
return this;
}

public Builder<T> withValuesPhrase(String valuesPhrase) {
this.valuesPhrase = valuesPhrase;
return this;
}

public Builder<T> withRecords(List<T> records) {
this.records.addAll(records);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* Copyright 2016-2019 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,6 +15,8 @@
*/
package org.mybatis.dynamic.sql.insert.render;

import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;

import java.util.Objects;
import java.util.function.Function;

Expand All @@ -36,10 +38,9 @@ public BatchInsert<T> render() {
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
FieldAndValueCollector<T> collector = model.mapColumnMappings(toFieldAndValue(visitor))
.collect(FieldAndValueCollector.collect());

return BatchInsert.withRecords(model.records())
.withTableName(model.table().name())
.withColumnsPhrase(collector.columnsPhrase())
.withValuesPhrase(collector.valuesPhrase())
.withInsertStatement(calculateInsertStatement(collector))
.build();
}

Expand All @@ -50,6 +51,13 @@ private Function<InsertMapping, FieldAndValue> toFieldAndValue(ValuePhraseVisito
private FieldAndValue toFieldAndValue(ValuePhraseVisitor visitor, InsertMapping insertMapping) {
return insertMapping.accept(visitor);
}

private String calculateInsertStatement(FieldAndValueCollector<T> collector) {
return "insert into" //$NON-NLS-1$
+ spaceBefore(model.table().name())
+ spaceBefore(collector.columnsPhrase())
+ spaceBefore(collector.valuesPhrase());
}

public static <T> Builder<T> withBatchInsertModel(BatchInsertModel<T> model) {
return new Builder<T>().withBatchInsertModel(model);
Expand Down
Loading