From f78bef96650480aa5d3052ab46e14c3a1cbb8f51 Mon Sep 17 00:00:00 2001 From: "mai.jh" Date: Fri, 27 Nov 2020 10:10:11 +0800 Subject: [PATCH] [ISSUE-#4333]Add MapRowMapper to RowMapperManager (#4334) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * for #4311,Fixed Derby data source related SQL LIMIT exception. * Revert "for #4311,Fixed Derby data source related SQL LIMIT exception." This reverts commit 49188f14 * fix #4333. * Remove case conversion. --- .../service/repository/RowMapperManager.java | 22 +++++++++++++++++++ .../EmbeddedStoragePersistServiceImpl.java | 22 ++++++++++++------- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java index b86e441f2f0..5b3c9c302a6 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/RowMapperManager.java @@ -35,8 +35,10 @@ import com.alibaba.nacos.config.server.model.User; import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.springframework.jdbc.core.RowMapper; @@ -84,6 +86,8 @@ public final class RowMapperManager { public static final PermissionRowMapper PERMISSION_ROW_MAPPER = new PermissionRowMapper(); + public static final MapRowMapper MAP_ROW_MAPPER = new MapRowMapper(); + public static Map mapperMap = new HashMap<>(16); static { @@ -161,12 +165,30 @@ public final class RowMapperManager { // PERMISSION_ROW_MAPPER mapperMap.put(PERMISSION_ROW_MAPPER.getClass().getCanonicalName(), PERMISSION_ROW_MAPPER); + + // MAP_ROW_MAPPER + + mapperMap.put(MAP_ROW_MAPPER.getClass().getCanonicalName(), MAP_ROW_MAPPER); } public static RowMapper getRowMapper(String classFullName) { return (RowMapper) mapperMap.get(classFullName); } + public static final class MapRowMapper implements RowMapper> { + + @Override + public Map mapRow(ResultSet resultSet, int rowNum) throws SQLException { + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + Map map = new LinkedHashMap<>(columnCount); + for (int i = 1; i <= columnCount; i++) { + map.put(metaData.getColumnLabel(i), resultSet.getObject(i)); + } + return map; + } + } + public static final class ConfigInfoWrapperRowMapper implements RowMapper { @Override 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 294fc589b23..6e5d2d039fd 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 @@ -72,6 +72,7 @@ import java.util.Objects; import java.util.Optional; import java.util.function.BiConsumer; +import java.util.stream.Collectors; import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ADVANCE_INFO_ROW_MAPPER; import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ALL_INFO_ROW_MAPPER; @@ -88,6 +89,7 @@ import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.HISTORY_DETAIL_ROW_MAPPER; import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.HISTORY_LIST_ROW_MAPPER; import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.TENANT_INFO_ROW_MAPPER; +import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.MAP_ROW_MAPPER; import static com.alibaba.nacos.config.server.utils.LogUtil.DEFAULT_LOG; /** @@ -1116,26 +1118,30 @@ public int configInfoTagCount() { @Override public List getTenantIdList(int page, int pageSize) { - PaginationHelper helper = createPaginationHelper(); + PaginationHelper> helper = createPaginationHelper(); String sql = "SELECT tenant_id FROM config_info WHERE tenant_id != '' GROUP BY tenant_id LIMIT ?,?"; int from = (page - 1) * pageSize; - Page pageList = helper.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, - (resultSet, i) -> resultSet.getString("tenant_id")); - return pageList.getPageItems(); + Page> pageList = helper + .fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, MAP_ROW_MAPPER); + return pageList.getPageItems().stream() + .map(map -> String.valueOf(map.get("TENANT_ID"))) + .collect(Collectors.toList()); } @Override public List getGroupIdList(int page, int pageSize) { - PaginationHelper helper = createPaginationHelper(); + PaginationHelper> helper = createPaginationHelper(); String sql = "SELECT group_id FROM config_info WHERE tenant_id ='' GROUP BY group_id LIMIT ?,?"; int from = (page - 1) * pageSize; - Page pageList = helper.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, - (resultSet, i) -> resultSet.getString("group_id")); - return pageList.getPageItems(); + Page> pageList = helper + .fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, MAP_ROW_MAPPER); + return pageList.getPageItems().stream() + .map(map -> String.valueOf(map.get("GROUP_ID"))) + .collect(Collectors.toList()); } @Override