Skip to content

Commit f20bead

Browse files
committed
Make a separate visitor for the multi-row inserts
1 parent 118fa3a commit f20bead

File tree

5 files changed

+75
-14
lines changed

5 files changed

+75
-14
lines changed

src/main/java/org/mybatis/dynamic/sql/insert/render/BatchInsertRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ private BatchInsertRenderer(Builder<T> builder) {
3333
}
3434

3535
public BatchInsert<T> render() {
36-
ValuePhraseVisitor visitor = new ValuePhraseVisitor(renderingStrategy);
36+
MultiRowValuePhraseVisitor visitor = new MultiRowValuePhraseVisitor(renderingStrategy, "record"); //$NON-NLS-1$)
3737
FieldAndValueCollector collector = model.mapColumnMappings(MultiRowRenderingUtilities.toFieldAndValue(visitor))
3838
.collect(FieldAndValueCollector.collect());
3939

src/main/java/org/mybatis/dynamic/sql/insert/render/MultiRowInsertRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ private MultiRowInsertRenderer(Builder<T> builder) {
3333
}
3434

3535
public MultiRowInsertStatementProvider<T> render() {
36-
ValuePhraseVisitor visitor = new MultiRowValuePhraseVisitor(renderingStrategy);
36+
MultiRowValuePhraseVisitor visitor = new MultiRowValuePhraseVisitor(renderingStrategy, "records[%s]"); //$NON-NLS-1$
3737
FieldAndValueCollector collector = model.mapColumnMappings(MultiRowRenderingUtilities.toFieldAndValue(visitor))
3838
.collect(FieldAndValueCollector.collect());
3939

src/main/java/org/mybatis/dynamic/sql/insert/render/MultiRowRenderingUtilities.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 the original author or authors.
2+
* Copyright 2016-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,11 +23,11 @@ public class MultiRowRenderingUtilities {
2323

2424
private MultiRowRenderingUtilities() {}
2525

26-
public static Function<AbstractColumnMapping, FieldAndValue> toFieldAndValue(ValuePhraseVisitor visitor) {
26+
public static Function<AbstractColumnMapping, FieldAndValue> toFieldAndValue(MultiRowValuePhraseVisitor visitor) {
2727
return insertMapping -> MultiRowRenderingUtilities.toFieldAndValue(visitor, insertMapping);
2828
}
2929

30-
public static FieldAndValue toFieldAndValue(ValuePhraseVisitor visitor, AbstractColumnMapping insertMapping) {
30+
public static FieldAndValue toFieldAndValue(MultiRowValuePhraseVisitor visitor, AbstractColumnMapping insertMapping) {
3131
return insertMapping.accept(visitor);
3232
}
3333
}

src/main/java/org/mybatis/dynamic/sql/insert/render/MultiRowValuePhraseVisitor.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,52 @@
1919

2020
import org.mybatis.dynamic.sql.SqlColumn;
2121
import org.mybatis.dynamic.sql.render.RenderingStrategy;
22+
import org.mybatis.dynamic.sql.util.ConstantMapping;
23+
import org.mybatis.dynamic.sql.util.MultiRowInsertMappingVisitor;
24+
import org.mybatis.dynamic.sql.util.NullMapping;
2225
import org.mybatis.dynamic.sql.util.PropertyMapping;
26+
import org.mybatis.dynamic.sql.util.StringConstantMapping;
2327

24-
public class MultiRowValuePhraseVisitor extends ValuePhraseVisitor {
28+
public class MultiRowValuePhraseVisitor extends MultiRowInsertMappingVisitor<FieldAndValue> {
2529

26-
public MultiRowValuePhraseVisitor(RenderingStrategy renderingStrategy) {
27-
super(renderingStrategy);
30+
private RenderingStrategy renderingStrategy;
31+
private String prefix;
32+
33+
public MultiRowValuePhraseVisitor(RenderingStrategy renderingStrategy, String prefix) {
34+
this.renderingStrategy = renderingStrategy;
35+
this.prefix = prefix;
36+
}
37+
38+
@Override
39+
public FieldAndValue visit(NullMapping mapping) {
40+
return FieldAndValue.withFieldName(mapping.mapColumn(SqlColumn::name))
41+
.withValuePhrase("null") //$NON-NLS-1$
42+
.build();
2843
}
2944

45+
@Override
46+
public FieldAndValue visit(ConstantMapping mapping) {
47+
return FieldAndValue.withFieldName(mapping.mapColumn(SqlColumn::name))
48+
.withValuePhrase(mapping.constant())
49+
.build();
50+
}
51+
52+
@Override
53+
public FieldAndValue visit(StringConstantMapping mapping) {
54+
return FieldAndValue.withFieldName(mapping.mapColumn(SqlColumn::name))
55+
.withValuePhrase("'" + mapping.constant() + "'") //$NON-NLS-1$ //$NON-NLS-2$
56+
.build();
57+
}
58+
3059
@Override
3160
public FieldAndValue visit(PropertyMapping mapping) {
3261
return FieldAndValue.withFieldName(mapping.mapColumn(SqlColumn::name))
33-
.withValuePhrase(mapping.mapColumn(toMultiRowJdbcPlaceholder(mapping.property())))
62+
.withValuePhrase(mapping.mapColumn(toJdbcPlaceholder(mapping.property())))
3463
.build();
3564
}
3665

37-
private Function<SqlColumn<?>, String> toMultiRowJdbcPlaceholder(String parameterName) {
66+
private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
3867
return column -> column.renderingStrategy().orElse(renderingStrategy)
39-
.getFormattedJdbcPlaceholder(column, "records[%s]", //$NON-NLS-1$
40-
parameterName);
68+
.getFormattedJdbcPlaceholder(column, prefix, parameterName);
4169
}
4270
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util;
17+
18+
public abstract class MultiRowInsertMappingVisitor<T> implements ColumnMappingVisitor<T> {
19+
@Override
20+
public final <R> T visit(ValueMapping<R> mapping) {
21+
throw new UnsupportedOperationException();
22+
}
23+
24+
@Override
25+
public final T visit(SelectMapping mapping) {
26+
throw new UnsupportedOperationException();
27+
}
28+
29+
@Override
30+
public final T visit(ColumnToColumnMapping columnMapping) {
31+
throw new UnsupportedOperationException();
32+
}
33+
}

0 commit comments

Comments
 (0)