Skip to content

Commit 260502d

Browse files
author
Paultagoras
committed
Adding array to prepared statement, adjusting get and set client info
1 parent 5bf67df commit 260502d

File tree

5 files changed

+98
-36
lines changed

5 files changed

+98
-36
lines changed

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

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package com.clickhouse.jdbc;
22

33
import com.clickhouse.client.api.Client;
4-
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
54
import com.clickhouse.client.api.query.GenericRecord;
6-
import com.clickhouse.client.api.query.QueryResponse;
75
import com.clickhouse.client.api.query.QuerySettings;
86
import com.clickhouse.jdbc.internal.JdbcConfiguration;
97
import org.slf4j.Logger;
108
import org.slf4j.LoggerFactory;
119

1210
import java.sql.*;
11+
import java.util.Collections;
12+
import java.util.HashMap;
13+
import java.util.List;
1314
import java.util.Map;
1415
import java.util.Properties;
1516
import java.util.concurrent.Executor;
16-
import java.util.concurrent.TimeUnit;
1717

1818
public class ConnectionImpl implements Connection, JdbcV2Wrapper {
1919
private static final Logger log = LoggerFactory.getLogger(ConnectionImpl.class);
@@ -47,7 +47,6 @@ public ConnectionImpl(String url, Properties info) {
4747
.setPassword(config.getPassword())
4848
.setClientName(clientName)
4949
.build();
50-
5150
this.defaultQuerySettings = new QuerySettings();
5251
}
5352

@@ -166,8 +165,8 @@ public DatabaseMetaData getMetaData() throws SQLException {
166165
@Override
167166
public void setReadOnly(boolean readOnly) throws SQLException {
168167
checkOpen();
169-
if (!readOnly) {
170-
throw new SQLFeatureNotSupportedException("read-only=false unsupported");
168+
if (readOnly) {
169+
throw new SQLFeatureNotSupportedException("read-only=true unsupported");
171170
}
172171
}
173172

@@ -324,19 +323,31 @@ public PreparedStatement prepareStatement(String sql, String[] columnNames) thro
324323
@Override
325324
public Clob createClob() throws SQLException {
326325
checkOpen();
327-
throw new SQLFeatureNotSupportedException("Clob not supported");
326+
try {
327+
return new com.clickhouse.jdbc.types.Clob();
328+
} catch (Exception e) {
329+
throw new SQLException("Failed to create Clob", e);
330+
}
328331
}
329332

330333
@Override
331334
public Blob createBlob() throws SQLException {
332335
checkOpen();
333-
throw new SQLFeatureNotSupportedException("Blob not supported");
336+
try {
337+
return new com.clickhouse.jdbc.types.Blob();
338+
} catch (Exception e) {
339+
throw new SQLException("Failed to create Blob", e);
340+
}
334341
}
335342

336343
@Override
337344
public NClob createNClob() throws SQLException {
338345
checkOpen();
339-
throw new SQLFeatureNotSupportedException("NClob not supported");
346+
try {
347+
return new com.clickhouse.jdbc.types.Clob.NClob();
348+
} catch (Exception e) {
349+
throw new SQLException("Failed to create NClob", e);
350+
}
340351
}
341352

342353
@Override
@@ -358,30 +369,48 @@ public boolean isValid(int timeout) throws SQLException {
358369

359370
@Override
360371
public void setClientInfo(String name, String value) throws SQLClientInfoException {
361-
throw new SQLClientInfoException("ClientInfo not supported", null);
372+
try {
373+
checkOpen();
374+
this.defaultQuerySettings.setOption(name, value);
375+
} catch (Exception e) {
376+
throw new SQLClientInfoException("Failed to set client info.", Collections.singletonMap(name, ClientInfoStatus.REASON_UNKNOWN), e);
377+
}
362378
}
363379

364380
@Override
365381
public void setClientInfo(Properties properties) throws SQLClientInfoException {
366-
throw new SQLClientInfoException("ClientInfo not supported", null);
382+
try {
383+
checkOpen();
384+
} catch (SQLException e) {
385+
throw new SQLClientInfoException("Failed to set client info.", new HashMap<>(), e);
386+
}
387+
388+
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
389+
setClientInfo(entry.getKey().toString(), entry.getValue().toString());
390+
}
367391
}
368392

369393
@Override
370394
public String getClientInfo(String name) throws SQLException {
371395
checkOpen();
372-
return null;
396+
return String.valueOf(this.defaultQuerySettings.getAllSettings().get(name));
373397
}
374398

375399
@Override
376400
public Properties getClientInfo() throws SQLException {
377401
checkOpen();
378-
return new Properties();
402+
Properties clientInfo = new Properties();
403+
clientInfo.putAll(this.defaultQuerySettings.getAllSettings());
404+
return clientInfo;
379405
}
380406

381407
@Override
382408
public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
383-
//TODO: Should this be supported?
384-
return null;
409+
try {
410+
return new com.clickhouse.jdbc.types.Array(List.of(elements));
411+
} catch (Exception e) {
412+
throw new SQLException("Failed to create array", e);
413+
}
385414
}
386415

387416
@Override

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,25 @@
77
import java.io.Reader;
88
import java.math.BigDecimal;
99
import java.net.URL;
10-
import java.sql.*;
10+
import java.sql.Array;
11+
import java.sql.Blob;
12+
import java.sql.Clob;
13+
import java.sql.Date;
14+
import java.sql.JDBCType;
15+
import java.sql.NClob;
16+
import java.sql.ParameterMetaData;
17+
import java.sql.PreparedStatement;
18+
import java.sql.Ref;
19+
import java.sql.ResultSet;
20+
import java.sql.ResultSetMetaData;
21+
import java.sql.RowId;
22+
import java.sql.SQLException;
23+
import java.sql.SQLFeatureNotSupportedException;
24+
import java.sql.SQLType;
25+
import java.sql.SQLXML;
26+
import java.sql.Time;
27+
import java.sql.Timestamp;
28+
import java.sql.Types;
1129
import java.time.LocalDate;
1230
import java.time.LocalDateTime;
1331
import java.time.LocalTime;
@@ -18,7 +36,6 @@
1836
import java.util.Calendar;
1937
import java.util.Collection;
2038
import java.util.GregorianCalendar;
21-
import java.util.List;
2239
import java.util.Map;
2340

2441
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
@@ -456,6 +473,16 @@ private static String encodeObject(Object x) throws SQLException {
456473
return "'" + DATETIME_FORMATTER.format(((Timestamp) x).toLocalDateTime()) + "'";
457474
} else if (x instanceof LocalDateTime) {
458475
return "'" + DATETIME_FORMATTER.format((LocalDateTime) x) + "'";
476+
} else if (x instanceof Array) {
477+
StringBuilder listString = new StringBuilder();
478+
listString.append("[");
479+
for (Object item : (Object[])((Array) x).getArray()) {
480+
listString.append(encodeObject(item)).append(", ");
481+
}
482+
listString.delete(listString.length() - 2, listString.length());
483+
listString.append("]");
484+
485+
return listString.toString();
459486
} else if (x instanceof Collection) {
460487
StringBuilder listString = new StringBuilder();
461488
listString.append("[");

jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Blob.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
public class Blob implements java.sql.Blob {
88
String data;
99

10+
public Blob() {
11+
this.data = "";
12+
}
13+
1014
public Blob(String data) {
1115
this.data = data;
1216
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Clob.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
public class Clob implements java.sql.Clob {
1010
String data;
1111

12+
public Clob() {
13+
this.data = "";
14+
}
15+
1216
public Clob(String data) {
1317
this.data = data;
1418
}
@@ -122,4 +126,15 @@ public Reader getCharacterStream(long pos, long length) throws SQLException {
122126
throw new SQLException(e);
123127
}
124128
}
129+
130+
131+
public static class NClob extends Clob implements java.sql.NClob {
132+
public NClob() {
133+
super();
134+
}
135+
136+
public NClob(String data) {
137+
super(data);
138+
}
139+
}
125140
}

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public void testArrayTypesSimpleStatement () throws SQLException {
453453
Random rand = new Random(seed);
454454
log.info("Random seed was: {}", seed);
455455

456-
int[] array = new int[rand.nextInt(10)];
456+
Integer[] array = new Integer[rand.nextInt(10)];
457457
for (int i = 0; i < array.length; i++) {
458458
array[i] = rand.nextInt(256) - 128;
459459
}
@@ -464,26 +464,13 @@ public void testArrayTypesSimpleStatement () throws SQLException {
464464
}
465465

466466
// Insert random (valid) values
467-
StringBuilder sb = new StringBuilder();
468-
sb.append("INSERT INTO test_arrays VALUES ( 1, [");
469-
for (int i = 0; i < array.length; i++) {
470-
if (i > 0) {
471-
sb.append(", ");
472-
}
473-
sb.append(array[i]);
474-
}
475-
sb.append("], [");
476-
for (int i = 0; i < arraystr.length; i++) {
477-
if (i > 0) {
478-
sb.append(", ");
467+
try (Connection conn = getConnection()) {
468+
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO test_arrays VALUES ( 1, ?, ? )")) {
469+
stmt.setArray(1, conn.createArrayOf("Int8", array));
470+
stmt.setArray(2, conn.createArrayOf("String", arraystr));
471+
stmt.executeUpdate();
479472
}
480-
sb.append("'");
481-
sb.append(arraystr[i]);
482-
sb.append("'");
483473
}
484-
sb.append("])");
485-
String sql = sb.toString();
486-
insertData(sql);
487474

488475
// Check the results
489476
try (Connection conn = getConnection()) {

0 commit comments

Comments
 (0)