Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

public class ClickHouseDataSource extends JdbcWrapper implements DataSource {
private final String url;
private final Properties props;

protected final ClickHouseDriver driver;
protected final ConnectionInfo connInfo;
Expand All @@ -31,6 +32,10 @@ public ClickHouseDataSource(String url, Properties properties) throws SQLExcepti
throw new IllegalArgumentException("Incorrect ClickHouse jdbc url. It must be not null");
}
this.url = url;
this.props = new Properties();
if (properties != null && !properties.isEmpty()) {
this.props.putAll(properties);
}

this.driver = new ClickHouseDriver();
this.connInfo = ClickHouseJdbcUrlParser.parse(url, properties);
Expand All @@ -51,10 +56,16 @@ public ClickHouseConnection getConnection(String username, String password) thro
password = "";
}

Properties props = new Properties(connInfo.getProperties());
props.setProperty(ClickHouseDefaults.USER.getKey(), username);
props.setProperty(ClickHouseDefaults.PASSWORD.getKey(), password);
return driver.connect(url, props);
if (username.equals(props.getProperty(ClickHouseDefaults.USER.getKey()))
&& password.equals(props.getProperty(ClickHouseDefaults.PASSWORD.getKey()))) {
return new ClickHouseConnectionImpl(connInfo);
}

Properties properties = new Properties();
properties.putAll(this.props);
properties.setProperty(ClickHouseDefaults.USER.getKey(), username);
properties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), password);
return new ClickHouseConnectionImpl(url, properties);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.clickhouse.jdbc;

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.clickhouse.client.config.ClickHouseClientOption;
import com.clickhouse.client.config.ClickHouseDefaults;

public class ClickHouseDataSourceTest extends JdbcIntegrationTest {
@Test(groups = "integration")
public void testGetConnection() throws SQLException {
String url = "jdbc:ch:" + DEFAULT_PROTOCOL.name() + "://" + getServerAddress(DEFAULT_PROTOCOL);
String urlWithCredentials = "jdbc:ch:" + DEFAULT_PROTOCOL.name() + "://default@"
+ getServerAddress(DEFAULT_PROTOCOL);
String clientName = "client1";
int maxExecuteTime = 1234;
boolean continueBatchOnError = true;

Properties properties = new Properties();
properties.setProperty(ClickHouseDefaults.USER.getKey(), "default");
properties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), "");
properties.setProperty(ClickHouseClientOption.CLIENT_NAME.getKey(), clientName);
properties.setProperty(ClickHouseClientOption.MAX_EXECUTION_TIME.getKey(), Integer.toString(maxExecuteTime));
properties.setProperty(JdbcConfig.PROP_CONTINUE_BATCH, Boolean.toString(continueBatchOnError));
String params = String.format("?%s=%s&%s=%d&%s", ClickHouseClientOption.CLIENT_NAME.getKey(), clientName,
ClickHouseClientOption.MAX_EXECUTION_TIME.getKey(), maxExecuteTime, JdbcConfig.PROP_CONTINUE_BATCH);

for (ClickHouseDataSource ds : new ClickHouseDataSource[] {
new ClickHouseDataSource(url, properties),
new ClickHouseDataSource(urlWithCredentials, properties),
new ClickHouseDataSource(url + params),
new ClickHouseDataSource(urlWithCredentials + params),
}) {
for (ClickHouseConnection connection : new ClickHouseConnection[] {
ds.getConnection(),
ds.getConnection("default", ""),
new ClickHouseDriver().connect(url, properties),
new ClickHouseDriver().connect(urlWithCredentials, properties),
new ClickHouseDriver().connect(url + params, new Properties()),
new ClickHouseDriver().connect(urlWithCredentials + params, new Properties()),
(ClickHouseConnection) DriverManager.getConnection(url, properties),
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials, properties),
(ClickHouseConnection) DriverManager.getConnection(url + params),
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params),
(ClickHouseConnection) DriverManager.getConnection(url + params, "default", ""),
(ClickHouseConnection) DriverManager.getConnection(urlWithCredentials + params, "default", ""),
}) {
try (ClickHouseConnection conn = connection; Statement stmt = conn.createStatement()) {
Assert.assertEquals(conn.getClientInfo(ClickHouseConnection.PROP_APPLICATION_NAME), clientName);
Assert.assertEquals(stmt.getQueryTimeout(), maxExecuteTime);
Assert.assertEquals(conn.getJdbcConfig().isContinueBatchOnError(), continueBatchOnError);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Properties;

import com.clickhouse.client.ClickHouseColumn;
import com.clickhouse.client.config.ClickHouseClientOption;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
Expand Down Expand Up @@ -42,6 +43,45 @@ public void testGetTypeInfo() throws SQLException {
}
}

@Test(groups = "integration")
public void testGetClientInfo() throws SQLException {
String clientName = "";
Properties props = new Properties();
try (ClickHouseConnection conn = newConnection(props);
ResultSet rs = conn.getMetaData().getClientInfoProperties()) {
while (rs.next()) {
if (ClickHouseConnection.PROP_APPLICATION_NAME.equals(rs.getString(1))) {
clientName = rs.getString(3);
}
}
Assert.assertEquals(clientName, ClickHouseClientOption.CLIENT_NAME.getDefaultValue());
}

props.setProperty(ClickHouseClientOption.CLIENT_NAME.getKey(), "client1");
try (ClickHouseConnection conn = newConnection(props)) {
clientName = "";
try (ResultSet rs = conn.getMetaData().getClientInfoProperties()) {
while (rs.next()) {
if (ClickHouseConnection.PROP_APPLICATION_NAME.equals(rs.getString(1))) {
clientName = rs.getString(3);
}
}
Assert.assertEquals(clientName, "client1");
}

conn.setClientInfo(ClickHouseConnection.PROP_APPLICATION_NAME, "client2");
clientName = "";
try (ResultSet rs = conn.getMetaData().getClientInfoProperties()) {
while (rs.next()) {
if (ClickHouseConnection.PROP_APPLICATION_NAME.equals(rs.getString(1))) {
clientName = rs.getString(3);
}
}
Assert.assertEquals(clientName, "client2");
}
}
}

@Test(dataProvider = "selectedColumns", groups = "integration")
public void testGetColumns(String columnType, Integer columnSize, Integer decimalDigits, Integer octectLength)
throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ public void testMap() throws SQLException {
public void testTuple() throws SQLException {
try (ClickHouseConnection conn = newConnection(new Properties());
Statement stmt = conn.createStatement()) {
ResultSet rs = stmt
.executeQuery(
"select (toInt16(1), 'a', toFloat32(1.2), cast([1,2] as Array(Nullable(UInt8))), map(toUInt32(1),'a')) v");
ResultSet rs = stmt.executeQuery(
"select (toInt16(1), 'a', toFloat32(1.2), cast([1,2] as Array(Nullable(UInt8))), map(toUInt32(1),'a')) v");
Assert.assertTrue(rs.next());
List<?> v = rs.getObject(1, List.class);
Assert.assertEquals(v.size(), 5);
Expand All @@ -257,6 +256,18 @@ public void testTuple() throws SQLException {
Assert.assertEquals(v.get(3), new Short[] { 1, 2 });
Assert.assertEquals(v.get(4), Collections.singletonMap(1L, "a"));
Assert.assertFalse(rs.next());

rs = stmt.executeQuery(
"select cast(tuple(1, [2,3], ('4', [5,6]), map('seven', 8)) as Tuple(Int16, Array(Nullable(Int16)), Tuple(String, Array(Int32)), Map(String, Int32))) v");
Assert.assertTrue(rs.next());
v = rs.getObject(1, List.class);
Assert.assertEquals(v.size(), 4);
Assert.assertEquals(v.get(0), Short.valueOf((short) 1));
Assert.assertEquals(v.get(1), new Short[] { 2, 3 });
Assert.assertEquals(((List<?>) v.get(2)).get(0), "4");
Assert.assertEquals(((List<?>) v.get(2)).get(1), new int[] { 5, 6 });
Assert.assertEquals(v.get(3), Collections.singletonMap("seven", 8));
Assert.assertFalse(rs.next());
}
}

Expand Down