-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathDatabaseUtils.java
More file actions
161 lines (143 loc) · 5.54 KB
/
DatabaseUtils.java
File metadata and controls
161 lines (143 loc) · 5.54 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package io.sentry.jdbc;
import com.p6spy.engine.common.ConnectionInformation;
import com.p6spy.engine.common.StatementInformation;
import io.sentry.util.StringUtils;
import java.net.URI;
import java.util.Locale;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class DatabaseUtils {
private static final @NotNull DatabaseDetails EMPTY = new DatabaseDetails(null, null);
public static DatabaseDetails readFrom(
final @Nullable StatementInformation statementInformation) {
if (statementInformation == null) {
return EMPTY;
}
final @Nullable ConnectionInformation connectionInformation =
statementInformation.getConnectionInformation();
return readFrom(connectionInformation);
}
public static DatabaseDetails readFrom(
final @Nullable ConnectionInformation connectionInformation) {
if (connectionInformation == null) {
return EMPTY;
}
return parse(connectionInformation.getUrl());
}
public static DatabaseDetails parse(final @Nullable String databaseConnectionUrl) {
if (databaseConnectionUrl == null) {
return EMPTY;
}
try {
final @NotNull String rawUrl =
removeP6SpyPrefix(databaseConnectionUrl.toLowerCase(Locale.ROOT));
final @NotNull String[] urlParts = rawUrl.split(":", -1);
if (urlParts.length > 1) {
final @NotNull String dbSystem = urlParts[0];
return parseDbSystemSpecific(dbSystem, urlParts, rawUrl);
}
} catch (Throwable t) {
// ignore
}
return EMPTY;
}
private static @NotNull DatabaseDetails parseDbSystemSpecific(
final @NotNull String dbSystem,
final @NotNull String[] urlParts,
final @NotNull String urlString) {
if ("hsqldb".equalsIgnoreCase(dbSystem)
|| "h2".equalsIgnoreCase(dbSystem)
|| "derby".equalsIgnoreCase(dbSystem)
|| "sqlite".equalsIgnoreCase(dbSystem)) {
if (urlString.contains("//")) {
return parseAsUri(dbSystem, StringUtils.removePrefix(urlString, dbSystem + ":"));
}
if (urlParts.length > 2) {
String dbNameAndMaybeMore = urlParts[2];
return new DatabaseDetails(dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";"));
}
if (urlParts.length > 1) {
String dbNameAndMaybeMore = urlParts[1];
return new DatabaseDetails(dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";"));
}
}
if ("mariadb".equalsIgnoreCase(dbSystem)
|| "mysql".equalsIgnoreCase(dbSystem)
|| "postgresql".equalsIgnoreCase(dbSystem)
|| "mongo".equalsIgnoreCase(dbSystem)) {
return parseAsUri(dbSystem, urlString);
}
if ("sqlserver".equalsIgnoreCase(dbSystem)) {
try {
String dbProperty = ";databasename=";
final int index = urlString.indexOf(dbProperty);
if (index >= 0) {
final @NotNull String dbNameAndMaybeMore =
urlString.substring(index + dbProperty.length());
return new DatabaseDetails(
dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";"));
}
} catch (Throwable t) {
// ignore
}
}
if ("oracle".equalsIgnoreCase(dbSystem)) {
String uriPrefix = "oracle:thin:@//";
final int indexOfUri = urlString.indexOf(uriPrefix);
if (indexOfUri >= 0) {
final @NotNull String uri =
"makethisaprotocol://" + urlString.substring(indexOfUri + uriPrefix.length());
return parseAsUri(dbSystem, uri);
}
final int indexOfTnsNames = urlString.indexOf("oracle:thin:@(");
if (indexOfTnsNames >= 0) {
String serviceNamePrefix = "(service_name=";
final int indexOfServiceName = urlString.indexOf(serviceNamePrefix);
if (indexOfServiceName >= 0) {
final int indexOfClosingBrace = urlString.indexOf(")", indexOfServiceName);
final @NotNull String serviceName =
urlString.substring(
indexOfServiceName + serviceNamePrefix.length(), indexOfClosingBrace);
return new DatabaseDetails(dbSystem, serviceName);
}
}
}
if ("datadirect".equalsIgnoreCase(dbSystem)
|| "tibcosoftware".equalsIgnoreCase(dbSystem)
|| "jtds".equalsIgnoreCase(dbSystem)
|| "microsoft".equalsIgnoreCase(dbSystem)) {
return parse(StringUtils.removePrefix(urlString, dbSystem + ":"));
}
return new DatabaseDetails(dbSystem, null);
}
private static @NotNull DatabaseDetails parseAsUri(
final @NotNull String dbSystem, final @NotNull String urlString) {
try {
final @NotNull URI url = new URI(urlString);
String path = StringUtils.removePrefix(url.getPath(), "/");
String pathWithoutProperties = StringUtils.substringBefore(path, ";");
return new DatabaseDetails(dbSystem, pathWithoutProperties);
} catch (Throwable t) {
System.out.println(t.getMessage());
// ignore
}
return new DatabaseDetails(dbSystem, null);
}
private static @NotNull String removeP6SpyPrefix(final @NotNull String url) {
return StringUtils.removePrefix(StringUtils.removePrefix(url, "jdbc:"), "p6spy:");
}
public static final class DatabaseDetails {
private final @Nullable String dbSystem;
private final @Nullable String dbName;
DatabaseDetails(final @Nullable String dbSystem, final @Nullable String dbName) {
this.dbSystem = dbSystem;
this.dbName = dbName;
}
public @Nullable String getDbSystem() {
return dbSystem;
}
public @Nullable String getDbName() {
return dbName;
}
}
}