Skip to content

Commit e3ca332

Browse files
committed
fix: url encoding for jdbc url
1 parent 51186fd commit e3ca332

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugins {
1818
}
1919

2020
group = 'dev.entree'
21-
version = '2.0.0'
21+
version = '2.0.1'
2222

2323
repositories {
2424
mavenCentral()

src/main/java/dev/entree/vchest/JDBCUtils.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.jetbrains.annotations.Nullable;
77

88
import java.io.File;
9+
import java.net.URLEncoder;
10+
import java.nio.charset.StandardCharsets;
11+
import java.util.stream.Stream;
912

1013
public class JDBCUtils {
1114
public static HikariDataSource getDataSource(JDBCContext ctx) {
@@ -18,14 +21,16 @@ public static HikariDataSource getDataSource(JDBCContext ctx) {
1821
String dbName = ctx.getDbName();
1922
// hikari
2023
HikariConfig hikariConfig = new HikariConfig();
24+
Stream<Object> args = dbDir != null
25+
? Stream.of(protocol, new File(dbDir, dbName + ".db").getAbsolutePath())
26+
: Stream.of(protocol, host, port, dbName, username, password);
27+
Object[] escapeArgs = args
28+
.map(xs -> URLEncoder.encode(xs.toString(), StandardCharsets.UTF_8))
29+
.toArray();
2130
String url = dbDir != null
22-
? String.format("jdbc:%s:%s", protocol, new File(dbDir, dbName + ".db").getAbsolutePath())
23-
: String.format("jdbc:%s://%s:%s/%s?createDatabaseIfNotExist=true&user=%s&password=%s&useSSL=false&allowPublicKeyRetrieval=true&useAffectedRows=true", protocol, host, port, dbName, username, password);
31+
? String.format("jdbc:%s:%s", escapeArgs)
32+
: String.format("jdbc:%s://%s:%s/%s?createDatabaseIfNotExist=true&user=%s&password=%s&useSSL=false&allowPublicKeyRetrieval=true&useAffectedRows=true", escapeArgs);
2433
hikariConfig.setJdbcUrl(url);
25-
if (dbDir == null) {
26-
hikariConfig.setUsername(username);
27-
hikariConfig.setPassword(password);
28-
}
2934
return new HikariDataSource(hikariConfig);
3035
}
3136

0 commit comments

Comments
 (0)