Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report driver version in metadata #61

Merged
merged 4 commits into from
Nov 22, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package software.aws.neptune;

import software.aws.neptune.jdbc.Connection;
import software.aws.neptune.jdbc.DatabaseMetaData;
import java.sql.SQLException;

public class NeptuneDatabaseMetadata extends DatabaseMetaData implements java.sql.DatabaseMetaData {
private final Connection connection;
private static final String DRIVER_NAME = "Amazon Neptune JDBC";

/**
* NeptuneDatabaseMetadata constructor, initializes super class.
Expand All @@ -30,7 +29,6 @@ public class NeptuneDatabaseMetadata extends DatabaseMetaData implements java.sq
*/
public NeptuneDatabaseMetadata(final java.sql.Connection connection) {
super(connection);
this.connection = (Connection) connection;
}

@Override
Expand All @@ -55,6 +53,6 @@ public String getCatalogSeparator() throws SQLException {

@Override
public String getDriverName() {
return connection.getDriverName();
return DRIVER_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,4 @@ public DatabaseMetaData getMetaData() {
public QueryExecutor getQueryExecutor() throws SQLException {
return new GremlinQueryExecutor(getGremlinConnectionProperties());
}

@Override
public String getDriverName() {
return "neptune:gremlin";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,4 @@ public DatabaseMetaData getMetaData() {
public QueryExecutor getQueryExecutor() throws SQLException {
return new SqlGremlinQueryExecutor(getGremlinConnectionProperties());
}

@Override
public String getDriverName() {
return "neptune:sqlgremlin";
simonz-bq marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 0 additions & 7 deletions src/main/java/software/aws/neptune/jdbc/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ protected Connection(@NonNull final ConnectionProperties connectionProperties) t
*/
public abstract QueryExecutor getQueryExecutor() throws SQLException;

/**
* Function to get Driver name as a String.
*
* @return Driver name as String.
*/
public abstract String getDriverName();

protected ConnectionProperties getConnectionProperties() {
return this.connectionProperties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public int getDriverMinorVersion() {

@Override
public String getDriverVersion() {
return Driver.DRIVER_VERSION;
return Driver.DRIVER_FULL_VERSION;
}

@Override
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/software/aws/neptune/jdbc/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import software.aws.neptune.jdbc.utilities.SqlError;
import software.aws.neptune.jdbc.utilities.SqlState;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets;
Expand All @@ -36,18 +38,32 @@
public abstract class Driver implements java.sql.Driver {
public static final int DRIVER_MAJOR_VERSION;
public static final int DRIVER_MINOR_VERSION;
public static final String DRIVER_VERSION;
public static final String APP_NAME_SUFFIX;
public static final String DRIVER_FULL_VERSION;
public static final String APPLICATION_NAME;
private static final String PROPERTIES_PATH = "/project.properties";
private static final String MAJOR_VERSION_KEY = "driver.major.version";
private static final String MINOR_VERSION_KEY = "driver.minor.version";
private static final String FULL_VERSION_KEY = "driver.full.version";
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Driver.class);

static {
int majorVersion = 0;
int minorVersion = 0;
String fullVersion = "";
try (InputStream input = Driver.class.getResourceAsStream(PROPERTIES_PATH)) {
final Properties properties = new Properties();
properties.load(input);
majorVersion = Integer.parseInt(properties.getProperty(MAJOR_VERSION_KEY));
minorVersion = Integer.parseInt(properties.getProperty(MINOR_VERSION_KEY));
fullVersion = properties.getProperty(FULL_VERSION_KEY);
} catch (IOException e) {
LOGGER.error("Error loading driver version: ", e);
}

DRIVER_MAJOR_VERSION = majorVersion;
DRIVER_MINOR_VERSION = minorVersion;
DRIVER_FULL_VERSION = fullVersion;
APPLICATION_NAME = getApplicationName();
// TODO: Get driver version, suffix
DRIVER_MAJOR_VERSION = 1;
DRIVER_MINOR_VERSION = 1;
APP_NAME_SUFFIX = "TODO";
DRIVER_VERSION = "0.0.0";
}

/**
Expand Down Expand Up @@ -101,15 +117,14 @@ public java.sql.DriverPropertyInfo[] getPropertyInfo(final String url, final Pro
return new java.sql.DriverPropertyInfo[0];
}

// TODO: Fix functions below.
@Override
public int getMajorVersion() {
return 0;
return DRIVER_MAJOR_VERSION;
}

@Override
public int getMinorVersion() {
return 0;
return DRIVER_MINOR_VERSION;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,4 @@ public DatabaseMetaData getMetaData() {
public QueryExecutor getQueryExecutor() throws SQLException {
return new OpenCypherQueryExecutor(getOpenCypherConnectionProperties());
}

@Override
public String getDriverName() {
return "neptune:opencypher";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,4 @@ public DatabaseMetaData getMetaData() throws SQLException {
public QueryExecutor getQueryExecutor() throws SQLException {
return new SparqlQueryExecutor(getSparqlConnectionProperties());
}

@Override
public String getDriverName() {
return "neptune:sparql";
}
}
24 changes: 24 additions & 0 deletions src/test/java/software/aws/neptune/NeptuneDriverTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import com.google.common.collect.ImmutableList;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.aws.neptune.jdbc.Driver;
import software.aws.neptune.jdbc.helpers.HelperFunctions;
import software.aws.neptune.jdbc.utilities.AuthScheme;
import software.aws.neptune.jdbc.utilities.SqlError;
import software.aws.neptune.opencypher.OpenCypherConnection;
import software.aws.neptune.opencypher.mock.MockOpenCypherDatabase;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
Expand All @@ -32,6 +36,10 @@
public abstract class NeptuneDriverTestBase {
private static MockOpenCypherDatabase database;
private static String validEndpoint;
private static final String PROPERTIES_PATH = "/project.properties";
private static final String MAJOR_VERSION_KEY = "driver.major.version";
private static final String MINOR_VERSION_KEY = "driver.minor.version";
private static final String FULL_VERSION_KEY = "driver.full.version";
private final List<String> invalidUrls = ImmutableList.of(
"jbdc:neptune:opencyher://;", "jdbc:netune:opencyher://;", "jdbc:neptune:openyher://;",
"jdbc:neptune:opencyher//;", "jdbc:neptune:opencypher:/");
Expand Down Expand Up @@ -136,4 +144,20 @@ void testDriverManagerGetDriver(final boolean useEncryption) throws SQLException
void testDriverProperties() {
HelperFunctions.expectFunctionThrows(SqlError.FEATURE_NOT_SUPPORTED, () -> driver.getParentLogger());
}

@Test
void testDriverVersion() {
try (InputStream input = Driver.class.getResourceAsStream(PROPERTIES_PATH)) {
final Properties properties = new Properties();
properties.load(input);
Assertions.assertEquals(driver.getMajorVersion(), Integer.parseInt(properties.getProperty(MAJOR_VERSION_KEY)));
Assertions.assertEquals(driver.getMinorVersion(), Integer.parseInt(properties.getProperty(MINOR_VERSION_KEY)));

// Ensure the version did not default
Assertions.assertNotEquals(driver.getMajorVersion(), 0);
Assertions.assertTrue(properties.containsKey(FULL_VERSION_KEY));
} catch (IOException e) {
Assertions.fail(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void testDriverVersion() {
.expectFunctionDoesntThrow(() -> databaseMetaData.getDriverMajorVersion(), Driver.DRIVER_MAJOR_VERSION);
HelperFunctions
.expectFunctionDoesntThrow(() -> databaseMetaData.getDriverMinorVersion(), Driver.DRIVER_MINOR_VERSION);
HelperFunctions.expectFunctionDoesntThrow(() -> databaseMetaData.getDriverVersion(), Driver.DRIVER_VERSION);
HelperFunctions.expectFunctionDoesntThrow(() -> databaseMetaData.getDriverVersion(), Driver.DRIVER_FULL_VERSION);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public QueryExecutor getQueryExecutor() {
return new MockQueryExecutor();
}

@Override
public String getDriverName() {
return "neptune:mock";
}

@Override
protected void doClose() {
}
Expand Down