连表查询支持逻辑删除

This commit is contained in:
yulichang 2021-11-19 11:07:56 +08:00
parent eb58a2987f
commit 7efad696da
3 changed files with 60 additions and 63 deletions

View File

@ -20,7 +20,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod implements MPJBas
*/ */
@Override @Override
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
return whereEntityWrapper(newLine); return mpjSqlWhereEntityWrapper(newLine, table);
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package com.github.yulichang.method; package com.github.yulichang.method;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
@ -11,16 +12,64 @@ import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
*/ */
public interface MPJBaseMethod extends Constants { public interface MPJBaseMethod extends Constants {
default String whereEntityWrapper(boolean newLine) { default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
String sqlScript = SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT), if (table.isWithLogicDelete()) {
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, String sqlScript = (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE);
WRAPPER_NONEMPTYOFWHERE), true); String normalSqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT),
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE; String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), WRAPPER_NONEMPTYOFNORMAL), true);
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, normalSqlScript += NEWLINE;
WRAPPER_EMPTYOFWHERE), true); normalSqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT),
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER), true); String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
return newLine ? NEWLINE + sqlScript : sqlScript; WRAPPER_EMPTYOFNORMAL), true);
sqlScript += normalSqlScript;
sqlScript = SqlScriptUtils.convertChoose(String.format("%s != null", WRAPPER), sqlScript,
table.getLogicDeleteSql(false, true));
sqlScript = SqlScriptUtils.convertWhere(sqlScript);
return newLine ? NEWLINE + sqlScript : sqlScript;
} else {
String sqlScript = SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_NONEMPTYOFWHERE), true);
sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_EMPTYOFWHERE), true);
sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER), true);
return newLine ? NEWLINE + sqlScript : sqlScript;
}
}
default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) {
if (tableInfo.isWithLogicDelete()) {
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
if (startWithAnd) {
logicDeleteSql = " AND " + logicDeleteSql;
}
return logicDeleteSql;
}
return EMPTY;
}
default String formatLogicDeleteSql(TableInfo tableInfo, boolean isWhere) {
final String value = isWhere ? tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue() :
tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
if (isWhere) {
if (NULL.equalsIgnoreCase(value)) {
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
} else {
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(
tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
}
}
final String targetStr = "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS;
if (NULL.equalsIgnoreCase(value)) {
return targetStr + NULL;
} else {
return targetStr + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
}
} }
} }

View File

@ -20,57 +20,5 @@ public interface TableAlias extends Constants, MPJBaseMethod {
+ SPACE + SqlScriptUtils.convertIf("${ew.from}", + SPACE + SqlScriptUtils.convertIf("${ew.from}",
String.format("%s != null and %s and %s != null and %s != ''", Constant.PARAM_TYPE, Constant.PARAM_TYPE, String.format("%s != null and %s and %s != null and %s != ''", Constant.PARAM_TYPE, Constant.PARAM_TYPE,
"ew.from", "ew.from"), false); "ew.from", "ew.from"), false);
}
default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
if (table.isWithLogicDelete()) {
String sqlScript = (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE);
String normalSqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_NONEMPTYOFNORMAL), true);
normalSqlScript += NEWLINE;
normalSqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT),
String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT,
WRAPPER_EMPTYOFNORMAL), true);
sqlScript += normalSqlScript;
sqlScript = SqlScriptUtils.convertChoose(String.format("%s != null", WRAPPER), sqlScript,
table.getLogicDeleteSql(false, true));
sqlScript = SqlScriptUtils.convertWhere(sqlScript);
return newLine ? NEWLINE + sqlScript : sqlScript;
} else {
return whereEntityWrapper(newLine);
}
}
default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) {
if (tableInfo.isWithLogicDelete()) {
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
if (startWithAnd) {
logicDeleteSql = " AND " + logicDeleteSql;
}
return logicDeleteSql;
}
return EMPTY;
}
default String formatLogicDeleteSql(TableInfo tableInfo, boolean isWhere) {
final String value = isWhere ? tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue() :
tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
if (isWhere) {
if (NULL.equalsIgnoreCase(value)) {
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
} else {
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(
tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
}
}
final String targetStr = "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS;
if (NULL.equalsIgnoreCase(value)) {
return targetStr + NULL;
} else {
return targetStr + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
}
} }
} }