forked from dork/tarantool-java
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
metadata: Tarantool instance version info
The driver can connect to different Tarantool instances within a range of server versions. Sometimes, it's required to set a driver behaviour depending on those versions (i.e. in scope of #213 the driver needs to generate the functions for JDBC C4 LTRIM/RTRIM that are recognizable by a particular Tarantool version). This commit also includes implementation of two public DatabaseMetaData methods getDatabaseMajorVersion and getDatabaseMinorVersion. Affects: #213 Closes: #106
- Loading branch information
1 parent
4ba88fb
commit 2c4baed
Showing
22 changed files
with
278 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/tarantool/jdbc/TarantoolDatabaseMetaData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.tarantool.jdbc; | ||
|
||
import org.tarantool.util.ServerVersion; | ||
|
||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
|
||
/** | ||
* Tarantool specific database meta data extension. | ||
*/ | ||
public interface TarantoolDatabaseMetaData extends DatabaseMetaData { | ||
|
||
/** | ||
* Gets the current Tarantool version. | ||
* | ||
* @return version of active connected database. | ||
*/ | ||
ServerVersion getDatabaseVersion() throws SQLException; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
src/main/java/org/tarantool/protocol/TarantoolGreeting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,36 @@ | ||
package org.tarantool.protocol; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class TarantoolGreeting { | ||
|
||
private static final Pattern GREETING_LINE = | ||
Pattern.compile("Tarantool\\s+(?<version>[-.0-9a-g]+)\\s+\\((?<protocol>.*)\\)\\s+(?<uuid>[-0-9a-f]*)"); | ||
|
||
private final String serverVersion; | ||
private final String protocolType; | ||
private final String instanceUuid; | ||
|
||
public TarantoolGreeting(String serverVersion) { | ||
this.serverVersion = serverVersion; | ||
public TarantoolGreeting(String greetingLine) { | ||
Matcher matcher = GREETING_LINE.matcher(greetingLine); | ||
if (!matcher.find()) { | ||
throw new IllegalArgumentException("Welcome message '" + greetingLine + "' is incorrect "); | ||
} | ||
serverVersion = matcher.group("version"); | ||
protocolType = matcher.group("protocol"); | ||
instanceUuid = matcher.group("uuid"); | ||
} | ||
|
||
public String getServerVersion() { | ||
return serverVersion; | ||
} | ||
|
||
public String getProtocolType() { | ||
return protocolType; | ||
} | ||
|
||
public String getInstanceUuid() { | ||
return instanceUuid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package org.tarantool.util; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* Server version holder. | ||
*/ | ||
public class ServerVersion implements Comparable<ServerVersion> { | ||
|
||
public static final ServerVersion V_1_9 = new ServerVersion(1, 9, 0); | ||
public static final ServerVersion V_1_10 = new ServerVersion(1, 10, 0); | ||
public static final ServerVersion V_2_1 = new ServerVersion(2, 1, 0); | ||
public static final ServerVersion V_2_2 = new ServerVersion(2, 2, 0); | ||
public static final ServerVersion V_2_2_1 = new ServerVersion(2, 2, 1); | ||
public static final ServerVersion V_2_3 = new ServerVersion(2, 3, 0); | ||
|
||
private final int majorVersion; | ||
private final int minorVersion; | ||
private final int patchVersion; | ||
|
||
/** | ||
* Makes a parsed server version container from | ||
* a string in format like {@code MAJOR.MINOR.PATCH[-BUILD-gCOMMIT]}. | ||
* | ||
* @param version string in the Tarantool version format. | ||
*/ | ||
public ServerVersion(String version) { | ||
String[] parts = splitVersionParts(version); | ||
if (parts.length < 3) { | ||
throw new IllegalArgumentException("Expected at least major, minor, and patch version parts"); | ||
} | ||
this.majorVersion = Integer.parseInt(parts[0]); | ||
this.minorVersion = Integer.parseInt(parts[1]); | ||
this.patchVersion = Integer.parseInt(parts[2]); | ||
} | ||
|
||
public ServerVersion(int majorVersion, | ||
int minorVersion, | ||
int patchVersion) { | ||
this.majorVersion = majorVersion; | ||
this.minorVersion = minorVersion; | ||
this.patchVersion = patchVersion; | ||
} | ||
|
||
public int getMajorVersion() { | ||
return majorVersion; | ||
} | ||
|
||
public int getMinorVersion() { | ||
return minorVersion; | ||
} | ||
|
||
public int getPatchVersion() { | ||
return patchVersion; | ||
} | ||
|
||
public boolean isEqual(String versionString) { | ||
return isEqual(new ServerVersion(versionString)); | ||
} | ||
|
||
public boolean isEqual(ServerVersion version) { | ||
return compareTo(version) == 0; | ||
} | ||
|
||
public boolean isLessOrEqualThan(String versionString) { | ||
return isLessOrEqualThan(new ServerVersion(versionString)); | ||
} | ||
|
||
public boolean isLessOrEqualThan(ServerVersion version) { | ||
return compareTo(version) <= 0; | ||
} | ||
|
||
public boolean isGreaterOrEqualThan(String versionString) { | ||
return isGreaterOrEqualThan(new ServerVersion(versionString)); | ||
} | ||
|
||
public boolean isGreaterOrEqualThan(ServerVersion version) { | ||
return compareTo(version) >= 0; | ||
} | ||
|
||
public boolean isGreaterThan(String versionString) { | ||
return isGreaterThan(new ServerVersion(versionString)); | ||
} | ||
|
||
public boolean isGreaterThan(ServerVersion version) { | ||
return compareTo(version) > 0; | ||
} | ||
|
||
public boolean isLessThan(String versionString) { | ||
return isLessThan(new ServerVersion(versionString)); | ||
} | ||
|
||
public boolean isLessThan(ServerVersion version) { | ||
return compareTo(version) < 0; | ||
} | ||
|
||
@Override | ||
public int compareTo(ServerVersion that) { | ||
return Integer.compare(this.toNumber(), that.toNumber()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object) { | ||
if (this == object) { | ||
return true; | ||
} | ||
if (object == null || getClass() != object.getClass()) { | ||
return false; | ||
} | ||
ServerVersion that = (ServerVersion) object; | ||
return majorVersion == that.majorVersion && | ||
minorVersion == that.minorVersion && | ||
patchVersion == that.patchVersion; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(majorVersion, minorVersion, patchVersion); | ||
} | ||
|
||
/** | ||
* Translates version parts to format XXXYYYZZZ. | ||
* For example, {@code 1.2.3} translates to number {@code 1002003} | ||
* | ||
* @return version as number | ||
*/ | ||
private int toNumber() { | ||
return (majorVersion * 1000 + minorVersion) * 1000 + patchVersion; | ||
} | ||
|
||
/** | ||
* Splits Tarantool version string into parts. | ||
* For example, {@code 2.1.1-423-g4007436aa} => {@code [2, 1, 1, 423, g4007436aa]}. | ||
* | ||
* @param version Tarantool version string | ||
* @return split parts | ||
*/ | ||
private String[] splitVersionParts(String version) { | ||
return version.split("[.\\-]"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.