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
68 changes: 68 additions & 0 deletions src/main/java/org/duckdb/DuckDBDataSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.duckdb;

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

public class DuckDBDataSource implements DataSource {
private final String url;
private final Properties props;
private PrintWriter logWriter = null;

public DuckDBDataSource() {
this(DuckDBDriver.DUCKDB_URL_PREFIX, null);
}

public DuckDBDataSource(String url) {
this(url, null);
}

public DuckDBDataSource(String url, Properties info) {
this.url = url;
props = info;
}

public Connection getConnection() throws SQLException {
return DriverManager.getDriver(url).connect(url, props);
}

public Connection getConnection(String username, String password) throws SQLException {
return getConnection();
}

public PrintWriter getLogWriter() throws SQLException {
return logWriter;
}

public void setLogWriter(PrintWriter out) throws SQLException {
logWriter = out;
}

public void setLoginTimeout(int seconds) throws SQLException {
}

public int getLoginTimeout() throws SQLException {
return 0;
}

public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException("no logger");
}

public <T> T unwrap(Class<T> iface) throws SQLException {
try {
return iface.cast(this);
} catch (ClassCastException e) {
throw new SQLException(e);
}
}

public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(this);
}
}
16 changes: 16 additions & 0 deletions src/test/java/org/duckdb/TestDuckDBJDBC.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;
import org.duckdb.test.TempDirectory;
Expand Down Expand Up @@ -137,6 +138,21 @@ public static void test_connection() throws Exception {
assertThrows(conn::createStatement, SQLException.class);
}

public static void test_datasource_connection() throws Exception {
DataSource source = new DuckDBDataSource(JDBC_URL);
Connection conn = source.getConnection();
Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT 42 as a");
assertTrue(rs.next());
int res = rs.getInt(1);
assertEquals(res, 42);

rs.close();
stmt.close();
conn.close();
}

public static void test_execute_exception() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
Expand Down
Loading