Skip to content

Commit 5a5bb5e

Browse files
authored
Merge pull request #130 from FOCONIS/support-merging-configs
Merge customProperties/clientInfo/initSql when loading config from multiple places
2 parents 605b515 + fc342fb commit 5a5bb5e

File tree

2 files changed

+89
-24
lines changed

2 files changed

+89
-24
lines changed

ebean-datasource-api/src/main/java/io/ebean/datasource/DataSourceConfig.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ public DataSourceConfig copy() {
143143
copy.clientInfo = new Properties();
144144
copy.clientInfo.putAll(clientInfo);
145145
}
146-
copy.initSql = initSql;
146+
if (initSql != null) {
147+
copy.initSql = new ArrayList<>(initSql);
148+
}
147149
copy.alert = alert;
148150
copy.listener = listener;
149151
copy.enforceCleanClose = enforceCleanClose;
@@ -194,10 +196,23 @@ public DataSourceConfig setDefaults(DataSourceBuilder builder) {
194196
}
195197
if (customProperties == null) {
196198
var otherCustomProps = other.getCustomProperties();
197-
if (otherCustomProps != null) {
199+
if (otherCustomProps != null && !otherCustomProps.isEmpty()) {
198200
customProperties = new LinkedHashMap<>(otherCustomProps);
199201
}
200202
}
203+
if (clientInfo == null) {
204+
var otherClientInfo = other.getClientInfo();
205+
if (otherClientInfo != null && !otherClientInfo.isEmpty()) {
206+
clientInfo = new Properties();
207+
clientInfo.putAll(otherClientInfo);
208+
}
209+
}
210+
if (initSql == null) {
211+
var otherInitSql = other.getInitSql();
212+
if (otherInitSql != null && !otherInitSql.isEmpty()) {
213+
initSql = new ArrayList<>(otherInitSql);
214+
}
215+
}
201216
return this;
202217
}
203218

@@ -809,49 +824,58 @@ private void loadSettings(ConfigPropertiesHelper properties) {
809824

810825
String isoLevel = properties.get("isolationLevel", _isolationLevel(isolationLevel));
811826
this.isolationLevel = _isolationLevel(isoLevel);
812-
this.initSql = parseSql(properties.get("initSql", null));
827+
String sql = properties.get("initSql", null);
828+
if (sql != null && !sql.isEmpty()) {
829+
if (this.initSql == null) {
830+
this.initSql = new ArrayList<>();
831+
}
832+
parseSql(sql, this.initSql);
833+
}
813834
this.failOnStart = properties.getBoolean("failOnStart", failOnStart);
814835

815836
String customProperties = properties.get("customProperties", null);
816837
if (customProperties != null && !customProperties.isEmpty()) {
817-
this.customProperties = parseCustom(customProperties);
838+
if (this.customProperties == null) {
839+
this.customProperties = new LinkedHashMap<>();
840+
}
841+
parseCustom(customProperties, this.customProperties);
818842
}
819843
String infoProperties = properties.get("clientInfo", null);
820844
if (infoProperties != null && !infoProperties.isEmpty()) {
821-
Map<String, String> pairs = parseCustom(infoProperties);
822-
if (!pairs.isEmpty()) {
845+
if (this.clientInfo == null) {
823846
this.clientInfo = new Properties();
824-
for (Map.Entry<String, String> entry : pairs.entrySet()) {
825-
this.clientInfo.setProperty(entry.getKey(), entry.getValue());
826-
}
827847
}
848+
parseCustom(infoProperties, this.clientInfo);
828849
}
829850
}
830851

831-
private List<String> parseSql(String sql) {
832-
List<String> ret = new ArrayList<>();
852+
void parseSql(String sql, List<String> target) {
833853
if (sql != null) {
834-
String[] queries = sql.split(";");
854+
String splitter = ";";
855+
if (sql.toLowerCase().startsWith("delimiter $$")) {
856+
sql = sql.substring("delimiter $$".length());
857+
splitter = "\\$\\$";
858+
}
859+
String[] queries = sql.split(splitter);
835860
for (String query : queries) {
836861
query = query.trim();
837862
if (!query.isEmpty()) {
838-
ret.add(query);
863+
target.add(query);
839864
}
840865
}
841866
}
842-
return ret;
843867
}
844868

845-
Map<String, String> parseCustom(String customProperties) {
846-
Map<String, String> propertyMap = new LinkedHashMap<>();
869+
@SuppressWarnings("unchecked")
870+
// we use raw map type here, so that we can also accept Properties as target
871+
void parseCustom(String customProperties, Map target) {
847872
String[] pairs = customProperties.split(";");
848873
for (String pair : pairs) {
849874
String[] split = pair.split("=");
850875
if (split.length == 2) {
851-
propertyMap.put(split[0], split[1]);
876+
target.put(split[0], split[1]);
852877
}
853878
}
854-
return propertyMap;
855879
}
856880

857881
@Override

ebean-datasource-api/src/test/java/io/ebean/datasource/DataSourceConfigTest.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import org.junit.jupiter.api.Test;
44

55
import java.io.IOException;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
68
import java.util.LinkedHashMap;
9+
import java.util.List;
710
import java.util.Map;
811
import java.util.Properties;
912

@@ -37,12 +40,50 @@ public void addProperty() {
3740
public void parseCustom() {
3841

3942
DataSourceConfig config = new DataSourceConfig();
40-
Map<String, String> map = config.parseCustom("a=1;b=2;c=3");
43+
Map<String, String> map = new HashMap<>();
44+
config.parseCustom("a=1;b=2;c=3", map);
4145

4246
assertThat(map).hasSize(3);
4347
assertThat(map.get("a")).isEqualTo("1");
4448
assertThat(map.get("b")).isEqualTo("2");
4549
assertThat(map.get("c")).isEqualTo("3");
50+
51+
config.parseCustom("a=4;b=5;d=6", map);
52+
assertThat(map).hasSize(4);
53+
assertThat(map.get("a")).isEqualTo("4");
54+
assertThat(map.get("b")).isEqualTo("5");
55+
assertThat(map.get("c")).isEqualTo("3");
56+
assertThat(map.get("d")).isEqualTo("6");
57+
}
58+
59+
@Test
60+
public void parseSql() {
61+
62+
DataSourceConfig config = new DataSourceConfig();
63+
List<String> list = new ArrayList<>();
64+
config.parseSql("SET NAMES utf8mb4;SET collation_connection = 'utf8mb4_bin';SET wait_timeout = 28800", list);
65+
assertThat(list).containsExactly("SET NAMES utf8mb4", "SET collation_connection = 'utf8mb4_bin'", "SET wait_timeout = 28800");
66+
67+
config.parseSql("delimiter $$CREATE PROCEDURE test1\n" +
68+
"BEGIN\n" +
69+
"DECLARE xy INT DEFAULT FALSE;\n" +
70+
"END$$CREATE PROCEDURE test2\n" +
71+
"BEGIN\n" +
72+
"DECLARE ab INT DEFAULT FALSE;\n" +
73+
"END$$", list);
74+
assertThat(list).containsExactly(
75+
"SET NAMES utf8mb4",
76+
"SET collation_connection = 'utf8mb4_bin'",
77+
"SET wait_timeout = 28800",
78+
"CREATE PROCEDURE test1\n" +
79+
"BEGIN\n" +
80+
"DECLARE xy INT DEFAULT FALSE;\n" +
81+
"END",
82+
"CREATE PROCEDURE test2\n" +
83+
"BEGIN\n" +
84+
"DECLARE ab INT DEFAULT FALSE;\n" +
85+
"END");
86+
4687
}
4788

4889
@Test
@@ -79,9 +120,9 @@ public void copy() {
79120
source.setSchema("sch");
80121
source.catalog("cat");
81122

82-
Map<String,String> customSource = new LinkedHashMap<>();
83-
customSource.put("a","a");
84-
customSource.put("b","b");
123+
Map<String, String> customSource = new LinkedHashMap<>();
124+
customSource.put("a", "a");
125+
customSource.put("b", "b");
85126
source.setCustomProperties(customSource);
86127

87128

@@ -95,8 +136,8 @@ public void copy() {
95136
assertEquals(42, copy.getMinConnections());
96137
assertEquals(45, copy.getMaxConnections());
97138

98-
customSource.put("a","modifiedA");
99-
customSource.put("c","newC");
139+
customSource.put("a", "modifiedA");
140+
customSource.put("c", "newC");
100141

101142
assertEquals("a", copy.getCustomProperties().get("a"));
102143
assertEquals("b", copy.getCustomProperties().get("b"));

0 commit comments

Comments
 (0)