forked from databricks/databricks-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSource.java
More file actions
174 lines (144 loc) · 4.8 KB
/
Copy pathDataSource.java
File metadata and controls
174 lines (144 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.databricks.client.jdbc;
import static com.databricks.jdbc.common.DatabricksJdbcConstants.*;
import com.databricks.jdbc.common.DatabricksJdbcConstants;
import com.databricks.jdbc.common.DatabricksJdbcUrlParams;
import com.databricks.jdbc.log.JdbcLogger;
import com.databricks.jdbc.log.JdbcLoggerFactory;
import com.databricks.jdbc.pooling.DatabricksPooledConnection;
import com.google.common.annotations.VisibleForTesting;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.PooledConnection;
public class DataSource implements javax.sql.DataSource, ConnectionPoolDataSource {
private static final JdbcLogger LOGGER = JdbcLoggerFactory.getLogger(DataSource.class);
private String user = DEFAULT_USERNAME;
private String host;
private int port;
private String httpPath;
private Properties properties = new Properties();
private final Driver driver;
public DataSource() {
this.driver = Driver.getInstance();
}
@VisibleForTesting
public DataSource(Driver driver) {
this.driver = driver;
}
@Override
public Connection getConnection() throws SQLException {
LOGGER.debug("public Connection getConnection()");
return getConnection(this.getUsername(), this.getPassword());
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
LOGGER.debug("public Connection getConnection(String, String)");
if (username != null) {
setUsername(username);
}
if (password != null) {
setPassword(password);
}
return driver.connect(getUrl(), properties);
}
@Override
public PooledConnection getPooledConnection() throws SQLException {
LOGGER.debug("public PooledConnection getPooledConnection()");
return new DatabricksPooledConnection(getConnection());
}
@Override
public PooledConnection getPooledConnection(String user, String password) throws SQLException {
LOGGER.debug("public PooledConnection getPooledConnection(String, String)");
return new DatabricksPooledConnection(getConnection(user, password));
}
@Override
public PrintWriter getLogWriter() throws SQLException {
throw new SQLFeatureNotSupportedException("public PrintWriter getLogWriter()");
}
@Override
public void setLogWriter(PrintWriter out) throws SQLException {
throw new SQLFeatureNotSupportedException("public void setLogWriter(PrintWriter out)");
}
@Override
public void setLoginTimeout(int seconds) {
LOGGER.debug("public void setLoginTimeout(int seconds = {})", seconds);
this.properties.put(DatabricksJdbcConstants.LOGIN_TIMEOUT, seconds);
}
@Override
public int getLoginTimeout() {
return (int) this.properties.get(DatabricksJdbcConstants.LOGIN_TIMEOUT);
}
@Override
public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("public Logger getParentLogger()");
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
public String getUrl() {
LOGGER.debug("public String getUrl()");
StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append(DatabricksJdbcConstants.JDBC_SCHEMA);
if (host == null) {
throw new IllegalStateException("Host is required");
}
urlBuilder.append(host);
if (port != 0) {
urlBuilder.append(PORT_DELIMITER).append(port);
}
if (httpPath != null) {
urlBuilder
.append(URL_DELIMITER)
.append(DatabricksJdbcUrlParams.HTTP_PATH.getParamName())
.append(PAIR_DELIMITER)
.append(httpPath);
}
return urlBuilder.toString();
}
public String getUsername() {
return user;
}
public void setUsername(String user) {
this.user = user;
}
public String getPassword() {
return properties.getProperty(
DatabricksJdbcUrlParams.PASSWORD.getParamName(),
properties.getProperty(DatabricksJdbcUrlParams.PWD.getParamName()));
}
public void setPassword(String password) {
properties.put(DatabricksJdbcUrlParams.PASSWORD.getParamName(), password);
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHttpPath() {
return httpPath;
}
public void setHttpPath(String httpPath) {
this.httpPath = httpPath;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}