Skip to content

Commit abb0b1f

Browse files
authored
Merge pull request #2793 from ClickHouse/03/17/26/update_jdbc_user_agent
fixed default jdbc driver client name
2 parents 6e8ea2d + bbf7f3f commit abb0b1f

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
7979
this.appName = "";
8080
this.readOnly = false;
8181
this.holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
82-
String clientName = "ClickHouse JDBC Driver V2/" + Driver.getLibraryVersion();
82+
String clientName = Driver.DRIVER_CLIENT_NAME + Driver.getLibraryVersion();
8383

8484
Map<String, String> clientProperties = config.getClientProperties();
8585
if (clientProperties.get(ClientConfigProperties.CLIENT_NAME.getKey()) != null) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -306,33 +306,53 @@ public void isValidTest() throws SQLException {
306306
}
307307

308308
@Test(groups = { "integration" }, dataProvider = "setAndGetClientInfoTestDataProvider")
309-
public void setAndGetClientInfoTest(String clientName) throws SQLException {
309+
public void setAndGetClientInfoTest(String clientName) throws Exception {
310310
final String unsupportedProperty = "custom-unsupported-property";
311+
312+
// case when set via config
313+
Properties clientNameConfig = new Properties();
314+
if (clientName != null) {
315+
clientNameConfig.setProperty(ClientConfigProperties.CLIENT_NAME.getKey(), clientName);
316+
}
317+
try (Connection localConnection = this.getJdbcConnection(clientNameConfig);
318+
Statement stmt = localConnection.createStatement()) {
319+
320+
executeQueryAndVerifyUserAgent(stmt, clientName);
321+
}
322+
323+
// case when set in runtime
311324
try (Connection localConnection = this.getJdbcConnection();
312325
Statement stmt = localConnection.createStatement()) {
326+
327+
executeQueryAndVerifyUserAgent(stmt, ""); // default value
328+
313329
localConnection.setClientInfo(unsupportedProperty, "i-am-unsupported-property");
314330
Assert.assertNull(localConnection.getClientInfo("custom-property"));
315331
localConnection.setClientInfo(ClientInfoProperties.APPLICATION_NAME.getKey(), clientName);
316332
Assert.assertEquals(localConnection.getClientInfo(ClientInfoProperties.APPLICATION_NAME.getKey()), clientName);
317333
Assert.assertNull(localConnection.getClientInfo(unsupportedProperty));
318334

319-
final String testQuery = "SELECT '" + UUID.randomUUID() + "'";
320-
stmt.execute(testQuery);
321-
String queryId = ((StatementImpl)stmt).getLastQueryId();
322-
stmt.getResultSet().close(); // close result set to finalize request.
323-
stmt.execute("SYSTEM FLUSH LOGS");
335+
executeQueryAndVerifyUserAgent(stmt, clientName);
336+
}
337+
}
324338

339+
private void executeQueryAndVerifyUserAgent(Statement stmt, String clientName) throws Exception {
340+
final String testQuery = "SELECT '" + UUID.randomUUID() + "'";
341+
stmt.execute(testQuery);
342+
String queryId = ((StatementImpl)stmt).getLastQueryId();
343+
stmt.getResultSet().close(); // close result set to finalize request.
344+
stmt.execute("SYSTEM FLUSH LOGS");
325345

326-
final String logQuery ="SELECT http_user_agent FROM clusterAllReplicas('default', system.query_log) WHERE query_id = " + stmt.enquoteLiteral(queryId);
327-
try (ResultSet rs = stmt.executeQuery(logQuery)) {
328-
Assert.assertTrue(rs.next());
329-
String userAgent = rs.getString("http_user_agent");
330-
if (clientName != null && !clientName.isEmpty()) {
331-
Assert.assertTrue(userAgent.startsWith(clientName), "Expected to start with '" + clientName + "' but value was '" + userAgent + "'");
332-
}
333-
Assert.assertTrue(userAgent.contains(Client.CLIENT_USER_AGENT), "Expected to contain '" + Client.CLIENT_USER_AGENT + "' but value was '" + userAgent + "'");
334-
Assert.assertTrue(userAgent.contains(Driver.DRIVER_CLIENT_NAME), "Expected to contain '" + Driver.DRIVER_CLIENT_NAME + "' but value was '" + userAgent + "'");
346+
347+
final String logQuery ="SELECT http_user_agent FROM clusterAllReplicas('default', system.query_log) WHERE query_id = " + stmt.enquoteLiteral(queryId);
348+
try (ResultSet rs = stmt.executeQuery(logQuery)) {
349+
Assert.assertTrue(rs.next());
350+
String userAgent = rs.getString("http_user_agent");
351+
if (clientName != null && !clientName.isEmpty()) {
352+
Assert.assertTrue(userAgent.startsWith(clientName), "Expected to start with '" + clientName + "' but value was '" + userAgent + "'");
335353
}
354+
Assert.assertTrue(userAgent.contains(Client.CLIENT_USER_AGENT), "Expected to contain '" + Client.CLIENT_USER_AGENT + "' but value was '" + userAgent + "'");
355+
Assert.assertTrue(userAgent.contains(Driver.DRIVER_CLIENT_NAME), "Expected to contain '" + Driver.DRIVER_CLIENT_NAME + "' but value was '" + userAgent + "'");
336356
}
337357
}
338358

0 commit comments

Comments
 (0)