|
| 1 | +package io.sentry.jdbc; |
| 2 | + |
| 3 | +import com.p6spy.engine.common.ConnectionInformation; |
| 4 | +import com.p6spy.engine.common.StatementInformation; |
| 5 | +import io.sentry.util.StringUtils; |
| 6 | +import java.net.URI; |
| 7 | +import java.util.Locale; |
| 8 | +import org.jetbrains.annotations.NotNull; |
| 9 | +import org.jetbrains.annotations.Nullable; |
| 10 | + |
| 11 | +public final class DatabaseUtils { |
| 12 | + |
| 13 | + private static final @NotNull DatabaseDetails EMPTY = new DatabaseDetails(null, null); |
| 14 | + |
| 15 | + public static DatabaseDetails readFrom( |
| 16 | + final @Nullable StatementInformation statementInformation) { |
| 17 | + if (statementInformation == null) { |
| 18 | + return EMPTY; |
| 19 | + } |
| 20 | + |
| 21 | + final @Nullable ConnectionInformation connectionInformation = |
| 22 | + statementInformation.getConnectionInformation(); |
| 23 | + if (connectionInformation == null) { |
| 24 | + return EMPTY; |
| 25 | + } |
| 26 | + |
| 27 | + return parse(connectionInformation.getUrl()); |
| 28 | + } |
| 29 | + |
| 30 | + public static DatabaseDetails parse(final @Nullable String databaseConnectionUrl) { |
| 31 | + if (databaseConnectionUrl == null) { |
| 32 | + return EMPTY; |
| 33 | + } |
| 34 | + try { |
| 35 | + final @NotNull String rawUrl = |
| 36 | + removeP6SpyPrefix(databaseConnectionUrl.toLowerCase(Locale.ROOT)); |
| 37 | + final @NotNull String[] urlParts = rawUrl.split(":", -1); |
| 38 | + if (urlParts.length > 1) { |
| 39 | + final @NotNull String dbSystem = urlParts[0]; |
| 40 | + return parseDbSystemSpecific(dbSystem, urlParts, rawUrl); |
| 41 | + } |
| 42 | + } catch (Throwable t) { |
| 43 | + // ignore |
| 44 | + } |
| 45 | + |
| 46 | + return EMPTY; |
| 47 | + } |
| 48 | + |
| 49 | + private static @NotNull DatabaseDetails parseDbSystemSpecific( |
| 50 | + final @NotNull String dbSystem, |
| 51 | + final @NotNull String[] urlParts, |
| 52 | + final @NotNull String urlString) { |
| 53 | + if ("hsqldb".equalsIgnoreCase(dbSystem) |
| 54 | + || "h2".equalsIgnoreCase(dbSystem) |
| 55 | + || "derby".equalsIgnoreCase(dbSystem) |
| 56 | + || "sqlite".equalsIgnoreCase(dbSystem)) { |
| 57 | + if (urlString.contains("//")) { |
| 58 | + return parseAsUri(dbSystem, StringUtils.removePrefix(urlString, dbSystem + ":")); |
| 59 | + } |
| 60 | + if (urlParts.length > 2) { |
| 61 | + String dbNameAndMaybeMore = urlParts[2]; |
| 62 | + return new DatabaseDetails(dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";")); |
| 63 | + } |
| 64 | + if (urlParts.length > 1) { |
| 65 | + String dbNameAndMaybeMore = urlParts[1]; |
| 66 | + return new DatabaseDetails(dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";")); |
| 67 | + } |
| 68 | + } |
| 69 | + if ("mariadb".equalsIgnoreCase(dbSystem) |
| 70 | + || "mysql".equalsIgnoreCase(dbSystem) |
| 71 | + || "postgresql".equalsIgnoreCase(dbSystem) |
| 72 | + || "mongo".equalsIgnoreCase(dbSystem)) { |
| 73 | + return parseAsUri(dbSystem, urlString); |
| 74 | + } |
| 75 | + if ("sqlserver".equalsIgnoreCase(dbSystem)) { |
| 76 | + try { |
| 77 | + String dbProperty = ";databasename="; |
| 78 | + final int index = urlString.indexOf(dbProperty); |
| 79 | + if (index >= 0) { |
| 80 | + final @NotNull String dbNameAndMaybeMore = |
| 81 | + urlString.substring(index + dbProperty.length()); |
| 82 | + return new DatabaseDetails( |
| 83 | + dbSystem, StringUtils.substringBefore(dbNameAndMaybeMore, ";")); |
| 84 | + } |
| 85 | + } catch (Throwable t) { |
| 86 | + // ignore |
| 87 | + } |
| 88 | + } |
| 89 | + if ("oracle".equalsIgnoreCase(dbSystem)) { |
| 90 | + String uriPrefix = "oracle:thin:@//"; |
| 91 | + final int indexOfUri = urlString.indexOf(uriPrefix); |
| 92 | + if (indexOfUri >= 0) { |
| 93 | + final @NotNull String uri = |
| 94 | + "makethisaprotocol://" + urlString.substring(indexOfUri + uriPrefix.length()); |
| 95 | + return parseAsUri(dbSystem, uri); |
| 96 | + } |
| 97 | + |
| 98 | + final int indexOfTnsNames = urlString.indexOf("oracle:thin:@("); |
| 99 | + if (indexOfTnsNames >= 0) { |
| 100 | + String serviceNamePrefix = "(service_name="; |
| 101 | + final int indexOfServiceName = urlString.indexOf(serviceNamePrefix); |
| 102 | + if (indexOfServiceName >= 0) { |
| 103 | + final int indexOfClosingBrace = urlString.indexOf(")", indexOfServiceName); |
| 104 | + final @NotNull String serviceName = |
| 105 | + urlString.substring( |
| 106 | + indexOfServiceName + serviceNamePrefix.length(), indexOfClosingBrace); |
| 107 | + return new DatabaseDetails(dbSystem, serviceName); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + if ("datadirect".equalsIgnoreCase(dbSystem) |
| 112 | + || "tibcosoftware".equalsIgnoreCase(dbSystem) |
| 113 | + || "jtds".equalsIgnoreCase(dbSystem) |
| 114 | + || "microsoft".equalsIgnoreCase(dbSystem)) { |
| 115 | + return parse(StringUtils.removePrefix(urlString, dbSystem + ":")); |
| 116 | + } |
| 117 | + |
| 118 | + return new DatabaseDetails(dbSystem, null); |
| 119 | + } |
| 120 | + |
| 121 | + private static @NotNull DatabaseDetails parseAsUri( |
| 122 | + final @NotNull String dbSystem, final @NotNull String urlString) { |
| 123 | + try { |
| 124 | + final @NotNull URI url = new URI(urlString); |
| 125 | + String path = StringUtils.removePrefix(url.getPath(), "/"); |
| 126 | + String pathWithoutProperties = StringUtils.substringBefore(path, ";"); |
| 127 | + return new DatabaseDetails(dbSystem, pathWithoutProperties); |
| 128 | + } catch (Throwable t) { |
| 129 | + System.out.println(t.getMessage()); |
| 130 | + // ignore |
| 131 | + } |
| 132 | + return new DatabaseDetails(dbSystem, null); |
| 133 | + } |
| 134 | + |
| 135 | + private static @NotNull String removeP6SpyPrefix(final @NotNull String url) { |
| 136 | + return StringUtils.removePrefix(StringUtils.removePrefix(url, "jdbc:"), "p6spy:"); |
| 137 | + } |
| 138 | + |
| 139 | + public static final class DatabaseDetails { |
| 140 | + private final @Nullable String dbSystem; |
| 141 | + private final @Nullable String dbName; |
| 142 | + |
| 143 | + DatabaseDetails(final @Nullable String dbSystem, final @Nullable String dbName) { |
| 144 | + this.dbSystem = dbSystem; |
| 145 | + this.dbName = dbName; |
| 146 | + } |
| 147 | + |
| 148 | + public @Nullable String getDbSystem() { |
| 149 | + return dbSystem; |
| 150 | + } |
| 151 | + |
| 152 | + public @Nullable String getDbName() { |
| 153 | + return dbName; |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments