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

fix(Database Dialect): Fix the bug that different databases get the current time function. #63

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 @@ -16,6 +16,8 @@

package com.alibaba.nacos.plugin.datasource.dialect;

import com.alibaba.nacos.plugin.datasource.mapper.Mapper;

/**
* DatabaseDialect interface.
* @author Long Yu
Expand Down Expand Up @@ -82,4 +84,12 @@ public interface DatabaseDialect {
*/
public String[] getReturnPrimaryKeys();

/**
* Get the function corresponding to the dialect according to the function name
* @author Mr.Muzhi
* @since 2025/1/7 16:30
* @param functionName functionName
* @return function
*/
String getFunction(String functionName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.plugin.datasource.dialect;

import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.enums.mysql.TrustedMysqlFunctionEnum;

/**
* defauLT database dialect.
Expand All @@ -29,4 +30,9 @@ public String getType() {
return DatabaseTypeConstant.MYSQL;
}

@Override
public String getFunction(String functionName) {
return TrustedMysqlFunctionEnum.getFunctionByName(functionName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) {
List<Object> paramList = CollectionUtils.list(dataId, groupId, tenantId);
return new MapperResult(sql, paramList);
}


@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext conte
+ " FROM ( " + sqlInner + " )" + " g, config_info_beta t WHERE g.id = t.id ";
return new MapperResult(sql, Collections.emptyList());
}

@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,9 @@ public MapperResult findAllConfigInfoFetchRows(MapperContext context) {
public String getTableName() {
return TableConstant.CONFIG_INFO;
}


@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,10 @@ public MapperResult findAllConfigInfoTagForDumpAllFetchRows(MapperContext contex
+ innerSql + " ) " + "g, config_info_tag t WHERE g.id = t.id ";
return new MapperResult(sql, Collections.emptyList());
}

@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,10 @@ public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
String sql = getLimitPageSqlWithOffset(sqlFetchRows + where, startRow, pageSize);
return new MapperResult(sql, paramList);
}

@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ public MapperResult selectGroupInfoBySize(MapperContext context) {
return new MapperResult(sql,
CollectionUtils.list(context.getWhereParameter(FieldConstant.ID), context.getPageSize()));
}

@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@ public MapperResult getCapacityList4CorrectUsage(MapperContext context) {
return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.ID),
context.getWhereParameter(FieldConstant.LIMIT_SIZE)));
}
@Override
public String getFunction(String functionName) {
return databaseDialect.getFunction(functionName);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.alibaba.nacos.plugin.datasource.impl.base;

import com.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect;
import com.alibaba.nacos.plugin.datasource.impl.mysql.TenantInfoMapperByMySql;
import com.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager;

/**
* The base implementation of TenantInfo.
Expand All @@ -25,4 +27,12 @@
**/
public class BaseTenantInfoMapper extends TenantInfoMapperByMySql {

private DatabaseDialect databaseDialect;

public BaseTenantInfoMapper() {
databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource());
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.constants.PrimaryKeyConstant;
import com.alibaba.nacos.plugin.datasource.emums.TrustedDaMengFunctionEnum;

/**
* dameng database dialect.
Expand All @@ -36,4 +37,10 @@ public String getType() {
return DatabaseTypeConstant.DM;
}


@Override
public String getFunction(String functionName) {
return TrustedDaMengFunctionEnum.getFunctionByName(functionName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2018 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.
*/

package com.alibaba.nacos.plugin.datasource.emums;

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 TrustedDaMengFunctionEnum {

/**
* NOW().
*/
NOW("NOW()", "SYSDATE");

private static final Map<String, TrustedDaMengFunctionEnum> LOOKUP_MAP = new HashMap<>();

static {
for (TrustedDaMengFunctionEnum entry : TrustedDaMengFunctionEnum.values()) {
LOOKUP_MAP.put(entry.functionName, entry);
}
}

private final String functionName;

private final String function;

TrustedDaMengFunctionEnum(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) {
TrustedDaMengFunctionEnum entry = LOOKUP_MAP.get(functionName);
if (entry != null) {
return entry.function;
}
throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.plugin.datasource.dialect;

import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlServerFunctionEnum;

/**
* Microsoft SQL Server database dialect.
Expand Down Expand Up @@ -51,4 +52,9 @@ public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSiz
return sql + " ORDER BY id OFFSET " + startOffset + " ROWS FETCH NEXT "
+ pageSize + " ROWS ONLY ";
}

@Override
public String getFunction(String functionName) {
return TrustedSqlServerFunctionEnum.getFunctionByName(functionName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 1999-2018 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.
*/

package com.alibaba.nacos.plugin.datasource.enums;

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 TrustedSqlServerFunctionEnum {

/**
* NOW().
*/
NOW("NOW()", "SYSDATETIME()");

private static final Map<String, TrustedSqlServerFunctionEnum> LOOKUP_MAP = new HashMap<>();

static {
for (TrustedSqlServerFunctionEnum entry : TrustedSqlServerFunctionEnum.values()) {
LOOKUP_MAP.put(entry.functionName, entry);
}
}

private final String functionName;

private final String function;

TrustedSqlServerFunctionEnum(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) {
TrustedSqlServerFunctionEnum entry = LOOKUP_MAP.get(functionName);
if (entry != null) {
return entry.function;
}
throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.common.utils.NamespaceUtil;
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
import com.alibaba.nacos.plugin.datasource.enums.TrustedOracleFunctionEnum;

/***
* oracle datasource dialect.
Expand Down Expand Up @@ -50,9 +51,15 @@ public String getLimitPageSqlWithMark(String sql) {
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize) {
return sql + " OFFSET " + startOffset + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY ";
}

@Override
public String getLimitPageSql(String sql, int pageNo, int pageSize) {
return sql + " OFFSET " + getPagePrevNum(pageNo, pageSize) + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY ";
}


@Override
public String getFunction(String functionName) {
return TrustedOracleFunctionEnum.getFunctionByName(functionName);
}
}
Loading