Skip to content

Commit a829daa

Browse files
committed
move logic to jdbc + structure a singleton
1 parent 9d96d03 commit a829daa

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

clickhouse-http-client/src/main/java/com/clickhouse/client/http/ClickHouseHttpConnection.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public abstract class ClickHouseHttpConnection implements AutoCloseable {
5353
private static final byte[] SUFFIX_FORMAT = "_format\"\r\n\r\n".getBytes(StandardCharsets.US_ASCII);
5454
private static final byte[] SUFFIX_STRUCTURE = "_structure\"\r\n\r\n".getBytes(StandardCharsets.US_ASCII);
5555
private static final byte[] SUFFIX_FILENAME = "\"; filename=\"".getBytes(StandardCharsets.US_ASCII);
56-
private static final List<String> FRAMEWORKS_TO_INFER = List.of("apache.spark");
5756

5857
private static StringBuilder appendQueryParameter(StringBuilder builder, String key, String value) {
5958
return builder.append(urlEncode(key, StandardCharsets.UTF_8)).append('=')
@@ -411,22 +410,10 @@ protected String getDefaultUserAgent() {
411410
return config.getClientName();
412411
}
413412

414-
protected String getAdditionalFrameworkUserAgent() {
415-
Set<String> inferredFrameworks = new LinkedHashSet<>();
416-
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
417-
for (String framework : FRAMEWORKS_TO_INFER) {
418-
if (ste.toString().contains(framework)) {
419-
inferredFrameworks.add(String.format("(%s)", framework));
420-
}
421-
}
422-
}
423-
return String.join("; ", inferredFrameworks);
424-
}
425-
426413
protected final String getUserAgent() {
427414
final ClickHouseConfig c = config;
428415
String name = c.getClientName();
429-
String userAgent = getDefaultUserAgent() + getAdditionalFrameworkUserAgent();
416+
String userAgent = getDefaultUserAgent();
430417

431418
if (!ClickHouseClientOption.CLIENT_NAME.getDefaultValue().equals(name)) {
432419
return name + " " + userAgent;

clickhouse-jdbc/src/main/java/com/clickhouse/jdbc/ClickHouseDriver.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
import java.sql.DriverPropertyInfo;
77
import java.sql.SQLException;
88
import java.sql.SQLFeatureNotSupportedException;
9-
import java.util.ArrayList;
10-
import java.util.Collections;
11-
import java.util.HashMap;
12-
import java.util.LinkedHashMap;
13-
import java.util.List;
14-
import java.util.Map;
15-
import java.util.Properties;
16-
import java.util.ServiceLoader;
9+
import java.util.*;
1710
import java.util.Map.Entry;
1811

1912
import com.clickhouse.client.ClickHouseClient;
@@ -49,6 +42,32 @@ public class ClickHouseDriver implements Driver {
4942

5043
static final java.util.logging.Logger parentLogger = java.util.logging.Logger.getLogger("com.clickhouse.jdbc");
5144

45+
static String frameworksDetected = null;
46+
47+
private static class FrameworksDetection {
48+
private static final List<String> FRAMEWORKS_TO_DETECT = List.of("apache.spark");
49+
static volatile String frameworksDetected = null;
50+
51+
private FrameworksDetection() {
52+
}
53+
public static String getFrameworksDetected() {
54+
if (frameworksDetected == null) {
55+
Set<String> inferredFrameworks = new LinkedHashSet<>();
56+
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
57+
for (String framework : FRAMEWORKS_TO_DETECT) {
58+
if (ste.toString().contains(framework)) {
59+
inferredFrameworks.add(String.format("(%s)", framework));
60+
}
61+
}
62+
}
63+
64+
frameworksDetected = String.join("; ", inferredFrameworks);
65+
}
66+
return frameworksDetected;
67+
}
68+
69+
}
70+
5271
static {
5372
String str = ClickHouseDriver.class.getPackage().getImplementationVersion();
5473
if (str != null && !str.isEmpty()) {
@@ -115,7 +134,8 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Properties pro
115134
options.put(o, ClickHouseOption.fromString(e.getValue().toString(), o.getValueType()));
116135
}
117136
}
118-
137+
if (frameworksDetected != null)
138+
options.put(ClickHouseClientOption.PRODUCT_NAME, frameworksDetected);
119139
return options;
120140
}
121141

@@ -128,7 +148,7 @@ private DriverPropertyInfo create(ClickHouseOption option, Properties props) {
128148

129149
Class<?> clazz = option.getValueType();
130150
if (Boolean.class == clazz || boolean.class == clazz) {
131-
propInfo.choices = new String[] { "true", "false" };
151+
propInfo.choices = new String[]{"true", "false"};
132152
} else if (clazz.isEnum()) {
133153
Object[] values = clazz.getEnumConstants();
134154
String[] names = new String[values.length];
@@ -152,6 +172,8 @@ public ClickHouseConnection connect(String url, Properties info) throws SQLExcep
152172
if (!acceptsURL(url)) {
153173
return null;
154174
}
175+
if (!url.toLowerCase().contains("disable-frameworks-detection"))
176+
frameworksDetected = FrameworksDetection.getFrameworksDetected();
155177

156178
log.debug("Creating connection");
157179
return new ClickHouseConnectionImpl(url, info);

0 commit comments

Comments
 (0)