47 lines
1.4 KiB
Java
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);
|
|
}
|
|
}
|