66import org .jetbrains .annotations .Nullable ;
77
88import java .io .File ;
9+ import java .net .URLEncoder ;
10+ import java .nio .charset .StandardCharsets ;
11+ import java .util .stream .Stream ;
912
1013public 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