diff --git a/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java b/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java new file mode 100644 index 000000000..edf938e67 --- /dev/null +++ b/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java @@ -0,0 +1,62 @@ +package org.github.tursodatabase; + +import org.github.tursodatabase.jdbc4.JDBC4Connection; + +import java.sql.*; +import java.util.Properties; +import java.util.logging.Logger; + +public class JDBC implements Driver { + + private static final String VALID_URL_PREFIX = "jdbc:limbo:"; + + public static LimboConnection createConnection(String url, Properties properties) throws SQLException { + if (!isValidURL(url)) return null; + + url = url.trim(); + return new JDBC4Connection(url, extractAddress(url), properties); + } + + private static boolean isValidURL(String url) { + return url != null && url.toLowerCase().startsWith(VALID_URL_PREFIX); + } + + private static String extractAddress(String url) { + return url.substring(VALID_URL_PREFIX.length()); + } + + @Override + public Connection connect(String url, Properties info) throws SQLException { + return null; + } + + @Override + public boolean acceptsURL(String url) throws SQLException { + return false; + } + + @Override + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + return new DriverPropertyInfo[0]; + } + + @Override + public int getMajorVersion() { + return 0; + } + + @Override + public int getMinorVersion() { + return 0; + } + + @Override + public boolean jdbcCompliant() { + return false; + } + + @Override + public Logger getParentLogger() throws SQLFeatureNotSupportedException { + return null; + } +} diff --git a/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java b/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java new file mode 100644 index 000000000..137e5b0ce --- /dev/null +++ b/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java @@ -0,0 +1,23 @@ +package org.github.tursodatabase; + +import org.junit.jupiter.api.Test; + +import java.util.Properties; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class JDBCTest { + + @Test + void null_is_returned_when_invalid_url_is_passed() throws Exception { + LimboConnection connection = JDBC.createConnection("jdbc:invalid:xxx", new Properties()); + assertThat(connection).isNull(); + } + + @Test + void non_null_connection_is_returned_when_valid_url_is_passed() throws Exception { + String fileUrl = TestUtils.createTempFile(); + LimboConnection connection = JDBC.createConnection("jdbc:limbo:" + fileUrl, new Properties()); + assertThat(connection).isNotNull(); + } +}