注:此次改造去掉了tentId这个字段,改造地方比较多
一.首先,下载官方源码包
二.改造
1.DataSourceConstant
public class DataSourceConstant {
public static final String MYSQL = "mysql";
public static final String DERBY = "derby";
public static final String ORACLE = "oracle";
}
2.添加oracle文件夹
3.TrustedOracleFunctionEnum
import java.util.HashMap;
import java.util.Map;
/**
* The TrustedSqlFunctionEnum enum class is used to enumerate and manage a list of trusted built-in SQL functions.
* By using this enum, you can verify whether a given SQL function is part of the trusted functions list
* to avoid potential SQL injection risks.
*
* @author blake.qiu
*/
public enum TrustedOracleFunctionEnum {
/**
* NOW().
*/
NOW("NOW()", "CURRENT_TIMESTAMP");
private static final Map<String, TrustedOracleFunctionEnum> LOOKUP_MAP = new HashMap<>();
static {
for (TrustedOracleFunctionEnum entry : TrustedOracleFunctionEnum.values()) {
LOOKUP_MAP.put(entry.functionName, entry);
}
}
private final String functionName;
private final String function;
TrustedOracleFunctionEnum(String functionName, String function) {
this.functionName = functionName;
this.function = function;
}
/**
* Get the function name.
*
* @param functionName function name
* @return function
*/
public static String getFunctionByName(String functionName) {
TrustedOracleFunctionEnum entry = LOOKUP_MAP.get(functionName);
if (entry != null) {
return entry.function;
}
throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName));
}
}
4.AbstractMapperByOracle
package com.alibaba.nacos.plugin.datasource.impl.oracle;
import com.alibaba.nacos.plugin.datasource.enums.oracle.TrustedOracleFunctionEnum;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
/**
* The abstract mysql mapper contains CRUD methods.
*
* @author blake.qiu
**/
public abstract class AbstractMapperByOracle extends AbstractMapper {
@Override
public String getFunction(String functionName) {
return TrustedOracleFunctionEnum.getFunctionByName(functionName);
}
}
5.ConfigInfoAggrMapperByOracle
package com.alibaba.nacos.plugin.datasource.impl.oracle;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import java.util.List;
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The mysql implementation of ConfigInfoAggrMapper.
*
* @author hyx
**/
public class ConfigInfoAggrMapperByOracle extends AbstractMapperByOracle implements ConfigInfoAggrMapper {
@Override
public String getDataSource() {
return DataSourceConstant.ORACLE;
}
@Override
public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID);
String groupId = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
String sql =
"SELECT * FROM (SELECT data_id,group_id,tenant_id,datum_id,app_name,content, ROWNUM as rnum"
+ " FROM config_info_aggr WHERE data_id= ? AND "
+ "group_id= ? AND tenant_id= ? ORDER BY datum_id) WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum";
List<Object> paramList = CollectionUtils.list(dataId, groupId, tenantId);
return new MapperResult(sql, paramList);
}
}
6.ConfigInfoBetaMapperByOracle
package com.alibaba.nacos.plugin.datasource.impl.oracle;
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import java.util.ArrayList;
import java.util.List;
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The mysql implementation of ConfigInfoAggrMapper.
*
* @author hyx
**/
public class ConfigInfoBetaMapperByOracle extends AbstractMapperByOracle implements ConfigInfoBetaMapper {
@Override
public String getDataSource() {
return DataSourceConstant.ORACLE;
}
@Override
public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext context) {
int startRow = context.getStartRow();
int pageSize = context.getPageSize();
String sql = " SELECT * FROM (SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key "
+ " FROM ( SELECT * FROM (SELECT id, ROWNUM as rnum FROM config_info_beta ORDER BY id) "
+ "WHERE rnum >= " + startRow + " and " + pageSize + " >= rnum" + " )"
+ " g, config_info_beta t WHERE g.id = t.id ";
List<Object> paramList = new ArrayList<>();
paramList.add(startRow);
paramList.add(pageSize);
return new MapperResult(sql, paramList);
}
}
7.ConfigInfoMapperByOracle
package com.alibaba.nacos.plugin.datasource.impl.oracle;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* The mysql implementation of ConfigInfoAggrMapper.
*
* @author hyx
**/
public class ConfigInfoMapperByOracle extends AbstractMapperByOracle implements ConfigInfoMapper {
@Override
public String getDataSource() {
return DataSourceConstant.ORACLE;
}
@Override
public MapperResult findConfigInfoByAppFetchRows(MapperContext context) {
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
String sql = "SELECT * FROM (SELECT id,data_id,group_id,tenant_id,app_name,content, ROWNUM as rnum FROM config_info"
+ " WHERE tenant_id LIKE ? AND app_name= ?)" + " WHERE rnum >= "
+ context.getStartRow() + " and " + context.getPageSize() + " >= rnum";
return new MapperResult(sql, CollectionUtils.list(tenantId,
appName));
}
@Override
public MapperResult getTenantIdList(MapperContext context) {
String sql = "SELECT * FROM (SELECT tenant_id, ROWNUM as rnum FROM"
+ " config_info WHERE tenant_id != '' GROUP BY tenant_id)"
+ " WHERE rnum >= " + context.getStartRow() + " and " + context.getPageSize() + " >= rnum";
return new MapperResult(sql, Collections.emptyList());
}
@Override
public MapperResult getGroupIdList(MapperContext context) {
String sql = "SELECT * FROM (SELECT group_id, ROWNUM as rnum FROM config_info WHERE tenant_id ='' GROUP BY group_id) "
+ " WHERE rnum >= " + context.getStartRow() + " and " + context.getPageSize() + " >= rnum";
return new MapperResult(sql, Collections.emptyList());
}
@Override
public MapperResult findAllConfigKey(MapperContext context) {
String sql = " SELECT data_id,group_id,app_name FROM ( "
+ " SELECT * FROM (SELECT id, ROWNUM as rnum FROM config_info WHERE tenant_id LIKE ? ORDER BY id)"
+ " WHERE rnum >= " + context.getStartRow() + " and " + context.getPageSize() + " >= rnum"
+ " )" + " g, config_info t WHERE g.id = t.id ";
return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID)));
}
@Override
public MapperResult findAllConfigInfoBaseFetchRows(MapperContext context) {
String sql = "SELECT t.id,data_id,group_id,content,md5"
+ " FROM (SELECT * FROM ( SELECT id, ROWNUM as rnum FROM config_info ORDER BY id)"
+ " WHERE rnum >= " + context.getStartRow() + " and " + context.getPageSize() + " >= rnum) "
+ " g, config_info t WHERE g.id = t.id ";
return new MapperResult(sql, Collections.emptyList());
}
@Override
public MapperResult findAllConfigInfoFragment(MapperContext context) {
String sql = "SELECT * FROM (SELECT id,ROWNUM as rnum,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,type,encrypted_data_key "
+ "FROM config_info WHERE id > ? ORDER BY id ASC) " + " WHERE rnum >= "
+ context.getStartRow() + " and " + context.getPageSize() + " >= rnum ";