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

Fix getMetaData error for queries containing TOP #2287

Merged
merged 2 commits into from
Jan 8, 2024
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
10 changes: 10 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ public String toString() {
/** Generate the statement's logging ID */
private static final AtomicInteger lastStatementID = new AtomicInteger(0);

/*
* T-SQL TOP syntax regex
*/
private final static Pattern TOP_SYNTAX = Pattern.compile("\\bTOP\\s*\\(\\s*\\?\\s*\\)", Pattern.CASE_INSENSITIVE);

private static int nextStatementID() {
return lastStatementID.incrementAndGet();
}
Expand Down Expand Up @@ -1098,6 +1103,11 @@ static String replaceParameterWithString(String str, char marker, String replace
* @return the result
*/
static String replaceMarkerWithNull(String sql) {
// replace all top(?) to top(1) as null is not valid
if (TOP_SYNTAX.matcher(sql).find()) {
sql = TOP_SYNTAX.matcher(sql).replaceAll("TOP(1)");
}

if (!sql.contains("'")) {
return replaceParameterWithString(sql, '?', "null");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.sql.Connection;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void exceptionTest() throws SQLException {
}

@Test
public void tempTableTest() throws SQLException {
public void getParameterMetaDataTest() throws SQLException {
String tempTableName = "[#jdbc_temp" + UUID.randomUUID() + "]";
try (Connection c = PrepUtil.getConnection(AbstractTest.connectionString + ";useFmtOnly=true;");
Statement s = c.createStatement()) {
Expand All @@ -118,6 +119,29 @@ public void tempTableTest() throws SQLException {
}
}

/**
* Tests sql containing TOP
*
* @throws SQLException
*/
@Test
public void getMetaDataTest() throws SQLException {
String tempTableName = "[#jdbc_temp" + UUID.randomUUID() + "]";
try (Connection c = PrepUtil.getConnection(AbstractTest.connectionString + ";useFmtOnly=true;");
Statement s = c.createStatement(); PreparedStatement p = c
.prepareStatement("SELECT TOP(?) [c1] FROM " + tempTableName + " WHERE c1 = ?")) {
TestUtils.dropTableIfExists(tempTableName, s);
s.execute("CREATE TABLE " + tempTableName + " (c1 int)");

ResultSetMetaData rmd = p.getMetaData();
assertTrue(rmd.getColumnCount() == 1);
} finally {
try (Statement s = connection.createStatement()) {
TestUtils.dropTableIfExists(tempTableName, s);
}
}
}

@Test
public void viewTest() throws SQLException {
String tempViewName = "[jdbc_view" + UUID.randomUUID() + "]";
Expand Down
Loading