Skip to content

Implement a General Insert Statement #215

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 16 commits into from
Jul 20, 2020
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

This log will detail notable changes to MyBatis Dynamic SQL. Full details are available on the GitHub milestone pages.

## Release 1.1.5 - Unreleased

GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.5+](https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.5+)

### Added

- Added a general insert statement that does not require a separate record class to hold values for the insert. ([#201](https://github.com/mybatis/mybatis-dynamic-sql/issues/201))

## Release 1.1.4 - November 23, 2019

GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.4+](https://github.com/mybatis/mybatis-dynamic-sql/issues?q=milestone%3A1.1.4+)
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 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,11 +17,13 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

import org.mybatis.dynamic.sql.delete.DeleteDSL;
import org.mybatis.dynamic.sql.delete.DeleteModel;
import org.mybatis.dynamic.sql.insert.BatchInsertDSL;
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
Expand Down Expand Up @@ -133,8 +135,8 @@ static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(Collection<T> record
return MultiRowInsertDSL.insert(records);
}

static InsertSelectDSL.InsertColumnGatherer insertInto(SqlTable table) {
return InsertSelectDSL.insertInto(table);
static InsertIntoNextStep insertInto(SqlTable table) {
return new InsertIntoNextStep(table);
}

static FromGatherer<SelectModel> select(BasicColumn...selectList) {
Expand Down Expand Up @@ -636,4 +638,28 @@ static IsNotInCaseInsensitiveWhenPresent isNotInCaseInsensitiveWhenPresent(Colle
static SortSpecification sortColumn(String name) {
return SimpleSortSpecification.of(name);
}

class InsertIntoNextStep {

private SqlTable table;

private InsertIntoNextStep(SqlTable table) {
this.table = Objects.requireNonNull(table);
}

public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
return InsertSelectDSL.insertInto(table)
.withSelectStatement(selectModelBuilder);
}

public InsertSelectDSL.SelectGatherer withColumnList(SqlColumn<?>...columns) {
return InsertSelectDSL.insertInto(table)
.withColumnList(columns);
}

public <T> GeneralInsertDSL.SetClauseFinisher<T> set(SqlColumn<T> column) {
return GeneralInsertDSL.insertInto(table)
.set(column);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@
import java.util.stream.Stream;

import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.util.InsertMapping;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;

public abstract class AbstractMultiRowInsertModel<T> {
private SqlTable table;
private List<T> records;
private List<InsertMapping> columnMappings;
private List<AbstractColumnMapping> columnMappings;

protected AbstractMultiRowInsertModel(AbstractBuilder<T, ?> builder) {
table = Objects.requireNonNull(builder.table);
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
columnMappings = Objects.requireNonNull(builder.columnMappings);
}

public <R> Stream<R> mapColumnMappings(Function<InsertMapping, R> mapper) {
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
return columnMappings.stream().map(mapper);
}

Expand All @@ -56,7 +56,7 @@ public int recordCount() {
public abstract static class AbstractBuilder<T, S extends AbstractBuilder<T, S>> {
private SqlTable table;
private List<T> records = new ArrayList<>();
private List<InsertMapping> columnMappings = new ArrayList<>();
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();

public S withTable(SqlTable table) {
this.table = table;
Expand All @@ -68,7 +68,7 @@ public S withRecords(Collection<T> records) {
return getThis();
}

public S withColumnMappings(List<InsertMapping> columnMappings) {
public S withColumnMappings(List<AbstractColumnMapping> columnMappings) {
this.columnMappings.addAll(columnMappings);
return getThis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.InsertMapping;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;
Expand All @@ -32,7 +32,7 @@ public class BatchInsertDSL<T> {

private Collection<T> records;
private SqlTable table;
private List<InsertMapping> columnMappings = new ArrayList<>();
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();

private BatchInsertDSL(Collection<T> records, SqlTable table) {
this.records = records;
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/insert/GeneralInsertDSL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Copyright 2016-2020 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.insert;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;
import org.mybatis.dynamic.sql.util.ValueMapping;

public class GeneralInsertDSL {
private List<AbstractColumnMapping> insertMappings = new ArrayList<>();
private SqlTable table;

private GeneralInsertDSL(SqlTable table) {
this.table = Objects.requireNonNull(table);
}

public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
return new SetClauseFinisher<>(column);
}

public GeneralInsertModel build() {
return new GeneralInsertModel.Builder()
.withTable(table)
.withInsertMappings(insertMappings)
.build();
}

public static GeneralInsertDSL insertInto(SqlTable table) {
return new GeneralInsertDSL(table);
}

public class SetClauseFinisher<T> {

private SqlColumn<T> column;

public SetClauseFinisher(SqlColumn<T> column) {
this.column = column;
}

public GeneralInsertDSL toNull() {
insertMappings.add(NullMapping.of(column));
return GeneralInsertDSL.this;
}

public GeneralInsertDSL toConstant(String constant) {
insertMappings.add(ConstantMapping.of(column, constant));
return GeneralInsertDSL.this;
}

public GeneralInsertDSL toStringConstant(String constant) {
insertMappings.add(StringConstantMapping.of(column, constant));
return GeneralInsertDSL.this;
}

public GeneralInsertDSL toValue(T value) {
return toValue(() -> value);
}

public GeneralInsertDSL toValue(Supplier<T> valueSupplier) {
insertMappings.add(ValueMapping.of(column, valueSupplier));
return GeneralInsertDSL.this;
}

public GeneralInsertDSL toValueWhenPresent(T value) {
return toValueWhenPresent(() -> value);
}

public GeneralInsertDSL toValueWhenPresent(Supplier<T> valueSupplier) {
if (valueSupplier.get() != null) {
insertMappings.add(ValueMapping.of(column, valueSupplier));
}
return GeneralInsertDSL.this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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.
* 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.insert;

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.SqlTable;
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;

public class GeneralInsertModel {

private SqlTable table;
private List<AbstractColumnMapping> insertMappings;

private GeneralInsertModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
insertMappings = builder.insertMappings;
}

public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
return insertMappings.stream().map(mapper);
}

public SqlTable table() {
return table;
}

public GeneralInsertStatementProvider render(RenderingStrategy renderingStrategy) {
return GeneralInsertRenderer.withInsertModel(this)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

public static class Builder {
private SqlTable table;
private List<AbstractColumnMapping> insertMappings = new ArrayList<>();

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

public Builder withInsertMappings(List<AbstractColumnMapping> insertMappings) {
this.insertMappings.addAll(insertMappings);
return this;
}

public GeneralInsertModel build() {
return new GeneralInsertModel(this);
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/insert/InsertDSL.java
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 @@ -21,8 +21,8 @@

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.InsertMapping;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;
Expand All @@ -31,7 +31,7 @@ public class InsertDSL<T> {

private T record;
private SqlTable table;
private List<InsertMapping> columnMappings = new ArrayList<>();
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();

private InsertDSL(T record, SqlTable table) {
this.record = record;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/mybatis/dynamic/sql/insert/InsertModel.java
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 @@ -25,20 +25,20 @@
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.InsertMapping;
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;

public class InsertModel<T> {
private SqlTable table;
private T record;
private List<InsertMapping> columnMappings;
private List<AbstractColumnMapping> columnMappings;

private InsertModel(Builder<T> builder) {
table = Objects.requireNonNull(builder.table);
record = Objects.requireNonNull(builder.record);
columnMappings = Objects.requireNonNull(builder.columnMappings);
}

public <R> Stream<R> mapColumnMappings(Function<InsertMapping, R> mapper) {
public <R> Stream<R> mapColumnMappings(Function<AbstractColumnMapping, R> mapper) {
return columnMappings.stream().map(mapper);
}

Expand All @@ -64,7 +64,7 @@ public static <T> Builder<T> withRecord(T record) {
public static class Builder<T> {
private SqlTable table;
private T record;
private List<InsertMapping> columnMappings = new ArrayList<>();
private List<AbstractColumnMapping> columnMappings = new ArrayList<>();

public Builder<T> withTable(SqlTable table) {
this.table = table;
Expand All @@ -76,7 +76,7 @@ public Builder<T> withRecord(T record) {
return this;
}

public Builder<T> withColumnMappings(List<InsertMapping> columnMappings) {
public Builder<T> withColumnMappings(List<AbstractColumnMapping> columnMappings) {
this.columnMappings.addAll(columnMappings);
return this;
}
Expand Down
Loading