mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-04 18:18:03 +00:00
Extract LimboPropertiesHolder
This commit is contained in:
parent
482d7b639e
commit
6b223421ae
4 changed files with 67 additions and 30 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue