Skip to content

Commit 4bf118e

Browse files
committed
implemented simple variant of client properties
1 parent f330fef commit 4bf118e

File tree

6 files changed

+114
-39
lines changed

6 files changed

+114
-39
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,10 @@ public void setDBRoles(Collection<String> dbRoles) {
21172117
this.configuration.get(ClientConfigProperties.SESSION_DB_ROLES.getKey())));
21182118
}
21192119

2120+
public void updateClientName(String name) {
2121+
this.configuration.put(ClientConfigProperties.CLIENT_NAME.getKey(), name);
2122+
}
2123+
21202124
private Collection<String> unmodifiableDbRolesView = Collections.emptyList();
21212125

21222126
/**

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

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import com.clickhouse.client.api.Client;
44
import com.clickhouse.client.api.query.GenericRecord;
55
import com.clickhouse.client.api.query.QuerySettings;
6+
import com.clickhouse.jdbc.internal.ClientInfoProperties;
67
import com.clickhouse.jdbc.internal.JdbcConfiguration;
78
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910

1011
import java.sql.*;
1112
import java.util.Collections;
1213
import java.util.HashMap;
14+
import java.util.HashSet;
1315
import java.util.List;
1416
import java.util.Collection;
1517
import java.util.Map;
1618
import java.util.Properties;
19+
import java.util.Set;
1720
import java.util.concurrent.Executor;
1821

1922
public class ConnectionImpl implements Connection, JdbcV2Wrapper {
@@ -169,7 +172,7 @@ public boolean isReadOnly() throws SQLException {
169172
@Override
170173
public void setCatalog(String catalog) throws SQLException {
171174
checkOpen();
172-
this.catalog = catalog;
175+
// this.catalog = catalog; currently not supported
173176
}
174177

175178
@Override
@@ -347,27 +350,34 @@ public boolean isValid(int timeout) throws SQLException {
347350

348351
@Override
349352
public void setClientInfo(String name, String value) throws SQLClientInfoException {
350-
// try {
351-
// checkOpen();
352-
// this.defaultQuerySettings.setOption(name, value);
353-
// } catch (Exception e) {
354-
// throw new SQLClientInfoException("Failed to set client info.", Collections.singletonMap(name, ClientInfoStatus.REASON_UNKNOWN), e);
355-
// }
356-
throw new SQLClientInfoException("Failed to set client info.", new HashMap<>(), new SQLFeatureNotSupportedException("setClientInfo not supported"));
353+
if (ClientInfoProperties.APPLICATION_NAME.getKey().equals(name)) {
354+
client.updateClientName(value);
355+
}
356+
// TODO: generate warning for unknown properties
357357
}
358358

359359
@Override
360360
public void setClientInfo(Properties properties) throws SQLClientInfoException {
361-
// try {
362-
// checkOpen();
363-
// } catch (SQLException e) {
364-
// throw new SQLClientInfoException("Failed to set client info.", new HashMap<>(), e);
365-
// }
366-
//
367-
// for (Map.Entry<Object, Object> entry : properties.entrySet()) {
368-
// setClientInfo(entry.getKey().toString(), entry.getValue().toString());
369-
// }
370-
throw new SQLClientInfoException("Failed to set client info.", new HashMap<>(), new SQLFeatureNotSupportedException("setClientInfo not supported"));
361+
Set<String> toSet = new HashSet<>();
362+
Set<String> toReset = new HashSet<>();
363+
for (ClientInfoProperties p : ClientInfoProperties .values()) {
364+
String key = p.getKey();
365+
if (properties.containsKey(key)) {
366+
toSet.add(key);
367+
} else {
368+
toReset.add(key);
369+
}
370+
}
371+
372+
// first we reset value
373+
for (String key : toReset) {
374+
setClientInfo(key, null);
375+
}
376+
377+
// then we set value, so aliases will not clean values accidentally
378+
for (String key : toSet) {
379+
setClientInfo(key, properties.getProperty(key));
380+
}
371381
}
372382

373383
@Override

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ public class StatementImpl implements Statement, JdbcV2Wrapper {
3232
private List<String> batch;
3333
private String lastQueryId;
3434

35-
public StatementImpl(ConnectionImpl connection) {
35+
private String schema;
36+
37+
public StatementImpl(ConnectionImpl connection) throws SQLException {
3638
this.connection = connection;
3739
this.queryTimeout = 0;
3840
this.closed = false;
3941
this.currentResultSet = null;
4042
this.metrics = null;
4143
this.batch = new ArrayList<>();
44+
this.schema = connection.getSchema(); // remember DB name
4245
}
4346

4447
protected void checkClosed() throws SQLException {
@@ -107,7 +110,7 @@ protected static String parseJdbcEscapeSyntax(String sql) {
107110
@Override
108111
public ResultSet executeQuery(String sql) throws SQLException {
109112
checkClosed();
110-
return executeQuery(sql, connection.getDefaultQuerySettings());
113+
return executeQuery(sql, new QuerySettings().setDatabase(schema));
111114
}
112115

113116
public ResultSet executeQuery(String sql, QuerySettings settings) throws SQLException {
@@ -136,7 +139,7 @@ public ResultSet executeQuery(String sql, QuerySettings settings) throws SQLExce
136139
@Override
137140
public int executeUpdate(String sql) throws SQLException {
138141
checkClosed();
139-
return executeUpdate(sql, connection.getDefaultQuerySettings());
142+
return executeUpdate(sql, new QuerySettings().setDatabase(schema));
140143
}
141144

142145
public int executeUpdate(String sql, QuerySettings settings) throws SQLException {
@@ -148,18 +151,13 @@ public int executeUpdate(String sql, QuerySettings settings) throws SQLException
148151

149152
QuerySettings mergedSettings = QuerySettings.merge(connection.getDefaultQuerySettings(), settings);
150153

151-
try {
152-
sql = parseJdbcEscapeSyntax(sql);
153-
QueryResponse response;
154-
if (queryTimeout == 0) {
155-
response = connection.client.query(sql, mergedSettings).get();
156-
} else {
157-
response = connection.client.query(sql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
158-
}
154+
sql = parseJdbcEscapeSyntax(sql);
155+
try (QueryResponse response = queryTimeout == 0 ? connection.client.query(sql, mergedSettings).get()
156+
: connection.client.query(sql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS)) {
157+
159158
currentResultSet = null;
160159
metrics = response.getMetrics();
161160
lastQueryId = response.getQueryId();
162-
response.close();
163161
} catch (Exception e) {
164162
throw new RuntimeException(e);
165163
}
@@ -249,16 +247,17 @@ public void setCursorName(String name) throws SQLException {
249247
@Override
250248
public boolean execute(String sql) throws SQLException {
251249
checkClosed();
252-
return execute(sql, new QuerySettings());
250+
return execute(sql, new QuerySettings().setDatabase(schema));
253251
}
254252

255-
public boolean execute(String sql, QuerySettings settings) throws SQLException {
253+
private boolean execute(String sql, QuerySettings settings) throws SQLException {
256254
checkClosed();
257255
StatementType type = parseStatementType(sql);
258256

259257
if (type == StatementType.SELECT) {
260-
executeQuery(sql, settings);
261-
return true;
258+
try (ResultSet rs = executeQuery(sql, settings)) {
259+
return true;
260+
}
262261
} else if(type == StatementType.SET) {
263262
//SET ROLE
264263
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.clickhouse.jdbc.internal;
2+
3+
public enum ClientInfoProperties {
4+
5+
APPLICATION_NAME("ApplicationName", 255, "", "Client application name."),
6+
;
7+
8+
private String key;
9+
private int maxValue;
10+
11+
private String defaultValue;
12+
13+
private String description;
14+
15+
ClientInfoProperties(String key, int maxValue, String defaultValue, String description) {
16+
this.key = key;
17+
this.maxValue = maxValue;
18+
this.defaultValue = defaultValue;
19+
this.description = description;
20+
}
21+
22+
public String getKey() {
23+
return key;
24+
}
25+
26+
public int getMaxValue() {
27+
return maxValue;
28+
}
29+
30+
public String getDefaultValue() {
31+
return defaultValue;
32+
}
33+
34+
public String getDescription() {
35+
return description;
36+
}
37+
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaData.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.jdbc.ConnectionImpl;
44
import com.clickhouse.jdbc.Driver;
55
import com.clickhouse.jdbc.JdbcV2Wrapper;
6+
import com.clickhouse.jdbc.internal.ClientInfoProperties;
67
import com.clickhouse.jdbc.internal.JdbcUtils;
78
import com.clickhouse.logging.Logger;
89
import com.clickhouse.logging.LoggerFactory;
@@ -11,6 +12,9 @@
1112
import java.sql.ResultSet;
1213
import java.sql.RowIdLifetime;
1314
import java.sql.SQLException;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import java.util.stream.Collectors;
1418

1519
public class DatabaseMetaData implements java.sql.DatabaseMetaData, JdbcV2Wrapper {
1620
private static final Logger log = LoggerFactory.getLogger(DatabaseMetaData.class);
@@ -1050,11 +1054,26 @@ public boolean autoCommitFailureClosesAllResultSets() throws SQLException {
10501054
return false;
10511055
}
10521056

1057+
private static final String CLIENT_INFO_PROPERTIES_SQL = getClientInfoPropertiesSql();
1058+
1059+
private static String getClientInfoPropertiesSql() {
1060+
StringBuilder sql = new StringBuilder();
1061+
sql.append("SELECT c1 as NAME, c2 as MAX_LEN, c3 as DEFAULT_VALUE, c4 as DESCRIPTION FROM VALUES (");
1062+
Arrays.stream(ClientInfoProperties.values()).forEach(p -> {
1063+
sql.append("('").append(p.getKey()).append("', ");
1064+
sql.append(p.getMaxValue()).append(", ");
1065+
sql.append("'").append(p.getDefaultValue()).append("', ");
1066+
sql.append("'").append(p.getDescription()).append("'), ");
1067+
});
1068+
sql.setLength(sql.length() - 2);
1069+
sql.append(")");
1070+
return sql.toString();
1071+
}
1072+
10531073
@Override
10541074
public ResultSet getClientInfoProperties() throws SQLException {
1055-
//Return an empty result set with the required columns
1056-
log.warn("getClientInfoProperties is not supported and may return invalid results");
1057-
return connection.createStatement().executeQuery("SELECT NULL AS NAME, NULL AS MAX_LEN, NULL AS DEFAULT_VALUE, NULL AS DESCRIPTION");
1075+
1076+
return connection.createStatement().executeQuery(CLIENT_INFO_PROPERTIES_SQL);
10581077
}
10591078

10601079
@Override

jdbc-v2/src/test/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.jdbc.metadata;
22

33
import com.clickhouse.jdbc.JdbcIntegrationTest;
4+
import com.clickhouse.jdbc.internal.ClientInfoProperties;
45
import org.testng.Assert;
56
import org.testng.annotations.Ignore;
67
import org.testng.annotations.Test;
@@ -198,9 +199,14 @@ public void testGetClientInfoProperties() throws Exception {
198199
try (Connection conn = getJdbcConnection()) {
199200
DatabaseMetaData dbmd = conn.getMetaData();
200201
try (ResultSet rs = dbmd.getClientInfoProperties()) {
201-
Assert.assertTrue(rs.next());
202+
for (ClientInfoProperties p : ClientInfoProperties.values()) {
203+
Assert.assertTrue(rs.next());
204+
Assert.assertEquals(rs.getString("NAME"), p.getKey());
205+
Assert.assertEquals(rs.getInt("MAX_LEN"), p.getMaxValue());
206+
Assert.assertEquals(rs.getString("DEFAULT_VALUE"), p.getDefaultValue());
207+
Assert.assertEquals(rs.getString("DESCRIPTION"), p.getDescription());
208+
}
202209
}
203210
}
204-
Assert.fail("Not implemented");
205211
}
206212
}

0 commit comments

Comments
 (0)