diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java index 428d2e06672..ffedd815f75 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java @@ -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; @@ -1131,11 +1133,13 @@ public Page findConfigInfoByApp(final int pageNo, final int pageSize final String appName) { String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; PaginationHelper 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 @@ -1144,9 +1148,11 @@ public Page 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 paramList = new ArrayList(); paramList.add(tenantTmp); if (StringUtils.isNotBlank(configTags)) { @@ -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"); @@ -1321,8 +1327,8 @@ public Page 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 helper = createPaginationHelper(); return helper.fetchPageLimit(sqlCountRows, sqlFetchRows, @@ -1335,8 +1341,8 @@ public Page findAllConfigInfo(final int pageNo, final int pageSize, public Page 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; @@ -1496,15 +1502,15 @@ public Page findConfigInfoLike(final int pageNo, final int pageSize, List params = new ArrayList(); 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)) { @@ -1512,7 +1518,7 @@ public Page findConfigInfoLike(final int pageNo, final int pageSize, 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 helper = createPaginationHelper(); @@ -1555,7 +1561,7 @@ public Page 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; } @@ -1563,7 +1569,7 @@ public Page findConfigInfoLike(final int pageNo, final int pageSize, 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; } @@ -1586,7 +1592,7 @@ public Page 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; } @@ -1594,7 +1600,7 @@ public Page findConfigInfoLike(final int pageNo, final int pageSize, 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; } @@ -1634,13 +1640,13 @@ public Page 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)) { @@ -1648,7 +1654,7 @@ public Page findConfigInfoLike4Page(final int pageNo, final int page 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)); } @@ -1663,13 +1669,13 @@ public Page 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)) { @@ -1677,7 +1683,7 @@ public Page findConfigInfoLike4Page(final int pageNo, final int page 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)); } } @@ -1700,15 +1706,15 @@ public Page findConfigInfoBaseLike(final int pageNo, final int p List params = new ArrayList(); 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 helper = createPaginationHelper(); @@ -1789,7 +1795,7 @@ public Page 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; } @@ -1797,7 +1803,7 @@ public Page findConfigInfoAggrLike(final int pageNo, final int p 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; } @@ -1820,7 +1826,7 @@ public Page 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; } @@ -1828,7 +1834,7 @@ public Page findConfigInfoAggrLike(final int pageNo, final int p 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; } @@ -1886,11 +1892,11 @@ public Page findChangeConfig(final String dataId, final Strin List params = new ArrayList(); 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)); } @@ -2348,6 +2354,10 @@ public List 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)) { @@ -2440,7 +2450,7 @@ public List 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)) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java index b16ce8fd46f..579ff9f72d1 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java @@ -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; @@ -2583,6 +2583,10 @@ public List 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)) {