Skip to content

Commit

Permalink
Driver cuts out the question mark from columns labels (aliases) (#2569)
Browse files Browse the repository at this point in the history
* Driver cuts out the question mark from columns labels (aliases)

* added SQLServerPreparedStatement
  • Loading branch information
Ananya2 authored Dec 20, 2024
1 parent 565ee02 commit 4497e05
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1246,16 +1246,25 @@ public final java.sql.ResultSetMetaData getMetaData() throws SQLServerException,
*/
private SQLServerResultSet buildExecuteMetaData() throws SQLServerException, SQLTimeoutException {
String fmtSQL = userSQL;

SQLServerResultSet emptyResultSet = null;
try {
fmtSQL = replaceMarkerWithNull(fmtSQL);
internalStmt = (SQLServerStatement) connection.createStatement();
emptyResultSet = internalStmt.executeQueryInternal("set fmtonly on " + fmtSQL + "\nset fmtonly off");
} catch (SQLServerException sqle) {
// Ignore empty result set errors, otherwise propagate the server error.
if (!sqle.getMessage().equals(SQLServerException.getErrString("R_noResultset"))) {
throw sqle;
//try by replacing ? characters in case that was an issue
try {
fmtSQL = replaceMarkerWithNull(fmtSQL);
internalStmt = (SQLServerStatement) connection.createStatement();
emptyResultSet = internalStmt.executeQueryInternal("set fmtonly on " + fmtSQL + "\nset fmtonly off");
} catch (SQLServerException ex) {
// Ignore empty result set errors, otherwise propagate the server error.
if (!ex.getMessage().equals(SQLServerException.getErrString("R_noResultset"))) {
throw ex;
}
}
}
}
return emptyResultSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import java.lang.reflect.Field;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
Expand Down Expand Up @@ -124,6 +126,25 @@ public void testPreparedStatementWithSpPrepare() throws SQLException {
}
}
}

@Test
void testDatabaseQueryMetaData() throws SQLException {
try (Connection connection = getConnection()) {
try (SQLServerPreparedStatement stmt = (SQLServerPreparedStatement) connection.prepareStatement(
"select 1 as \"any questions ???\"")) {
ResultSetMetaData metaData = stmt.getMetaData();
String actualLabel = metaData.getColumnLabel(1);
String actualName = metaData.getColumnName(1);

String expected = "any questions ???";
assertEquals(expected, actualLabel, "Column label should match the expected value");
assertEquals(expected, actualName, "Column name should match the expected value");
}
} catch (SQLException e) {
e.printStackTrace();
fail("SQLException occurred during test: " + e.getMessage());
}
}

@Test
public void testPreparedStatementParamNameSpacingWithMultipleParams() throws SQLException {
Expand Down Expand Up @@ -927,5 +948,5 @@ private static void dropTables() throws Exception {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName5), stmt);
}
}

}

0 comments on commit 4497e05

Please sign in to comment.