All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.jn.sqlhelper.dialect.urlparser.MariaDBUrlParser Maven / Gradle / Ivy
package com.jn.sqlhelper.dialect.urlparser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MariaDBUrlParser extends CommonUrlParser {
private static final String URL_PREFIX = "jdbc:mariadb:";
private static final String MYSQL_URL_PREFIX = "jdbc:mysql:";
private static final Logger logger = LoggerFactory.getLogger(MariaDBUrlParser.class);
private static final List URL_SCHEMAS = Arrays.asList(new String[]{URL_PREFIX});
@Override
public List getUrlSchemas() {
return URL_SCHEMAS;
}
public MariaDBUrlParser() {
}
@Override
public String getName() {
return "mariadb";
}
private DatabaseInfo parse0(final String url, final Type type) {
if (this.isLoadbalanceUrl(url, type)) {
return this.parseLoadbalancedUrl(url, type);
}
return this.parseNormal(url, type);
}
private boolean isLoadbalanceUrl(final String url, final Type type) {
final String loadbalanceUrlPrefix = type.getLoadbalanceUrlPrefix();
return url.regionMatches(true, 0, loadbalanceUrlPrefix, 0, loadbalanceUrlPrefix.length());
}
private DatabaseInfo parseLoadbalancedUrl(final String url, final Type type) {
final StringMaker maker = new StringMaker(url);
maker.after(type.getUrlPrefix());
final String host = maker.after("//").before('/').value();
final String[] parsedHost = host.split(",");
final List hostList = Arrays.asList(parsedHost);
final String databaseId = maker.next().afterLast('/').before('?').value();
final String normalizedUrl = maker.clear().before('?').value();
return new DefaultDatabaseInfo(getName(), url, normalizedUrl, hostList, databaseId);
}
private DatabaseInfo parseNormal(final String url, final Type type) {
final StringMaker maker = new StringMaker(url);
maker.after(type.getUrlPrefix());
final String host = maker.after("//").before('/').value();
final List hostList = new ArrayList(1);
hostList.add(host);
final String databaseId = maker.next().afterLast('/').before('?').value();
final String normalizedUrl = maker.clear().before('?').value();
return new DefaultDatabaseInfo("mariadb", url, normalizedUrl, hostList, databaseId);
}
public enum Type {
MARIA("jdbc:mariadb:"),
MYSQL("jdbc:mysql:");
private final String urlPrefix;
private final String loadbalanceUrlPrefix;
private Type(final String urlPrefix) {
this.urlPrefix = urlPrefix;
this.loadbalanceUrlPrefix = urlPrefix + "loadbalance:";
}
private String getUrlPrefix() {
return this.urlPrefix;
}
private String getLoadbalanceUrlPrefix() {
return this.urlPrefix + "loadbalance:";
}
public static Type findType(final String jdbcUrl) {
for (final Type type : values()) {
if (jdbcUrl.startsWith(type.urlPrefix)) {
return type;
}
}
return null;
}
}
@Override
public DatabaseInfo parse(final String jdbcUrl) {
if (jdbcUrl == null) {
logger.info("jdbcUrl may not be null");
return UnKnownDatabaseInfo.INSTANCE;
}
final Type type = Type.findType(jdbcUrl);
if (type == null) {
logger.info("jdbcUrl has invalid prefix.(url:{}, prefix:{}, {})", new Object[]{jdbcUrl, "jdbc:mariadb:", "jdbc:mysql:"});
return UnKnownDatabaseInfo.INSTANCE;
}
DatabaseInfo result = null;
try {
result = this.parse0(jdbcUrl, type);
} catch (Exception e) {
logger.info("MaridDBJdbcUrl parse error. url: {}, Caused: {}", new Object[]{jdbcUrl, e.getMessage(), e});
result = UnKnownDatabaseInfo.createUnknownDataBase("mariadb", jdbcUrl);
}
return result;
}
}