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

Fixed CallableStatement default value regression #2452

Merged
merged 5 commits into from
Jun 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private void setPreparedStatementHandle(int handle) {
* Regex for JDBC 'call' escape syntax
*/
private static final Pattern callEscapePattern = Pattern
.compile("^\\s*(?i)\\{(\\s*\\??\\s*=?\\s*)call (.+)\\s*\\(?\\?*,?\\)?\\s*}\\s*$");
.compile("^\\s*(?i)\\{(\\s*\\??\\s*=?\\s*)call [^\\(\\)]+\\s*(\\(\\s*\\?\\s*(,\\s*\\?\\s*)*\\))?\\s*}");

/**
* Regex for 'exec' escape syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ protected Object[][] getContents() {
{"R_givenValueType", "The given value of type"},
{"R_lengthTruncated", " The inserted length is truncated or not correct!"},
{"R_timeValueTruncated", " The time value is truncated or not correct!"},
{"R_nullPointerExceptionFromResultSet", "Cannot invoke \"java.sql.ResultSet.next()\" because \"rs\" is null"},
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved
{"R_invalidErrorMessage", "Invalid Error Message: "},
{"R_kerberosNativeGSSFailure",
"No valid credentials provided (Mechanism level: Failed to find any Kerberos tgt)"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class CallableStatementTest extends AbstractTest {
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_setNull_SP"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Run formatter on file.

private static String inputParamsProcedureName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_inputParams_SP"));
private static String conditionalSproc = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_conditionalSproc"));
private static String getObjectLocalDateTimeProcedureName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_getObjectLocalDateTime_SP"));
private static String getObjectOffsetDateTimeProcedureName = AbstractSQLGenerator
Expand Down Expand Up @@ -97,6 +99,7 @@ public static void setupTest() throws Exception {
TestUtils.dropProcedureIfExists(zeroParamSproc, stmt);
TestUtils.dropProcedureIfExists(outOfOrderSproc, stmt);
TestUtils.dropProcedureIfExists(byParamNameSproc, stmt);
TestUtils.dropProcedureIfExists(conditionalSproc, stmt);
TestUtils.dropFunctionIfExists(userDefinedFunction, stmt);
TestUtils.dropUserDefinedTypeIfExists(manyParamUserDefinedType, stmt);
TestUtils.dropProcedureIfExists(manyParamProc, stmt);
Expand All @@ -115,6 +118,7 @@ public static void setupTest() throws Exception {
createProcedureZeroParams();
createOutOfOrderSproc();
createByParamNameSproc();
createConditionalProcedure();
createUserDefinedFunction();
}
}
Expand Down Expand Up @@ -1150,6 +1154,38 @@ public void testExecDocumentedSystemStoredProceduresIndexedParameters() throws S
}
}

@Test
public void testCallableStatementDefaultValues() throws SQLException {
String call0 = "{call " + conditionalSproc + " (?, ?, 1)}";
String call1 = "{call " + conditionalSproc + " (?, ?, 2)}";
int expectedValue = 5; // The sproc should return this value

try (CallableStatement cstmt = connection.prepareCall(call0)) {
cstmt.setInt(1, 1);
cstmt.setInt(2, 2);
cstmt.execute();
ResultSet rs = cstmt.getResultSet();
rs.next();
fail(TestResource.getResource("R_expectedFailPassed"));

} catch (Exception e) {
String msg = e.getMessage();
assertTrue(TestResource
.getResource("R_nullPointerExceptionFromResultSet").equalsIgnoreCase(msg)
|| msg == null);
}

try (CallableStatement cstmt = connection.prepareCall(call1)) {
cstmt.setInt(1, 1);
cstmt.setInt(2, 2);
cstmt.execute();
ResultSet rs = cstmt.getResultSet();
rs.next();

assertEquals(Integer.toString(expectedValue), rs.getString(1));
}
}

@Test
@Tag(Constants.reqExternalSetup)
@Tag(Constants.xAzureSQLDB)
Expand Down Expand Up @@ -1257,6 +1293,7 @@ public static void cleanup() throws SQLException {
TestUtils.dropProcedureIfExists(outOfOrderSproc, stmt);
TestUtils.dropProcedureIfExists(byParamNameSproc, stmt);
TestUtils.dropProcedureIfExists(currentTimeProc, stmt);
TestUtils.dropProcedureIfExists(conditionalSproc, stmt);
TestUtils.dropFunctionIfExists(userDefinedFunction, stmt);
}
}
Expand Down Expand Up @@ -1317,6 +1354,14 @@ private static void createProcedureCurrentTime() throws SQLException {
}
}

private static void createConditionalProcedure() throws SQLException {
String sql = "CREATE PROCEDURE " + conditionalSproc + " @param0 INT, @param1 INT, @maybe bigint = 2 " +
"AS BEGIN IF @maybe >= 2 BEGIN SELECT 5 END END";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
}
}

private static void createTableManyParams() throws SQLException {
String type = manyParamUserDefinedType;
String sql = "CREATE TABLE" + manyParamsTable + " (c1 " + type + " null, " + "c2 " + type + " null, " + "c3 "
Expand Down
Loading