Files
JEF/src/main/java/jef/DatabaseOptions.java
2022-11-23 13:50:54 +01:00

47 lines
1.4 KiB
Java

package jef;
import lombok.Getter;
import java.net.MalformedURLException;
import java.util.regex.Pattern;
@Getter
public class DatabaseOptions {
protected final String url;
protected final String user;
protected final String password;
protected final String migrationsPackage;
protected final String host;
protected final String database;
public DatabaseOptions(String url, String user, String password, String migrationsPackage) {
this.url = url;
this.user = user;
this.password = password;
host = extractHost(url);
database = extractDatabase(url);
this.migrationsPackage = migrationsPackage;
}
private static String extractHost(String url) {
var pattern = Pattern.compile("^.*?//(.*?)/.*$");
var matcher = pattern.matcher(url);
if (!matcher.matches()) {
throw new RuntimeException(new MalformedURLException("Could not extract host for url: " + url));
}
return matcher.group(1);
}
private static String extractDatabase(String url) {
var pattern = Pattern.compile("^.*?//.*?/(.*?)([?/].*)?$");
var matcher = pattern.matcher(url);
if (!matcher.matches()) {
throw new RuntimeException(new MalformedURLException("Could not extract database for url: " + url));
}
return matcher.group(1);
}
}