|
1 | 1 | package com.clickhouse.jdbc; |
2 | 2 |
|
3 | 3 | import javax.sql.DataSource; |
4 | | - |
5 | | -import com.clickhouse.client.config.ClickHouseDefaults; |
6 | | -import com.clickhouse.jdbc.internal.ClickHouseConnectionImpl; |
7 | | -import com.clickhouse.jdbc.internal.ClickHouseJdbcUrlParser; |
8 | | -import com.clickhouse.jdbc.internal.ClickHouseJdbcUrlParser.ConnectionInfo; |
9 | | - |
10 | 4 | import java.io.PrintWriter; |
11 | 5 | import java.sql.Connection; |
12 | | -import java.sql.DriverManager; |
13 | 6 | import java.sql.SQLException; |
14 | 7 | import java.sql.SQLFeatureNotSupportedException; |
15 | 8 | import java.util.Properties; |
16 | | -import java.util.logging.Logger; |
17 | | - |
18 | | -public class ClickHouseDataSource extends JdbcWrapper implements DataSource { |
19 | | - private final String url; |
20 | | - private final Properties props; |
21 | | - |
22 | | - protected final ClickHouseDriver driver; |
23 | | - protected final ConnectionInfo connInfo; |
24 | 9 |
|
25 | | - protected PrintWriter printWriter; |
26 | | - protected int loginTimeoutSeconds = 0; |
| 10 | +public class ClickHouseDataSource implements javax.sql.DataSource, com.clickhouse.jdbc.JdbcV2Wrapper { |
| 11 | + private final DataSource dataSource; |
| 12 | + private final ClickHouseDriver driver; |
27 | 13 |
|
28 | 14 | public ClickHouseDataSource(String url) throws SQLException { |
29 | 15 | this(url, new Properties()); |
30 | 16 | } |
31 | 17 |
|
32 | 18 | public ClickHouseDataSource(String url, Properties properties) throws SQLException { |
33 | | - if (url == null) { |
34 | | - throw new IllegalArgumentException("Incorrect ClickHouse jdbc url. It must be not null"); |
35 | | - } |
36 | | - this.url = url; |
37 | | - this.props = new Properties(); |
38 | | - if (properties != null && !properties.isEmpty()) { |
39 | | - this.props.putAll(properties); |
40 | | - } |
41 | | - |
42 | 19 | this.driver = new ClickHouseDriver(); |
43 | | - new VersionSelectingDriver();//This is a workaround to make sure the driver is loaded |
44 | | - this.connInfo = ClickHouseJdbcUrlParser.parse(url, properties); |
| 20 | + |
| 21 | + if (driver.isV2(url)) { |
| 22 | + //v2 |
| 23 | + this.dataSource = new com.clickhouse.jdbc.DataSourceImpl(url, properties); |
| 24 | + } else { |
| 25 | + //v1 |
| 26 | + this.dataSource = new DataSourceV1(url, properties); |
| 27 | + } |
45 | 28 | } |
46 | 29 |
|
47 | 30 | @Override |
48 | | - public ClickHouseConnection getConnection() throws SQLException { |
49 | | - return new ClickHouseConnectionImpl(connInfo); |
| 31 | + public Connection getConnection() throws SQLException { |
| 32 | + return dataSource.getConnection(); |
50 | 33 | } |
51 | 34 |
|
52 | 35 | @Override |
53 | | - public ClickHouseConnection getConnection(String username, String password) throws SQLException { |
54 | | - if (username == null || username.isEmpty()) { |
55 | | - throw SqlExceptionUtils.clientError("Non-empty user name is required"); |
56 | | - } |
57 | | - |
58 | | - if (password == null) { |
59 | | - password = ""; |
60 | | - } |
61 | | - |
62 | | - if (username.equals(props.getProperty(ClickHouseDefaults.USER.getKey())) |
63 | | - && password.equals(props.getProperty(ClickHouseDefaults.PASSWORD.getKey()))) { |
64 | | - return new ClickHouseConnectionImpl(connInfo); |
65 | | - } |
66 | | - |
67 | | - Properties properties = new Properties(); |
68 | | - properties.putAll(this.props); |
69 | | - properties.setProperty(ClickHouseDefaults.USER.getKey(), username); |
70 | | - properties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), password); |
71 | | - return new ClickHouseConnectionImpl(url, properties); |
| 36 | + public Connection getConnection(String username, String password) throws SQLException { |
| 37 | + return dataSource.getConnection(username, password); |
72 | 38 | } |
73 | 39 |
|
74 | 40 | @Override |
75 | 41 | public PrintWriter getLogWriter() throws SQLException { |
76 | | - return printWriter; |
| 42 | + return dataSource.getLogWriter(); |
77 | 43 | } |
78 | 44 |
|
79 | 45 | @Override |
80 | 46 | public void setLogWriter(PrintWriter out) throws SQLException { |
81 | | - printWriter = out; |
| 47 | + dataSource.setLogWriter(out); |
82 | 48 | } |
83 | 49 |
|
84 | 50 | @Override |
85 | 51 | public void setLoginTimeout(int seconds) throws SQLException { |
86 | | - loginTimeoutSeconds = seconds; |
| 52 | + dataSource.setLoginTimeout(seconds); |
87 | 53 | } |
88 | 54 |
|
89 | 55 | @Override |
90 | 56 | public int getLoginTimeout() throws SQLException { |
91 | | - return loginTimeoutSeconds; |
| 57 | + return dataSource.getLoginTimeout(); |
92 | 58 | } |
93 | 59 |
|
94 | | - public Logger getParentLogger() throws SQLFeatureNotSupportedException { |
95 | | - return ClickHouseDriver.parentLogger; |
| 60 | + @Override |
| 61 | + public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException { |
| 62 | + return dataSource.getParentLogger(); |
96 | 63 | } |
97 | 64 | } |
0 commit comments