Skip to content

Add Map to Row Function for Insert Statements #612

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 10 commits into from
Apr 20, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ The pull request for this change is ([#591](https://github.com/mybatis/mybatis-d
5. Add `SqlBuilder.concat` and the equivalent in Kotlin. This is a concatenate function that works on more databases.
([#573](https://github.com/mybatis/mybatis-dynamic-sql/pull/573))
6. Several classes and methods in the Kotlin DSL are deprecated in response to the new "having" support
7. Added support for inserting a list of simple classes like Integers, Strings, etc. This is via a new "map to row"
function on the insert, batch insert, and multirow insert statements. ([#612](https://github.com/mybatis/mybatis-dynamic-sql/pull/612))

## Release 1.4.1 - October 7, 2022

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.RowMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;

public class BatchInsertDSL<T> implements Buildable<BatchInsertModel<T>> {
Expand Down Expand Up @@ -103,6 +104,11 @@ public BatchInsertDSL<T> toStringConstant(String constant) {
columnMappings.add(StringConstantMapping.of(column, constant));
return BatchInsertDSL.this;
}

public BatchInsertDSL<T> toRow() {
columnMappings.add(RowMapping.of(column));
return BatchInsertDSL.this;
}
}

public abstract static class AbstractBuilder<T, B extends AbstractBuilder<T, B>> {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/insert/InsertDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping;
import org.mybatis.dynamic.sql.util.RowMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;

public class InsertDSL<T> implements Buildable<InsertModel<T>> {
Expand Down Expand Up @@ -104,6 +105,11 @@ public InsertDSL<T> toStringConstant(String constant) {
columnMappings.add(StringConstantMapping.of(column, constant));
return InsertDSL.this;
}

public InsertDSL<T> toRow() {
columnMappings.add(RowMapping.of(column));
return InsertDSL.this;
}
}

public static class Builder<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.RowMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;

public class MultiRowInsertDSL<T> implements Buildable<MultiRowInsertModel<T>> {
Expand Down Expand Up @@ -102,6 +103,11 @@ public MultiRowInsertDSL<T> toStringConstant(String constant) {
columnMappings.add(StringConstantMapping.of(column, constant));
return MultiRowInsertDSL.this;
}

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

public static class Builder<T> extends BatchInsertDSL.AbstractBuilder<T, Builder<T>> {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private BatchInsertRenderer(Builder<T> builder) {
}

public BatchInsert<T> render() {
BatchValuePhraseVisitor visitor = new BatchValuePhraseVisitor(renderingStrategy, "row"); //$NON-NLS-1$)
MultiRowValuePhraseVisitor visitor = new MultiRowValuePhraseVisitor(renderingStrategy, "row"); //$NON-NLS-1$)
FieldAndValueCollector collector = model.mapColumnMappings(m -> m.accept(visitor))
.collect(FieldAndValueCollector.collect());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,65 @@
*/
package org.mybatis.dynamic.sql.insert.render;

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.MultiRowInsertMappingVisitor;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.RowMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;

public class MultiRowValuePhraseVisitor extends AbstractMultiRowValuePhraseVisitor {
public class MultiRowValuePhraseVisitor extends MultiRowInsertMappingVisitor<FieldAndValueAndParameters> {
protected final RenderingStrategy renderingStrategy;
protected final String prefix;

public MultiRowValuePhraseVisitor(RenderingStrategy renderingStrategy, String prefix) {
super(renderingStrategy, prefix);
protected MultiRowValuePhraseVisitor(RenderingStrategy renderingStrategy, String prefix) {
this.renderingStrategy = renderingStrategy;
this.prefix = prefix;
}

@Override
public FieldAndValueAndParameters visit(NullMapping mapping) {
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
.withValuePhrase("null") //$NON-NLS-1$
.build();
}

@Override
public FieldAndValueAndParameters visit(ConstantMapping mapping) {
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
.withValuePhrase(mapping.constant())
.build();
}

@Override
public FieldAndValueAndParameters visit(StringConstantMapping mapping) {
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
.withValuePhrase("'" + mapping.constant() + "'") //$NON-NLS-1$ //$NON-NLS-2$
.build();
}

@Override
public FieldAndValueAndParameters visit(PropertyMapping mapping) {
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
.withValuePhrase(mapping.mapColumn(c -> calculateJdbcPlaceholder(c, mapping.property())))
.build();
}

@Override
public FieldAndValueAndParameters visit(RowMapping mapping) {
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
.withValuePhrase(mapping.mapColumn(this::calculateJdbcPlaceholder))
.build();
}

private String calculateJdbcPlaceholder(SqlColumn<?> column) {
return column.renderingStrategy().orElse(renderingStrategy).getRecordBasedInsertBinding(column, prefix);
}

private String calculateJdbcPlaceholder(SqlColumn<?> column, String parameterName) {
return column.renderingStrategy().orElse(renderingStrategy)
.getRecordBasedInsertBinding(column, prefix, parameterName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.PropertyMapping;
import org.mybatis.dynamic.sql.util.PropertyWhenPresentMapping;
import org.mybatis.dynamic.sql.util.RowMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;

public class ValuePhraseVisitor extends InsertMappingVisitor<Optional<FieldAndValueAndParameters>> {
Expand Down Expand Up @@ -71,6 +72,18 @@ public Optional<FieldAndValueAndParameters> visit(PropertyWhenPresentMapping map
}
}

@Override
public Optional<FieldAndValueAndParameters> visit(RowMapping mapping) {
return FieldAndValueAndParameters.withFieldName(mapping.columnName())
.withValuePhrase(mapping.mapColumn(this::calculateJdbcPlaceholder))
.buildOptional();
}

private String calculateJdbcPlaceholder(SqlColumn<?> column) {
return column.renderingStrategy().orElse(renderingStrategy)
.getRecordBasedInsertBinding(column, "row"); //$NON-NLS-1$
}

private String calculateJdbcPlaceholder(SqlColumn<?> column, String parameterName) {
return column.renderingStrategy().orElse(renderingStrategy)
.getRecordBasedInsertBinding(column, "row", parameterName); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefi
+ "}"; //$NON-NLS-1$
}

@Override
public String getRecordBasedInsertBinding(BindableColumn<?> column, String parameterName) {
return "#{" //$NON-NLS-1$
+ parameterName
+ renderJdbcType(column)
+ renderJavaType(column)
+ renderTypeHandler(column)
+ "}"; //$NON-NLS-1$
}

private String renderTypeHandler(BindableColumn<?> column) {
return column.typeHandler()
.map(th -> ",typeHandler=" + th) //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,29 @@ public String formatParameterMapKey(AtomicInteger sequence) {
*
* @param column column definition used for generating type details in a MyBatis binding. Ignored for Spring.
* @param prefix parameter prefix used for locating the parameters in a SQL provider object. Typically, will be
* {@link RenderingStrategy#DEFAULT_PARAMETER_PREFIX}. This is ignored for Spring.
* @param parameterName name of the parameter. Typically generated by calling
* {@link RenderingStrategy#formatParameterMapKey(AtomicInteger)}
* either "row" or "records[x]" to match the properties of the generated statement object class.
* @param parameterName name of the parameter. Typically, this is a property in the row class associated with the
* insert statement.
* @return the generated binding
*/
public String getRecordBasedInsertBinding(BindableColumn<?> column, String prefix, String parameterName) {
return getFormattedJdbcPlaceholder(column, prefix, parameterName);
}

/**
* This method generates a binding for a parameter to a placeholder in a record based insert statement.
*
* <p>This binding is specifically for use with insert, batch insert, and multirow insert statements and the
* MapToRow mapping. These statements bind parameters to the row class directly.
*
* <p>For MyBatis, a binding looks like this: "#{parameterName,jdbcType=xxx,typeHandler=xxx,javaType=xxx}"
*
* <p>For Spring, a binding looks like this: ":parameterName"
*
* @param column column definition used for generating type details in a MyBatis binding. Ignored for Spring.
* @param parameterName name of the parameter. Typically, will be
* either "row" or "records[x]" to match the properties of the generated statement object class.
* @return the generated binding
*/
public abstract String getRecordBasedInsertBinding(BindableColumn<?> column, String parameterName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public String getFormattedJdbcPlaceholder(String prefix, String parameterName) {
public String getRecordBasedInsertBinding(BindableColumn<?> column, String prefix, String parameterName) {
return ":" + prefix + "." + parameterName; //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
public String getRecordBasedInsertBinding(BindableColumn<?> column, String parameterName) {
return ":" + parameterName; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ public interface ColumnMappingVisitor<R> {

R visit(PropertyWhenPresentMapping mapping);

R visit(ColumnToColumnMapping columnMapping);
R visit(ColumnToColumnMapping mapping);

R visit(RowMapping mapping);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public final R visit(PropertyWhenPresentMapping mapping) {
public final R visit(ColumnToColumnMapping columnMapping) {
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_4));
}

@Override
public final R visit(RowMapping mapping) {
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_14));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public enum InternalError {
INTERNAL_ERROR_10(10),
INTERNAL_ERROR_11(11),
INTERNAL_ERROR_12(12),
INTERNAL_ERROR_13(13);
INTERNAL_ERROR_13(13),
INTERNAL_ERROR_14(14),
INTERNAL_ERROR_15(15);

private final int number;

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/util/RowMapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2016-2023 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.util;

import org.mybatis.dynamic.sql.SqlColumn;

public class RowMapping extends AbstractColumnMapping {
private RowMapping(SqlColumn<?> column) {
super(column);
}

public static RowMapping of(SqlColumn<?> column) {
return new RowMapping(column);
}

@Override
public <R> R accept(ColumnMappingVisitor<R> visitor) {
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public final R visit(PropertyMapping mapping) {
public final R visit(PropertyWhenPresentMapping mapping) {
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_11));
}

@Override
public final R visit(RowMapping mapping) {
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_15));
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy