Skip to content

Commit

Permalink
[ISSUE-alibaba#4333]Add MapRowMapper to RowMapperManager (alibaba#4334)
Browse files Browse the repository at this point in the history
* for alibaba#4311,Fixed Derby data source related SQL LIMIT exception.

* Revert "for alibaba#4311,Fixed Derby data source related SQL LIMIT exception."

This reverts commit 49188f1

* fix alibaba#4333.

* Remove case conversion.
  • Loading branch information
Maijh97 authored Nov 27, 2020
1 parent 67a0ba3 commit f78bef9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, RowMapper> mapperMap = new HashMap<>(16);

static {
Expand Down Expand Up @@ -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 <D> RowMapper<D> getRowMapper(String classFullName) {
return (RowMapper<D>) mapperMap.get(classFullName);
}

public static final class MapRowMapper implements RowMapper<Map<String, Object>> {

@Override
public Map<String, Object> mapRow(ResultSet resultSet, int rowNum) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
Map<String, Object> 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<ConfigInfoWrapper> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -1116,26 +1118,30 @@ public int configInfoTagCount() {

@Override
public List<String> getTenantIdList(int page, int pageSize) {
PaginationHelper<String> helper = createPaginationHelper();
PaginationHelper<Map<String, Object>> helper = createPaginationHelper();

String sql = "SELECT tenant_id FROM config_info WHERE tenant_id != '' GROUP BY tenant_id LIMIT ?,?";
int from = (page - 1) * pageSize;

Page<String> pageList = helper.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize,
(resultSet, i) -> resultSet.getString("tenant_id"));
return pageList.getPageItems();
Page<Map<String, Object>> 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<String> getGroupIdList(int page, int pageSize) {
PaginationHelper<String> helper = createPaginationHelper();
PaginationHelper<Map<String, Object>> helper = createPaginationHelper();

String sql = "SELECT group_id FROM config_info WHERE tenant_id ='' GROUP BY group_id LIMIT ?,?";
int from = (page - 1) * pageSize;

Page<String> pageList = helper.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize,
(resultSet, i) -> resultSet.getString("group_id"));
return pageList.getPageItems();
Page<Map<String, Object>> 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
Expand Down

0 comments on commit f78bef9

Please sign in to comment.