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

net.moznion.mysql.diff.MySqlConnectionUri Maven / Gradle / Ivy

Go to download

Detect and extract diff between two table declarations from schema of MySQL

There is a newer version: 1.1.0
Show newest version
package net.moznion.mysql.diff;

import lombok.Getter;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
class MySqlConnectionUri {
  private String host;
  private int port;
  private List queries;

  private static final int DEFAULT_PORT = 3306;
  private static final Pattern URL_PATTERN = Pattern.compile("^([^/]*:)[^:/]+:");

  public MySqlConnectionUri(String url) throws URISyntaxException {
    Matcher urlMatcher = URL_PATTERN.matcher(url);
    if (!urlMatcher.find()) {
      throw new URISyntaxException(url, "It doesn't contain connection protocol schema of jdbc");
    }

    String cleanUrl = url.substring(urlMatcher.group(1).length());
    URI uri = new URI(cleanUrl);

    host = uri.getHost();

    port = uri.getPort();
    if (port < 0) {
      port = DEFAULT_PORT;
    }

    queries = parseQueryString(uri.getQuery());
  }

  private List parseQueryString(String queryString) {
    ArrayList queries = new ArrayList<>();

    if (queryString != null) {
      Arrays.asList(queryString.split("&")).forEach(property -> {
        queries.add(property);
      });
    }

    return queries;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy