Skip to content

Commit

Permalink
Add implementation of DataSource
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoyeur committed Nov 7, 2023
1 parent e3b1973 commit df389f3
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 9 deletions.
107 changes: 107 additions & 0 deletions jdbc/src/main/java/tech/ydb/jdbc/YdbDataSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package tech.ydb.jdbc;

import tech.ydb.jdbc.context.YdbConfig;
import tech.ydb.jdbc.context.YdbContext;
import tech.ydb.jdbc.impl.YdbConnectionImpl;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;

public class YdbDataSource implements DataSource {
private final String jdbcUrl;
private final Properties connectionProperties;

protected PrintWriter printWriter;
protected int loginTimeoutInSeconds = 0;


public YdbDataSource(String jdbcUrl) {
this(jdbcUrl, new Properties());
}

public YdbDataSource(String jdbcUrl, Properties connInfo) {
if (jdbcUrl == null) {
throw new IllegalArgumentException("jdbcUrl must not be null");
}
this.jdbcUrl = jdbcUrl;
connectionProperties = new Properties();
if (connInfo != null) {
connectionProperties.putAll(connInfo);
}
}

@Override
public Connection getConnection() throws SQLException {
return getConnection(connectionProperties);
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
if (username == null) {
throw new IllegalArgumentException("user cannot be null");
}
Properties properties = new Properties();
properties.putAll(connectionProperties);
properties.put("user", username);
if (password == null) {
properties.put("password", "");
} else {
properties.put("password", password);
}
return getConnection(properties);
}

public Connection getConnection(Properties properties) throws SQLException {
final YdbConfig config = new YdbConfig(jdbcUrl, properties);
final YdbContext context = YdbContext.createContext(config);
return new YdbConnectionImpl(context) {
@Override
public void close() throws SQLException {
super.close();
context.close();
}
};
}

@Override
public PrintWriter getLogWriter() {
return printWriter;
}

@Override
public void setLogWriter(PrintWriter out) {
printWriter = out;
}

@Override
public void setLoginTimeout(int seconds) {
loginTimeoutInSeconds = seconds;
}

@Override
public int getLoginTimeout() {
return loginTimeoutInSeconds;
}

@Override
public Logger getParentLogger() {
return YdbDriver.PARENT_LOGGER;
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
if (iface.isAssignableFrom(getClass())) {
return iface.cast(this);
}
throw new SQLException(YdbConst.CANNOT_UNWRAP_TO + iface);
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isAssignableFrom(getClass());
}
}
2 changes: 1 addition & 1 deletion jdbc/src/main/java/tech/ydb/jdbc/YdbDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
@SuppressWarnings("ClassWithMultipleLoggers")
public class YdbDriver implements Driver {
private static final Logger PARENT_LOGGER = Logger.getLogger("tech.ydb.jdbc");
static final Logger PARENT_LOGGER = Logger.getLogger("tech.ydb.jdbc");
private static final Logger LOGGER = Logger.getLogger(YdbDriver.class.getName());

@Nullable
Expand Down
4 changes: 2 additions & 2 deletions jdbc/src/main/java/tech/ydb/jdbc/context/YdbConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public YdbConfig(String url, Properties properties) throws SQLException {
public Properties getSafeProps() {
Properties safe = new Properties();
for (String key: properties.stringPropertyNames()) {
if (isSensetive(key)) {
if (isSensitive(key)) {
safe.put(key, "***");
} else {
safe.put(key, properties.get(key));
Expand All @@ -39,7 +39,7 @@ public Properties getSafeProps() {
return safe;
}

private static boolean isSensetive(String key) {
private static boolean isSensitive(String key) {
return YdbConnectionProperty.TOKEN.getName().equalsIgnoreCase(key)
|| "password".equalsIgnoreCase(key);
}
Expand Down
28 changes: 28 additions & 0 deletions jdbc/src/test/java/tech/ydb/jdbc/YdbDataSourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tech.ydb.jdbc;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import tech.ydb.jdbc.impl.helper.JdbcUrlHelper;
import tech.ydb.test.junit5.YdbHelperExtension;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class YdbDataSourceTest {
@RegisterExtension
public static final YdbHelperExtension ydb = new YdbHelperExtension();

private static final JdbcUrlHelper jdbcUrlBuilder = new JdbcUrlHelper(ydb);

@Test
void testConnection() throws SQLException {
final DataSource dataSource = new YdbDataSource(jdbcUrlBuilder.build());

try (Connection connection = dataSource.getConnection()) {
Assertions.assertFalse(connection.isClosed());
Assertions.assertTrue(connection.isValid(5000));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private ConnectionSupplier connectByProperties(String username, String password)
}

private ConnectionSupplier connectByAuthority(String username, String password) {
return () -> DriverManager.getConnection(jdbcURL.disableToken().withAutority(username, password).build());
return () -> DriverManager.getConnection(jdbcURL.disableToken().withAuthority(username, password).build());
}

private void testConnection(ConnectionSupplier connectionSupplier) throws SQLException {
Expand Down Expand Up @@ -94,7 +94,7 @@ public void connectOK() throws SQLException {
}

@Test
public void connectWrong() throws SQLException {
public void connectWrong() {
wrongConnection(connectByProperties("user1", "a"));
wrongConnection(connectByAuthority("user1", "a"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public JdbcConnectionExtention(YdbHelperExtension ydb) {
}

private void register(ExtensionContext ctx) throws SQLException {
Assert.assertFalse("Dublicate of context registration", map.containsKey(ctx));
Assert.assertFalse("Duplicate of context registration", map.containsKey(ctx));

Connection connection = DriverManager.getConnection(jdbcURL());
map.put(ctx, connection);
Expand All @@ -63,7 +63,7 @@ public String jdbcURL() {
}

public Connection connection() {
Assert.assertFalse("Retrive connection before initialization", stack.isEmpty());
Assert.assertFalse("Retrieve connection before initialization", stack.isEmpty());
return stack.peek();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public JdbcUrlHelper withArg(String arg, String value) {
return new JdbcUrlHelper(ydb, newExtra, authority, disableToken);
}

public JdbcUrlHelper withAutority(String username, String password) {
public JdbcUrlHelper withAuthority(String username, String password) {
StringBuilder newAuthority = new StringBuilder(encode(username));
if (password != null && !password.isEmpty()) {
newAuthority = newAuthority.append(":").append(encode(password));
newAuthority.append(":").append(encode(password));
}
return new JdbcUrlHelper(ydb, extra, newAuthority.append("@").toString(), disableToken);
}
Expand Down

0 comments on commit df389f3

Please sign in to comment.