Skip to content

Multiple Row Insert Support #117

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 15, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
### Added

- Added support for `count(distinct ...)` [#112](https://github.com/mybatis/mybatis-dynamic-sql/issues/112)
- Added support for multiple row inserts [#116](https://github.com/mybatis/mybatis-dynamic-sql/issues/116)


## Release 1.1.2 - July 5, 2019
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
<version>3.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mybatis.dynamic.sql.insert.BatchInsertDSL;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
Expand Down Expand Up @@ -118,6 +119,15 @@ static <T> BatchInsertDSL.IntoGatherer<T> insert(Collection<T> records) {
return BatchInsertDSL.insert(records);
}

@SafeVarargs
static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(T...records) {
return MultiRowInsertDSL.insert(records);
}

static <T> MultiRowInsertDSL.IntoGatherer<T> insertMultiple(Collection<T> records) {
return MultiRowInsertDSL.insert(records);
}

static InsertSelectDSL.InsertColumnGatherer insertInto(SqlTable table) {
return InsertSelectDSL.insertInto(table);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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.Collection;
import java.util.Collections;
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.util.InsertMapping;

public abstract class AbstractMultiRowInsertModel<T> {
private SqlTable table;
private List<T> records;
private List<InsertMapping> 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) {
return columnMappings.stream().map(mapper);
}

public List<T> records() {
return records;
}

public SqlTable table() {
return table;
}

public int recordCount() {
return records.size();
}

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<>();

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

public S withRecords(Collection<T> records) {
this.records.addAll(records);
return getThis();
}

public S withColumnMappings(List<InsertMapping> columnMappings) {
this.columnMappings.addAll(columnMappings);
return getThis();
}

protected abstract S getThis();
}
}
10 changes: 5 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ private BatchInsertDSL(Collection<T> records, SqlTable table) {
this.table = table;
}

public <F> BatchColumnMappingFinisher<F> map(SqlColumn<F> column) {
return new BatchColumnMappingFinisher<>(column);
public <F> ColumnMappingFinisher<F> map(SqlColumn<F> column) {
return new ColumnMappingFinisher<>(column);
}

public BatchInsertModel<T> build() {
Expand All @@ -52,7 +52,7 @@ public BatchInsertModel<T> build() {

@SafeVarargs
public static <T> IntoGatherer<T> insert(T...records) {
return new IntoGatherer<>(Arrays.asList(records));
return BatchInsertDSL.insert(Arrays.asList(records));
}

public static <T> IntoGatherer<T> insert(Collection<T> records) {
Expand All @@ -71,10 +71,10 @@ public BatchInsertDSL<T> into(SqlTable table) {
}
}

public class BatchColumnMappingFinisher<F> {
public class ColumnMappingFinisher<F> {
private SqlColumn<F> column;

public BatchColumnMappingFinisher(SqlColumn<F> column) {
public ColumnMappingFinisher(SqlColumn<F> column) {
this.column = column;
}

Expand Down
49 changes: 5 additions & 44 deletions src/main/java/org/mybatis/dynamic/sql/insert/BatchInsertModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,18 @@
*/
package org.mybatis.dynamic.sql.insert;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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.BatchInsert;
import org.mybatis.dynamic.sql.insert.render.BatchInsertRenderer;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.util.InsertMapping;

public class BatchInsertModel<T> {
private SqlTable table;
private List<T> records;
private List<InsertMapping> columnMappings;
public class BatchInsertModel<T> extends AbstractMultiRowInsertModel<T> {

private BatchInsertModel(Builder<T> builder) {
table = Objects.requireNonNull(builder.table);
records = Collections.unmodifiableList(Objects.requireNonNull(builder.records));
columnMappings = Objects.requireNonNull(builder.columnMappings);
super(builder);
}

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

public List<T> records() {
return records;
}

public SqlTable table() {
return table;
}

public BatchInsert<T> render(RenderingStrategy renderingStrategy) {
return BatchInsertRenderer.withBatchInsertModel(this)
.withRenderingStrategy(renderingStrategy)
Expand All @@ -63,23 +38,9 @@ public static <T> Builder<T> withRecords(Collection<T> records) {
return new Builder<T>().withRecords(records);
}

public static class Builder<T> {
private SqlTable table;
private List<T> records = new ArrayList<>();
private List<InsertMapping> columnMappings = new ArrayList<>();

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

public Builder<T> withRecords(Collection<T> records) {
this.records.addAll(records);
return this;
}

public Builder<T> withColumnMappings(List<InsertMapping> columnMappings) {
this.columnMappings.addAll(columnMappings);
public static class Builder<T> extends AbstractBuilder<T, Builder<T>> {
@Override
protected Builder<T> getThis() {
return this;
}

Expand Down
101 changes: 101 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/insert/MultiRowInsertDSL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* 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.Arrays;
import java.util.Collection;
import java.util.List;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
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;

public class MultiRowInsertDSL<T> {

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

private MultiRowInsertDSL(Collection<T> records, SqlTable table) {
this.records = records;
this.table = table;
}

public <F> ColumnMappingFinisher<F> map(SqlColumn<F> column) {
return new ColumnMappingFinisher<>(column);
}

public MultiRowInsertModel<T> build() {
return MultiRowInsertModel.withRecords(records)
.withTable(table)
.withColumnMappings(columnMappings)
.build();
}

@SafeVarargs
public static <T> IntoGatherer<T> insert(T...records) {
return MultiRowInsertDSL.insert(Arrays.asList(records));
}

public static <T> IntoGatherer<T> insert(Collection<T> records) {
return new IntoGatherer<>(records);
}

public static class IntoGatherer<T> {
private Collection<T> records;

private IntoGatherer(Collection<T> records) {
this.records = records;
}

public MultiRowInsertDSL<T> into(SqlTable table) {
return new MultiRowInsertDSL<>(records, table);
}
}

public class ColumnMappingFinisher<F> {
private SqlColumn<F> column;

public ColumnMappingFinisher(SqlColumn<F> column) {
this.column = column;
}

public MultiRowInsertDSL<T> toProperty(String property) {
columnMappings.add(PropertyMapping.of(column, property));
return MultiRowInsertDSL.this;
}

public MultiRowInsertDSL<T> toNull() {
columnMappings.add(NullMapping.of(column));
return MultiRowInsertDSL.this;
}

public MultiRowInsertDSL<T> toConstant(String constant) {
columnMappings.add(ConstantMapping.of(column, constant));
return MultiRowInsertDSL.this;
}

public MultiRowInsertDSL<T> toStringConstant(String constant) {
columnMappings.add(StringConstantMapping.of(column, constant));
return MultiRowInsertDSL.this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.Collection;

import org.mybatis.dynamic.sql.insert.render.MultiRowInsertRenderer;
import org.mybatis.dynamic.sql.insert.render.MultiRowInsertStatementProvider;
import org.mybatis.dynamic.sql.render.RenderingStrategy;

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

private MultiRowInsertModel(Builder<T> builder) {
super(builder);
}

public MultiRowInsertStatementProvider<T> render(RenderingStrategy renderingStrategy) {
return MultiRowInsertRenderer.withMultiRowInsertModel(this)
.withRenderingStrategy(renderingStrategy)
.build()
.render();
}

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

public static class Builder<T> extends AbstractBuilder<T, Builder<T>> {
@Override
protected Builder<T> getThis() {
return this;
}

public MultiRowInsertModel<T> build() {
return new MultiRowInsertModel<>(this);
}
}
}
Loading