Skip to content

Commit

Permalink
Fixed timestamp string conversion error for cstmt (#2449) (#2455)
Browse files Browse the repository at this point in the history
* Fixed timestamp string conversion error for cstmt

* Code review comments p1

* Fixed sproc used in test

Co-authored-by: Jeff Wasty <v-jeffwasty@microsoft.com>
  • Loading branch information
tkyc and Jeffery-Wasty committed Jun 20, 2024
1 parent df57d98 commit ee044a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/dtv.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -1627,6 +1628,8 @@ final void executeOp(DTVExecuteOp op) throws SQLServerException {
op.execute(this, ((Geometry) value).serialize());
} else if (JDBCType.GEOGRAPHY == jdbcType) {
op.execute(this, ((Geography) value).serialize());
} else if (JDBCType.TIMESTAMP == jdbcType) {
op.execute(this, Timestamp.valueOf((String) value));
} else {
if (null != cryptoMeta) {
// if streaming types check for allowed data length in AE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class CallableStatementTest extends AbstractTest {
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Table"));
private static String manyParamProc = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Procedure"));
private static String currentTimeProc = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("currentTime_Procedure"));
private static String manyParamUserDefinedType = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_definedType"));
private static String zeroParamSproc = AbstractSQLGenerator
Expand Down Expand Up @@ -114,6 +116,7 @@ public static void setupTest() throws Exception {
createUserDefinedType();
createTableManyParams();
createProcedureManyParams();
createProcedureCurrentTime();
createGetObjectOffsetDateTimeProcedure(stmt);
createProcedureZeroParams();
createOutOfOrderSproc();
Expand Down Expand Up @@ -1260,6 +1263,17 @@ public void testFourPartSyntaxCallEscapeSyntax() throws SQLException {
}
}

@Test
public void testTimestampStringConversion() throws SQLException {
try (CallableStatement stmt = connection.prepareCall("{call " + currentTimeProc + "(?)}")) {
String timestamp = "2024-05-29 15:35:53.461";
stmt.setObject(1, timestamp, Types.TIMESTAMP);
stmt.registerOutParameter(1, Types.TIMESTAMP);
stmt.execute();
stmt.getObject("currentTimeStamp");
}
}

/**
* Cleanup after test
*
Expand All @@ -1278,6 +1292,7 @@ public static void cleanup() throws SQLException {
TestUtils.dropProcedureIfExists(zeroParamSproc, stmt);
TestUtils.dropProcedureIfExists(outOfOrderSproc, stmt);
TestUtils.dropProcedureIfExists(byParamNameSproc, stmt);
TestUtils.dropProcedureIfExists(currentTimeProc, stmt);
TestUtils.dropProcedureIfExists(conditionalSproc, stmt);
TestUtils.dropFunctionIfExists(userDefinedFunction, stmt);
}
Expand Down Expand Up @@ -1331,6 +1346,14 @@ private static void createProcedureManyParams() throws SQLException {
}
}

private static void createProcedureCurrentTime() throws SQLException {
String sql = "CREATE PROCEDURE " + currentTimeProc + " @currentTimeStamp datetime = null OUTPUT " +
"AS BEGIN SET @currentTimeStamp = CURRENT_TIMESTAMP; END";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
}
}

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";
Expand Down

0 comments on commit ee044a8

Please sign in to comment.