From da787edd99ced4498d26dcb59dbbc6c197e69bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EC=9A=B0?= Date: Fri, 10 Jan 2025 20:22:58 +0900 Subject: [PATCH] Implement JDBC so that DriverManager can detect limbo connection --- .../java/org/github/tursodatabase/JDBC.java | 18 ++++++++--- .../org/github/tursodatabase/LimboConfig.java | 31 +++++++++++++++++++ .../org/github/tursodatabase/JDBCTest.java | 10 ++++++ 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java b/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java index edf938e67..87200ff5c 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/JDBC.java @@ -7,9 +7,16 @@ import java.util.Properties; import java.util.logging.Logger; public class JDBC implements Driver { - private static final String VALID_URL_PREFIX = "jdbc:limbo:"; + static { + try { + DriverManager.registerDriver(new JDBC()); + } catch (Exception e) { + // TODO: log + } + } + public static LimboConnection createConnection(String url, Properties properties) throws SQLException { if (!isValidURL(url)) return null; @@ -27,26 +34,28 @@ public class JDBC implements Driver { @Override public Connection connect(String url, Properties info) throws SQLException { - return null; + return createConnection(url, info); } @Override public boolean acceptsURL(String url) throws SQLException { - return false; + return isValidURL(url); } @Override public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { - return new DriverPropertyInfo[0]; + return LimboConfig.getDriverPropertyInfo(); } @Override public int getMajorVersion() { + // TODO return 0; } @Override public int getMinorVersion() { + // TODO return 0; } @@ -57,6 +66,7 @@ public class JDBC implements Driver { @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { + // TODO return null; } } diff --git a/bindings/java/src/main/java/org/github/tursodatabase/LimboConfig.java b/bindings/java/src/main/java/org/github/tursodatabase/LimboConfig.java index 388419e60..7f2a2cdf0 100644 --- a/bindings/java/src/main/java/org/github/tursodatabase/LimboConfig.java +++ b/bindings/java/src/main/java/org/github/tursodatabase/LimboConfig.java @@ -1,5 +1,7 @@ package org.github.tursodatabase; +import java.sql.DriverPropertyInfo; +import java.util.Arrays; import java.util.Properties; /** @@ -12,9 +14,38 @@ public class LimboConfig { this.pragma = properties; } + public static DriverPropertyInfo[] getDriverPropertyInfo() { + return Arrays.stream(Pragma.values()) + .map(p -> { + DriverPropertyInfo info = new DriverPropertyInfo(p.pragmaName, null); + info.description = p.description; + info.choices = p.choices; + info.required = false; + return info; + }) + .toArray(DriverPropertyInfo[]::new); + } + public Properties toProperties() { Properties copy = new Properties(); copy.putAll(pragma); return copy; } + + public enum Pragma { + ; + private final String pragmaName; + private final String description; + private final String[] choices; + + Pragma(String pragmaName, String description, String[] choices) { + this.pragmaName = pragmaName; + this.description = description; + this.choices = choices; + } + + public String getPragmaName() { + return pragmaName; + } + } } diff --git a/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java b/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java index 137e5b0ce..ff629bacc 100644 --- a/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java +++ b/bindings/java/src/test/java/org/github/tursodatabase/JDBCTest.java @@ -2,6 +2,9 @@ package org.github.tursodatabase; import org.junit.jupiter.api.Test; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; import java.util.Properties; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @@ -20,4 +23,11 @@ class JDBCTest { LimboConnection connection = JDBC.createConnection("jdbc:limbo:" + fileUrl, new Properties()); assertThat(connection).isNotNull(); } + + @Test + void connection_can_be_retrieved_from_DriverManager() throws SQLException { + JDBC jdbc = new JDBC(); + Connection connection = DriverManager.getConnection("jdbc:limbo:sample.db"); + assertThat(connection).isNotNull(); + } }