|
@@ -61,6 +61,7 @@ import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
import javax.sql.DataSource;
|
|
|
|
|
+import java.sql.Connection;
|
|
|
import java.time.Duration;
|
|
import java.time.Duration;
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -492,7 +493,7 @@ public class AppConfiguration {
|
|
|
@Bean
|
|
@Bean
|
|
|
public AdBidReportStore adBidReportStore(@Nullable DataSource tidbDataSource) {
|
|
public AdBidReportStore adBidReportStore(@Nullable DataSource tidbDataSource) {
|
|
|
if (tidbDataSource == null) return null;
|
|
if (tidbDataSource == null) return null;
|
|
|
- String mainSchema = extractDatabaseName(props.getTidbUrl());
|
|
|
|
|
|
|
+ String mainSchema = resolveDatabaseName(tidbDataSource, props.getTidbUrl());
|
|
|
if (mainSchema == null || mainSchema.isBlank()) return null;
|
|
if (mainSchema == null || mainSchema.isBlank()) return null;
|
|
|
AdBidReportStore store = new AdBidReportStore(
|
|
AdBidReportStore store = new AdBidReportStore(
|
|
|
tidbDataSource,
|
|
tidbDataSource,
|
|
@@ -546,16 +547,46 @@ public class AppConfiguration {
|
|
|
return p;
|
|
return p;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private static String resolveDatabaseName(DataSource dataSource, String jdbcUrl) {
|
|
|
|
|
+ try (Connection connection = dataSource.getConnection()) {
|
|
|
|
|
+ String catalog = connection.getCatalog();
|
|
|
|
|
+ if (catalog != null && !catalog.isBlank()) {
|
|
|
|
|
+ return catalog.trim();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
|
+ }
|
|
|
|
|
+ return extractDatabaseName(jdbcUrl);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
private static String extractDatabaseName(String jdbcUrl) {
|
|
private static String extractDatabaseName(String jdbcUrl) {
|
|
|
if (jdbcUrl == null || jdbcUrl.isBlank()) {
|
|
if (jdbcUrl == null || jdbcUrl.isBlank()) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
- int slash = jdbcUrl.lastIndexOf('/');
|
|
|
|
|
- if (slash < 0 || slash + 1 >= jdbcUrl.length()) {
|
|
|
|
|
|
|
+ String value = jdbcUrl.trim();
|
|
|
|
|
+ int scheme = value.indexOf("://");
|
|
|
|
|
+ int searchFrom = scheme >= 0 ? scheme + 3 : 0;
|
|
|
|
|
+ int slash = value.indexOf('/', searchFrom);
|
|
|
|
|
+ if (slash < 0 || slash + 1 >= value.length()) {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
- int question = jdbcUrl.indexOf('?', slash + 1);
|
|
|
|
|
- String db = question >= 0 ? jdbcUrl.substring(slash + 1, question) : jdbcUrl.substring(slash + 1);
|
|
|
|
|
|
|
+ int end = value.length();
|
|
|
|
|
+ int question = value.indexOf('?', slash + 1);
|
|
|
|
|
+ if (question >= 0) {
|
|
|
|
|
+ end = Math.min(end, question);
|
|
|
|
|
+ }
|
|
|
|
|
+ int hash = value.indexOf('#', slash + 1);
|
|
|
|
|
+ if (hash >= 0) {
|
|
|
|
|
+ end = Math.min(end, hash);
|
|
|
|
|
+ }
|
|
|
|
|
+ int semicolon = value.indexOf(';', slash + 1);
|
|
|
|
|
+ if (semicolon >= 0) {
|
|
|
|
|
+ end = Math.min(end, semicolon);
|
|
|
|
|
+ }
|
|
|
|
|
+ int nextSlash = value.indexOf('/', slash + 1);
|
|
|
|
|
+ if (nextSlash >= 0 && nextSlash < end) {
|
|
|
|
|
+ end = nextSlash;
|
|
|
|
|
+ }
|
|
|
|
|
+ String db = value.substring(slash + 1, end);
|
|
|
return db == null || db.isBlank() ? null : db.trim();
|
|
return db == null || db.isBlank() ? null : db.trim();
|
|
|
}
|
|
}
|
|
|
|
|
|