Skip to content

Commit

Permalink
Merge dedcb08 into d53917e
Browse files Browse the repository at this point in the history
  • Loading branch information
malafeev committed May 6, 2019
2 parents d53917e + dedcb08 commit c525d56
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
43 changes: 41 additions & 2 deletions src/main/java/io/opentracing/contrib/jdbc/parser/H2URLParser.java
Expand Up @@ -19,14 +19,22 @@ public class H2URLParser extends AbstractURLParser {

private static final String LOCALHOST = "localhost";
private static final int DEFAULT_PORT = 8084;
/**
* Flag that H2 running with memory mode.
*/
private static final String MEMORY_MODE_FLAG = "mem";
/**
* Flag that H2 running with tcp mode.
*/
private static final String TCP_MODE_FLAG = "h2:tcp";
/**
* Flag that H2 running with file mode.
*/
private static final String FILE_MODE_FLAG = "file";
/**
* Flag that H2 running with memory mode.
* Flag that H2 running with implicit file mode.
*/
private static final String MEMORY_MODE_FLAG = "mem";
private static final String IMPLICIT_FILE_MODE_FLAG = "jdbc:h2";
private static final String H2_DB_TYPE = "h2";

@Override
Expand Down Expand Up @@ -60,6 +68,12 @@ public ConnectionInfo parse(String url) {
.dbInstance(fetchDatabaseNameFromURL(url, databaseNameRangeIndex)).build();
}

databaseNameRangeIndex = fetchDatabaseNameRangeIndexFromURLForH2ImplicitFileMode(url);
if (databaseNameRangeIndex != null) {
return new ConnectionInfo.Builder(LOCALHOST, -1).dbType(H2_DB_TYPE)
.dbInstance(fetchDatabaseNameFromURL(url, databaseNameRangeIndex)).build();
}

String[] hostAndPort = fetchDatabaseHostsFromURL(url).split(":");
if (hostAndPort.length == 1) {
return new ConnectionInfo.Builder(hostAndPort[0], DEFAULT_PORT).dbType(H2_DB_TYPE)
Expand Down Expand Up @@ -90,6 +104,29 @@ private int[] fetchDatabaseNameRangeIndexFromURLForH2FileMode(String url) {
}
}

/**
* Fetch range index that the database name from connection url if H2 database running with
* implicit file mode.
*
* @return range index that the database name.
*/
private int[] fetchDatabaseNameRangeIndexFromURLForH2ImplicitFileMode(String url) {
if (url.contains(TCP_MODE_FLAG)) {
return null;
}
int fileLabelIndex = url.indexOf(IMPLICIT_FILE_MODE_FLAG);
int parameterLabelIndex = url.indexOf(";", fileLabelIndex);
if (parameterLabelIndex == -1) {
parameterLabelIndex = url.length();
}

if (fileLabelIndex != -1) {
return new int[]{fileLabelIndex + IMPLICIT_FILE_MODE_FLAG.length() + 1, parameterLabelIndex};
} else {
return null;
}
}

/**
* Fetch range index that the database name from connection url if H2 database running with memory
* mode.
Expand All @@ -109,4 +146,6 @@ private int[] fetchDatabaseNameRangeIndexFromURLForH2MemMode(String url) {
return null;
}
}


}
Expand Up @@ -26,7 +26,7 @@ public class URLParser {
private static final String ORACLE_JDBC_URL_PREFIX = "jdbc:oracle";
private static final String H2_JDBC_URL_PREFIX = "jdbc:h2";
private static final String POSTGRESQL_JDBC_URL_PREFIX = "jdbc:postgresql";
private static final Map<String, ConnectionURLParser> parserRegister = new LinkedHashMap<String, ConnectionURLParser>();
private static final Map<String, ConnectionURLParser> parserRegister = new LinkedHashMap<>();

static {
// put mysql parser firstly
Expand Down
Expand Up @@ -136,6 +136,14 @@ public void testParseH2JDBCURLWithEmbeddedRunningInWindows() {
assertEquals("localhost:-1", connectionInfo.getDbPeer());
}

@Test
public void testParseH2JDBCURLWithImplicitFile() {
ConnectionInfo connectionInfo = URLParser.parser("jdbc:h2:c:/data/sample");
assertEquals(H2, connectionInfo.getDbType());
assertEquals("c:/data/sample", connectionInfo.getDbInstance());
assertEquals("localhost:-1", connectionInfo.getDbPeer());
}

@Test
public void testParseH2JDBCURLWithMemoryMode() {
ConnectionInfo connectionInfo = URLParser.parser("jdbc:h2:mem:test_mem");
Expand Down

0 comments on commit c525d56

Please sign in to comment.