All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jn.sqlhelper.dialect.urlparser.CubridUrlParser 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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CubridUrlParser extends CommonUrlParser {
    public static final String DEFAULT_HOSTNAME = "localhost";
    public static final int DEFAULT_PORT = 30000;
    public static final String DEFAULT_USER = "public";
    public static final String DEFAULT_PASSWORD = "";
    private static final String URL_PREFIX_PATTERN = "jdbc:cubrid(-oracle|-mysql)?:";
    private static final Pattern PREFIX_PATTERN;

    private static final String URL_PATTERN = "jdbc:cubrid(-oracle|-mysql)?:([a-zA-Z_0-9\\.-]*):([0-9]*):([^:]+):([^:]*):([^:]*):(\\?[a-zA-Z_0-9]+=[^&=?]+(&[a-zA-Z_0-9]+=[^&=?]+)*)?";
    private static final Pattern PATTERN;
    private final static Logger logger = LoggerFactory.getLogger(CubridUrlParser.class);
    private static final String URL_PREFIX = "jdbc:cubrid";
    private static final List URL_SCHEMAS = Arrays.asList(new String[]{URL_PREFIX});

    @Override
    public List getUrlSchemas() {
        return URL_SCHEMAS;
    }

    public CubridUrlParser() {
    }

    @Override
    public String getName() {
        return "cubrid";
    }

    @Override
    public DatabaseInfo parse(final String jdbcUrl) {
        final Matcher matcher = CubridUrlParser.PREFIX_PATTERN.matcher(jdbcUrl);
        if (!matcher.find()) {
            logger.info("jdbcUrl has invalid prefix.(url:{}, prefix-pattern:{})", (Object) jdbcUrl, (Object) "jdbc:cubrid(-oracle|-mysql)?:");
            return UnKnownDatabaseInfo.INSTANCE;
        }
        DatabaseInfo result = null;
        try {
            result = this.parse0(jdbcUrl);
        } catch (Exception e) {
            logger.info("CubridJdbcUrl parse error. url: {}, Caused: {}", new Object[]{jdbcUrl, e.getMessage(), e});
            result = UnKnownDatabaseInfo.createUnknownDataBase("cubrid", jdbcUrl);
        }
        return result;
    }

    private DatabaseInfo parse0(final String jdbcUrl) {
        final Matcher matcher = CubridUrlParser.PATTERN.matcher(jdbcUrl);
        if (!matcher.find()) {
            throw new IllegalArgumentException();
        }
        String host = matcher.group(2);
        final String portString = matcher.group(3);
        final String db = matcher.group(4);
        String user = matcher.group(5);
        if (host == null || host.length() == 0) {
            host = "localhost";
        }
        if (user == null) {
            user = "public";
        }
        final StringMaker maker = new StringMaker(jdbcUrl);
        final String normalizedUrl = maker.clear().before('?').value();
        final List hostList = new ArrayList(1);
        final String hostAndPort = host + ":" + portString;
        hostList.add(hostAndPort);
        return new DefaultDatabaseInfo(getName(), jdbcUrl, normalizedUrl, hostList, db);
    }

    static {
        PREFIX_PATTERN = Pattern.compile("jdbc:cubrid(-oracle|-mysql)?:", 2);
        PATTERN = Pattern.compile("jdbc:cubrid(-oracle|-mysql)?:([a-zA-Z_0-9\\.-]*):([0-9]*):([^:]+):([^:]*):([^:]*):(\\?[a-zA-Z_0-9]+=[^&=?]+(&[a-zA-Z_0-9]+=[^&=?]+)*)?", 2);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy