Skip to content

Commit 81b66d1

Browse files
author
Paultagoras
committed
Co-mingle Drivers
1 parent 9762e36 commit 81b66d1

File tree

15 files changed

+149
-30
lines changed

15 files changed

+149
-30
lines changed

clickhouse-jdbc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
<artifactId>gson</artifactId>
5959
<optional>true</optional>
6060
</dependency>
61+
<dependency>
62+
<groupId>com.clickhouse</groupId>
63+
<artifactId>jdbc-v2</artifactId>
64+
<version>${revision}</version>
65+
</dependency>
6166
<dependency>
6267
<groupId>io.opencensus</groupId>
6368
<artifactId>opencensus-impl</artifactId>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.clickhouse.jdbc.internal.ClickHouseJdbcUrlParser.ConnectionInfo;
99

1010
import java.io.PrintWriter;
11+
import java.sql.Connection;
12+
import java.sql.DriverManager;
1113
import java.sql.SQLException;
1214
import java.sql.SQLFeatureNotSupportedException;
1315
import java.util.Properties;
@@ -43,7 +45,12 @@ public ClickHouseDataSource(String url, Properties properties) throws SQLExcepti
4345

4446
@Override
4547
public ClickHouseConnection getConnection() throws SQLException {
46-
return new ClickHouseConnectionImpl(connInfo);
48+
return (ClickHouseConnection) DriverManager.getConnection(url, props);
49+
//return new ClickHouseConnectionImpl(connInfo);
50+
}
51+
52+
public Connection getGenericConnection() throws SQLException {
53+
return DriverManager.getConnection(url, props);
4754
}
4855

4956
@Override

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public static String getFrameworksDetected() {
6969
}
7070

7171
static {
72+
String versionString = System.getProperty("com.clickhouse.jdbc.version", "v2");
73+
boolean useV2 = versionString != null && versionString.equalsIgnoreCase("v2");
74+
75+
System.out.println("Static block. Use V2: " + useV2);
76+
7277
String str = ClickHouseDriver.class.getPackage().getImplementationVersion();
7378
if (str != null && !str.isEmpty()) {
7479
char[] chars = str.toCharArray();
@@ -85,14 +90,6 @@ public static String getFrameworksDetected() {
8590
driverVersion = ClickHouseVersion.of(driverVersionString);
8691
specVersion = ClickHouseVersion.of(ClickHouseDriver.class.getPackage().getSpecificationVersion());
8792

88-
try {
89-
DriverManager.registerDriver(new ClickHouseDriver());
90-
} catch (SQLException e) {
91-
throw new IllegalStateException(e);
92-
}
93-
94-
log.debug("ClickHouse Driver %s(JDBC: %s) registered", driverVersion, specVersion);
95-
9693
// client-specific options
9794
Map<Object, ClickHouseOption> m = new LinkedHashMap<>();
9895
try {
@@ -111,6 +108,30 @@ public static String getFrameworksDetected() {
111108
}
112109

113110
clientSpecificOptions = Collections.unmodifiableMap(m);
111+
112+
if (useV2) {
113+
com.clickhouse.jdbc.Driver.load();
114+
} else {
115+
load();
116+
}
117+
}
118+
119+
public static void load() {
120+
try {
121+
DriverManager.registerDriver(new ClickHouseDriver());
122+
} catch (SQLException e) {
123+
throw new IllegalStateException(e);
124+
}
125+
126+
log.debug("ClickHouse Driver %s(JDBC: %s) registered", driverVersion, specVersion);
127+
}
128+
129+
public static void unload() {
130+
try {
131+
DriverManager.deregisterDriver(new ClickHouseDriver());
132+
} catch (SQLException e) {
133+
throw new IllegalStateException(e);
134+
}
114135
}
115136

116137
public static Map<ClickHouseOption, Serializable> toClientOptions(Properties props) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.clickhouse.jdbc;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
import java.sql.*;
7+
import java.util.Properties;
8+
9+
public class GenericJDBCTest extends JdbcIntegrationTest {
10+
public Connection getConnection(Properties properties) throws SQLException {
11+
if (properties == null) {
12+
properties = new Properties();
13+
}
14+
15+
return newDataSource(properties).getGenericConnection();
16+
}
17+
18+
@Test
19+
public void connectionTest() throws SQLException {
20+
try (Connection connection = getConnection(null)) {
21+
Assert.assertNotNull(connection);
22+
Assert.assertTrue(connection.isValid(1));
23+
}
24+
}
25+
26+
@Test
27+
public void connectionWithPropertiesTest() throws SQLException {
28+
Properties properties = new Properties();
29+
properties.setProperty("user", "default");
30+
properties.setProperty("password", "123456");
31+
32+
try (Connection connection = getConnection(properties)) {
33+
Assert.assertNotNull(connection);
34+
Assert.assertTrue(connection.isValid(1));
35+
}
36+
}
37+
38+
@Test
39+
public void basicStatementTest() {
40+
try (Connection connection = getConnection(null);
41+
Statement statement = connection.createStatement()) {
42+
Assert.assertNotNull(statement);
43+
ResultSet resultSet = statement.executeQuery("SELECT 1");
44+
Assert.assertNotNull(resultSet);
45+
Assert.assertTrue(resultSet.next());
46+
Assert.assertEquals(resultSet.getInt(1), 1);
47+
} catch (SQLException e) {
48+
Assert.fail("Failed to create statement", e);
49+
}
50+
}
51+
52+
@Test
53+
public void basicPreparedStatementTest() {
54+
try (Connection connection = getConnection(null);
55+
PreparedStatement statement = connection.prepareStatement("SELECT ?")) {
56+
Assert.assertNotNull(statement);
57+
statement.setInt(1, 1);
58+
ResultSet resultSet = statement.executeQuery();
59+
Assert.assertNotNull(resultSet);
60+
Assert.assertTrue(resultSet.next());
61+
Assert.assertEquals(resultSet.getInt(1), 1);
62+
} catch (SQLException e) {
63+
Assert.fail("Failed to create statement", e);
64+
}
65+
}
66+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
55
import com.clickhouse.client.api.query.QueryResponse;
66
import com.clickhouse.jdbc.internal.JdbcConfiguration;
7-
import com.clickhouse.logging.Logger;
8-
import com.clickhouse.logging.LoggerFactory;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
99

1010
import java.sql.*;
1111
import java.util.Map;
1212
import java.util.Properties;
1313
import java.util.concurrent.Executor;
1414
import java.util.concurrent.TimeUnit;
1515

16-
public class ConnectionImpl implements Connection, JdbcWrapper {
16+
public class ConnectionImpl implements Connection, JdbcV2Wrapper {
1717
private static final Logger log = LoggerFactory.getLogger(ConnectionImpl.class);
1818

1919
protected final String url;
@@ -25,6 +25,8 @@ public class ConnectionImpl implements Connection, JdbcWrapper {
2525
private String schema;
2626

2727
public ConnectionImpl(String url, Properties info) {
28+
log.debug("Creating connection to {}", url);
29+
2830
this.url = url;
2931
this.config = new JdbcConfiguration(url, info);
3032
this.client = new Client.Builder()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.Properties;
1111
import java.util.logging.Logger;
1212

13-
public class DataSourceImpl implements DataSource, JdbcWrapper {
13+
public class DataSourceImpl implements DataSource, JdbcV2Wrapper {
1414
private String url;
1515
private Properties info;
1616

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.*;
55

66
import com.clickhouse.jdbc.internal.JdbcConfiguration;
7-
import com.clickhouse.logging.Logger;
8-
import com.clickhouse.logging.LoggerFactory;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
99

1010
/**
1111
* JDBC driver for ClickHouse.
@@ -25,13 +25,27 @@ public class Driver implements java.sql.Driver {
2525
driverVersion = tempDriverVersion;
2626
log.info("ClickHouse JDBC driver version: {}", driverVersion);
2727

28+
//Load the driver
29+
//load(); //Commented out to avoid loading the driver multiple times, because we're referenced in V1
30+
}
31+
32+
public static void load() {
2833
try {
2934
DriverManager.registerDriver(new Driver());
3035
} catch (SQLException e) {
3136
log.error("Failed to register ClickHouse JDBC driver", e);
3237
}
3338
}
3439

40+
public static void unload() {
41+
try {
42+
DriverManager.deregisterDriver(new Driver());
43+
} catch (SQLException e) {
44+
log.error("Failed to deregister ClickHouse JDBC driver", e);
45+
}
46+
}
47+
48+
3549

3650
@Override
3751
public Connection connect(String url, Properties info) throws SQLException {

jdbc-v2/src/main/java/com/clickhouse/jdbc/JdbcWrapper.java renamed to jdbc-v2/src/main/java/com/clickhouse/jdbc/JdbcV2Wrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.sql.SQLException;
44
import java.sql.Wrapper;
55

6-
public interface JdbcWrapper extends Wrapper {
6+
public interface JdbcV2Wrapper extends Wrapper {
77
default boolean isWrapperFor(Class<?> iface) throws SQLException {
88
return iface != null && iface.isAssignableFrom(getClass());
99
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.Calendar;
1616
import java.util.GregorianCalendar;
1717

18-
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcWrapper {
18+
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
1919
private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class);
2020

2121
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
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.logging.Logger;
21-
import com.clickhouse.logging.LoggerFactory;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
2222

23-
public class ResultSetImpl implements ResultSet, JdbcWrapper {
23+
public class ResultSetImpl implements ResultSet, JdbcV2Wrapper {
2424
private static final Logger log = LoggerFactory.getLogger(ResultSetImpl.class);
2525
private QueryResponse response;
2626
protected ClickHouseBinaryFormatReader reader;

0 commit comments

Comments
 (0)