Skip to content
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

Add another way to like placeholder #2900

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,7 +34,7 @@
*/
public class SqlSourceBuilder extends BaseBuilder {

private static final String PARAMETER_PROPERTIES = "javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName";
private static final String PARAMETER_PROPERTIES = "javaType,jdbcType,mode,numericScale,resultMap,typeHandler,jdbcTypeName,like";

public SqlSourceBuilder(Configuration configuration) {
super(configuration);
Expand Down Expand Up @@ -131,6 +131,8 @@ private ParameterMapping buildParameterMapping(String content) {
typeHandlerAlias = value;
} else if ("jdbcTypeName".equals(name)) {
builder.jdbcTypeName(value);
} else if ("like".equals(name)) {
builder.like(value);
} else if ("property".equals(name)) {
// Do Nothing
} else if ("expression".equals(name)) {
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/org/apache/ibatis/mapping/ParameterMapping.java
Expand Up @@ -16,6 +16,7 @@
package org.apache.ibatis.mapping;

import java.sql.ResultSet;
import java.util.Optional;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.JdbcType;
Expand All @@ -37,11 +38,42 @@ public class ParameterMapping {
private TypeHandler<?> typeHandler;
private String resultMapId;
private String jdbcTypeName;
private LikeEnum like;
private String expression;

private ParameterMapping() {
}

public enum LikeEnum {

NONE,

LEFT {
@Override
public String format(String source) {
return "%" + source;
}
},

RIGHT {
@Override
public String format(String source) {
return source + "%";
}
},

ALL {
@Override
public String format(String source) {
return RIGHT.format(LEFT.format(source));
}
};

public String format(String source) {
return source;
}
}

public static class Builder {
private final ParameterMapping parameterMapping = new ParameterMapping();

Expand Down Expand Up @@ -94,6 +126,18 @@ public Builder jdbcTypeName(String jdbcTypeName) {
return this;
}

public Builder like(String likeMode) {
LikeEnum likeEnum = null;
for (LikeEnum value : LikeEnum.values()) {
if (value.name().equalsIgnoreCase(likeMode)) {
likeEnum = value;
break;
}
}
parameterMapping.like = Optional.ofNullable(likeEnum).orElse(LikeEnum.NONE);
return this;
}

public Builder expression(String expression) {
parameterMapping.expression = expression;
return this;
Expand Down Expand Up @@ -196,6 +240,15 @@ public String getJdbcTypeName() {
return jdbcTypeName;
}

/**
* Used for handling concat parameter with %.
*
* @return like pattern
*/
public LikeEnum getLike() {
return like;
}

/**
* Expression 'Not used'.
*
Expand All @@ -217,6 +270,7 @@ public String toString() {
// sb.append(", typeHandler=").append(typeHandler); // typeHandler also doesn't have a useful .toString()
sb.append(", resultMapId='").append(resultMapId).append('\'');
sb.append(", jdbcTypeName='").append(jdbcTypeName).append('\'');
sb.append(", like='").append(like).append('\'');
sb.append(", expression='").append(expression).append('\'');
sb.append('}');
return sb.toString();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-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.
Expand Down Expand Up @@ -83,6 +83,9 @@ public void setParameters(PreparedStatement ps) {
if (value == null && jdbcType == null) {
jdbcType = configuration.getJdbcTypeForNull();
}
if (parameterMapping.getLike() != null && value instanceof String) {
value = parameterMapping.getLike().format((String) value);
}
try {
typeHandler.setParameter(ps, i + 1, value, jdbcType);
} catch (TypeException | SQLException e) {
Expand Down
@@ -0,0 +1,16 @@
package org.apache.ibatis.submitted.select_with_like;

import org.apache.ibatis.annotations.Insert;

interface Mapper {

@Insert({ "<script>", "select * from test where name like #{name, like = RIGHT}", "</script>" })
void selectWithLikeRight(String name);

@Insert({ "<script>", "select * from test where name like #{name}", "</script>" })
void selectWithNoLike(String name);

@Insert({ "<script>", "select * from test where name like #{name, like = placeHolder}", "</script>" })
void selectWithOodLike(String name);

}
@@ -0,0 +1,77 @@
package org.apache.ibatis.submitted.select_with_like;


import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.session.Configuration;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;

public class SelectWithLikeTest {


private static Configuration configuration;

@BeforeAll
static void setUp() {
configuration = new Configuration();
configuration.addMapper(Mapper.class);
}


@Test
void testSelectWithLikeLeft() {
Object parameterObject = "jack";
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithLikeRight");
PreparedStatement preparedStatement = (PreparedStatement) Proxy.newProxyInstance(
SelectWithLikeTest.class.getClassLoader(), new Class[] { PreparedStatement.class }, (proxy, method, args) -> {
if (method.getName().equals("setString")) {
Assertions.assertEquals(args[1], parameterObject + "%");
}
return null;
});

BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler.setParameters(preparedStatement);
}

@Test
void testSelectWithNoLike() {
Object parameterObject = "jack";
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithNoLike");
PreparedStatement preparedStatement = (PreparedStatement) Proxy.newProxyInstance(
SelectWithLikeTest.class.getClassLoader(), new Class[] { PreparedStatement.class }, (proxy, method, args) -> {
if (method.getName().equals("setString")) {
Assertions.assertEquals(args[1], parameterObject);
}
return null;
});

BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler.setParameters(preparedStatement);
}

@Test
void testSelectWithOodLike() {
Object parameterObject = "jack";
MappedStatement mappedStatement = configuration.getMappedStatement("selectWithOodLike");
PreparedStatement preparedStatement = (PreparedStatement) Proxy.newProxyInstance(
SelectWithLikeTest.class.getClassLoader(), new Class[] { PreparedStatement.class }, (proxy, method, args) -> {
if (method.getName().equals("setString")) {
Assertions.assertEquals(args[1], parameterObject);
}
return null;
});

BoundSql boundSql = mappedStatement.getBoundSql(parameterObject);
DefaultParameterHandler parameterHandler = new DefaultParameterHandler(mappedStatement, parameterObject, boundSql);
parameterHandler.setParameters(preparedStatement);
}
}