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

[ISSUE #7694] Fix fuzzy search with underscore returns wrong results #7695

Open
wants to merge 1 commit into
base: develop
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService {
private static final String RESOURCE_ROLE_ID = "role-id";

private static final String RESOURCE_PERMISSIONS_ID = "permissions_id";

private static final String SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE = " ESCAPE '\\' ";

private DataSourceService dataSourceService;

Expand Down Expand Up @@ -1131,11 +1133,13 @@ public Page<ConfigInfo> findConfigInfoByApp(final int pageNo, final int pageSize
final String appName) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
PaginationHelper<ConfigInfo> helper = createPaginationHelper();
return helper.fetchPage("SELECT count(*) FROM config_info WHERE tenant_id LIKE ? AND app_name=?",
"SELECT ID,data_id,group_id,tenant_id,app_name,content FROM config_info WHERE tenant_id LIKE ? AND "
+ "app_name=?", new Object[] {generateLikeArgument(tenantTmp), appName}, pageNo, pageSize,
return helper.fetchPage("SELECT count(*) FROM config_info WHERE tenant_id LIKE ? "
+ SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE + "AND app_name=?",
"SELECT ID,data_id,group_id,tenant_id,app_name,content FROM config_info "
+ "WHERE tenant_id LIKE ?" + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE
+ " AND app_name=?", new Object[] {generateLikeArgument(tenantTmp), appName}, pageNo, pageSize,
CONFIG_INFO_ROW_MAPPER);

}

@Override
Expand All @@ -1144,9 +1148,11 @@ public Page<ConfigInfo> findConfigInfoByAdvance(final int pageNo, final int page
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
StringBuilder sqlCount = new StringBuilder("SELECT count(*) FROM config_info WHERE tenant_id LIKE ? ");
StringBuilder sqlCount = new StringBuilder("SELECT count(*) FROM config_info WHERE tenant_id LIKE ? "
+ SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
StringBuilder sql = new StringBuilder(
"SELECT id,data_id,group_id,tenant_id,app_name,content FROM config_info where tenant_id LIKE ? ");
"SELECT id,data_id,group_id,tenant_id,app_name,content FROM config_info where tenant_id LIKE ? "
+ SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
List<String> paramList = new ArrayList<String>();
paramList.add(tenantTmp);
if (StringUtils.isNotBlank(configTags)) {
Expand Down Expand Up @@ -1210,7 +1216,7 @@ public int configInfoCount() {

@Override
public int configInfoCount(String tenant) {
String sql = " SELECT count(*) FROM config_info WHERE tenant_id LIKE ?";
String sql = " SELECT count(*) FROM config_info WHERE tenant_id LIKE ?" + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
Integer result = databaseOperate.queryOne(sql, new Object[] {tenant}, Integer.class);
if (result == null) {
throw new IllegalArgumentException("configInfoCount error");
Expand Down Expand Up @@ -1321,8 +1327,8 @@ public Page<ConfigInfo> findAllConfigInfo(final int pageNo, final int pageSize,
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
String sqlCountRows = "SELECT count(*) FROM config_info";
String sqlFetchRows = " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 "
+ " FROM ( SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT ?,? )"
+ " g, config_info t WHERE g.id = t.id ";
+ " FROM ( SELECT id FROM config_info WHERE tenant_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE
+ "ORDER BY id LIMIT ?,? ) g, config_info t WHERE g.id = t.id ";

PaginationHelper<ConfigInfo> helper = createPaginationHelper();
return helper.fetchPageLimit(sqlCountRows, sqlFetchRows,
Expand All @@ -1335,8 +1341,8 @@ public Page<ConfigInfo> findAllConfigInfo(final int pageNo, final int pageSize,
public Page<ConfigKey> findAllConfigKey(final int pageNo, final int pageSize, final String tenant) {
final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
final String select = " SELECT data_id,group_id,app_name FROM "
+ " ( SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT ?, ? ) "
+ "g, config_info t WHERE g.id = t.id ";
+ " ( SELECT id FROM config_info WHERE tenant_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE
+ "ORDER BY id LIMIT ?, ? ) g, config_info t WHERE g.id = t.id ";

final int totalCount = configInfoCount(tenant);
int pageCount = totalCount / pageSize;
Expand Down Expand Up @@ -1496,23 +1502,23 @@ public Page<ConfigInfo> findConfigInfoLike(final int pageNo, final int pageSize,
List<String> params = new ArrayList<String>();

if (!StringUtils.isBlank(dataId)) {
where += " AND data_id LIKE ? ";
where += " AND data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where += " AND group_id LIKE ? ";
where += " AND group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(group));
}

where += " AND tenant_id LIKE ? ";
where += " AND tenant_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(tenantTmp));

if (!StringUtils.isBlank(appName)) {
where += " AND app_name = ? ";
params.add(appName);
}
if (!StringUtils.isBlank(content)) {
where += " AND content LIKE ? ";
where += " AND content LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(content));
}
PaginationHelper<ConfigInfo> helper = createPaginationHelper();
Expand Down Expand Up @@ -1555,15 +1561,15 @@ public Page<ConfigInfo> findConfigInfoLike(final int pageNo, final int pageSize,
where.append('(');
boolean isFirstSub = true;
if (!StringUtils.isBlank(dataId)) {
where.append(" data_id NOT LIKE ? ");
where.append(" data_id NOT LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(dataId));
isFirstSub = false;
}
if (!StringUtils.isBlank(group)) {
if (!isFirstSub) {
where.append(" OR ");
}
where.append(" group_id NOT LIKE ? ");
where.append(" group_id NOT LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(group));
isFirstSub = false;
}
Expand All @@ -1586,15 +1592,15 @@ public Page<ConfigInfo> findConfigInfoLike(final int pageNo, final int pageSize,
where.append('(');
boolean isFirstSub = true;
if (!StringUtils.isBlank(dataId)) {
where.append(" data_id LIKE ? ");
where.append(" data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(dataId));
isFirstSub = false;
}
if (!StringUtils.isBlank(group)) {
if (!isFirstSub) {
where.append(" AND ");
}
where.append(" group_id LIKE ? ");
where.append(" group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(group));
isFirstSub = false;
}
Expand Down Expand Up @@ -1634,21 +1640,21 @@ public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int page
"SELECT a.ID,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info a LEFT JOIN "
+ "config_tags_relation b ON a.id=b.id ";

where.append(" a.tenant_id LIKE ? ");
where.append(" a.tenant_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
if (!StringUtils.isBlank(dataId)) {
where.append(" AND a.data_id LIKE ? ");
where.append(" AND a.data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where.append(" AND a.group_id LIKE ? ");
where.append(" AND a.group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(group));
}
if (!StringUtils.isBlank(appName)) {
where.append(" AND a.app_name = ? ");
params.add(appName);
}
if (!StringUtils.isBlank(content)) {
where.append(" AND a.content LIKE ? ");
where.append(" AND a.content LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(content));
}

Expand All @@ -1663,21 +1669,21 @@ public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int page
}
where.append(") ");
} else {
where.append(" tenant_id LIKE ? ");
where.append(" tenant_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
if (!StringUtils.isBlank(dataId)) {
where.append(" AND data_id LIKE ? ");
where.append(" AND data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where.append(" AND group_id LIKE ? ");
where.append(" AND group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(group));
}
if (!StringUtils.isBlank(appName)) {
where.append(" AND app_name = ? ");
params.add(appName);
}
if (!StringUtils.isBlank(content)) {
where.append(" AND content LIKE ? ");
where.append(" AND content LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(content));
}
}
Expand All @@ -1700,15 +1706,15 @@ public Page<ConfigInfoBase> findConfigInfoBaseLike(final int pageNo, final int p
List<String> params = new ArrayList<String>();

if (!StringUtils.isBlank(dataId)) {
where += " AND data_id LIKE ? ";
where += " AND data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where += " AND group_id LIKE ? ";
where += " AND group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(group));
}
if (!StringUtils.isBlank(content)) {
where += " AND content LIKE ? ";
where += " AND content LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(content));
}
PaginationHelper<ConfigInfoBase> helper = createPaginationHelper();
Expand Down Expand Up @@ -1789,15 +1795,15 @@ public Page<ConfigInfoAggr> findConfigInfoAggrLike(final int pageNo, final int p
where.append('(');
boolean isFirstSub = true;
if (!StringUtils.isBlank(dataId)) {
where.append(" data_id NOT LIKE ? ");
where.append(" data_id NOT LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(dataId));
isFirstSub = false;
}
if (!StringUtils.isBlank(group)) {
if (!isFirstSub) {
where.append(" OR ");
}
where.append(" group_id NOT LIKE ? ");
where.append(" group_id NOT LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(group));
isFirstSub = false;
}
Expand All @@ -1820,15 +1826,15 @@ public Page<ConfigInfoAggr> findConfigInfoAggrLike(final int pageNo, final int p
where.append('(');
boolean isFirstSub = true;
if (!StringUtils.isBlank(dataId)) {
where.append(" data_id LIKE ? ");
where.append(" data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(dataId));
isFirstSub = false;
}
if (!StringUtils.isBlank(group)) {
if (!isFirstSub) {
where.append(" AND ");
}
where.append(" group_id LIKE ? ");
where.append(" group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
params.add(generateLikeArgument(group));
isFirstSub = false;
}
Expand Down Expand Up @@ -1886,11 +1892,11 @@ public Page<ConfigInfoWrapper> findChangeConfig(final String dataId, final Strin
List<Object> params = new ArrayList<Object>();

if (!StringUtils.isBlank(dataId)) {
where += " AND data_id LIKE ? ";
where += " AND data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(dataId));
}
if (!StringUtils.isBlank(group)) {
where += " AND group_id LIKE ? ";
where += " AND group_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE;
params.add(generateLikeArgument(group));
}

Expand Down Expand Up @@ -2348,6 +2354,10 @@ public List<ConfigInfoWrapper> listGroupKeyMd5ByPage(int pageNo, int pageSize) {

@Override
public String generateLikeArgument(String s) {
String underscore = "_";
if (s.contains(underscore)) {
s = s.replaceAll(underscore, "\\\\_");
}
String fuzzySearchSign = "\\*";
String sqlLikePercentSign = "%";
if (s.contains(PATTERN_STR)) {
Expand Down Expand Up @@ -2440,7 +2450,7 @@ public List<ConfigAllInfo> findAllConfigInfo4Export(final String dataId, final S
where.append(" tenant_id=? ");
paramList.add(tenantTmp);
if (!StringUtils.isBlank(dataId)) {
where.append(" AND data_id LIKE ? ");
where.append(" AND data_id LIKE ? " + SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
paramList.add(generateLikeArgument(dataId));
}
if (StringUtils.isNotBlank(group)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class ExternalStoragePersistServiceImpl implements PersistService {
private static final String SQL_FIND_CONFIG_INFO_BY_IDS = "SELECT ID,data_id,group_id,tenant_id,app_name,content,md5 FROM config_info WHERE ";

private static final String SQL_DELETE_CONFIG_INFO_BY_IDS = "DELETE FROM config_info WHERE ";

private static final String PATTERN_STR = "*";

private static final int QUERY_LIMIT_SIZE = 50;
Expand Down Expand Up @@ -2583,6 +2583,10 @@ public List<ConfigInfoWrapper> listGroupKeyMd5ByPage(int pageNo, int pageSize) {

@Override
public String generateLikeArgument(String s) {
String underscore = "_";
if (s.contains(underscore)) {
s = s.replaceAll(underscore, "\\\\_");
}
String fuzzySearchSign = "\\*";
String sqlLikePercentSign = "%";
if (s.contains(PATTERN_STR)) {
Expand Down