diff --git a/bindings/java/src/main/java/tech/turso/JDBC.java b/bindings/java/src/main/java/tech/turso/JDBC.java index 24cb9c2ae..86d6bde9e 100644 --- a/bindings/java/src/main/java/tech/turso/JDBC.java +++ b/bindings/java/src/main/java/tech/turso/JDBC.java @@ -5,6 +5,7 @@ import java.util.Locale; import java.util.Properties; import tech.turso.annotations.Nullable; import tech.turso.annotations.SkipNullableCheck; +import tech.turso.core.LimboPropertiesHolder; import tech.turso.jdbc4.JDBC4Connection; import tech.turso.utils.Logger; import tech.turso.utils.LoggerFactory; @@ -57,14 +58,12 @@ public final class JDBC implements Driver { @Override public int getMajorVersion() { - // TODO - return 0; + return Integer.parseInt(LimboPropertiesHolder.getDriverVersion().split("\\.")[0]); } @Override public int getMinorVersion() { - // TODO - return 0; + return Integer.parseInt(LimboPropertiesHolder.getDriverVersion().split("\\.")[1]); } @Override diff --git a/bindings/java/src/main/java/tech/turso/core/LimboPropertiesHolder.java b/bindings/java/src/main/java/tech/turso/core/LimboPropertiesHolder.java new file mode 100644 index 000000000..c33b9dc10 --- /dev/null +++ b/bindings/java/src/main/java/tech/turso/core/LimboPropertiesHolder.java @@ -0,0 +1,39 @@ +package tech.turso.core; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import tech.turso.jdbc4.JDBC4DatabaseMetaData; +import tech.turso.utils.Logger; +import tech.turso.utils.LoggerFactory; + +public class LimboPropertiesHolder { + private static final Logger logger = LoggerFactory.getLogger(LimboPropertiesHolder.class); + + private static String driverName = ""; + private static String driverVersion = ""; + + static { + try (InputStream limboJdbcPropStream = + JDBC4DatabaseMetaData.class.getClassLoader().getResourceAsStream("limbo-jdbc.properties")) { + if (limboJdbcPropStream == null) { + throw new IOException("Cannot load limbo-jdbc.properties from jar"); + } + + final Properties properties = new Properties(); + properties.load(limboJdbcPropStream); + driverName = properties.getProperty("driverName"); + driverVersion = properties.getProperty("driverVersion"); + } catch (IOException e) { + logger.error("Failed to load driverName and driverVersion"); + } + } + + public static String getDriverName() { + return driverName; + } + + public static String getDriverVersion() { + return driverVersion; + } +} diff --git a/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4DatabaseMetaData.java b/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4DatabaseMetaData.java index 1da206a4e..fd159d2bd 100644 --- a/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4DatabaseMetaData.java +++ b/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4DatabaseMetaData.java @@ -1,7 +1,5 @@ package tech.turso.jdbc4; -import java.io.IOException; -import java.io.InputStream; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; @@ -9,9 +7,9 @@ import java.sql.ResultSet; import java.sql.RowIdLifetime; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; -import java.util.Properties; import tech.turso.annotations.Nullable; import tech.turso.annotations.SkipNullableCheck; +import tech.turso.core.LimboPropertiesHolder; import tech.turso.utils.Logger; import tech.turso.utils.LoggerFactory; @@ -19,9 +17,6 @@ public final class JDBC4DatabaseMetaData implements DatabaseMetaData { private static final Logger logger = LoggerFactory.getLogger(JDBC4DatabaseMetaData.class); - private static String driverName = ""; - private static String driverVersion = ""; - private final JDBC4Connection connection; @Nullable private PreparedStatement getTables = null; @Nullable private PreparedStatement getTableTypes = null; @@ -41,22 +36,6 @@ public final class JDBC4DatabaseMetaData implements DatabaseMetaData { @Nullable private PreparedStatement getVersionColumns = null; @Nullable private PreparedStatement getColumnPrivileges = null; - static { - try (InputStream limboJdbcPropStream = - JDBC4DatabaseMetaData.class.getClassLoader().getResourceAsStream("limbo-jdbc.properties")) { - if (limboJdbcPropStream == null) { - throw new IOException("Cannot load limbo-jdbc.properties from jar"); - } - - final Properties properties = new Properties(); - properties.load(limboJdbcPropStream); - driverName = properties.getProperty("driverName"); - driverVersion = properties.getProperty("driverVersion"); - } catch (IOException e) { - logger.error("Failed to load driverName and driverVersion"); - } - } - public JDBC4DatabaseMetaData(JDBC4Connection connection) { this.connection = connection; } @@ -120,22 +99,22 @@ public final class JDBC4DatabaseMetaData implements DatabaseMetaData { @Override public String getDriverName() { - return driverName; + return LimboPropertiesHolder.getDriverName(); } @Override public String getDriverVersion() { - return driverVersion; + return LimboPropertiesHolder.getDriverVersion(); } @Override public int getDriverMajorVersion() { - return Integer.parseInt(driverVersion.split("\\.")[0]); + return Integer.parseInt(getDriverVersion().split("\\.")[0]); } @Override public int getDriverMinorVersion() { - return Integer.parseInt(driverVersion.split("\\.")[1]); + return Integer.parseInt(getDriverVersion().split("\\.")[1]); } @Override diff --git a/bindings/java/src/test/java/tech/turso/JDBCTest.java b/bindings/java/src/test/java/tech/turso/JDBCTest.java index e1e1197f7..9cb5d8eb3 100644 --- a/bindings/java/src/test/java/tech/turso/JDBCTest.java +++ b/bindings/java/src/test/java/tech/turso/JDBCTest.java @@ -1,8 +1,10 @@ package tech.turso; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import java.sql.Connection; +import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; @@ -30,4 +32,22 @@ class JDBCTest { assertThat(connection).isNotNull(); } } + + @Test + void retrieve_version() { + assertDoesNotThrow(() -> DriverManager.getDriver("jdbc:sqlite:").getMajorVersion()); + assertDoesNotThrow(() -> DriverManager.getDriver("jdbc:sqlite:").getMinorVersion()); + } + + @Test + void all_driver_property_info_should_have_a_description() throws Exception { + Driver driver = DriverManager.getDriver("jdbc:sqlite:"); + assertThat(driver.getPropertyInfo(null, null)) + .allSatisfy((info) -> assertThat(info.description).isNotNull()); + } + + @Test + void return_null_when_protocol_can_not_be_handled() throws Exception { + assertThat(JDBC.createConnection("jdbc:unknownprotocol:", null)).isNull(); + } }