-
Notifications
You must be signed in to change notification settings - Fork 199
/
MySQLDatabaseMetadata.java
96 lines (81 loc) · 2.85 KB
/
MySQLDatabaseMetadata.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package io.vertx.mysqlclient.impl;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.sqlclient.spi.DatabaseMetadata;
public class MySQLDatabaseMetadata implements DatabaseMetadata {
private static final Logger LOGGER = LoggerFactory.getLogger(MySQLDatabaseMetadata.class);
private final String fullVersion;
private final String productName;
private final int majorVersion;
private final int minorVersion;
private final int microVersion;
private MySQLDatabaseMetadata(String fullVersion,
String productName,
int majorVersion,
int minorVersion,
int microVersion) {
this.fullVersion = fullVersion;
this.productName = productName;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.microVersion = microVersion;
}
public static MySQLDatabaseMetadata parse(String serverVersion) {
int majorVersion = 0;
int minorVersion = 0;
int microVersion = 0;
int len = serverVersion.length();
boolean isMariaDb = serverVersion.contains("MariaDB");
String productName = isMariaDb ? "MariaDB" : "MySQL";
String fullServerVersion = serverVersion;
if (isMariaDb) {
// MariaDB server version < 11.x.x is by default prefixed by "5.5.5-"
serverVersion = serverVersion.replace("5.5.5-", "");
}
String versionToken = null;
int versionTokenStartIdx = 0;
int versionTokenEndIdx = versionTokenStartIdx;
while (versionTokenEndIdx < len) {
char c = serverVersion.charAt(versionTokenEndIdx);
if (c == '-' || c == ' ') {
versionToken = serverVersion.substring(versionTokenStartIdx, versionTokenEndIdx);
break;
}
versionTokenEndIdx++;
}
if (versionToken == null) {
// if there's no '-' char
versionToken = serverVersion;
}
// we assume the server version tokens follows the syntax: ${major}.${minor}.${micro}
String[] versionTokens = versionToken.split("\\.");
try {
majorVersion = Integer.parseInt(versionTokens[0]);
minorVersion = Integer.parseInt(versionTokens[1]);
microVersion = Integer.parseInt(versionTokens[2]);
} catch (Exception ex) {
// make sure it does fail the connection phase
LOGGER.warn("Incorrect parsing server version tokens", ex);
}
return new MySQLDatabaseMetadata(fullServerVersion, productName, majorVersion, minorVersion, microVersion);
}
@Override
public String productName() {
return productName;
}
@Override
public String fullVersion() {
return fullVersion;
}
@Override
public int majorVersion() {
return majorVersion;
}
@Override
public int minorVersion() {
return minorVersion;
}
public int microVersion() {
return microVersion;
}
}