Skip to content

Commit

Permalink
feat: join string sql support param
Browse files Browse the repository at this point in the history
  • Loading branch information
yulichang committed Oct 31, 2024
1 parent ce3548c commit 034c341
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ public <R> Children join(String keyWord, BaseColumn<R> baseColumn, MFunction<Apt
* @param joinSql sql
*/
@Override
public Children join(String keyWord, boolean condition, String joinSql) {
public Children join(String keyWord, boolean condition, String joinSql, Object... objects) {
if (condition) {
Children wrapper = instanceEmpty();
wrapper.from.setStringValue(joinSql);
wrapper.from.setStringValue(formatSqlMaybeWithParam(joinSql, objects));
wrapper.keyWord = keyWord;
onWrappers.add(wrapper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public AptQueryWrapper<T> selectFunc(String sql, MFunction<AptConsumer> column,
if (ArrayUtils.isEmpty(apply.getValues())) {
formatSql = sql;
} else {
formatSql = formatSqlMaybeWithParam(sql, null, apply.getValues());
formatSql = formatSqlMaybeWithParam(sql, apply.getValues());
}
getSelectColum().add(new SelectApt(apply.getColumns(), () -> formatSql, alias));
return typedThis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
Expand Down Expand Up @@ -281,7 +283,7 @@ public Children or(boolean condition) {
@Override
public Children apply(boolean condition, String applySql, Object... values) {
return maybeDo(condition, () -> appendSqlSegments(APPLY,
() -> formatSqlMaybeWithParam(applySql, null, values)));
() -> formatSqlMaybeWithParam(applySql, values)));
}

public Children applyFunc(String applySql, MFunction<AptConsumer> consumerFunction, Object... values) {
Expand All @@ -294,7 +296,7 @@ public Children applyFunc(boolean condition, String applySql,
() -> {
AptConsumer apply = consumerFunction.apply(new AptConsumer());
return formatSqlMaybeWithParam(String.format(applySql,
Arrays.stream(apply.getColumns()).map(this::columnToString).toArray()), null,
Arrays.stream(apply.getColumns()).map(this::columnToString).toArray()),
ArrayUtils.isNotEmpty(values) ? values : apply.getValues());
}));
}
Expand Down Expand Up @@ -333,7 +335,7 @@ public Children around(boolean condition, String firstSql, String lastSql) {
@Override
public Children exists(boolean condition, String existsSql, Object... values) {
return maybeDo(condition, () -> appendSqlSegments(EXISTS,
() -> String.format("(%s)", formatSqlMaybeWithParam(existsSql, null, values))));
() -> String.format("(%s)", formatSqlMaybeWithParam(existsSql, values))));
}

@Override
Expand Down Expand Up @@ -481,7 +483,7 @@ protected Column columnSqlInjectFilter(Column column) {
@Override
public Children having(boolean condition, String sqlHaving, Object... params) {
return maybeDo(condition, () -> appendSqlSegments(HAVING,
() -> formatSqlMaybeWithParam(sqlHaving, null, params)));
() -> formatSqlMaybeWithParam(sqlHaving, params)));
}

@Override
Expand Down Expand Up @@ -573,21 +575,27 @@ protected Children addNestedCondition(boolean condition, Consumer<Children> cons
* <p>
* 支持 "{0}" 这种,或者 "sql {0} sql" 这种
*
* @param sqlStr 可能是sql片段
* @param mapping 例如: "javaType=int,jdbcType=NUMERIC,typeHandler=xxx.xxx.MyTypeHandler" 这种
* @param params 参数
* @param sqlStr 可能是sql片段
* @param params 参数
* @return sql片段
*/
@SuppressWarnings("SameParameterValue")
protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
protected final String formatSqlMaybeWithParam(String sqlStr, Object... params) {
if (StrUtils.isBlank(sqlStr)) {
// todo 何时会这样?
return null;
}
if (ArrayUtils.isNotEmpty(params)) {
for (int i = 0; i < params.length; ++i) {
final String target = Constants.LEFT_BRACE + i + Constants.RIGHT_BRACE;
sqlStr = sqlStr.replace(target, formatParam(mapping, params[i]));
String target = Constants.LEFT_BRACE + i + Constants.RIGHT_BRACE;
if (sqlStr.contains(target)) {
sqlStr = sqlStr.replace(target, formatParam(null, params[i]));
} else {
Matcher matcher = Pattern.compile("[{]" + i + ",[a-zA-Z0-9.,=]+}").matcher(sqlStr);
if (!matcher.find()) {
throw ExceptionUtils.mpe("Please check the syntax correctness! sql not contains: \"%s\"", target);
}
String group = matcher.group();
sqlStr = sqlStr.replace(group, formatParam(group.substring(target.length(), group.length() - 1), params[i]));
}
}
}
return sqlStr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ public Children join(String keyWord, Class<?> clazz, String tableAlias, BiConsum
* @param joinSql sql
*/
@Override
public Children join(String keyWord, boolean condition, String joinSql) {
public Children join(String keyWord, boolean condition, String joinSql,Object... args) {
if (condition) {
Children wrapper = instanceEmpty();
wrapper.from.setStringValue(joinSql);
wrapper.from.setStringValue(formatSqlMaybeWithParam(joinSql,args));
wrapper.keyWord = keyWord;
onWrappers.add(wrapper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;
import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY;
Expand Down Expand Up @@ -304,7 +306,7 @@ public Children or(boolean condition) {
@Override
public Children apply(boolean condition, String applySql, Object... values) {
return maybeDo(condition, () -> appendSqlSegments(APPLY,
() -> formatSqlMaybeWithParam(applySql, null, values)));
() -> formatSqlMaybeWithParam(applySql, values)));
}

public Children applyFunc(String applySql, Function<FuncArgs, SelectFunc.Arg[]> consumerFunction, Object... values) {
Expand All @@ -316,7 +318,8 @@ public Children applyFunc(boolean condition, String applySql,
return maybeDo(condition, () -> appendSqlSegments(APPLY,
() -> formatSqlMaybeWithParam(String.format(applySql,
Arrays.stream(consumerFunction.apply(new FuncArgs())).map(func ->
columnToString(index, null, (KProperty<?>) func.getProperty(), false, PrefixEnum.CD_FIRST)).toArray()), null, values)));
columnToString(index, null, (KProperty<?>) func.getProperty(),
false, PrefixEnum.CD_FIRST)).toArray()), values)));
}

@Override
Expand Down Expand Up @@ -353,7 +356,7 @@ public Children around(boolean condition, String firstSql, String lastSql) {
@Override
public Children exists(boolean condition, String existsSql, Object... values) {
return maybeDo(condition, () -> appendSqlSegments(EXISTS,
() -> String.format("(%s)", formatSqlMaybeWithParam(existsSql, null, values))));
() -> String.format("(%s)", formatSqlMaybeWithParam(existsSql, values))));
}

@Override
Expand Down Expand Up @@ -492,7 +495,7 @@ protected KProperty<?> columnSqlInjectFilter(KProperty<?> column) {
@Override
public Children having(boolean condition, String sqlHaving, Object... params) {
return maybeDo(condition, () -> appendSqlSegments(HAVING,
() -> formatSqlMaybeWithParam(sqlHaving, null, params)));
() -> formatSqlMaybeWithParam(sqlHaving, params)));
}

@Override
Expand Down Expand Up @@ -591,21 +594,28 @@ protected Children addNestedCondition(boolean condition, Consumer<Children> cons
* <p>
* 支持 "{0}" 这种,或者 "sql {0} sql" 这种
*
* @param sqlStr 可能是sql片段
* @param mapping 例如: "javaType=int,jdbcType=NUMERIC,typeHandler=xxx.xxx.MyTypeHandler" 这种
* @param params 参数
* @param sqlStr 可能是sql片段
* @param params 参数
* @return sql片段
*/
@SuppressWarnings("SameParameterValue")
protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
protected final String formatSqlMaybeWithParam(String sqlStr, Object... params) {
if (StrUtils.isBlank(sqlStr)) {
// todo 何时会这样?
return null;
}
if (ArrayUtils.isNotEmpty(params)) {
for (int i = 0; i < params.length; ++i) {
final String target = Constants.LEFT_BRACE + i + Constants.RIGHT_BRACE;
sqlStr = sqlStr.replace(target, formatParam(mapping, params[i]));
String target = Constants.LEFT_BRACE + i + Constants.RIGHT_BRACE;
if (sqlStr.contains(target)) {
sqlStr = sqlStr.replace(target, formatParam(null, params[i]));
} else {
Matcher matcher = Pattern.compile("[{]" + i + ",[a-zA-Z0-9.,=]+}").matcher(sqlStr);
if (!matcher.find()) {
throw ExceptionUtils.mpe("Please check the syntax correctness! sql not contains: \"%s\"", target);
}
String group = matcher.group();
sqlStr = sqlStr.replace(group, formatParam(group.substring(target.length(), group.length() - 1), params[i]));
}
}
}
return sqlStr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public KtLambdaWrapper<T> selectFunc(String sql, MFunction<FuncArgs> column, Str
if (ArrayUtils.isEmpty(apply.getValues())) {
formatSql = sql;
} else {
formatSql = formatSqlMaybeWithParam(sql, null, apply.getValues());
formatSql = formatSqlMaybeWithParam(sql, apply.getValues());
}
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> formatSql, apply.getFuncArg(),
isHasAlias(), getAlias()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -391,10 +393,33 @@ public void clear() {
}

@Override
public MPJLambdaQueryWrapper<T> join(String keyWord, boolean condition, String joinSql) {
public MPJLambdaQueryWrapper<T> join(String keyWord, boolean condition, String joinSql, Object... args) {
if (condition) {
from.setStringValue(from.getStringValue() + keyWord + joinSql);
from.setStringValue(from.getStringValue() + keyWord + mpjFormatSqlMaybeWithParam(joinSql, args));
}
return typedThis;
}

@SuppressWarnings("DuplicatedCode")
protected final String mpjFormatSqlMaybeWithParam(String sqlStr, Object... params) {
if (StrUtils.isBlank(sqlStr)) {
return null;
}
if (ArrayUtils.isNotEmpty(params)) {
for (int i = 0; i < params.length; ++i) {
String target = Constants.LEFT_BRACE + i + Constants.RIGHT_BRACE;
if (sqlStr.contains(target)) {
sqlStr = sqlStr.replace(target, formatParam(null, params[i]));
} else {
Matcher matcher = Pattern.compile("[{]" + i + ",[a-zA-Z0-9.,=]+}").matcher(sqlStr);
if (!matcher.find()) {
throw ExceptionUtils.mpe("Please check the syntax correctness! sql not contains: \"%s\"", target);
}
String group = matcher.group();
sqlStr = sqlStr.replace(group, formatParam(group.substring(target.length(), group.length() - 1), params[i]));
}
}
}
return sqlStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -408,10 +410,34 @@ public void clear() {
}

@Override
public MPJQueryWrapper<T> join(String keyWord, boolean condition, String joinSql) {
public MPJQueryWrapper<T> join(String keyWord, boolean condition, String joinSql, Object... params) {
if (condition) {
from.setStringValue(from.getStringValue() + StringPool.SPACE + keyWord + StringPool.SPACE + joinSql);
from.setStringValue(from.getStringValue() + StringPool.SPACE + keyWord +
StringPool.SPACE + mpjFormatSqlMaybeWithParam(joinSql, params));
}
return typedThis;
}

@SuppressWarnings("DuplicatedCode")
protected final String mpjFormatSqlMaybeWithParam(String sqlStr, Object... params) {
if (StrUtils.isBlank(sqlStr)) {
return null;
}
if (ArrayUtils.isNotEmpty(params)) {
for (int i = 0; i < params.length; ++i) {
String target = Constants.LEFT_BRACE + i + Constants.RIGHT_BRACE;
if (sqlStr.contains(target)) {
sqlStr = sqlStr.replace(target, formatParam(null, params[i]));
} else {
Matcher matcher = Pattern.compile("[{]" + i + ",[a-zA-Z0-9.,=]+}").matcher(sqlStr);
if (!matcher.find()) {
throw ExceptionUtils.mpe("Please check the syntax correctness! sql not contains: \"%s\"", target);
}
String group = matcher.group();
sqlStr = sqlStr.replace(group, formatParam(group.substring(target.length(), group.length() - 1), params[i]));
}
}
}
return sqlStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ default Children leftJoin(String joinSql) {
return leftJoin(true, joinSql);
}

default Children leftJoin(String joinSql, Object... args) {
return leftJoin(true, joinSql, args);
}


default Children leftJoin(boolean condition, String joinSql) {
return join(Constant.LEFT_JOIN, condition, joinSql);
}

default Children leftJoin(boolean condition, String joinSql, Object... args) {
return join(Constant.LEFT_JOIN, condition, joinSql, args);
}


/**
* right join
Expand All @@ -28,10 +37,17 @@ default Children rightJoin(String joinSql) {
return rightJoin(true, joinSql);
}

default Children rightJoin(String joinSql, Object... args) {
return rightJoin(true, joinSql, args);
}

default Children rightJoin(boolean condition, String joinSql) {
return join(Constant.RIGHT_JOIN, condition, joinSql);
}

default Children rightJoin(boolean condition, String joinSql, Object... args) {
return join(Constant.RIGHT_JOIN, condition, joinSql, args);
}

/**
* inner join
Expand All @@ -40,20 +56,42 @@ default Children innerJoin(String joinSql) {
return innerJoin(true, joinSql);
}

default Children innerJoin(String joinSql, Object... args) {
return innerJoin(true, joinSql, args);
}

default Children innerJoin(boolean condition, String joinSql) {
return join(Constant.INNER_JOIN, condition, joinSql);
}

default Children innerJoin(boolean condition, String joinSql, Object... args) {
return join(Constant.INNER_JOIN, condition, joinSql, args);
}

/**
* full join
*/
default Children fullJoin(String joinSql) {
return fullJoin(true, joinSql);
}

default Children fullJoin(String joinSql, Object... args) {
return fullJoin(true, joinSql, args);
}

default Children fullJoin(boolean condition, String joinSql) {
return join(Constant.FULL_JOIN, condition, joinSql);
}

Children join(String keyWord, boolean condition, String joinSql);
default Children fullJoin(boolean condition, String joinSql, Object... args) {
return join(Constant.FULL_JOIN, condition, joinSql, args);
}

default Children join(String keyWord, boolean condition, String joinSql) {
return join(keyWord, condition, joinSql, new Object[0]);
}

default Children join(String keyWord, boolean condition, String joinSql, Object... args){
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ public <R> Children join(String keyWord, Class<R> clazz, MConsumer<MPJLambdaWrap
* @param joinSql sql
*/
@Override
public Children join(String keyWord, boolean condition, String joinSql) {
public Children join(String keyWord, boolean condition, String joinSql, Object... args) {
if (condition) {
Children wrapper = instanceEmpty();
wrapper.from.setStringValue(joinSql);
wrapper.from.setStringValue(formatSqlMaybeWithParam(joinSql, args));
wrapper.keyWord = keyWord;
onWrappers.add(wrapper);
}
Expand Down
Loading

0 comments on commit 034c341

Please sign in to comment.