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: optimize jdbcUrl params config #1311

Merged
merged 1 commit into from
May 11, 2022
Merged
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
42 changes: 42 additions & 0 deletions core/src/main/java/datart/core/common/UrlUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package datart.core.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class UrlUtils {

public static Map<String, Object> getParamsMap(String paramsStr) {
Map<String, Object> map = new HashMap<>();
if (StringUtils.isBlank(paramsStr)) {
return map;
}
String[] params = paramsStr.split("&");
for (int i = 0; i < params.length; i++) {
String[] param = params[i].split("=");
if (param.length == 2) {
map.put(param[0], param[1]);
}
}
return map;
}

public static String covertMapToUrlParams(Map<String, Object> map) {
if (map == null) {
return "";
}
StringBuffer sb = new StringBuffer();
for (Map.Entry<String, Object> entry : map.entrySet()) {
sb.append(entry.getKey() + "=" + entry.getValue());
sb.append("&");
}
String str = sb.toString();
if (str.endsWith("&")) {
str = StringUtils.substringBeforeLast(str, "&");
}
return str;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.mysql.cj.conf.ConnectionUrl;
import datart.core.base.exception.Exceptions;
import datart.core.common.FileUtils;
import datart.core.common.UrlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
Expand All @@ -26,7 +27,6 @@
import java.lang.reflect.Field;
import java.util.*;

@Order(Integer.MIN_VALUE)
public class CustomPropertiesValidate implements EnvironmentPostProcessor {

private static final String CONFIG_HOME = "config/datart.conf";
Expand All @@ -44,6 +44,11 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
//this.validateConfig(properties);
propertySources.addFirst(new PropertiesPropertySource("datartConfig", properties));
switchProfile(environment);
String jdbcUrl = processDBUrl(environment);
if (StringUtils.isNotBlank(jdbcUrl)) {
properties.setProperty(DATABASE_URL, jdbcUrl);
propertySources.addFirst(new PropertiesPropertySource("datartConfig", properties));
}
}

private Properties loadCustomProperties() {
Expand Down Expand Up @@ -104,6 +109,34 @@ private void switchProfile(ConfigurableEnvironment environment) {
}
}

private String processDBUrl(ConfigurableEnvironment environment){
String jdbcUrl = environment.getProperty(DATABASE_URL);
if (!ConnectionUrl.acceptsUrl(jdbcUrl)) {
return "";
}
Boolean isModify = false;
String[] split = StringUtils.split(jdbcUrl, "?");
if (split.length>1) {
Map<String, Object> urlParams = UrlUtils.getParamsMap(split[1]);
if (!"true".equals(urlParams.getOrDefault("allowMultiQueries", "false"))) {
isModify = true;
urlParams.put("allowMultiQueries", "true");
}
if (!urlParams.containsKey("characterEncoding")) {
isModify = true;
urlParams.put("characterEncoding", "utf-8");
}
jdbcUrl = split[0] + "?" + UrlUtils.covertMapToUrlParams(urlParams);
} else {
isModify = true;
jdbcUrl = jdbcUrl+"?allowMultiQueries=true&characterEncoding=utf-8";
}
if (isModify) {
return jdbcUrl;
}
return "";
}

private String getDefaultDBUrl(ConfigurableEnvironment environment) {
List<String> activeProfiles = Arrays.asList(environment.getActiveProfiles());
if (activeProfiles.size() > 0 && !Arrays.asList("demo", "config").containsAll(activeProfiles)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public boolean update(BaseUpdateParam updateParam) {
BeanUtils.copyProperties(updateParam, schedule);
schedule.setUpdateBy(getCurrentUser().getId());
schedule.setUpdateTime(new Date());
if (schedule.getIsFolder()) {
if (Objects.equals(schedule.getIsFolder(), Boolean.TRUE)) {
schedule.setType(ResourceType.FOLDER.name());
} else {
schedule.setType(scheduleUpdateParam.getType().name());
Expand Down