Skip to content

Commit

Permalink
Extracting version from database V10
Browse files Browse the repository at this point in the history
  • Loading branch information
SiddharthBITS committed Jan 22, 2025
1 parent 0a77c97 commit 0301fab
Showing 1 changed file with 54 additions and 41 deletions.
95 changes: 54 additions & 41 deletions test/JDBC/src/test/java/com/sqlsamples/TestQueryFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,57 @@ public static void setup() throws IOException {
String logFile = testRunDir + timestamp;
configureLogger(logFile, logger);

String URL = properties.getProperty("URL");
String tsql_port = properties.getProperty("tsql_port");
String databaseName = properties.getProperty("databaseName");
String user = properties.getProperty("user");
String password = properties.getProperty("password");

connectionString = createSQLServerConnectionString(URL, tsql_port, databaseName, user, password);
connection_bbl = DriverManager.getConnection(connectionString);

// Query against database to find test version
try
{
Statement stmt = connection_bbl.createStatement();
ResultSet rs = stmt.executeQuery("SELECT @@VERSION;");

int columnCount = rs.getMetaData().getColumnCount();

StringBuilder queryOutputBuilder = new StringBuilder();
while (rs.next()) {
for (int i = 1; i <= columnCount; i++)
{
queryOutputBuilder.append(rs.getString(i) + " ");
}
}
String queryOutput = queryOutputBuilder.toString();

Pattern pattern = Pattern.compile("PostgreSQL (\\d+\\.\\d+)");
Matcher matcher = pattern.matcher(queryOutput);

if(matcher.find())
{
String versionString = matcher.group(1);
majorVersion = Integer.parseInt(versionString.split("\\.")[0]);
minorVersion = Integer.parseInt(versionString.split("\\.")[1]);
}
else
{
majorVersion = 0;
minorVersion = 0;
}
}
catch (SQLException e)
{
majorVersion = 0;
minorVersion = 0;
System.err.println("Error executing query: " + e.getMessage());
}

System.out.println("VersionCheck : Version : " + majorVersion + "_" + minorVersion);
closeConnections();

summaryLogger.info("Started test suite. Now running tests...");
}

Expand All @@ -281,6 +332,7 @@ public void closeConnections() throws SQLException, ClassNotFoundException, Thro
}
try
{
System.out.println("VersionCloseCheck : Version : " + majorVersion + "_" + minorVersion + " RESET");
connection_bbl.createStatement().execute("EXEC sys.sp_reset_connection");
}
catch(Exception e)
Expand All @@ -293,6 +345,7 @@ public void closeConnections() throws SQLException, ClassNotFoundException, Thro
{
if (connection_bbl != null)
{
System.out.println("VersionCloseCheck : Version : " + majorVersion + "_" + minorVersion + " CLOSE");
connection_bbl.close();
}
connection_bbl = null;
Expand All @@ -307,6 +360,7 @@ public void closeConnections() throws SQLException, ClassNotFoundException, Thro
}
try
{
System.out.println("VersionCloseCheck : Version : " + majorVersion + "_" + minorVersion + " RESET");
connection_bbl.createStatement().execute("EXEC sys.sp_reset_connection");
}
catch (Exception e)
Expand Down Expand Up @@ -472,47 +526,6 @@ public void TestQueryBatch(String inputFileName) throws SQLException, ClassNotFo
connection_bbl = DriverManager.getConnection(connectionString);
}

// Query against database to find test version
try
{
Statement stmt = connection_bbl.createStatement();
ResultSet rs = stmt.executeQuery("SELECT @@VERSION;");

int columnCount = rs.getMetaData().getColumnCount();

StringBuilder queryOutputBuilder = new StringBuilder();
while (rs.next()) {
for (int i = 1; i <= columnCount; i++)
{
queryOutputBuilder.append(rs.getString(i) + " ");
}
}
String queryOutput = queryOutputBuilder.toString();

Pattern pattern = Pattern.compile("PostgreSQL (\\d+\\.\\d+)");
Matcher matcher = pattern.matcher(queryOutput);

if(matcher.find())
{
String versionString = matcher.group(1);
majorVersion = Integer.parseInt(versionString.split("\\.")[0]);
minorVersion = Integer.parseInt(versionString.split("\\.")[1]);
}
else
{
majorVersion = 0;
minorVersion = 0;
}
}
catch (SQLException e)
{
majorVersion = 0;
minorVersion = 0;
System.err.println("Error executing query: " + e.getMessage());
}

System.out.println("VersionCheck : Version : " + majorVersion + "_" + minorVersion);

summaryLogger.info("RUNNING " + inputFileName);

logger.info("Running " + inputFileName + "...");
Expand Down

0 comments on commit 0301fab

Please sign in to comment.