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

refactor: fix getDriverVersion and getDriverName and getJDBCMajor/MinorVersion methods #668

Merged
merged 1 commit into from Jan 20, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 4 additions & 27 deletions pgjdbc/src/main/java/org/postgresql/Driver.java
Expand Up @@ -447,37 +447,14 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
return props;
}

public static final int MAJORVERSION =
/*$mvn.project.property.parsedversion.majorversion+";"$*//*-*/9;

/**
* Gets the drivers major version number
*
* @return the drivers major version number
*/
@Override
public int getMajorVersion() {
return MAJORVERSION;
return org.postgresql.util.DriverInfo.MAJOR_VERSION;
}

public static final int MINORVERSION =
/*$mvn.project.property.parsedversion.minorversion+";"$*//*-*/4;

/**
* Get the drivers minor version number
*
* @return the drivers minor version number
*/
@Override
public int getMinorVersion() {
return MINORVERSION;
}

/**
* Returns the server version series of this driver and the specific build number.
*
* @return JDBC driver version
*/
public static String getVersion() {
return "PostgreSQL /*$mvn.project.property.parsedversion.osgiversion$*/";
return org.postgresql.util.DriverInfo.MINOR_VERSION;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/PGProperty.java
Expand Up @@ -5,6 +5,7 @@

package org.postgresql;

import org.postgresql.util.DriverInfo;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;
Expand Down Expand Up @@ -281,7 +282,7 @@ public enum PGProperty {
/**
* The application name (require server version >= 9.0)
*/
APPLICATION_NAME("ApplicationName", null, "name of the application (backend >= 9.0)"),
APPLICATION_NAME("ApplicationName", DriverInfo.DRIVER_NAME, "Name of the Application (backend >= 9.0)"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this default absolutely necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why not?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, one might want a connection without this property. I don't have a real world example for that, just being cautious about unnecessary changes :-)


/**
* Specifies the name of the JAAS system or application login configuration.
Expand Down
Expand Up @@ -44,7 +44,7 @@ public class PGConnectionPoolDataSource extends BaseDataSource
* Gets a description of this DataSource.
*/
public String getDescription() {
return "ConnectionPoolDataSource from " + org.postgresql.Driver.getVersion();
return "ConnectionPoolDataSource from " + org.postgresql.util.DriverInfo.DRIVER_FULL_NAME;
}

/**
Expand Down
Expand Up @@ -78,7 +78,7 @@ public static PGPoolingDataSource getDataSource(String name) {
* Gets a description of this DataSource.
*/
public String getDescription() {
return "Pooling DataSource '" + dataSourceName + " from " + org.postgresql.Driver.getVersion();
return "Pooling DataSource '" + dataSourceName + " from " + org.postgresql.util.DriverInfo.DRIVER_FULL_NAME;
}

/**
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class PGSimpleDataSource extends BaseDataSource implements DataSource, Se
* Gets a description of this DataSource.
*/
public String getDescription() {
return "Non-Pooling DataSource from " + org.postgresql.Driver.getVersion();
return "Non-Pooling DataSource from " + org.postgresql.util.DriverInfo.DRIVER_FULL_NAME;
}

private void writeObject(ObjectOutputStream out) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
Expand Up @@ -214,7 +214,7 @@ public PgConnection(HostSpec[] hostSpecs,

// Print out the driver version number
if (logger.logInfo()) {
logger.info(Driver.getVersion());
logger.info(org.postgresql.util.DriverInfo.DRIVER_FULL_NAME);
}

// Now make the initial connection and set up local state
Expand Down
45 changes: 25 additions & 20 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Expand Up @@ -5,7 +5,6 @@

package org.postgresql.jdbc;

import org.postgresql.Driver;
import org.postgresql.core.BaseStatement;
import org.postgresql.core.Field;
import org.postgresql.core.Oid;
Expand Down Expand Up @@ -37,7 +36,6 @@ public PgDatabaseMetaData(PgConnection conn) {
this.connection = conn;
}


private static final String keywords = "abort,acl,add,aggregate,append,archive,"
+ "arch_store,backward,binary,boolean,change,cluster,"
+ "copy,database,delimiter,delimiters,do,extend,"
Expand Down Expand Up @@ -138,35 +136,39 @@ public boolean nullsAreSortedAtEnd() throws SQLException {
}

/**
* What is the name of this database product - we hope that it is PostgreSQL, so we return that
* Retrieves the name of this database product. We hope that it is PostgreSQL, so we return that
* explicitly.
*
* @return the database product name
*
* @exception SQLException if a database access error occurs
* @return "PostgreSQL"
*/
@Override
public String getDatabaseProductName() throws SQLException {
return "PostgreSQL";
}

@Override
public String getDatabaseProductVersion() throws SQLException {
return connection.getDBVersionNumber();
}

public String getDriverName() throws SQLException {
return "PostgreSQL Native Driver";
@Override
public String getDriverName() {
return org.postgresql.util.DriverInfo.DRIVER_NAME;
}

public String getDriverVersion() throws SQLException {
return Driver.getVersion();
@Override
public String getDriverVersion() {
return org.postgresql.util.DriverInfo.DRIVER_VERSION;
}

@Override
public int getDriverMajorVersion() {
return Driver.MAJORVERSION;
return org.postgresql.util.DriverInfo.MAJOR_VERSION;
}

@Override
public int getDriverMinorVersion() {
return Driver.MINORVERSION;
return org.postgresql.util.DriverInfo.MINOR_VERSION;
}

/**
Expand Down Expand Up @@ -2536,11 +2538,6 @@ public ResultSet getFunctionColumns(String catalog, String schemaPattern,
return getProcedureColumns(catalog, schemaPattern, functionNamePattern, columnNamePattern);
}

public int getJDBCMajorVersion() throws SQLException {
// FIXME: dependent on JDBC version
return 4;
}

public ResultSet getPseudoColumns(String catalog, String schemaPattern, String tableNamePattern,
String columnNamePattern) throws SQLException {
throw org.postgresql.Driver.notImplemented(this.getClass(),
Expand Down Expand Up @@ -2596,20 +2593,28 @@ public int getResultSetHoldability() throws SQLException {
return ResultSet.HOLD_CURSORS_OVER_COMMIT;
}

@Override
public int getDatabaseMajorVersion() throws SQLException {
return connection.getServerMajorVersion();
}

@Override
public int getDatabaseMinorVersion() throws SQLException {
return connection.getServerMinorVersion();
}

public int getJDBCMinorVersion() throws SQLException {
return 0; // This class implements JDBC 3.0
@Override
public int getJDBCMajorVersion() {
return org.postgresql.util.DriverInfo.JDBC_MAJOR_VERSION;
}

@Override
public int getJDBCMinorVersion() {
return org.postgresql.util.DriverInfo.JDBC_MINOR_VERSION;
}

public int getSQLStateType() throws SQLException {
return sqlStateSQL99;
return sqlStateSQL;
}

public boolean locatorsUpdateCopy() throws SQLException {
Expand Down
Expand Up @@ -24,8 +24,8 @@ public class PGBundleActivator implements BundleActivator {
public void start(BundleContext context) throws Exception {
Dictionary<String, Object> properties = new Hashtable<String, Object>();
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, Driver.class.getName());
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "PostgreSQL JDBC Driver");
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, Driver.getVersion());
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, org.postgresql.util.DriverInfo.DRIVER_NAME);
properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_VERSION, org.postgresql.util.DriverInfo.DRIVER_VERSION);
try {
_registration = context.registerService(DataSourceFactory.class.getName(),
new PGDataSourceFactory(), properties);
Expand Down
33 changes: 33 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/util/DriverInfo.java
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2017, PostgreSQL Global Development Group
* See the LICENSE file in the project root for more information.
*/

package org.postgresql.util;

/**
* Utility class with constants of Driver information.
*/
public final class DriverInfo {

private DriverInfo() {
}

// Driver name
public static final String DRIVER_NAME = "PostgreSQL JDBC Driver";
public static final String DRIVER_SHORT_NAME = "PgJDBC";
public static final String DRIVER_VERSION = "/*$mvn.project.property.parsedversion.osgiversion$*/";
public static final String DRIVER_FULL_NAME = DRIVER_NAME + " " + DRIVER_VERSION;

// Driver version
public static final int MAJOR_VERSION = /*$mvn.project.property.parsedversion.majorversion+";"$*//*-*/42;
public static final int MINOR_VERSION = /*$mvn.project.property.parsedversion.minorversion+";"$*//*-*/0;
public static final int PATCH_VERSION = /*$mvn.project.property.parsedversion.incrementalversion+";"$*//*-*/0;

// JDBC specification
public static final String JDBC_VERSION = "/*$mvn.project.property.jdbc.specification.version$*/";
private static final int JDBC_INTVERSION = /*$mvn.project.property.jdbc.specification.version.nodot+";"$*//*-*/42;
public static final int JDBC_MAJOR_VERSION = JDBC_INTVERSION / 10;
public static final int JDBC_MINOR_VERSION = JDBC_INTVERSION % 10;

}
20 changes: 12 additions & 8 deletions pgjdbc/src/main/java/org/postgresql/util/PGJDBCMain.java
Expand Up @@ -5,19 +5,23 @@

package org.postgresql.util;

import org.postgresql.Driver;

public class PGJDBCMain {

public static void main(String[] args) {

PSQLDriverVersion.main(args);
java.net.URL url = Driver.class.getResource("/org/postgresql/Driver.class");
System.out.printf("%n%s%n", org.postgresql.util.DriverInfo.DRIVER_FULL_NAME);
System.out.printf("Found in: %s%n%n", url);

System.out.println("\nThe PgJDBC driver is not an executable Java program.\n\n"
+ "You must install it according to the JDBC driver installation "
+ "instructions for your application / container / appserver, "
+ "then use it by specifying a JDBC URL of the form \n" + " jdbc:postgresql://\n"
+ "or using an application specific method.\n\n"
+ "See the PgJDBC documentation: http://jdbc.postgresql.org/documentation/head/index.html\n\n"
+ "This command has had no effect.\n");
System.out.printf("The PgJDBC driver is not an executable Java program.%n%n"
+ "You must install it according to the JDBC driver installation "
+ "instructions for your application / container / appserver, "
+ "then use it by specifying a JDBC URL of the form %n jdbc:postgresql://%n"
+ "or using an application specific method.%n%n"
+ "See the PgJDBC documentation: http://jdbc.postgresql.org/documentation/head/index.html%n%n"
+ "This command has had no effect.%n");

System.exit(1);
}
Expand Down
25 changes: 0 additions & 25 deletions pgjdbc/src/main/java/org/postgresql/util/PSQLDriverVersion.java

This file was deleted.

2 changes: 1 addition & 1 deletion pgjdbc/src/main/java/org/postgresql/xa/PGXADataSource.java
Expand Up @@ -50,7 +50,7 @@ public XAConnection getXAConnection(String user, String password) throws SQLExce
}

public String getDescription() {
return "JDBC3 XA-enabled DataSource from " + org.postgresql.Driver.getVersion();
return "XA-enabled DataSource from " + org.postgresql.util.DriverInfo.DRIVER_FULL_NAME;
}

/**
Expand Down
Expand Up @@ -195,16 +195,21 @@ public void testDbProductDetails() throws SQLException {
assertNotNull(dbmd);

assertTrue(dbmd.getDatabaseProductName().equals("PostgreSQL"));
assertTrue(dbmd.getDatabaseMajorVersion() >= 8);
assertTrue(dbmd.getDatabaseMinorVersion() >= 0);
assertTrue(dbmd.getDatabaseProductVersion().startsWith(String.valueOf(dbmd.getDatabaseMajorVersion())));
}

@Test
public void testDriverVersioning() throws SQLException {
DatabaseMetaData dbmd = con.getMetaData();
assertNotNull(dbmd);

assertTrue(dbmd.getDriverVersion().equals(org.postgresql.Driver.getVersion()));
assertTrue(dbmd.getDriverMajorVersion() == org.postgresql.Driver.MAJORVERSION);
assertTrue(dbmd.getDriverMinorVersion() == org.postgresql.Driver.MINORVERSION);
assertTrue(dbmd.getDriverName().equals("PostgreSQL JDBC Driver"));
assertTrue(dbmd.getDriverVersion().equals(org.postgresql.util.DriverInfo.DRIVER_VERSION));
assertTrue(dbmd.getDriverMajorVersion() == new org.postgresql.Driver().getMajorVersion());
assertTrue(dbmd.getDriverMinorVersion() == new org.postgresql.Driver().getMinorVersion());
assertTrue(dbmd.getJDBCMajorVersion() >= 4);
assertTrue(dbmd.getJDBCMinorVersion() >= 0);
}
}