Skip to content

Commit 5bf67df

Browse files
author
Paultagoras
committed
Adding types, adjusting constructors
1 parent 4c02535 commit 5bf67df

File tree

9 files changed

+327
-39
lines changed

9 files changed

+327
-39
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import java.util.logging.Logger;
1212

1313
public class DataSourceImpl implements DataSource, JdbcV2Wrapper {
14+
private static final Logger log = Logger.getLogger(DataSourceImpl.class.getName());
1415
private String url;
1516
private Properties info;
16-
private Driver driver;
17+
private final Driver driver;
18+
private PrintWriter logWriter;
1719

1820
public void setUrl(String url) {
1921
this.url = url;
@@ -24,22 +26,20 @@ private Properties getProperties() {
2426
copy.putAll(info);
2527
return copy;
2628
}
29+
2730
public void setProperties(Properties info) {
2831
this.info = info;
2932
}
3033

31-
public DataSourceImpl() {
34+
35+
public DataSourceImpl() {//No-arg constructor required by the standard
3236
this(null, new Properties());
3337
}
3438

35-
public DataSourceImpl(String url) {
36-
this(url, new Properties());
37-
}
38-
3939
public DataSourceImpl(String url, Properties info) {
4040
this.url = url;
4141
this.info = info;
42-
this.driver = new Driver();
42+
this.driver = new Driver(this);
4343
}
4444

4545
@Override
@@ -58,12 +58,12 @@ public Connection getConnection(String username, String password) throws SQLExce
5858

5959
@Override
6060
public PrintWriter getLogWriter() throws SQLException {
61-
throw new SQLFeatureNotSupportedException("Method not supported");
61+
return logWriter;
6262
}
6363

6464
@Override
6565
public void setLogWriter(PrintWriter out) throws SQLException {
66-
throw new SQLFeatureNotSupportedException("Method not supported");
66+
logWriter = out;
6767
}
6868

6969
@Override

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
package com.clickhouse.jdbc;
22

3-
import java.sql.*;
4-
import java.util.*;
53

64
import com.clickhouse.jdbc.internal.JdbcConfiguration;
75
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
97

8+
import java.sql.Connection;
9+
import java.sql.DriverManager;
10+
import java.sql.DriverPropertyInfo;
11+
import java.sql.SQLException;
12+
import java.sql.SQLFeatureNotSupportedException;
13+
import java.util.Arrays;
14+
import java.util.LinkedHashSet;
15+
import java.util.List;
16+
import java.util.Properties;
17+
import java.util.Set;
18+
1019
/**
1120
* JDBC driver for ClickHouse.
1221
*/
1322
public class Driver implements java.sql.Driver {
1423
private static final Logger log = LoggerFactory.getLogger(Driver.class);
1524
public static final String driverVersion;
25+
private final DataSourceImpl dataSource;
1626

1727
public static String frameworksDetected = null;
1828
public static class FrameworksDetection {
@@ -54,6 +64,14 @@ public static String getFrameworksDetected() {
5464
//load(); //Commented out to avoid loading the driver multiple times, because we're referenced in V1
5565
}
5666

67+
public Driver() {
68+
this.dataSource = null;
69+
}
70+
71+
public Driver(DataSourceImpl dataSourceImpl) {
72+
this.dataSource = dataSourceImpl;
73+
}
74+
5775
public static void load() {
5876
try {
5977
DriverManager.registerDriver(new Driver());
@@ -74,6 +92,9 @@ public static void unload() {
7492

7593
@Override
7694
public Connection connect(String url, Properties info) throws SQLException {
95+
if (!acceptsURL(url)) {
96+
return null;
97+
}
7798
return new ConnectionImpl(url, info);
7899
}
79100

@@ -84,9 +105,6 @@ public boolean acceptsURL(String url) throws SQLException {
84105

85106
@Override
86107
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
87-
// if (!JdbcConfiguration.acceptsURL(url)) {
88-
// return new DriverPropertyInfo[0];
89-
// }
90108
return new DriverPropertyInfo[0];
91109
}
92110

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import java.time.format.DateTimeFormatterBuilder;
1717
import java.time.temporal.ChronoField;
1818
import java.util.Calendar;
19+
import java.util.Collection;
1920
import java.util.GregorianCalendar;
21+
import java.util.List;
2022
import java.util.Map;
2123

2224
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
@@ -439,7 +441,7 @@ private static String encodeObject(Object x) throws SQLException {
439441
if (x == null) {
440442
return "NULL";
441443
} else if (x instanceof String) {
442-
return "'" + x + "'";
444+
return "'" + escapeString((String) x) + "'";
443445
} else if (x instanceof Boolean) {
444446
return (Boolean) x ? "1" : "0";
445447
} else if (x instanceof Date) {
@@ -454,8 +456,18 @@ private static String encodeObject(Object x) throws SQLException {
454456
return "'" + DATETIME_FORMATTER.format(((Timestamp) x).toLocalDateTime()) + "'";
455457
} else if (x instanceof LocalDateTime) {
456458
return "'" + DATETIME_FORMATTER.format((LocalDateTime) x) + "'";
459+
} else if (x instanceof Collection) {
460+
StringBuilder listString = new StringBuilder();
461+
listString.append("[");
462+
for (Object item : (Collection<?>) x) {
463+
listString.append(encodeObject(item)).append(", ");
464+
}
465+
listString.delete(listString.length() - 2, listString.length());
466+
listString.append("]");
467+
468+
return listString.toString();
457469
} else if (x instanceof Map) {
458-
Map tmpMap = (Map) x;
470+
Map<?, ?> tmpMap = (Map<?, ?>) x;
459471
StringBuilder mapString = new StringBuilder();
460472
mapString.append("{");
461473
for (Object key : tmpMap.keySet()) {
@@ -473,7 +485,7 @@ private static String encodeObject(Object x) throws SQLException {
473485
while ((len = reader.read(buffer)) != -1) {
474486
sb.append(buffer, 0, len);
475487
}
476-
return "'" + sb + "'";
488+
return "'" + escapeString(sb.toString()) + "'";
477489
} else if (x instanceof InputStream) {
478490
StringBuilder sb = new StringBuilder();
479491
InputStream is = (InputStream) x;
@@ -482,13 +494,17 @@ private static String encodeObject(Object x) throws SQLException {
482494
while ((len = is.read(buffer)) != -1) {
483495
sb.append(new String(buffer, 0, len));
484496
}
485-
return "'" + sb + "'";
497+
return "'" + escapeString(sb.toString()) + "'";
486498
}
487499

488-
return x.toString();
500+
return escapeString(x.toString());//Escape single quotes
489501
} catch (Exception e) {
490502
LOG.error("Error encoding object", e);
491503
throw new SQLException("Error encoding object", e);
492504
}
493505
}
506+
507+
private static String escapeString(String x) {
508+
return x.replace("'", "''");//Escape single quotes
509+
}
494510
}

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
1818
import com.clickhouse.client.api.metadata.TableSchema;
1919
import com.clickhouse.client.api.query.QueryResponse;
20-
import com.clickhouse.jdbc.internal.SimpleArray;
20+
import com.clickhouse.jdbc.types.Array;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

@@ -27,8 +27,10 @@ public class ResultSetImpl implements ResultSet, JdbcV2Wrapper {
2727
protected ClickHouseBinaryFormatReader reader;
2828
private final ResultSetMetaData metaData;
2929
private boolean closed;
30+
private Statement parentStatement;
3031

31-
public ResultSetImpl(QueryResponse response, ClickHouseBinaryFormatReader reader) {
32+
public ResultSetImpl(Statement parentStatement, QueryResponse response, ClickHouseBinaryFormatReader reader) {
33+
this.parentStatement = parentStatement;
3234
this.response = response;
3335
this.reader = reader;
3436
this.metaData = new com.clickhouse.jdbc.metadata.ResultSetMetaData(this);
@@ -883,7 +885,7 @@ public void moveToCurrentRow() throws SQLException {
883885
@Override
884886
public Statement getStatement() throws SQLException {
885887
checkClosed();
886-
return null;
888+
return this.parentStatement;
887889
}
888890

889891
@Override
@@ -902,20 +904,28 @@ public Ref getRef(int columnIndex) throws SQLException {
902904
@Override
903905
public Blob getBlob(int columnIndex) throws SQLException {
904906
checkClosed();
905-
throw new SQLFeatureNotSupportedException("Blob is not supported.");
907+
try {
908+
return new com.clickhouse.jdbc.types.Blob(reader.getString(columnIndex));
909+
} catch (Exception e) {
910+
throw new SQLException(e);
911+
}
906912
}
907913

908914
@Override
909-
public Clob getClob(int columnIndex) throws SQLException {
915+
public java.sql.Clob getClob(int columnIndex) throws SQLException {
910916
checkClosed();
911-
throw new SQLFeatureNotSupportedException("Clob is not supported.");
917+
try {
918+
return new com.clickhouse.jdbc.types.Clob(reader.getString(columnIndex));
919+
} catch (Exception e) {
920+
throw new SQLException(e);
921+
}
912922
}
913923

914924
@Override
915-
public Array getArray(int columnIndex) throws SQLException {
925+
public java.sql.Array getArray(int columnIndex) throws SQLException {
916926
checkClosed();
917927
try {
918-
return new SimpleArray(reader.getList(columnIndex));
928+
return new Array(reader.getList(columnIndex));
919929
} catch (Exception e) {
920930
throw new SQLException(e);
921931
}
@@ -936,20 +946,28 @@ public Ref getRef(String columnLabel) throws SQLException {
936946
@Override
937947
public Blob getBlob(String columnLabel) throws SQLException {
938948
checkClosed();
939-
throw new SQLFeatureNotSupportedException("Blob is not supported.");
949+
try {
950+
return new com.clickhouse.jdbc.types.Blob(reader.getString(columnLabel));
951+
} catch (Exception e) {
952+
throw new SQLException(e);
953+
}
940954
}
941955

942956
@Override
943957
public Clob getClob(String columnLabel) throws SQLException {
944958
checkClosed();
945-
throw new SQLFeatureNotSupportedException("Clob is not supported.");
959+
try {
960+
return new com.clickhouse.jdbc.types.Clob(reader.getString(columnLabel));
961+
} catch (Exception e) {
962+
throw new SQLException(e);
963+
}
946964
}
947965

948966
@Override
949-
public Array getArray(String columnLabel) throws SQLException {
967+
public java.sql.Array getArray(String columnLabel) throws SQLException {
950968
checkClosed();
951969
try {
952-
return new SimpleArray(reader.getList(columnLabel));
970+
return new Array(reader.getList(columnLabel));
953971
} catch (Exception e) {
954972
throw new SQLException(e);
955973
}
@@ -1048,13 +1066,13 @@ public void updateClob(String columnLabel, Clob x) throws SQLException {
10481066
}
10491067

10501068
@Override
1051-
public void updateArray(int columnIndex, Array x) throws SQLException {
1069+
public void updateArray(int columnIndex, java.sql.Array x) throws SQLException {
10521070
checkClosed();
10531071
throw new SQLFeatureNotSupportedException("Writes are not supported.");
10541072
}
10551073

10561074
@Override
1057-
public void updateArray(String columnLabel, Array x) throws SQLException {
1075+
public void updateArray(String columnLabel, java.sql.Array x) throws SQLException {
10581076
checkClosed();
10591077
throw new SQLFeatureNotSupportedException("Writes are not supported.");
10601078
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public ResultSet executeQuery(String sql, QuerySettings settings) throws SQLExce
114114
sql = parseJdbcEscapeSyntax(sql);
115115
QueryResponse response = connection.client.query(sql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
116116
ClickHouseBinaryFormatReader reader = connection.client.newBinaryFormatReader(response);
117-
currentResultSet = new ResultSetImpl(response, reader);
117+
currentResultSet = new ResultSetImpl(this, response, reader);
118118
metrics = response.getMetrics();
119119
} catch (Exception e) {
120120
throw new SQLException(e);

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/SimpleArray.java renamed to jdbc-v2/src/main/java/com/clickhouse/jdbc/types/Array.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.clickhouse.jdbc.internal;
1+
package com.clickhouse.jdbc.types;
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
@@ -10,12 +10,12 @@
1010
import java.util.List;
1111
import java.util.Map;
1212

13-
public class SimpleArray implements java.sql.Array {
14-
private static final Logger log = LoggerFactory.getLogger(SimpleArray.class);
13+
public class Array implements java.sql.Array {
14+
private static final Logger log = LoggerFactory.getLogger(Array.class);
1515
Object[] array;
1616
int type; //java.sql.Types
1717

18-
public SimpleArray(List<Object> list) {
18+
public Array(List<Object> list) {
1919
if (list == null) {
2020
throw new IllegalArgumentException("List cannot be null");
2121
}

0 commit comments

Comments
 (0)